【发布时间】:2017-07-20 05:44:27
【问题描述】:
我有一个带有复选框的列表视图。在按下一个项目时,它会进入另一个活动。长按会显示复选框并选择当前项目。实现 OnItemClickListener 时,单独可以正常工作,但使用 OnItemLOngClickListener 时,没有注册正常的点击
我的代码:
-
活动:
myAdapter = new MyCursorCheckboxAdapter(UserViewLibraryActivity.this,cursor,0); bookList = (ListView) findViewById(R.id.android_list); bookList.setAdapter(myAdapter); bookList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { /* Stuff */ Intent intent = new Intent(view.getContext(), swipeBorrowActivity.class); intent.putExtra("number", i); intent.putExtra("USER_NAME", user); startActivity(intent); finish(); } }); bookList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { inSelectMode=true; myAdapter.setCheckMode(true); CheckBox checkBox = view.findViewById(R.id.checkBox); checkBox.setChecked(true); return true; } });-
适配器:
package com.example.blah.library1; import android.content.Context; import android.database.Cursor; import android.support.v4.widget.CursorAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.TextView; /** * Created by blah on date. */ public class MyCursorCheckboxAdapter extends CursorAdapter { private LayoutInflater cursorInflater; private boolean[] checkedArray; Checker checker; int checkCount; boolean isCheckMode; public boolean[] getSelection() { return checkedArray; } public int getCheckCount() { return checkCount; } public void setCheckMode(boolean checkMode) { this.isCheckMode = checkMode; notifyDataSetChanged(); } public boolean getCheckMode() { return isCheckMode; } public interface Checker { public void isAnyChecked(boolean isChecked); } public MyCursorCheckboxAdapter(Context context, Cursor c, int flags) { super(context, c, flags); cursorInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); checkedArray = new boolean[c.getCount()]; checkCount = 0; isCheckMode = false; try { checker = ((Checker) context); } catch (ClassCastException e) { throw new ClassCastException("Activity Must Implement Checker"); } checker.isAnyChecked(false); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return cursorInflater.inflate(R.layout.row_layout_checkbox, parent, false); } @Override public void bindView(View v, Context context, Cursor cursor) { // Log.d("Cursor :", cursor.getString(1)); TextView title = (TextView) v.findViewById(R.id.listTitle); title.setText(cursor.getString(1)); TextView author = (TextView) v.findViewById(R.id.listAuthor); author.setText(cursor.getString(2)); TextView copies = (TextView) v.findViewById(R.id.listCopies); copies.setText(cursor.getString(4)); final CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox); checkBox.setVisibility(isCheckMode ? View.VISIBLE : View.GONE); final int position = cursor.getPosition(); if (cursor.getInt(4) == 0) { // Log.d("Position :", ((Integer)cursor.getPosition()).toString()); checkBox.setEnabled(false); } checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean isChecked = ((CheckBox) view).isChecked(); if (isChecked) { //Log.d("Checkbox :","isChecked "+Integer.toString(position)); checkCount++; if (checkCount == 1) checker.isAnyChecked(true); checkedArray[position] = true; } else { // Log.d("Checkbox :","isNotChecked "+Integer.toString(position)); checkCount--; if (checkCount == 0) { checker.isAnyChecked(false); checkedArray[position] = false; // Log.d("Checker :","No items are checked"); } } } }); } }
-
-
我的列表项布局:
编辑:删除两个属性 android:clickable="true" 和 android:longClickable="true" 使其工作
【问题讨论】:
-
在 XML 中删除
android:longClickable="true"后尝试 -
哇!删除
longClickable和clickable都有效!但为什么呢?
标签: android listview adapter onclicklistener