1

I want to create a list view which shows some detail (batch name in my case) on the top of some listview items like this screenshot of play store. Here it shows downloading on top of a few items and updates or installed on others. How can I do this?

Aayush Gupta
  • 55
  • 11
  • You can use CardView – Jéwôm' Mar 29 '17 at 08:29
  • Try Using ListView Header its simple [http://stackoverflow.com/questions/13590627/android-listview-headers](http://stackoverflow.com/questions/13590627/android-listview-headers) – g7pro Mar 29 '17 at 08:40

2 Answers2

2

Your official reference for creating RecyclerView:

Creating Lists and Cards

Also, you may follow this:

Using lists and grids in Android with RecylerView - Tutorial

You will exactly get the most of building RecyclerView considering design patterns

Here is an example of creating RecyclerView with header:

    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);
        }
    }
}
blueware
  • 5,205
  • 1
  • 40
  • 60
1

You can use CardView - Design library.

add library in app level Gradle-

compile 'com.android.support:cardview-v7:25.0.+'

here is guide of card view

https://developer.android.com/reference/android/support/v7/widget/CardView.html

ITSGuru
  • 194
  • 8