2017-07-20 42 views
0

我有一個帶複選框的列表視圖。在按下某個項目時,它會轉到另一項活動。在長按時,顯示覆選框並選擇當前項目。當OnItemClickListener實現,它單獨工作正常,但使用OnItemLOngClickListener時,正常點擊未註冊OnItemLongClickListener塊OnItemClickListener

我的代碼:

  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; 
         } 
        }); 
    
  2. 適配器:

    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:點擊= 「真」 和android:longClickable = 「真」 使得它的工作

    +0

    嘗試刪除XML中的android:longClickable =「true」' –

    +0

    哇!刪除'longClickable'和'可點擊的作品!但爲什麼? – ColonD

    回答

    1

    顯然同時去除android:clickable="true"android:longClickable="true"使它工作。 (爲什麼我不明白)