2015-09-30 30 views
-1

我试图设置onClickListenerImageView里面ListView项目。 我的适配器: ContactListAdapter.javaImageView不能点击里面ListView

public class ContactListAdapter extends ArrayAdapter<ContactItemInterface> { 

    private int resource; // store the resource layout id for 1 row 
    private boolean inSearchMode = false; 

    private ContactsSectionIndexer indexer = null; 

    public ContactListAdapter(Context _context, int _resource, 
      List<ContactItemInterface> _items) { 
     super(_context, _resource, _items); 
     resource = _resource; 

     // need to sort the items array first, then pass it to the indexer 
     Collections.sort(_items, new ContactItemComparator()); 

     setIndexer(new ContactsSectionIndexer(_items)); 

    } 



    // do all the data population for the row here 
    // subclass overwrite this to draw more items 
    public void populateDataForRow(View parentView, ContactItemInterface item, 
      int position) { 
     // default just draw the item only 

    } 

    // this should be override by subclass if necessary 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewGroup parentView; 
     ContactItemInterface item = null; 
     if (getCount() > position) { 
      item = getItem(position); 
     } 
     // Log.i("ContactListAdapter", "item: " + item.getItemForIndex()); 



     parentView = (ViewGroup) LayoutInflater.from(getContext()).inflate(resource, parent, false); 

     // for the very first section item, we will draw a section on top 
     // showSectionViewIfFirstItem(parentView, item, position); 

     // set row items here 
     if (getCount() > position) { 
      populateDataForRow(parentView, item, position); 
     } 
     return parentView; 

    } 

    public boolean isInSearchMode() { 
     return inSearchMode; 
    } 

    public void setInSearchMode(boolean inSearchMode) { 
     this.inSearchMode = inSearchMode; 
    } 

    public ContactsSectionIndexer getIndexer() { 
     return indexer; 
    } 

    public void setIndexer(ContactsSectionIndexer indexer) { 
     this.indexer = indexer; 
    } 

} 

AdapterContactList.java

public class AdapterContactList extends ContactListAdapter { 

    private OnClickListener listener; 
    private OnClickListener onInfoClickListener; 
    private OnClickListener onItemClickListener; 
    private ArrayList<User> select = null; 

    public AdapterContactList(Context _context, int _resource, 
      List<ContactItemInterface> _items) { 
     super(_context, _resource, _items); 
    } 

    public void setSelectArray(ArrayList<User> select) { 
     this.select = select; 
     this.notifyDataSetChanged(); 
    } 

    public void setOnClickListener(OnClickListener listener) { 
     this.listener = listener; 
    } 

    public void setOnInfoClickListener(OnClickListener l){ 
     this.onInfoClickListener = l; 
    } 

    public void setOnItemClickListenerCustom(OnClickListener l){ 
     this.onInfoClickListener = l; 
    } 

