将“Horizo​​ntalScrollView”添加到listview后,“setOnItemClickListener / setOnItemLongClickListener”无法正常工作

时间:2017-04-25 15:55:02

标签: android listview android-arrayadapter onclicklistener

我让听众的工作对我的'标准'列表视图很好,但是我想让各个listview项目可以水平滚动,这需要大量的试验和错误,但它终于奏效了。但是,这使我在听取任何点击时的敏感度。我希望有一个简单的解决方法,比如添加另一个属性等。

这是我的听众(最小化):

+-------------------+---------+
|                url| category|
+-------------------+---------+
| http://example.com|category1|
|http://example1.com|category1|
| http://example.com|category3|
|http://example1.com|category3|
+-------------------+---------+

这是我的xml:

        listViewAllCards.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {

                Log.i("LOG: (DP) SHORT","position " + position + " ID: " + id);

                switch (position) {

                // Training
                case 0:
                break;
              }
            }
        });

[编辑]有趣的是,当我单击水平滚动区域时,它可以正常工作,因此可能是滚动从listview项目中窃取“焦点”的问题:

enter image description here

[编辑]我在类代码中添加了以下内容,OnItem监听器仍然在'onCreate'代码段中(我希望没问题)。我看到这个射击,但对实际物品仍然没有敏感度:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/database_page_list_selector"
                android:orientation="horizontal"
                android:padding="5dp">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/thumbnailDatabase"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="5dp"
            android:background="@drawable/gradient_blue_bg"
            android:orientation="horizontal"
            android:padding="3dp"
            android:weightSum="1">

            <TextView
                android:text="KNOW"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#040404"
                android:textSize="12sp"
                android:textStyle="italic"
                android:scrollHorizontally="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"
                android:id="@+id/databaseListViewKnowLevel" />

            <TextView
                android:text="SLEEP"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#040404"
                android:textSize="12sp"
                android:textStyle="italic"
                android:scrollHorizontally="true"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true"
                android:id="@+id/databaseListViewSleepLevel" />

            <TextView
                android:id="@+id/txtlvDictionaryLookUpForeignWord"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/databaseListViewKnowLevel"
                android:background="@android:color/transparent"
                android:cacheColorHint="@android:color/transparent"
                android:ellipsize="end"
                android:listSelector="@android:color/transparent"
                android:maxLines="2"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:scrollHorizontally="true"
                android:text="CHAR"
                android:textColor="#FFFFFF"
                android:textSize="24sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/databaseListViewForeignAlt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/txtlvDictionaryLookUpForeignWord"
                android:text="ALT"
                android:textColor="#040404"
                android:textSize="12sp"
                android:scrollHorizontally="true"
                android:textStyle="italic" />

            <TextView
                android:id="@+id/databaseListViewForeignWord"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/databaseListViewForeignAlt"
                android:paddingLeft="10dp"
                android:text="WORD"
                android:textColor="#040404"
                android:textSize="12sp"
                android:scrollHorizontally="true"
                android:textStyle="italic" />

            <TextView
                android:id="@+id/databaseListViewEnglishWord"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ENGLISH"
                android:textColor="#040404"
                android:textSize="12sp"
                android:textStyle="italic"
                android:layout_alignParentBottom="true"
                android:ellipsize="end"
                android:maxLines="2"
                android:scrollHorizontally="true"
                android:layout_toRightOf="@+id/txtlvDictionaryLookUpForeignWord"
                android:layout_toEndOf="@+id/txtlvDictionaryLookUpForeignWord" />

        </RelativeLayout>

    </HorizontalScrollView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

添加:

android:descendantFocusability="blocksDescendants"

到列表项的父布局..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:descendantFocusability="blocksDescendants"   //add this
            android:background="@drawable/database_page_list_selector"
            android:orientation="horizontal"
            android:padding="5dp">

修改

requestDisallowInterceptTouchEvent(true);触摸事件添加scrollview

scrollView1 = (ScrollView) findViewById(R.id.scrollview);
scrollView1.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        scrollView1.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});