OnItemCLickLIstener不上的ListView工作 [英] OnItemCLickLIstener doesn't work on ListView
问题描述
我有一个ListView的活动。 ListView的使用自定义视图。我想补充OnItemClickLIstener到ListView。当我点击项目,在结果,我什么也看不见。
与ListView的活动:
I have an activity with a ListView. ListView with custom views. I add OnItemClickLIstener to the ListView. and when i click on item, in result i see nothing. Activity with ListView:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/silver_conv">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="40dp" android:layout_alignParentTop="true" android:id="@+id/topcontainer"
android:background="@color/black">
</FrameLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/chat_list" android:layout_below="@+id/topcontainer"
android:layout_above="@+id/last_action"
android:cacheColorHint="#00000000"
android:layout_marginRight="2dp" android:clickable="true"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="last action was at time" android:id="@+id/last_action"
android:longClickable="false"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="false" android:layout_above="@+id/action_container"
android:layout_marginBottom="5dp" android:layout_alignParentLeft="false" android:layout_marginLeft="5dp"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="43dp" android:layout_alignParentBottom="true" android:id="@+id/action_container"
android:background="@drawable/conv_botom_action_gradient">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/send"
android:id="@+id/send_message_btn" android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="@drawable/blue_button_selector" android:textColor="@color/white"
android:layout_marginLeft="3dp" android:layout_marginTop="3dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add_attach_btn"
android:layout_alignParentLeft="true" android:layout_alignParentBottom="true"
android:background="@drawable/add_attach_button_selector"
android:layout_marginRight="2dp" android:layout_marginBottom="3dp" android:layout_marginTop="3dp"/>
<EditText android:layout_width="40dp" android:layout_height="wrap_content" android:id="@+id/message_et"
android:layout_toRightOf="@+id/add_attach_btn" android:layout_toLeftOf="@+id/send_message_btn"
android:singleLine="true" android:layout_alignParentBottom="true" android:hint="Type message here"
android:background="@drawable/message_input" android:layout_marginBottom="3dp"
android:gravity="center_vertical" android:layout_marginTop="3dp"/>
</RelativeLayout>
查看资料:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:gravity="center_vertical|left" android:focusable="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/incoming_message"
android:id="@+id/container" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:focusable="false">
<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:focusableInTouchMode="true" android:id="@+id/attach_container" android:focusable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="saa"
android:id="@+id/message_text" android:textSize="17sp" android:textColor="@color/black"
android:focusable="false"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/date" android:textColor="@color/black" android:singleLine="true" android:lines="1"
android:maxLines="1" android:ellipsize="none" android:layout_marginLeft="5dp" android:focusable="false"/>
最后clickListener:
And finally clickListener:
private AdapterView.OnItemClickListener clickLister = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
try {
LinearLayout container = (LinearLayout)view.findViewById(R.id.container);
TextView message = (TextView)container.findViewById(R.id.message_text);
message.setTextColor(mContext.getResources().getColor(R.color.white));
Log.e("My item is", "" + pos);
}catch (Exception e){
e.printStackTrace();
}
}
};
这是ListView控件初始化:
And here is Initialization of ListView:
mConvListView = (ListView)findViewById(R.id.chat_list);
mConvListView.setDivider(null);
mConvListView.setDividerHeight(0);
mConvListView.setItemsCanFocus(false);
mConvListView.setOnItemClickListener(clickLister);
mConvListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
mConvListView.setStackFromBottom(true);
P.S。对不起,很多code。但是,我找不到任何建议第二天。
P.s. Sorry for a lot code. But I can't find any suggestion a second day.
推荐答案
通过在您排布置设置聚焦的对象,你是preventing从得到的触摸事件ListView控件。
By setting focusable objects in your row layout, you are preventing the ListView from getting the touch event.
这是的FrameLayout消耗触摸事件:
This FrameLayout is consuming the touch event:
<FrameLayout
android:id="@+id/attach_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:focusable="false"/>
删除可获得焦点的设置,所以它看起来是这样的:
Remove the focusable settings so it looks like this:
<FrameLayout
android:id="@+id/attach_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
(你真的应该组织你的XML这样即是可读的,在Eclipse中使用<大骨节病>控制骨节病> + <大骨节病>移骨节病> + <大骨节病>˚F骨节病>)
这篇关于OnItemCLickLIstener不上的ListView工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!