Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created March 26, 2013 13:32
Show Gist options
  • Save daichan4649/5245378 to your computer and use it in GitHub Desktop.
Save daichan4649/5245378 to your computer and use it in GitHub Desktop.
Checkable ListView (for Android)
package daichan4649.test;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.LinearLayout;
public class CheckableLayout extends LinearLayout implements Checkable {
private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };
public CheckableLayout(Context context) {
super(context, null);
}
public CheckableLayout(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public CheckableLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private boolean checked;
@Override
public boolean isChecked() {
return checked;
}
@Override
public void setChecked(boolean checked) {
if (this.checked != checked) {
this.checked = checked;
refreshDrawableState();
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child instanceof Checkable) {
((Checkable) child).setChecked(checked);
}
}
}
}
@Override
public void toggle() {
setChecked(!checked);
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
}
<?xml version="1.0" encoding="utf-8"?>
<daichan4649.test.CheckableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/check_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<TextView
android:id="@+id/text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical" />
<RadioButton
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
</daichan4649.test.CheckableLayout>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test, container, false);
// 表示データ
List<String> dataList = new ArrayList<String>();
// 初期選択位置
int initSelectedPosition = 3;
// リスト設定
TestAdapter adapter = new TestAdapter(getActivity(), dataList);
ListView listView = (ListView) view.findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setItemChecked(initSelectedPosition, true);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 選択状態を要素(checkable)へ反映
Checkable child = (Checkable) parent.getChildAt(position);
child.toggle();
}
});
return view;
}
private static class TestAdapter extends ArrayAdapter<String> {
private LayoutInflater inflater;
public TestAdapter(Context context, List<String> dataList) {
super(context, 0, dataList);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.inflater_list_column, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// bindData
holder.text.setText(getItem(position));
return convertView;
}
}
private static class ViewHolder {
TextView text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment