我的ListView
项目布局在中心使用了HorizontalScrollView
。我在父android:descendantFocusability="blocksDescendants"
上使用了android属性“LinearLayout
”,因此ListView
项仍然可以选择。
我遇到的问题是,当点击ListView
项HorizontalScrollView
的部分时,不会调用ListView
项目点击事件。
如何让HorizontalScrollView
的点击事件调用ListView
列表项单击事件?
答案 0 :(得分:1)
HorizontalScrollView没有“onClick()”,请看这个 http://developer.android.com/reference/android/widget/HorizontalScrollView.html
它支持手势并且“onTouchEvent(MotionEvent ev)”
因此您可以将其用作点击。请参阅我准备的以下演示。
// Followin code will not work for HorizontalScrollView
/*hsv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(HorizontalListActivity.this, tvMiddle.getText().toString().trim(), Toast.LENGTH_SHORT).show();
}
});*/
hsv1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(YourActivity.this, "Your Msg", Toast.LENGTH_SHORT).show();
return false;
}
});
答案 1 :(得分:0)
如下所示,将布尔变量touchDown和touchUp添加到适配器类似乎工作得很好:
private class MyListAdapter extends ArrayAdapter<MyObject>{
...
//touch down + touch up with no other motion events in between = click
boolean touchDown = false;
boolean touchUp = false;
private int iHostViewID;
...
public MyListAdapter(Context context,int viewResourceId, List<MyObject> objects) {
super(context, textViewResourceId, objects);
iHostViewID = viewResourceId;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent){
View itemView = convertView;
//iff we cannot re-use a view
if(itemView == null){
LayoutInflater inflater = (
(LayoutInflater)hContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(iHostViewID, null);
}
final View hItemView = itemView;
final int hPosition = pos;
...
final HorizontalScrollView textDataSV =
(HorizontalScrollView)itemView.findViewById(R.id.widget_hsv);
textDataSV.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
touchDown = true;
}
else if(event.getAction() == MotionEvent.ACTION_UP){
touchUp = true;
}
else{
touchDown = false;
touchUp = false;
}
if(touchDown && touchUp){
//click
//mMyListView is the reference to the list view
//instantiated in the view controller class responsible
//for setting this adapter class as the list view's adapter
mMyListView.performItemClick(hItemView, hPosition,
hItemView.getId());
}
return false;
}
});
}
}
这远非完美,但应该适用于大多数标准用例