Android 如何创建符合物料指南的ListView项目
如何像这样创建ListView 谷歌有相关文档 但是没有解释如何做到这一点 这是我迄今为止的尝试,我不确定边距/填充和分隔符,是否有任何教程如何制作此布局 这是RecyclerView项目布局:Android 如何创建符合物料指南的ListView项目,android,android-recyclerview,material-design,Android,Android Recyclerview,Material Design,如何像这样创建ListView 谷歌有相关文档 但是没有解释如何做到这一点 这是我迄今为止的尝试,我不确定边距/填充和分隔符,是否有任何教程如何制作此布局 这是RecyclerView项目布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:transitionName="parent_view"
tools:ignore="UnusedAttribute">
<ImageView
android:id="@+id/image_layanan"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="16dp"
android:transitionName="image_layanan"/>
<TextView
android:id="@+id/text_layanan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:layout_toEndOf="@id/image_layanan"
android:layout_toRightOf="@+id/image_layanan"
android:textAppearance="?attr/textAppearanceListItem"
android:textSize="16sp"
android:transitionName="text_layanan"
tools:text="string/item_title"/>
<TextView
android:id="@+id/text_dokter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_layanan"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="@+id/image_layanan"
android:layout_toRightOf="@+id/image_layanan"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceListItem"
android:textColor="#212121"
android:textSize="14sp"
android:transitionName="text_dokter"
tools:text="string/item_desc"/>
<TextView
android:id="@+id/text_jam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_dokter"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="@id/image_layanan"
android:layout_toRightOf="@id/image_layanan"
android:textSize="13sp"
android:transitionName="text_jam"/>
<TextView
android:id="@+id/text_pasien"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_jam"
android:layout_marginBottom="14dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="@id/image_layanan"
android:layout_toRightOf="@id/image_layanan"
android:textSize="13sp"
android:transitionName="text_pasien"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignLeft="@+id/text_pasien"
android:layout_alignStart="@+id/text_pasien"
android:layout_below="@+id/text_pasien"
android:background="#212121"/>
</RelativeLayout>
要获得部分除法器(或任何名称),我使用以下方法:
创建一个类extendRecyclerView.ItemDecoration
public class RecyclerViewItemDivider extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private Drawable mDivider;
public RecyclerViewItemDivider(Context context) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
// this is the left start point for divider,
// I think there is better method without hardcoded the view
// main_content is my RectclerView item main content
// R.dimen.activity_horizontal_margin should be 16dp if use google guidelines
int left = (int)parent.findViewById(R.id.main_content).getX() +
(int)parent.getContext().getResources().getDimension(R.dimen.activity_horizontal_margin);
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
你想知道什么?关于设计部分?或者在Java中完整实现设计部分,我认为我的设计是不对的。例如,将父布局作为解决方案的一部分进行分割,使其无法使用。视图需要是通用的。
RecyclerViewItemDivider divider = new RecyclerViewItemDivider(recyclerView.getContext());
recyclerView.addItemDecoration(divider);