Created
October 17, 2013 22:38
-
-
Save jasonofearth/7033565 to your computer and use it in GitHub Desktop.
How to make a custom view on a multiselect listview. Including a select all checkbox. To use the multiselect your custom view root must implement Checkable. You have to be careful about using the list view to check and uncheck things.
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 checkedLayout extends RelativeLayout implements Checkable { | |
public UnpaidItemCheckedLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
boolean isChecked; | |
@Override | |
public boolean isChecked() { | |
return isChecked; | |
} | |
@Override | |
public void setChecked(boolean checked) { | |
isChecked = checked; | |
CheckBox cb; | |
if((cb = (CheckBox)findViewById(R.id.itemCheckbox)) != null) { | |
cb.setChecked(checked); | |
} | |
} | |
@Override | |
public void toggle() { | |
setChecked(!isChecked); | |
} | |
} |
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
@Override | |
public void onViewCreated(View view, Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
checkAllCheckbox.setChecked(true); | |
checkAllCheckbox.setOnClickListener(new OnClickListener(){ | |
@Override | |
public void onClick(View v) { | |
//note that the checking action happens before onclick so the isChecked is valid for the state it will be | |
if(((CheckBox)v).isChecked()) { | |
for(int i = 0; i < adapter.getCount(); i ++ ) { | |
listView.setItemChecked(i, true); | |
} | |
} | |
else { | |
for(int i = 0; i < adapter.getCount(); i ++ ) { | |
listView.setItemChecked(i, false); | |
} | |
} | |
}}); | |
adapter = new ArrayAdapter<Visit>(getActivity(),android.R.layout.simple_list_item_multiple_choice){ | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View newView = convertView; | |
if (newView == null) { | |
LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
newView = li.inflate(R.layout.unpaid_item, null); | |
} | |
((TextView)newView.findViewById(R.id.NameLabel)).setText(getItem(position).name); | |
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM d, yyyy", Locale.US); | |
((TextView)newView.findViewById(R.id.TimeLabel)).setText("on " + dateFormat.format(getItem(position).startDateTime.getTime())); | |
newView.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
//toggle, if it is checked uncheck it | |
if(((checkedLayout)v).isChecked()) { | |
listView.setItemChecked((Integer) v.getTag(), false); | |
checkAllCheckbox.setChecked(false); | |
} | |
else{ | |
listView.setItemChecked((Integer) v.getTag(), true); | |
if(listView.getCheckedItemCount() == adapter.getCount()) { | |
checkAllCheckbox.setChecked(true); | |
} | |
} | |
}}); | |
//set the tag of the position so it can be used in the onclick | |
newView.setTag(position); | |
return newView; | |
} | |
}; | |
listView.setAdapter(adapter); | |
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); | |
} | |
//reading the items that are checked | |
ArrayList<Item> retVal = new ArrayList<Item>(); | |
SparseBooleanArray checkedPositions = listView.getCheckedItemPositions(); | |
for(int i=0; i < adapter.getCount(); i++) { | |
if(checkedPositions.get(i, false)) { | |
retVal.add(adapter.getItem(i)); | |
} | |
} |
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"?> | |
<com.example.UnpaidItemCheckedLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="10dp" | |
android:clickable="true" > | |
<CheckBox | |
android:id="@+id/itemCheckbox" | |
android:layout_width="wrap_content" | |
android:layout_centerVertical="true" | |
android:layout_height="wrap_content" | |
android:clickable="false" /> | |
<TextView | |
android:id="@+id/NameLabel" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
style="@style/PrimaryItemText" | |
android:layout_marginLeft="10dp" | |
android:layout_toRightOf="@+id/itemCheckbox" /> | |
<TextView | |
android:id="@+id/TimeLabel" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
style="@style/SecondaryItemText" | |
android:layout_alignLeft="@+id/NameLabel" | |
android:layout_below="@+id/NameLabel"/> | |
</com.example.UnpaidItemCheckedLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment