点击TableRow和ListView的效果相同

3

我有一个动态列表视图:

<ListView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fastScrollEnabled="true"
    android:choiceMode="singleChoice"
    android:dividerHeight="1dp"
    android:divider="#1f000000"
    android:background="@android:color/background_light"
    android:drawSelectorOnTop="true"/>

使用单元格:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_ripple">

使用ripple:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/list_selected" android:state_activated="true"/>
</selector>

这个完美地运行了。

我的问题出现在我正在创建的另一个列表上。这是一个静态列表,所以我只是使用TableLayout和TableRow在xml中定义列表。有没有一种方法可以显示相同的“selected”状态?同时也能显示涟漪效果吗?

    <TableLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0">

        <TableRow android:id="@+id/about_row"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="16dp"
            android:paddingLeft="16dp"
            android:paddingBottom="20dp"
            android:clickable="true"
            android:background="@drawable/list_ripple">

我试图将涟漪效果放在背景元素上,但并没有起作用。最好的情况是能够显示灰色的选定状态,以表明该表格行已被选中,并尽可能地显示涟漪效果。


到目前为止,我发现最好的方法是使用ListView并实现BaseAdapter来自定义静态行的样式。这样一来,我就可以获得ListView的涟漪和活动行为。 - lostintranslation
2个回答

1

这适用于TableLayout内部的单个TableRow,谢谢。 - lasec0203

1
我已经通过动态添加涟漪的方式完成了它,您可以尝试静态和动态两种方式。(v21)
您的涟漪文件:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="?android:textColorHintInverse" />
        </shape>
    </item>
</ripple>

让你的row.setClickable(true),并在点击监听器中设置背景为下面的涟漪图案:
tableRow.setOnClickListener(new View.OnClickListener() {
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onClick(View v) {
                    tableRow.setBackground(mContext.getDrawable(R.drawable.list_ripple));
                    Snackbar.make(v, "row Clicked", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

希望有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接