【问题标题】:Compound Drawable as Action in RecyclerView复合 Drawable 作为 RecyclerView 中的操作
【发布时间】:2016-01-10 05:36:12
【问题描述】:
我有一个实现字符串项目线性列表的 RecyclerView。
我希望列表项在 每个 项的右侧有一个辅助操作“共享”。我在 XML 中创建了一个像 Google 建议的复合可绘制对象,但我如何访问可绘制对象并在其上设置 OnClickListener()?我最好只有两个元素吗?
在我的列表项布局中复合 Drawable TextView:
<TextView
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/text_margin"
android:textSize="17sp"
android:layout_marginTop="1dp"
android:background="#101010"
android:drawableRight="@drawable/ic_share_white_24dp"
android:textColor="#fff"
android:textAppearance="?attr/textAppearanceListItem" />
【问题讨论】:
标签:
android
android-layout
android-drawable
android-recyclerview
【解决方案1】:
得到了一些很好的提示,但特别是对于我的项目,我发现拥有两个元素会更好地管理。查看@Vishnuvathsan 对此StackOverflow post 的复合drawable 的回答。
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/text_margin"
android:textSize="17sp"
android:layout_marginTop="1dp"
android:background="#101010"
android:textColor="#fff"
android:textAppearance="?attr/textAppearanceListItem" />
<ImageView
android:id="@+id/shareBtn"
android:contentDescription="@string/action_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:src="@drawable/ic_share_white_24dp"
android:background="#101010"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />