Same effect on TableRow clicks on ListView click
I have a dynamic list view:
<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"/>
With cell:
<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">
With ripple:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/list_selected" android:state_activated="true"/>
</selector>
This works great.
My problem is with another list I am creating. Its a static list, so I just define the list in xml using TableLayout and TableRow. Is there any way that I can get the same "selected" state to show. And also a ripple to show?
<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">
I tried to put ripples on the background element, but it didn't work. Again, the best thing would be to get a gray selected state showing that the table row is selected and ripples if possible.
+3
source to share
1 answer
I did this by adding dynamic ripple dynamically, you can try both static and dynamic. (v21)
your ripples:
<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>
do row.setClickable(true)
and
on your background screen to listen for clicks to draw like below:
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();
}
});
hope to be helpful
0
source to share