0

带有复选框和 CustomCursorAdapter 的列表视图

在 ListActivity.java

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.list);

    ListView listView = (ListView) findViewById(R.id.lists);
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    //create Cursor called cursor
    listAdapter = new DetailListAdapter(this, cursor);
    getLoaderManager().initLoader(LOADER_ID, null, this);
    listView.setAdapter(listAdapter);
    listView.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3)
        {
            //want to call startactionmode with class implementing ListView.MultiChoiceModeListener
        }

    });

}

在 DetailListAdapter.java 中

public class DetailListAdapter extends CursorAdapter 
{
    private Context         context;
    private ArrayList<Boolean> checked = new ArrayList<Boolean>();
    private LayoutInflater  mLayoutInflater;


    public DetailListAdapter(Context context, Cursor c)
    {
        super(context, c);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        View v = mLayoutInflater.inflate(R.layout.list_item, parent, false);
        return v;
    }

    @Override
    public void bindView(View v, Context context, Cursor c)
    {
        final View view = v;
        final int position = c.getInt(0);
        String data = c.getString(c.getColumnIndexOrThrow("DATA"));

        /**
         * Next set the name of the entry.
         */
        TextView list_text = (TextView) view.findViewById(R.id.listitemtext);

        CheckBox checkbox_text = (CheckBox) view.findViewById(R.id.listitemcheckbox);

        list_text.setText(data);

        checked.add(position - 1, false);
        checkbox_text.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    view.setBackgroundColor(Color.GRAY);
                    checked.set(position - 1, true);
                } else
                {
                    view.setBackgroundColor(Color.BLACK);
                    checked.set(position - 1, false);
                }
            }
        });

        checkbox_text.setChecked(!checkbox_text.isChecked());
    }
}

列表行 xml 是,

<?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="match_parent"
android:orientation="vertical" 
android:descendantFocusability="blocksDescendants">

<CheckBox
    android:id="@+id/check1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:focusable="false"
  />

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/check1"
    android:layout_toRightOf="@+id/check1"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" 
   />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/text1"
    android:layout_alignBottom="@+id/text1"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/text1"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" 
   />

<TextView
    android:id="@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/text2"
    android:layout_below="@+id/text1"
    android:layout_toRightOf="@+id/check1"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall" 
   />

 </RelativeLayout>

当我单击模拟器中的列表项时,复选框被选中并且行变为灰色。但是 onitemclick 没有被调用。我在布局中的所有项目上都有 android:focusable="false" 和 android:focusableInTouchMode="false" ,这会在列表中创建一行。

4

1 回答 1

1

我希望选中复选框并在顶部显示菜单,其中包含可以在该选定列表项上执行的操作。

不能从一次触摸事件中调用两个 OnClickListener。如您所见,第一个侦听器的 onCheckedChanged()方法消耗了阻止它到达该onItemClick()方法的事件。

我建议在您的行中使用 Checkable 布局,该布局将为您维护检查状态并更改您的适配器getView()以根据视图回收器设置适当的背景颜色。

(如果您需要帮助,请在您的问题中发布您的 XML。)

于 2012-11-16T18:01:25.887 回答