如何将 Hashmap<String,List<Items>> 膨胀到 Recyclerview
Posted
技术标签:
【中文标题】如何将 Hashmap<String,List<Items>> 膨胀到 Recyclerview【英文标题】:How to inflate Hashmap<String,List<Items>> into the Recyclerview 【发布时间】:2016-07-20 23:17:15 【问题描述】:我希望键的字符串必须充当标题,并且列表必须在 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;
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);
【问题讨论】:
发布您的适配器代码,我们将提供帮助 尝试使用这个:github.com/timehop/sticky-headers-recyclerview 我想在 recyclerview 中使用键作为部分并在这些部分下列出值 我无法使用其他库。 你不能做你想做的事。回收器视图实现(除非你使用或制作一个复杂的库)不能显示嵌套的对象集(在你的情况下是标题和子项)。您需要将所有项目移动到指定不同项目类型的单个数组列表中,然后使用回收站视图的不同视图类型功能显示它们。 【参考方案1】:您可以使用库SectionedRecyclerViewAdapter 轻松实现它。您可以将项目分组并为每个部分添加标题:
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);
然后你用你的部分设置 RecyclerView:
// 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);
如果你不能使用 3rd 方库,你可以看看它是如何实现的 here。
【讨论】:
@ymerdrengene 是的,它工作,看看在库中......你需要根据你的需要自定义它...... @ShikhaRatra 感谢您的回复!【参考方案2】:试试这个
Map<String, List<String>> maplist = new HashMap<String, List<String>>();
List<String> arraylist = new ArrayList<String>();
arraylist.add("Sample text"); // Add more strings...
maplist.put("key",arraylist); // Add more lists...
【讨论】:
我有正确的数据作为 Map以上是关于如何将 Hashmap<String,List<Items>> 膨胀到 Recyclerview的主要内容,如果未能解决你的问题,请参考以下文章
Java Spark 如何将 JavaPairRDD<HashSet<String>, HashMap<String, Double>> 保存到文件中?
如何将 Hashmap<String,List<Items>> 膨胀到 Recyclerview
Android - 如何在活动之间传递 HashMap<String,String>?