我想实现一个ListView
可以处理多种选择并检测点击的方法。我希望每个列表都包含一个复选框,因此如果您单击复选框,它将记住选择,如果您单击该行中的文本,它将转到另一个活动。
我怎样才能做到这一点?
您需要ListView
使用自定义创建一个Adapter
. 这是带有复选框和可单击项的列表视图的代码:
private class MySecondAdapter extends ArrayAdapter<MiniTask>
{
private ArrayList<MiniTask> list;
public MySecondAdapter(Context context, int textViewResourceId, ArrayList<MiniTask> miniTaskList)
{
super(context, textViewResourceId, miniTaskList);
this.list = new ArrayList<MiniTask>();
this.list.addAll(miniTaskList);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
miniTask = miniTaskList.get(position);
ViewHolder holder = new ViewHolder();
{
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.check_list_item_new, null);
holder.title = (TextView) convertView.findViewById(R.id.tvItemTitle);
holder.commentsPicturesButton = (ImageView) convertView.findViewById(R.id.iAddCommetOrPicture);
holder.commentsPicturesButton.setTag(position);
holder.commentsPicturesButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent = new Intent(getApplicationContext(), PicturesAndCommentsActivity.class);
intent.putExtra(TasksListActivity.KEY_ID, task.getId());
intent.putExtra("mini_task_text", miniTask.getTitle());
startActivity(intent);
}
});
holder.selected = (CheckBox) convertView.findViewById(R.id.cbCheckListItem);
holder.selected.setTag(position);
holder.selected.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
{
Log.d(TAG, "pressed the checkbox: " + v.getId() + " in position: " + position + " tag: " +v.getTag() +" and item from array: " + miniTaskList.get(position) );
CheckBox checkbox = (CheckBox) v;
miniTaskList.get(position).setSelected(checkbox.isChecked());
numOfCheckedMiniTasks = 0;
for(int i=0;i<miniTaskList.size();i++)
{
miniTask = miniTaskList.get(i);
if(miniTask.isSelected())
{
numOfCheckedMiniTasks ++;
}
}
int percent = (int)(numOfCheckedMiniTasks * 100.0f) / miniTaskList.size();
Log.d(TAG, "the percentage is: " +percent);
tasksRepository.get(tasksRepository.indexOf(task)).setMiniTasksPercentageComplete(percent);
}
}
});
}
holder.title.setText(miniTask.getTitle());
holder.selected.setChecked(miniTask.isSelected());
return convertView;
}
和ViewHolder
:
static class ViewHolder
{
TextView title;
CheckBox selected;
ImageView commentsPicturesButton;
}
和行布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/try2"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/cbCheckListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/checkbox_checklist_selector"
android:button="@drawable/checkbox_checklist_selector" />
<TextView
android:id="@+id/tvItemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="10dp"
android:paddingTop="13dp"
android:text="@string/checklist_item_string"
android:textColor="@color/my_darker_gray" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingTop="6.5dp" >
<ImageView
android:id="@+id/iAddCommetOrPicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:contentDescription="@drawable/comment_or_photo_icon"
android:src="@drawable/comment_or_photo_icon" />
</RelativeLayout>