2016-04-01 13 views
6

Tôi muốn rằng chuỗi khóa phải hoạt động như tiêu đề và danh sách phải được thổi phồng dưới khóa bản đồ đó trong RecyclerView.Làm thế nào để phồng băm Hashmap <String, List <Items>> vào Recyclerview

nhờ sự giúp đỡ nào

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

}

+0

đăng mã bộ điều hợp của bạn, chúng tôi sẽ trợ giúp từ đó – inkedTechie

+0

thử sử dụng: https://github.com/timehop/sticky-headers-recyclerview –

+0

tôi muốn tạo các phần trong recyclerview bằng cách sử dụng khóa làm phần và giá trị danh sách theo những phần –

Trả lời

1

Bạn có thể đạt được điều đó một cách dễ dàng với thư viện SectionedRecyclerViewAdapter. Bạn có thể nhóm các mục của bạn thành nhiều phần và thêm một tiêu đề cho mỗi phần:

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

Sau đó, bạn thiết lập các RecyclerView với mục của bạn:

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

Nếu bạn không thể sử dụng libs bên thứ 3 bạn có thể có một cái nhìn về cách nó được thực hiện here.

+0

hey thanks..i sẽ thử nó .. –

+0

@ShikhaRatra đã làm giải pháp làm việc cho bạn? – ymerdrengene

+0

@ymerdrengene có nó hoạt động, có một cái nhìn trong lib .... bạn cần phải tùy chỉnh nó theo nhu cầu của bạn .... –

0

Hãy thử điều này

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... 
+0

tôi có dữ liệu thích hợp như Bản đồ >. Tôi không biết làm thế nào để thổi phồng dữ liệu đó đúng trong recyclerview như là chìa khóa như tiêu đề và danh sách theo phím đó –

Các vấn đề liên quan