【问题标题】:Same effect on TableRow click as ListView clickTableRow 单击与 ListView 单击的效果相同
【发布时间】:2015-08-08 22:22:42
【问题描述】:

我有一个动态列表视图:

<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">

有波纹:

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

这很好用。

我的问题在我正在创建的另一个列表中。它是一个静态列表,所以我只是使用 TableLayout 和 TableRow 在 xml 中定义列表。有没有一种方法可以让我显示相同的“选定”状态。还有要显示的涟漪?

    <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 来设置我想要的静态行样式。那时我得到列表视图涟漪和活动行为。

标签: android android-listview android-tablelayout


【解决方案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();
                }
            });

希望对你有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-07
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多