所以我很快就有了一个带有自定义适配器的listView,并且它包含了一个包含horizontalScrollView和textview等的视图。我遇到的问题是当我尝试将一个监听器附加到这个listView时它不是收到任何回调。
我认为问题与我的列表项包含滚动视图这一事实有关,这个视图拦截了点击事件(虽然我认为它应该只拦截其他手势)。
代码...(我的列表项xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey" >
<TextView
android:id="@+id/headerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="4dp"
android:textColor="@color/text_header"
android:textStyle="bold"
android:text="TextView" />
</RelativeLayout>
<View
android:background="@color/border"
android:layout_width="fill_parent"
android:layout_height="1px" />
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="4dp">
</LinearLayout>
</HorizontalScrollView>
<View
android:background="@color/border"
android:layout_width="fill_parent"
android:layout_height="1px" />
</LinearLayout>
然后在我的onCreate ......
lv.setAdapter(myobj.adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.w("dsdsds", "sdsdsds");
}});
非常感谢任何帮助或建议
答案 0 :(得分:3)
将其添加到最顶层的布局:
android:descendantFocusability="blocksDescendants"
它为我做了诀窍,现在onItemClickListener正在被解雇。
答案 1 :(得分:1)
您可以在适配器的getView()方法中为lineMain调用setOnClickListener。
答案 2 :(得分:1)
private class ScrollViewOnTouchListener implements OnTouchListener{
HorizontalScrollView view;
float historicX = Float.NaN;
static final int DELTA_ON_CLICK = 20;
long historicTime;
int position;
public ScrollViewOnTouchListener(HorizontalScrollView view, int position){
this.view = view;
this.position = position;
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
historicX = event.getX();
historicTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP:
if (Math.abs(event.getX() - historicX) < DELTA_ON_CLICK)
// seems like onclick
// do what you want for onClick
break;
default: return false;
}
return true;
}
}
和适配器的getView方法:
public View getView(int position, View convertView, ViewGroup parent) {
...
HorizontalScrollView scrollItem;
scrollItem =( HorizontalScrollView )itemView.findViewById(R.id.item_scroll_view);
scrollItem .setOnTouchListener(new ScrollViewOnTouchListener(scrollItem , position));
}
答案 3 :(得分:0)
在list_item.xml里面用一些id标记你的布局:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
...
在getView(...)
写:
layout = view.findViewById(R.id.layout);
layout.setTag(position);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (callback != null) {
callback.showItem(getItem((int) v.getTag()));
}
}
});
在适配器下面写:
public interface OnClickInterface {
void showItem(YourClass item);
}
在适配器的构造函数中分配一个回调:
public CustomAdapter(Context context, List<YourClass> list, OnClickInterface callback, int resId) {
this.callback = callback;
...
在包含ListView的activity / fragment中创建一个如下所示的适配器:
CustomAdapter.OnClickInterface callback = new CustomAdapter.OnClickInterface() {
@Override
public void showItem(YourClass item) {
// Do something with an item.
}
}
adapter = new Customdapter(getActivity(), new ArrayList<YourClass>(), callback, R.layout.list_item);