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?
Asked
Active
Viewed 546 times
1
-
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 Answers
2
Your official reference for creating RecyclerView
:
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
-
Will I be able to add description at the top dynamically? @blueware – Aayush Gupta Mar 29 '17 at 08:17
-
-
-
You can use the `getItemViewType` to determine which to notify the adapter about – blueware Mar 29 '17 at 10:49
-
And what if I want to create multiple headers (like in the image in description) – Aayush Gupta Mar 29 '17 at 11:41
-
You can use a `RecyclerView` inside your header item and push multiple items to it to show downaloding – blueware Mar 29 '17 at 12:28
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
-
-
I hope it's helpful for make cardview design. It's works on everywhere. like listview, gridview, layout etc. – ITSGuru Mar 29 '17 at 08:33