2016-06-21 53 views
1

我有一個listview與左側的imageview。我想要做的是;如果我按imageView選擇當前行,如果我再次按下,取消選擇該行。使用我當前的代碼,當我點擊imageView但未選中當前行時,我可以選擇帶有performLongClick()的行。看起來是隨機的。如何在列表視圖中選擇一行只按下一個ImageView

public View getView(int position, View convertView, ViewGroup parent) { 
     final HmViewHolder holder; 
     row = convertView; 


     if (row == null) { 
      row = inflater.inflate(R.layout.list_item, parent, false); 
      holder = new HmViewHolder(); 
      holder.title = (TextView) row.findViewById(R.id.title); 
      holder.contact = (ImageView) row.findViewById(R.id.imageview); 

      row.setTag(holder); 
     } else { 
      holder = (HmViewHolder) row.getTag(); 
     } 

     holder.contact.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (multiMode) { 
        row.getTag(); 
        row.setSelected(!row.isSelected()); 
       holder.contact.setImageResource(R.drawable.ic_check_circle_black); 
       } else { 
        row.performLongClick(); 
        holder.contact.setImageResource(R.drawable.ic_contact); 
       } 
      } 
     }); 
    return row; 
    } 

在此先感謝

+0

是否有任何理由,你也不會轉回去要使用的onItemClickListener? https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html –

+0

@ D.O。你可以使用POJO類實現來解決這個問題。 – Nisarg

回答

0

ListView它的自我clikcable,並使用內部的另一個 點擊一個可點擊的是不好的。所以最好不要在列表視圖中設置項目 (ImageView和TextView)上的clicklistener,因爲如果你喜歡 那樣,它會混淆環境,並且不能 明白你想要點擊什麼。因此,僅在 listview上使用onclick。

0

有關於此主題的Chris Banes's blogpost

給它一個閱讀,我認爲你所有的麻煩都解決了。在我看來,這是創建可檢查列表的最佳方式。

0

將您的ImageView設置爲可在您的行佈局xml上單擊,如下所示。

<ImageView 
    android:clickable="true"/> 
0
//ViewHolder Class 

    class ViewHolder { 
      TextView tv_details; 
      RelativeLayout rl_expense_list; 
      ImageView Iv_close; 
     } 

// `here is getView Method` 
public View getView(final int position, View convertView, ViewGroup parent) 
{   final ViewHolder viewHolder; 

      if (convertView == null) { 
       viewHolder = new ViewHolder(); 
       LayoutInflater inflater= (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = inflater.inflate(R.layout.expenses_list_row, parent, false); 

       viewHolder.rl_expense_list = (RelativeLayout) convertView.findViewById(id.rl_expense_list); 
       viewHolder.tv_details = (TextView) convertView.findViewById(id.tv_details); 
       viewHolder.bt_close = (ImageView) convertView.findViewById(id.iv_close); 

       convertView.setTag(viewHolder); 
      } 
      else { 
       viewHolder = (ViewHolder) convertView.getTag(); 
      } 
      viewHolder.Iv_close.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        final AlertDialog alertDialog = new AlertDialog.Builder(ExpensesList.this).create(); 
        alertDialog.setTitle("Expense List"); 
        alertDialog.setMessage("Do You Want To Delete This Expense List?"); 
        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          expensesDataModels.remove(position); 
          Customer_Feedback.expensesDataModels = expensesDataModels; 
          notifyDataSetChanged(); 
         } 
        }); 
        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          alertDialog.dismiss(); 
         } 
        }); 
        alertDialog.show(); 
       } 
      } 
+0

這段代碼適合我 –

相關問題