getItemViewType在RecyclerView
问题描述:
型动物节项目我使用的FlexibleAdapter 图书馆我有3头和3 SectionItems, 我需要实现这些部分项目库:Slice 在每个SectionItems显示半径弯道,getItemViewType在RecyclerView
但是半径仅归于第一部分,我不知道或者是问题所在。我无法获得其他部分的圆角。
public class SectionItem extends AbstractSectionableItem<SectionItem.DataCellViewHolder,HeaderItem> {
public static String NAME = SectionItem.class.getName();
public class DataCellViewHolder extends FlexibleViewHolder implements DataCellViewHolder2 {
FrameLayout frame;
TextView title;
DataCellViewHolder(View itemView, FlexibleAdapter adapter) {
super(itemView, adapter);
frame = (FrameLayout)itemView.findViewById(R.id.frame);
title = (TextView) itemView.findViewById(R.id.title);
}
@Override
public FrameLayout getFrame() {
return frame;
}
@Override
public int getItemCount() {
mAdapter.getSectionItems(getHeader()).size();
}
@Override
public int getItemViewType(int position) {
if (position==1){
return VIEW_TYPE_TOP;
} else if (position == getItemCount()) {
return VIEW_TYPE_BOTTOM;
} else {
return VIEW_TYPE_CENTER;
}
}
}
private String title;
public SectionItem(String title, HeaderItem header) {
super(header);
this.title = title;
setDraggable(true);
}
public String getTitle() {
return title;
}
@Override
public HeaderItem getHeader() {
return super.getHeader();
}
public void setTitle(String title) {
this.title = title;
}
@Override
public void bindViewHolder(FlexibleAdapter adapter, DataCellViewHolder holder, int position, List payloads) {
holder.title.setText(title);
Slice slice = new Slice(holder.getFrame());
slice.setElevation(2.0f);
int view = holder.getItemViewType(position);
if (view == VIEW_TYPE_TOP) {
slice.setRadius(8.0f);
slice.showLeftTopRect(false);
slice.showRightTopRect(false);
slice.showRightBottomRect(true);
slice.showLeftBottomRect(true);
slice.showTopEdgeShadow(true);
slice.showBottomEdgeShadow(false);
} else if (view == VIEW_TYPE_BOTTOM) {
slice.setRadius(8.0f);
slice.showLeftTopRect(true);
slice.showRightTopRect(true);
slice.showRightBottomRect(false);
slice.showLeftBottomRect(false);
slice.showTopEdgeShadow(false);
slice.showBottomEdgeShadow(true);
} else {
slice.setRadius(0.0f);
slice.showTopEdgeShadow(false);
slice.showBottomEdgeShadow(false);
}
}
@Override
public boolean equals(Object inObject) {
if (inObject instanceof SectionItem) {
SectionItem inItem = (SectionItem) inObject;
return this.title.equals(inItem.title);
}
return false;
}
@Override
public int getLayoutRes() {
return R.layout.item_center;
}
@Override
public DataCellViewHolder createViewHolder(View view, FlexibleAdapter adapter) {
Log.w(NAME, " : createViewHolder");
return new DataCellViewHolder(view, adapter);
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3be13e"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="10dp"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.ahmed.flexibleadapterdemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
item_center.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/plus_sign">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:layout_marginTop="0dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="0dp"
android:clickable="true">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_margin="0dp" />
</FrameLayout>
</FrameLayout>
答
我不知道该怎么FlexibleAdapter工作,所以我建议使用ATTR:背景你的视角是“R.id.frame”来制作半径;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp"/>
<solid android:color="@color/color_bg"/>
</shape>
+0
我试试这个方法,但不幸的是它返回的所有项目的背景,我想一个部分一个 –
+0
@RabahBoulhares哎呀,不能帮你。 –
你可以发布项目使用的xmls代码吗? –
的职位是编辑处理XML文件 –