OnItemLongClickListener阻止OnItemClickListener

时间:2017-07-20 05:44:27

标签: android listview adapter onclicklistener

我有一个带复选框的列表视图。按下一个项目,它进入另一个活动。长按,显示复选框并选择当前项目。当实现OnItemClickListener时,它可以单独工作,但是当使用OnItemLOngClickListener时,正常点击没有注册

我的代码:

  1. 活动:

    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;
            }
        });
    
    1. 适配器:

      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");
                  }
              }
          }
      });
      
          }
      }
      
  2. 我的列表项布局:

  3. 编辑:删除两个属性android:clickable =&#34; true&#34;和android:longClickable =&#34; true&#34;使它工作

1 个答案:

答案 0 :(得分:1)

显然删除android:clickable="true"android:longClickable="true"会使其正常工作。 (为什么我不明白)