我的OnItemClickListener
无法使用ListView
我就像这样设置:
ChatAdapter ca = new ChatAdapter(this, chatProfileList);
chatListView.setAdapter(ca);
chatListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(ChatAct.this, "Yay its clicked.", Toast.LENGTH_LONG).show();
}
});
错误:当我点击列表项时没有任何反应,它根本没有触发。
事实:
尝试:
android:descendantFocusability="blocksDescendants"
添加到list_item.xml的根目录android:focusable="false"
添加到list_item.xml android:focusableInTouchMode="false"
添加到list_item.xml android:clickable="false"
添加到list_item.xml 我不能添加声誉奖励YET,但第一个工作解决方案将获得100个代表。
请帮助,Android再次如此随机。
E D I T:
我的适配器没有任何东西(?)来窃取焦点
public class ChatAdapter extends BaseAdapter {
public List<ChatUnit> adapterData = new ArrayList<ChatUnit>();
LayoutInflater inflater;
Activity act;
public ChatAdapter(Activity act, List<ChatUnit> adapterData) {
this.act = act;
inflater = LayoutInflater.from(act);
this.adapterData = adapterData;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
ChatListView chatListItemView = (ChatListView) convertView;
if (chatListItemView == null) {
chatListItemView = new ChatListView(act);
chatListItemView.initView(this.act);
viewHolder = new ViewHolder();
viewHolder.nameTv = (TextView) chatListItemView.findViewById(R.id.chat_name_tv);
viewHolder.iconIv = (ImageView) chatListItemView.findViewById(R.id.chat_profile_iv);
viewHolder.gameRequestsTv = (TextView) chatListItemView.findViewById(R.id.chat_naptar_tv);
viewHolder.newMessagesTv = (TextView) chatListItemView.findViewById(R.id.chat_bubi_tv);
viewHolder.backgroundViewGroup = (ViewGroup) chatListItemView.findViewById(R.id.chat_list_item_backgroun);
viewHolder.chatSchedImg = (ImageView) chatListItemView.findViewById(R.id.chat_sched_img);
chatListItemView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) chatListItemView.getTag();
}
final ChatUnit chatUnit = adapterData.get(position);
try {
if (chatUnit != null) {
viewHolder.nameTv.setText(chatUnit.name);
viewHolder.gameRequestsTv.setText(chatUnit.gameRequest);
viewHolder.newMessagesTv.setText(chatUnit.newMessage);
Picasso.with(this.act).load(chatUnit.pictureUrl).into(viewHolder.iconIv);
// Set!!!!
chatListItemView.setData(chatUnit);
}
} catch (Exception e) {
e.printStackTrace();
}
return chatListItemView;
}
class ViewHolder {
// ..
public ViewGroup backgroundViewGroup;
public TextView nameTv;
public ImageView iconIv;
public TextView gameRequestsTv;
public TextView newMessagesTv;
public ImageView chatSchedImg;
}
@Override
public int getCount() {
if (adapterData != null) {
return adapterData.size();
} else {
return 0;
}
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public List<ChatUnit> getAdapterData() {
return adapterData;
}
}
我的list_item_view.xml:
不包含任何请求焦点或有任何可点击内容的视图。
<ImageView
android:id="@+id/chat_profile_iv"
android:layout_width="51dp"
android:layout_height="70dp"
android:layout_marginRight="7dp"
android:focusable="false"
android:src="@drawable/male1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="7dp"
android:layout_weight="1"
android:focusable="false"
android:orientation="vertical" >
<hu.illion.sporttech.views.other.SportTechTextView
android:id="@+id/chat_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:ellipsize="end"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center|left"
android:text="Ez egy név itt"
android:textColor="#fff"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false" >
<ImageView
android:id="@+id/chat_online_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="false"
android:src="@drawable/online" />
<TextView
android:id="@+id/chat_online_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="3dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Online"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#68ff5d" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:focusable="false"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false" >
<TextView
android:id="@+id/chat_bubi_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="false"
android:text="0"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#8b8b8b"
android:textSize="12sp" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:focusable="false" >
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:src="@drawable/chatke" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false" >
<TextView
android:id="@+id/chat_naptar_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="0"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#8b8b8b"
android:textSize="12sp" />
<ImageView
android:id="@+id/chat_sched_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="false"
android:src="@drawable/naptarka" />
</LinearLayout>
</LinearLayout>
E D I T 2:
正如你们中的一些人所说的那样,我逐个删除了我的元素,现在我最终得到了一个线性布局。还是行不通。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_list_item_backgroun"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#123"
android:orientation="horizontal"
>
答案 0 :(得分:1)
如果列表中膨胀视图中的某个元素正在接收触摸事件(onClick
,onKeyDown
等),它将在到达列表项之前被消耗,因此听众不会被称为