Android 具有彩色背景和波纹效果的列表视图选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25443496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listview selector with colored background and ripple effect
提问by artkoenig
Standard ListView
selector in android L developer preview uses colorControlHighlight
for the ripple effect on touch and has a transparent background in unfocused state.
ListView
android L 开发者预览版中的标准选择器colorControlHighlight
用于触摸时的涟漪效果,并在未聚焦状态下具有透明背景。
I would like to define a ListView
item that has a colored background and still shows the ripple effect on touch with the same highlight color. Now, if I define the following drawable:
我想定义一个ListView
具有彩色背景的项目,并且仍然显示具有相同高亮颜色的触摸时的涟漪效果。现在,如果我定义以下可绘制对象:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="@color/my_background_color"/>
</ripple>
it works, but the ripple starts in the middle of the ListView
item, regardless of the touch position. If I use the same background outside of the ListView
, e.g. for a LinearLayout
, it works like expected (the ripple starts on the touch position).
它有效,但波纹从ListView
项目的中间开始,无论触摸位置如何。如果我在 之外使用相同的背景ListView
,例如对于 a LinearLayout
,它会像预期的那样工作(波纹从触摸位置开始)。
回答by dyancat
I've managed to get individually colored list items while maintaining the ripple effect. Set the background of your list items using whatever adapter you have and set the listview to show the selector on top:
我设法在保持涟漪效应的同时获得了单独着色的列表项。使用您拥有的任何适配器设置列表项的背景,并将列表视图设置为在顶部显示选择器:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true" />
This will draw the ripple effect above the background.
这将在背景上方绘制波纹效果。
回答by miguel
As far as I can tell this bug is only in Android 5.0, not 5.1. The trick seems to be to use Drawable#setHotspot as a Google dev hints to here https://twitter.com/crafty/status/561768446149410816(because obscure twitter hints are a great form of documentation!)
据我所知,这个错误只存在于 Android 5.0,而不是 5.1。诀窍似乎是使用 Drawable#setHotspot 作为谷歌开发人员的提示https://twitter.com/crafty/status/561768446149410816(因为晦涩的推特提示是一种很好的文档形式!)
Assume you have a row layout something like this
假设你有一个像这样的行布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/row_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="?attr/selectableItemBackground">
.... content here .....
</LinearLayout>
</FrameLayout>
The following worked for me
以下对我有用
row.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.findViewById(R.id.row_content)
.getBackground()
.setHotspot(event.getX(), event.getY());
return(false);
}
});
回答by JoeyCK
I've found that it only seems to work correctly if you apply the background to the root elementof the list item.
我发现只有将背景应用于列表项的根元素,它才能正常工作。
Also, consider using the new RecyclerViewinstead of a ListView
另外,考虑使用新的RecyclerView而不是 ListView
List item view example:
列表项视图示例:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/list_padding"
android:layout_marginLeft="@dimen/list_padding"
android:layout_marginRight="@dimen/list_padding"
android:padding="@dimen/list_padding"
android:background="@drawable/ripple_bg">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:id="@+id/tvTitle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:id="@+id/tvSubtitle" />
</RelativeLayout>
回答by Arhat Baid
The sample layout, which contains the Ripple effect as Background of the the parent layout.
示例布局,其中包含波纹效果作为父布局的背景。
<RelativeLayout
android:id="@+id/id4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_effect"
android:clickable="true">
<ImageView
android:id="@+id/id3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/image"
android:layout_centerVertical="true"/>
<LinearLayout
android:id="@+id/id2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:layout_centerVertical="true">
<TextView
android:id="@+id/id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"/>
</LinearLayout>
</RelativeLayout>
Ripple_effect.xml Here you can use any colour of you choice. Make sure that you use sdk version 21 and have drawable-v21 and style-v21 folder and put all the file related to v21 in them.
Ripple_effect.xml 在这里您可以使用您选择的任何颜色。确保您使用 sdk 版本 21 并具有 drawable-v21 和 style-v21 文件夹,并将与 v21 相关的所有文件放入其中。
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
Here you can use different shape like rectangle instead of oval...
在这里你可以使用不同的形状,比如矩形而不是椭圆形......
回答by cV2
i adapted @ArhatBaid 's answer a littlebit, tested it and it works perfectly:
我稍微调整了@ArhatBaid 的回答,对其进行了测试,效果很好:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="@color/light_grey_header_navigation_drawer"/>
</ripple>
So, this allows you to set a background color and still have the ripple effect.
for me target and minSdk are 21.
因此,这允许您设置背景颜色并仍然具有波纹效果。
对我来说 target 和 minSdk 是 21。
回答by Sascha
You can achieve this with a nested Layout. Just create e.g. a LinearLayout as root layout around your existing layout, set the ripple effect on the root layout and your background color to the nested one.
您可以使用嵌套布局来实现这一点。只需在现有布局周围创建一个 LinearLayout 作为根布局,将根布局上的涟漪效果和背景颜色设置为嵌套的。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_effect"
android:orientation="vertical">
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/containterContent"
android:background="@color/yourCOLOR"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your content -->
</RelativeLayout>
</LinearLayout>