Reputation: 2539
I have a ListView
that its items have some buttons as child/sub view (buttons like share, reply, like, etc). I want to disable clicks on items but not for childs. For example when clicking on a like button, its color changes and nothing happens to the list view item. (see nine gag app)
I read similar questions saying about overriding areAllItemsEnabled
and isEnabled
and I did that (returning false for both) but didn't get proper result (childs also are unabled)
Can you please help me out? And sorry for my bad English
in custom adapter:
public CustomCommentAdaptor(Context context){
ctx = context;
listData = new ArrayList();
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return false;
}
listview in main layout xml file
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:groupIndicator="@null"
android:layout_above="@+id/leave_comment" />
and listview items layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<!-- android:padding="10dip" -->
<RelativeLayout
android:id="@+id/comment_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:minHeight="40dp"
android:padding="5dp">
<ImageView
android:id="@+id/user_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@+drawable/brad" />
<TextView
android:id="@+id/comment_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="10dip"
android:layout_toLeftOf="@+id/user_image"
android:textSize="15sp" />
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="10dip"
android:layout_toLeftOf="@+id/user_image"
android:textSize="12sp" />
<RatingBar
style="@style/myRatingBar"
android:layout_height="12dp"
android:layout_width="wrap_content"
android:numStars="5"
android:rating="5"
android:isIndicator="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:stepSize="1"
android:layout_toLeftOf="@id/comment_title"
/>
</RelativeLayout>
<TextView
android:id="@+id/comment_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/comment_header"
android:textSize="8sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/comment_content"
android:orientation="horizontal"
>
<include
android:id="@+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="@layout/comment_actions" />
<include
android:id="@+id/dislike"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="@layout/comment_actions"
/>
<include
android:id="@+id/reply"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="@layout/comment_actions"
/>
<include
android:id="@+id/share"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="@layout/comment_actions"
/>
</LinearLayout>
</RelativeLayout>
Upvotes: 2
Views: 1633
Reputation: 2272
Set a click listener only on your buttons. So, in your adapter:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
View likeView = convertView.findViewById(R.id.like);
likeView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Do something.
}
}
}
Don't set a click listener on your list. So, if you are doing this anywhere, take it out!
listView.setOnItemClickListener(new OnItemClickListener() ...);
Finally, to make the like button change colors when pressed, set a selector as the background of your comment_actions
layout.
Upvotes: 1