如何给HashMap< String,List< Items>充气>进入Recyclerview [英] How to inflate Hashmap<String,List<Items>> into the Recyclerview

查看:218
本文介绍了如何给HashMap< String,List< Items>充气>进入Recyclerview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个键的字符串必须作为头部,并且该列表必须在 RecyclerView 中的映射键下面充气。



感谢您的帮助

  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;
$ b $公共适配器(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);


$ b @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);
回报持有者;

$ b @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,int position){
String key = getKey(position);
WeakHashMap< String,List< VideoItem>> value = hashMapArrayList.get(position);
MyViewHolder holder1 =(MyViewHolder)持有者;
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){
//返回hashMapArrayList.get(mKeys.get(position));
//}

@Override
public long getItemId(int position){
return position;
}
class MyViewHolder扩展RecyclerView.ViewHolder {
TextView标题;
TextView值;
public MyViewHolder(View itemView){
super(itemView);
header =(TextView)itemView.findViewById(R.id.header);
value =(TextView)itemView.findViewById(R.id.task_name);
}
}

}

SectionedRecyclerViewAdapter 轻松实现此功能。 >。你可以将你的项目分成不同的部分,并为每个部分添加一个标题:

  class MySection extends StatelessSection {

字符串标题;
列表< VideoItem>列表;
$ b $ public MySection(String title,List< VideoItem> list){
//调用此部分标题和项目
super(R.layout.section_header, R.layout.section_item);

this.title = title;
this.list = list;
}

@Override
public int getContentItemsTotal(){
return list.size(); //本节的项目数
}

@Override
public RecyclerView.ViewHolder getItemViewHolder(View view){
//返回一个ViewHolder自定义实例这部分的项目
返回新的MyItemViewHolder(view);

$ b @Override
public void onBindItemViewHolder(RecyclerView.ViewHolder持有者,int位置){
MyItemViewHolder itemHolder =(MyItemViewHolder)持有者;

//在此处绑定您的视图
itemHolder.tvItem.setText(list.get(position).getName());
}

@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view){
return new SimpleHeaderViewHolder(view);

$ b @Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder持有者){
MyHeaderViewHolder headerHolder =(MyHeaderViewHolder)持有者;

//在这里绑定你的标题视图
headerHolder.tvItem.setText(title);


然后你用你的章节设置RecyclerView:

  //创建SectionedRecyclerViewAdapter的实例
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

//使用HashMap中的数据列表创建您的部分
(Map.Entry< String,List< VideoItem>>条目:mData.entrySet()){
MySection section = new MySection(entry.getKey(),entry.getValue());
//将您的部分添加到适配器
sectionAdapter.addSection(section);



//用SectionedRecyclerViewAdapter设置你的RecyclerView
RecyclerView recyclerView =(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(新的LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);

如果你不能使用第三方库,你可以看看它是如何实现的这里


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);
    }
}

}

解决方案

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.

这篇关于如何给HashMap&lt; String,List&lt; Items&gt;充气&gt;进入Recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