android listview 里面view的监听问题
部分listview布局:
<ListView
android:id="@+id/hehelistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:descendantFocusability="blocksDescendants"
android:divider="#00000000"
android:listSelector="#00000000" />
部分item布局:
iamgebutton:
<ImageView
android:id="@+id/hehelistview_item_imgbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:padding="2dp"
android:scaleType="fitXY"
android:src="@drawable/usermorenpic" />
button:
<Button
android:id="@+id/hehelistview_item_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/messagebtnpic"
android:focusable="false"
android:gravity="center"
android:text="消息"
android:textColor="#b63401" />
接下 展开
1、监听listview的itemclick(楼主实现办法:在activity监听)
2、在myadapter(继承baseadapter)里面监听view(imagebutton和button的onclick事件) (楼主实现办法:getview里面监听mViewHolder的两个按钮的onclick)
运行效果:
1、如果listview有1条数据。运行结果毫无问题。都能正确监听。
2、如果listview有多条数据,第1条数据的imgbutton和button监听有问题。(比如点击5次imgbutton,这时不会触发imgbutton的onclick的。再紧接着点击第2条数据的itemclick,奇特事件发生了,先触发itemclick事件,然后触发5次imgbutton的onclick事件。)
到这里,楼主就不懂了。(楼主就15个财富值。。。高手别吝啬吧。求指导)
2023-05-10 广告
public class Main extends Activity {
private List<Object[]> mDatas;
protected ListView mListView ;
protected BaseAdapter mAdapter;
protected LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflater = LayoutInflater.from(this);
initDatas();
initViews();
}
protected void initViews() {
mListView = (ListView) findViewById(R.id.list);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Main.this, "onItemClick - " + position, Toast.LENGTH_SHORT).show();
}
});
}
protected void initDatas() {
mDatas = new ArrayList<Object[]>();
mAdapter = new ListAdapter();
Object[] objs = null;
for(int i = 0; i < 20; i++) {
objs = new Object[3];
objs[0] = R.drawable.ic_launcher;
objs[1] = "TextView_" + i;
objs[2] = "Button_" + i;
mDatas.add(objs);
}
}
class ListAdapter extends BaseAdapter {
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView ;
if(view == null) {
view = inflater.inflate(R.layout.item, null);
View[] holders = new View[3];
holders[0] = view.findViewById(R.id.img);
holders[1] = view.findViewById(R.id.tv);
holders[2] = view.findViewById(R.id.btn);
for(int i = 0; i < 3; i++) {
if(i == 1)
continue;
//这里需要设置,不知道LZ设置没有
holders[i].setFocusable(false);
holders[i].setFocusableInTouchMode(false);
holders[i].setClickable(true);
holders[i].setOnClickListener(viewClicker);
}
view.setTag(holders);
}else{
view = convertView;
}
Object[] data = (Object[]) getItem(position);
View[] holders = (View[]) view.getTag();
ImageButton img = (ImageButton)holders[0];
TextView tv = (TextView) holders[1];
Button btn = (Button) holders[2];
img.setImageResource((Integer) data[0]);
tv.setText(data[1].toString());
btn.setText(data[2].toString());
img.setTag(position);
tv.setTag(position);
btn.setTag(position);
return view;
}
}
View.OnClickListener viewClicker = new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
String str = (v instanceof ImageView) ? "ImageView OnClick - " : "Button OnClick";
Toast.makeText(Main.this, str + position, Toast.LENGTH_SHORT).show();
}
};
}
你好 、你说的那几个属性我没全加。但是我加上之后问题依然存在 。总感觉是itemclick阻塞了onclick 。着急啊
那可以把你的代码片段发过来看看吗,主要是ListView的Adapter(yq135314@163.com)