ListView 与 Horizo​​ntalScrollView OnItemClick 不起作用

发布于 2024-12-29 13:14:36 字数 2437 浏览 3 评论 0原文

所以很快我就有了一个带有自定义适配器的listView,它会膨胀一个包含horizo​​ntalScrollView以及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");
        }});

任何帮助或建议将不胜感激

So really quickly i have a listView with a custom adapter and it inflates a view that contains a horizontalScrollView as well as a textview etc. The problem I am having is that when i try to attach a listener to this listView it is not receiving any callbacks.

I believe the problem has to do with the fact that my list item contains a scroll view which is intercepting the click events (although i thought it should only intercept other gestures).

code... (my list item 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>

and then in my 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");
        }});

Any help or suggestions would be greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情未る 2025-01-05 13:14:36

将其添加到最顶层的布局:

android:descendantFocusability="blocksDescendants"

它对我有用,现在 onItemClickListener 被触发。

Add this to the topmost Layout:

android:descendantFocusability="blocksDescendants"

It did the trick for me, now onItemClickListener is being fired.

允世 2025-01-05 13:14:36

您可以在适配器的 getView() 方法中调用 LinearMain 的 setOnClickListener 。

You can call setOnClickListener for linearMain in the getView() method of your adapter.

爱的故事 2025-01-05 13:14:36

对于ScrollView,您应该使用OnTouchListener,例如:

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));       
} 

for ScrollView you should use OnTouchListener, like:

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;
        }


    }

and in getView method of adapter:

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));       
} 
等风来 2025-01-05 13:14:36

在 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(...) 中写入:

View layout = view.findViewById(R.id.layout);
layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (callback != null) {
                callback.showItem(position);
            }
        }
    });

适配器下面写入:

public interface OnClickInterface {
    void showItem(int position);
}

并在适配器的构造函数中分配一个回调:

public CustomAdapter(Context context, List<YourClass> list, OnClickInterface callback, int resId) {
    this.callback = callback;
    ...

在 包含 ListView 的活动/片段创建一个适配器,如下所示:

CustomAdapter.OnClickInterface callback = new CustomAdapter.OnClickInterface() {
        @Override
        public void showItem(int position) {
            // Do something with an item.
        }
}
adapter = new Customdapter(getActivity(), new ArrayList<YourClass>(), callback, R.layout.list_item);

Inside list_item.xml mark your layout with some 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">

...

Open your custom adapter, in getView(...) write:

View layout = view.findViewById(R.id.layout);
layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (callback != null) {
                callback.showItem(position);
            }
        }
    });

Below in the adapter write:

public interface OnClickInterface {
    void showItem(int position);
}

And in the constructor of the adapter assign a callback:

public CustomAdapter(Context context, List<YourClass> list, OnClickInterface callback, int resId) {
    this.callback = callback;
    ...

In your activity/fragment that contains a ListView create an adapter like so:

CustomAdapter.OnClickInterface callback = new CustomAdapter.OnClickInterface() {
        @Override
        public void showItem(int position) {
            // Do something with an item.
        }
}
adapter = new Customdapter(getActivity(), new ArrayList<YourClass>(), callback, R.layout.list_item);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文