我想为我的Android应用程序创建自定义行适配器。 所以我建立了这个:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp"
android:clickable="true"
android:orientation="vertical">
<TextView
android:id="@+id/medication"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold"
android:layout_width="120dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/instructions"
android:textColor="@color/white"
android:layout_toRightOf="@+id/medication"
android:layout_width="170dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/startDate"
android:textColor="@color/white"
android:layout_width="100dp"
android:layout_toRightOf="@+id/instructions"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/active"
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_toRightOf="@+id/startDate"
android:layout_height="wrap_content"
/>
</RelativeLayout>
现在我已经构建了这个适配器类“MedicationAdapter.java”:
public class MedicationAdapter extends RecyclerView.Adapter<MedicationAdapter.MyMedicationViewHolder> {
private List<Medication> moviesList;
public class MyMedicationViewHolder extends RecyclerView.ViewHolder {
public TextView medication, instruction, startDate,active;
public MyMedicationViewHolder(View view) {
super(view);
medication = (TextView) view.findViewById(R.id.medication);
instruction = (TextView) view.findViewById(R.id.instructions);
startDate = (TextView) view.findViewById(R.id.startDate);
active = (TextView) view.findViewById(R.id.active);
}
}
public MedicationAdapter(List<Medication> moviesList) {
this.moviesList = moviesList;
}
@Override
public MyMedicationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.medications_list_row, parent, false);
return new MyMedicationViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyMedicationViewHolder holder, int position) {
Medication medication = moviesList.get(position);
holder.medication.setText(medication.getDrugInfo().getDisplayName());
holder.instruction.setText(medication.getFrequency()+" "+ medication.getDose());
holder.startDate.setText(medication.getDateStart());
holder.active.setText("SI");
}
@Override
public int getItemCount() {
return moviesList.size();
}
}
现在,如果我尝试运行我的Android应用程序,我可以看到我的活动:
现在我想在此列表的头部插入标题文本,并且我想在每个单元格中插入边框线。
可以这样做吗?
问候
答案 0 :(得分:1)
是的,这是可能的。您可以为RecyclerView实现标头。 请看这个答案https://stackoverflow.com/a/26573338/1554094
您可以使用item decoration。对同一问题How to add dividers and spaces between items in RecyclerView?
有答案或者您可以在视图项目底部添加高度为1dp的视图
答案 1 :(得分:0)
您可以尝试以下代码在recyclerview中设置标题
public class HeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
String[] data;
public HeaderAdapter(String[] data) {
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
//inflate your layout and pass it to view holder
return new VHItem(null);
} else if (viewType == TYPE_HEADER) {
//inflate your layout and pass it to view holder
return new VHHeader(null);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof VHItem) {
String dataItem = getItem(position);
//cast holder to VHItem and set data
} else if (holder instanceof VHHeader) {
//cast holder to VHHeader and set data for header.
}
}
@Override
public int getItemCount() {
return data.length + 1;
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
private String getItem(int position) {
return data[position - 1];
}
class VHItem extends RecyclerView.ViewHolder {
TextView title;
public VHItem(View itemView) {
super(itemView);
}
}
class VHHeader extends RecyclerView.ViewHolder {
Button button;
public VHHeader(View itemView) {
super(itemView);
}
}
}
答案 2 :(得分:0)
for Recyclerview中的边框
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
//设置适配器 recyclerView.setAdapter(mAdapter);
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}