Skip to content

Commit

Permalink
Added abstract adapter class and implemented a simple adapter using it.
Browse files Browse the repository at this point in the history
  • Loading branch information
psaravan committed Sep 1, 2014
1 parent 37a9869 commit 1efcb63
Show file tree
Hide file tree
Showing 22 changed files with 327 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.psaravan.filebrowserview.lib.FileBrowserEngine.AdapterData;
import com.psaravan.filebrowserview.lib.FileBrowserEngine.FileBrowserEngine;
import com.psaravan.filebrowserview.lib.R;
import com.psaravan.filebrowserview.lib.Utils.Utils;
import com.psaravan.filebrowserview.lib.View.AbstractAdapter;

/**
* Adapter for the ListView layout.
*
* @author Saravan Pantham
*/
public class ListLayoutAdapter extends ArrayAdapter<String> {
public class ListLayoutAdapter extends AbstractAdapter {

//Context.
private Context mContext;
Expand All @@ -42,7 +44,7 @@ public class ListLayoutAdapter extends ArrayAdapter<String> {
private AdapterData mAdapterData;

public ListLayoutAdapter(Context context, int resource, AdapterData adapterData) {
super(context, resource);
super(context, resource, adapterData);
mContext = context;
mAdapterData = adapterData;
}
Expand All @@ -55,7 +57,7 @@ public View getView(int position, View convertView, ViewGroup parent) {

convertView = LayoutInflater.from(mContext).inflate(R.layout.list_view_item, parent, false);
ListView.LayoutParams params = (ListView.LayoutParams) convertView.getLayoutParams();
params.height = (int) mApp.convertDpToPixels(72.0f, mContext);
params.height = (int) Utils.convertDpToPixels(72.0f, mContext);
convertView.setLayoutParams(params);

holder = new FoldersViewHolder();
Expand All @@ -64,18 +66,9 @@ public View getView(int position, View convertView, ViewGroup parent) {
holder.fileFolderNameText = (TextView) convertView.findViewById(R.id.listViewTitleText);
holder.overflowButton = (ImageButton) convertView.findViewById(R.id.listViewOverflow);
holder.rightSubText = (TextView) convertView.findViewById(R.id.listViewRightSubText);

holder.fileFolderIcon.setScaleX(0.5f);
holder.fileFolderIcon.setScaleY(0.55f);
holder.rightSubText.setVisibility(View.INVISIBLE);

holder.fileFolderNameText.setTextColor(UIElementsHelper.getThemeBasedTextColor(mContext));
holder.fileFolderNameText.setTypeface(TypefaceHelper.getTypeface(mContext, "Roboto-Regular"));

holder.fileFolderSizeText.setTextColor(UIElementsHelper.getSmallTextColor(mContext));
holder.fileFolderSizeText.setTypeface(TypefaceHelper.getTypeface(mContext, "Roboto-Regular"));

holder.overflowButton.setImageResource(UIElementsHelper.getIcon(mContext, "ic_action_overflow"));
holder.overflowButton.setImageResource(R.drawable.ic_action_overflow_universal);
holder.overflowButton.setFocusable(false);
holder.overflowButton.setFocusableInTouchMode(false);
holder.overflowButton.setOnClickListener(overflowClickListener);
Expand All @@ -85,45 +78,54 @@ public View getView(int position, View convertView, ViewGroup parent) {
holder = (FoldersViewHolder) convertView.getTag();
}

holder.fileFolderNameText.setText(mFileFolderNameList.get(position));
holder.fileFolderSizeText.setText(mFileFolderSizeList.get(position));
holder.fileFolderNameText.setText(getNamesList().get(position));
holder.fileFolderSizeText.setText(getSizesList().get(position));

//Set the icon based on whether the item is a folder or a file.
if (mFileFolderTypeList.get(position)==FilesFoldersFragment.FOLDER) {
if (getTypesList().get(position)== FileBrowserEngine.FOLDER) {
holder.fileFolderIcon.setImageResource(R.drawable.icon_folderblue);
convertView.setTag(R.string.folder_list_item_type, FilesFoldersFragment.FOLDER);
convertView.setTag(R.string.folder_path, mFileFolderPathsList.get(position));
convertView.setTag(R.string.type, FileBrowserEngine.FOLDER);
convertView.setTag(R.string.path, getPathsList().get(position));
convertView.setTag(R.string.position, position);

} else if (mFileFolderTypeList.get(position)==FilesFoldersFragment.AUDIO_FILE) {
} else if (getTypesList().get(position)==FileBrowserEngine.FILE_AUDIO) {
holder.fileFolderIcon.setImageResource(R.drawable.icon_mp3);
convertView.setTag(R.string.folder_list_item_type, FilesFoldersFragment.AUDIO_FILE);
convertView.setTag(R.string.folder_path, mFileFolderPathsList.get(position));
convertView.setTag(R.string.type, FileBrowserEngine.FILE_AUDIO);
convertView.setTag(R.string.path, getPathsList().get(position));
convertView.setTag(R.string.position, position);

} else if (mFileFolderTypeList.get(position)==FilesFoldersFragment.PICTURE_FILE) {
} else if (getTypesList().get(position)==FileBrowserEngine.FILE_PICTURE) {
holder.fileFolderIcon.setImageResource(R.drawable.icon_png);
convertView.setTag(R.string.folder_list_item_type, FilesFoldersFragment.PICTURE_FILE);
convertView.setTag(R.string.folder_path, mFileFolderPathsList.get(position));
convertView.setTag(R.string.type, FileBrowserEngine.FILE_PICTURE);
convertView.setTag(R.string.path, getPathsList().get(position));
convertView.setTag(R.string.position, position);

} else if (mFileFolderTypeList.get(position)==FilesFoldersFragment.VIDEO_FILE) {
} else if (getTypesList().get(position)==FileBrowserEngine.FILE_VIDEO) {
holder.fileFolderIcon.setImageResource(R.drawable.icon_avi);
convertView.setTag(R.string.folder_list_item_type, FilesFoldersFragment.VIDEO_FILE);
convertView.setTag(R.string.folder_path, mFileFolderPathsList.get(position));
convertView.setTag(R.string.type, FileBrowserEngine.FILE_VIDEO);
convertView.setTag(R.string.path, getPathsList().get(position));
convertView.setTag(R.string.position, position);

} else {
holder.fileFolderIcon.setImageResource(R.drawable.icon_default);
convertView.setTag(R.string.folder_list_item_type, FilesFoldersFragment.FILE);
convertView.setTag(R.string.folder_path, mFileFolderPathsList.get(position));
convertView.setTag(R.string.type, FileBrowserEngine.FILE_GENERIC);
convertView.setTag(R.string.path, getPathsList().get(position));
convertView.setTag(R.string.position, position);

}

return convertView;
}

private View.OnClickListener overflowClickListener = new View.OnClickListener() {

@Override
public void onClick(View view) {

}

};

/**
* Holder class for the ListView layout.
*/
Expand All @@ -133,6 +135,7 @@ static class FoldersViewHolder {
public ImageView fileFolderIcon;
public ImageButton overflowButton;
public TextView rightSubText;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2014 Saravan Pantham
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.psaravan.filebrowserview.lib.Utils;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;

/**
* Helper class with a set of commonly used helper methods.
*
* @author Saravan Pantham
*/
public class Utils {

/**
* Converts the input dp unit to pixels, depending on device density.
*
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* @param context Context to get resources and device specific display metrics
* @return A floating point value to represent px equivalent to dp depending on device density.
*/
public static float convertDpToPixels(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (C) 2014 Saravan Pantham
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.psaravan.filebrowserview.lib.View;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.psaravan.filebrowserview.lib.FileBrowserEngine.AdapterData;

import java.util.ArrayList;

/**
* Extend this abstract class if you want to create your own adapter for the ListView/GridView.
*
* @author Saravan Pantham
*/
public abstract class AbstractAdapter extends ArrayAdapter<String> {

private Context mContext;
private AdapterData mAdapterData;

public AbstractAdapter(Context context, int layoutResourceId, AdapterData adapterData) {
super(context, layoutResourceId);
mContext = context;
mAdapterData = adapterData;
}

/**
* Get a View that displays the data at the specified position in the data set. You can either
* create a View manually or inflate it from an XML layout file. When the View is inflated, the
* parent View (GridView, ListView...) will apply default layout parameters unless you use
* {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
* to specify a root view and to prevent attachment to the root.
*
* @param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param convertView The old view to reuse, if possible. Note: You should check that this view
* is non-null and of an appropriate type before using. If it is not possible to convert
* this view to display the correct data, this method can create a new view.
* Heterogeneous lists can specify their number of view types, so that this View is
* always of the right type (see {@link #getViewTypeCount()} and
* {@link #getItemViewType(int)}).
* @param parent The parent that this view will eventually be attached to
* @return A View corresponding to the data at the specified position.
*/
@Override
public abstract View getView(int position, View convertView, ViewGroup parent);

/**
* @return The list of file/subfolder names within the current directory.
*/
public ArrayList<String> getNamesList() {
if (mAdapterData!=null)
return mAdapterData.getNamesList();
else
return new ArrayList<String>();

}

/**
* @return The list of file/subfolder types within the current directory.
*/
public ArrayList<Integer> getTypesList() {
if (mAdapterData!=null)
return mAdapterData.getTypesList();
else
return new ArrayList<Integer>();

}

/**
* @return The list of file/subfolder paths within the current directory.
*/
public ArrayList<String> getPathsList() {
if (mAdapterData!=null)
return mAdapterData.getPathsList();
else
return new ArrayList<String>();

}

/**
* @return The list of file/subfolder sizes within the current directory.
*/
public ArrayList<String> getSizesList() {
if (mAdapterData!=null)
return mAdapterData.getSizesList();
else
return new ArrayList<String>();

}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions library/src/main/res/drawable/empty_color_patch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#11AAAAAA"/>
</shape>
12 changes: 12 additions & 0 deletions library/src/main/res/drawable/empty_color_patch_circular.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid
android:color="#11AAAAAA"/>

<size
android:width="80dp"
android:height="80dp"/>
</shape>
12 changes: 12 additions & 0 deletions library/src/main/res/drawable/empty_color_patch_circular_light.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid
android:color="#33323232"/>

<size
android:width="80dp"
android:height="80dp"/>
</shape>
4 changes: 4 additions & 0 deletions library/src/main/res/drawable/empty_color_patch_light.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#33323232"/>
</shape>
Loading

0 comments on commit 1efcb63

Please sign in to comment.