    // override this for custom drawing 
    public void populateDataForRow(View parentView, ContactItemInterface item, 
      int position) { 
     // default just draw the item only 
     View infoView = parentView.findViewById(R.id.infoRowContainer); 
     infoView.setTag(position); 
     TextView fullNameView = (TextView) infoView.findViewById(R.id.tv_name); 
     View tv_grey = infoView.findViewById(R.id.tv_grey); 
     View tv_green = infoView.findViewById(R.id.tv_green); 
     View tv_red = infoView.findViewById(R.id.tv_red); 
     View tv_yellow = infoView.findViewById(R.id.tv_yellow); 
     ImageView iv_user = (ImageView) infoView.findViewById(R.id.im_calls); 
     View pending_cont = infoView.findViewById(R.id.pending_cont); 
     ImageView btnInfo = (ImageView)infoView.findViewById(R.id.btn_info); 
     if(btnInfo != null){ 
      btnInfo.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Toast.makeText(getContext(), "Button", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 


     if (tv_green != null) { 
      if(tv_grey != null){ 
       tv_grey.setVisibility(View.GONE); 
      } 
      tv_green.setVisibility(View.GONE); 
      tv_red.setVisibility(View.GONE); 
      tv_yellow.setVisibility(View.GONE); 
     } 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      View line_end = infoView.findViewById(R.id.line_end); 
      line_end.setVisibility(View.VISIBLE); 
     } 

     if (item instanceof ContactListItem) { 
      ContactListItem contactItem = (ContactListItem) item; 
      fullNameView.setText(contactItem.getFullName()); 

      if (contactItem.getAvatar() != null) { 
       MyProject.loadImage(contactItem.getAvatar(), iv_user); 
      } else { 
       User us1 = MyProject.getTempUser(contactItem.getMyProjectid()); 
       if (us1 != null) { 
        MyProject.loadImage(us1.getAvatar(), iv_user); 
       } 
      } 

      if (tv_green != null) { 
       int status = MyProject.getUserStatus(contactItem.getMyProjectid()); 
       MyProject.debug("" + status); 
       if (status != MyProject.STATUS_NONE) { 
        if (status == MyProject.STATUS_ONLINE) { 
         tv_green.setVisibility(View.VISIBLE); 
        } 
        if (status == MyProject.STATUS_AWAY) { 
         tv_yellow.setVisibility(View.VISIBLE); 
        } 
        if (status == MyProject.STATUS_DND) { 
         tv_red.setVisibility(View.VISIBLE); 
        } 
       }else{ 
        if(tv_grey != null) { 
         tv_grey.setVisibility(View.VISIBLE); 
        } 
       } 
      } 

      if (infoView.findViewById(R.id.checkbox) != null) { 
       CheckBox checkbox = (CheckBox) infoView 
         .findViewById(R.id.checkbox); 
       if (select != null) { 
        checkbox.setChecked(select.indexOf(contactItem.getUser()) >= 0); 
       } else { 
        checkbox.setChecked(false); 
       } 
      } 

      if (listener != null) { 
       View button_add = infoView.findViewById(R.id.add_cont); 
       if (contactItem.getUser().isPending()) { 
        pending_cont.setVisibility(View.VISIBLE); 
        button_add.setVisibility(View.GONE); 
        infoView.setOnClickListener(listener); 
       } else { 
        pending_cont.setVisibility(View.GONE); 
        button_add.setVisibility(View.VISIBLE); 
        button_add.setOnClickListener(listener); 
        infoView.setOnClickListener(listener); 
       } 
      } 

      infoView.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Toast.makeText(v.getContext(), "Avatar", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
    } 

    @Override 
    public boolean isEnabled(int position) { 
     return true; 
    } 
} 

请看btnInfo。我设置好的他onClickListener,并在调试模式下我相信它设置好的,但它不工作。它只是忽略了。

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/infoRowContainer" 
    android:layout_width="match_parent" 
    android:layout_height="44dp" 
    android:background="@drawable/dialog_buttons_background" 
    android:descendantFocusability="blocksDescendants"> 

    <com.myproject.custom.CFRoundImageView 
     android:id="@+id/im_calls" 
     android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:layout_marginLeft="6dp" 
     android:layout_marginTop="6dp" 
     android:contentDescription="@string/temp" 
     android:scaleType="fitXY" 
     android:src="@drawable/fb_placeholder"/> 

    <com.myproject.custom.CFTextView 
     android:id="@+id/tv_grey" 
     android:layout_width="@dimen/status_side_size" 
     android:layout_height="@dimen/status_side_size" 
     android:layout_alignBottom="@+id/im_calls" 
     android:layout_alignRight="@+id/im_calls" 
     android:background="@drawable/ic_not_in_contact"/> 

    <com.myproject.custom.CFTextView 
     android:id="@+id/tv_green" 
     android:layout_width="@dimen/status_side_size" 
     android:layout_height="@dimen/status_side_size" 
     android:layout_alignBottom="@+id/im_calls" 
     android:layout_alignRight="@+id/im_calls" 
     android:background="@drawable/ic_status_online"/> 

    <com.myproject.custom.CFTextView 
     android:id="@+id/tv_yellow" 
     android:layout_width="@dimen/status_side_size" 
     android:layout_height="@dimen/status_side_size" 
     android:layout_alignBottom="@+id/im_calls" 
     android:layout_alignRight="@+id/im_calls" 
     android:background="@drawable/ic_status_not_presents"/> 

    <com.myproject.custom.CFTextView 
     android:id="@+id/tv_red" 
     android:layout_width="@dimen/status_side_size" 
     android:layout_height="@dimen/status_side_size" 
     android:layout_alignBottom="@+id/im_calls" 
     android:layout_alignRight="@+id/im_calls" 
     android:background="@drawable/ic_status_offline"/> 

    <com.myproject.custom.CFTextView 
     android:id="@+id/tv_name" 
     android:layout_width="match_parent" 
     android:layout_height="44dp" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="44dp" 
     android:layout_marginRight="30dp" 
     android:ellipsize="end" 
     android:gravity="left|center_vertical" 
     android:maxLines="1" 
     android:text="@string/text_contacts" 
     android:textColor="@color/color_grey5" 
     android:textSize="16sp" 
     app:typeFace="OpenSans-Light"/> 

    <TextView 
     android:id="@+id/line_end" 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_marginRight="36dp" 
     android:layout_marginTop="43dp" 
     android:background="@color/color_gray2" 
     android:visibility="gone"/> 

    <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btn_info" 
      android:src="@drawable/ic_info" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      android:clickable="true" 
      android:padding="8dp" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="30dp" 
      /> 

</RelativeLayout> 

ImageView从来没有点击! 我试过设置clcikable=trueImageView

我试着为项目的所有子项设置'focusable = false focusableInTouchMode = false'。

我尝试在项目根视图中使用descendantFocusability="blocksDescendants"

我试图与另一这种方法合成一个。

而且ImageView仍不可​​点击。

回答

0
Try this (things in *** .... ****) 
...... 
public void populateDataForRow(View parentView, ContactItemInterface item, 
      int position) { 
     // default just draw the item only 
     View infoView = parentView.findViewById(R.id.infoRowContainer); 
     infoView.setTag(position); 
     TextView fullNameView = (TextView) infoView.findViewById(R.id.tv_name); 
     View tv_grey = infoView.findViewById(R.id.tv_grey); 
     View tv_green = infoView.findViewById(R.id.tv_green); 
     View tv_red = infoView.findViewById(R.id.tv_red); 
     View tv_yellow = infoView.findViewById(R.id.tv_yellow); 
     ImageView iv_user = (ImageView) infoView.findViewById(R.id.im_calls); 
     View pending_cont = infoView.findViewById(R.id.pending_cont); 
     ImageView btnInfo = (ImageView)infoView.findViewById(R.id.btn_info); 
     ****btnInfo.setFocusable(false);**** 
     if(btnInfo != null){ 
      btnInfo.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Toast.makeText(getContext(), "Button", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
....... // Your code 
+0

它不工作,我试图用它xml – Alexander

+0

请在代码上做,我指出(我也面临类似的问题,并得到重新解决这个注意:它的setFocus不setClickClickable – dex

+0

它的工作? – dex

0

添加到您的行XML:机器人:可点击= “假”机器人:可调焦= “假”

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/infoRowContainer" 
android:layout_width="match_parent" 
android:layout_height="44dp" 
android:background="@drawable/dialog_buttons_background" 
android:clickable="false" 
android:focusable="false"> 
.... 
+0

我alredy做到了。我再次尝试,并没有帮助 – Alexander