I want that the string of keys must act as header and the list must be inflated under that map key in the RecyclerView
.
thanks for any help
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private WeakHashMap<String, List<VideoItem>> mData = new WeakHashMap<>();
private ArrayList<String> mKeys;
ArrayList<WeakHashMap<String,List<VideoItem>>> hashMapArrayList;
public Adapter(WeakHashMap<String, List<VideoItem>> mData, ArrayList<WeakHashMap<String,List<VideoItem>>> hashMapArrayList) {
this.mData = mData;
this.hashMapArrayList=hashMapArrayList;
mKeys = new ArrayList<String>(mData.keySet());
}
public String getKey(int position)
{
return (String) mKeys.get(position);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.player_item, parent, false);
MyViewHolder holder=new MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
String key = getKey(position);
WeakHashMap<String, List<VideoItem>> value = hashMapArrayList.get(position);
MyViewHolder holder1=(MyViewHolder)holder;
holder1.header.setText(key);
holder1.value.setText(value.get( key ).get( position ).getDuration());
Log.v( "KEY",key );
Log.v( "VALUE", String.valueOf( value ) );
}
@Override
public int getItemCount() {
return (null != hashMapArrayList ? hashMapArrayList.size() : 0);
}
// public ArrayList<WeakHashMap<String,List<VideoItem>>> getItem(int position) {
// return hashMapArrayList.get(mKeys.get(position));
// }
@Override
public long getItemId(int position) {
return position;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView header ;
TextView value;
public MyViewHolder(View itemView) {
super(itemView);
header= (TextView) itemView.findViewById(R.id.header);
value= (TextView) itemView.findViewById(R.id.task_name);
}
}
}
RecyclerView. Adapter base class for presenting paged data from PagingData s in a RecyclerView . Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView . A class that extends ViewHolder that will be used by the adapter.
A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.
How is this implemented? Typically in a simple RecyclerView, we load elements to the adapter from a Data Structure. In order to show the loading icon view at the bottom of the RecyclerView, we need to first add a NULL element to the end of the Data Structure.
You can achieve it easily with the library SectionedRecyclerViewAdapter. You can group your items into sections and add a header to each section:
class MySection extends StatelessSection {
String title;
List<VideoItem> list;
public MySection(String title, List<VideoItem> list) {
// call constructor with layout resources for this Section header and items
super(R.layout.section_header, R.layout.section_item);
this.title = title;
this.list = list;
}
@Override
public int getContentItemsTotal() {
return list.size(); // number of items of this section
}
@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
// return a custom instance of ViewHolder for the items of this section
return new MyItemViewHolder(view);
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
// bind your view here
itemHolder.tvItem.setText(list.get(position).getName());
}
@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
// bind your header view here
headerHolder.tvItem.setText(title);
}
}
Then you set up the RecyclerView with your Sections:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Create your sections with the list of data from your HashMap
for(Map.Entry<String, List<VideoItem>> entry : mData.entrySet()) {
MySection section = new MySection(entry.getKey(), entry.getValue());
// add your section to the adapter
sectionAdapter.addSection(section);
}
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
If you can't use 3rd party libs you can have a look on how it is implemented here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With