Instantly share code, notes, and snippets.
Created
July 19, 2015 23:38
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save CodyEngel/a4c01ab8f7c2a827b45d to your computer and use it in GitHub Desktop.
Android ListView Implementation Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:paddingTop="@dimen/activity_padding_small" | |
android:paddingBottom="@dimen/activity_padding_small"> | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="@dimen/activity_horizontal_margin" | |
android:layout_marginStart="@dimen/activity_horizontal_margin" | |
android:id="@+id/ivIcon" | |
android:src="@drawable/example_icon" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textAppearance="?android:attr/textAppearanceLarge" | |
android:text="You should put a string here. Hardcoding values is a no-no." | |
android:id="@+id/tvTitle" | |
android:layout_alignTop="@+id/ivChevronRight" | |
android:layout_toRightOf="@+id/ivIcon" | |
android:layout_toLeftOf="@+id/ivChevronRight" | |
android:layout_toStartOf="@+id/ivChevronRight" | |
android:layout_alignBottom="@+id/ivIcon" | |
android:gravity="center_vertical" /> | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginRight="@dimen/activity_horizontal_margin" | |
android:layout_marginEnd="@dimen/activity_horizontal_margin" | |
android:id="@+id/ivChevronRight" | |
android:src="@drawable/ic_chevron_right_white" | |
android:layout_centerVertical="true" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentEnd="true" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="You should put a string here. Hardcoding values is a no-no." | |
android:id="@+id/tvDescription" | |
android:layout_below="@+id/ivIcon" | |
android:layout_alignLeft="@+id/ivIcon" | |
android:layout_alignStart="@+id/ivIcon" | |
android:layout_alignRight="@+id/tvTitle" | |
android:layout_alignEnd="@+id/tvTitle" | |
android:visibility="gone" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NavigationItem { | |
Drawable mIcon; | |
FragmentTransaction mFragmentTransaction; | |
String mTitle; | |
public NavigationItem(String title, Drawable icon, FragmentTransaction fragmentTransaction) { | |
mTitle = title; | |
mIcon = icon; | |
mFragmentTransaction = fragmentTransaction; | |
} | |
public Drawable getIcon() { | |
return mIcon; | |
} | |
public String getTitle() { | |
return mTitle; | |
} | |
public FragmentTransaction getFragmentTransaction() { | |
return mFragmentTransaction; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NavigationListAdapter extends ArrayAdapter<NavigationItem> { | |
private Context mContext; | |
private ArrayList<NavigationItem> mItems; | |
private LayoutInflater mLayoutInflater; | |
public NavigationListAdapter(Context context, ArrayList<NavigationItem> items) { | |
super(context, 0, items); | |
mContext = context; | |
mItems = items; | |
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
} | |
@Override | |
public View getView(final int position, View convertView, ViewGroup parent) { | |
final ViewHolder viewHolder; | |
final NavigationItem navigationItems = mItems.get(position); | |
if(navigationItems != null) { | |
if(convertView == null) { | |
convertView = mLayoutInflater.inflate(R.layout.list_item_navigation, null); | |
viewHolder = new ViewHolder(); | |
viewHolder.tvTitle = (TextView)convertView.findViewById(R.id.tvTitle); | |
viewHolder.ivIcon = (ImageView)convertView.findViewById(R.id.ivIcon); | |
convertView.setTag(viewHolder); | |
} else { | |
viewHolder = (ViewHolder) convertView.getTag(); | |
} | |
viewHolder.tvTitle.setText(navigationItems.getTitle()); | |
viewHolder.ivIcon.setImageDrawable(navigationItems.getIcon()); | |
} | |
convertView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if(navigationItems.getFragmentTransaction() != null) { | |
navigationItems.getFragmentTransaction().commit(); | |
} else { | |
Log.d("ExampleListView", "Fragment Is Null"); | |
} | |
} | |
}); | |
return convertView; | |
} | |
static class ViewHolder { | |
TextView tvTitle; | |
ImageView ivIcon; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment