-1

I want to create a custom row adapter to my Android application. So I have build this:

<?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>

Now I have build this adaptor class "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();
    }
}

Now if I try to run my Android application, I can see my activity like this:

enter image description here

Now I want to insert a title Text in the head of this list, and I want to insert a border line to every cell.

It is possible to do this?

regards

bircastri
  • 2,169
  • 13
  • 50
  • 119

3 Answers3

1

Yes, it's possible. You can implement header for your RecyclerView. Pls look at this answer https://stackoverflow.com/a/26573338/1554094

You can use item decoration. There are answers on same question How to add dividers and spaces between items in RecyclerView?

Or you can just add view with height 1dp at bottom of you view item

Community
  • 1
  • 1
Alexander
  • 857
  • 6
  • 10
0

you can try below code to set header in 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);
    }
}

}

Anil Kanani
  • 260
  • 2
  • 19
0

for border in Recyclerview

  recyclerView.addItemDecoration(new DividerItemDecoration(this,  LinearLayoutManager.VERTICAL));

// set the adapter 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);
    }
}

}

Anil Kanani
  • 260
  • 2
  • 19