I have List with objects where I have filds like: complete, incomplete, continue.
I want to sort my list on this three sections.
First I want to add header: INCOMPLETE and all elements if this field is true. After that I want to add header COMPLETE and again elements but with field complete is true, etc.
This is possible in recycler view? How can I do that?
I believe android doesn't have such control. You've just described ios table view control. Probably you'll have to search for a custom sectioned recycler view control.
For example, check out the following component https://android-arsenal.com/details/1/2921
I am thinking the easiest way to achieve this would be to have 3 recyclerviews in your layout!
Then simply add items to each view based on the three sections.
Now, to add headers, you simply have textviews that have the STATUS of the items i.e COMPLETE, etc
I hope this helps!
You can achieve this 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<String> list;
public MySection(String title, List<String> 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));
}
#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 sorted list of data
MySection incompleteSection = new MySection("", incompleteList);
MySection completeSection = new MySection("", completeList);
// add your sections to the adapter
sectionAdapter.addSection(incompleteSection);
sectionAdapter.addSection(completeSection);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
Related
I have been using cursor along with recyclerview.
I have a queried cursor object (passed from loader) and an array of header Strings[].
String headers[] = {"apples", "bananas"...};
Now I want to show items as
Apples
cursor row 1
cursor row 2
cursor row 3
Bananas
cursor row 4
cursor row 5
I don't want to tweak with getItemCount() method. So, planning to pass a single cursor with proper length.
One possible way is to use MatrixCursor and MergeCursor to add dummy rows as mentioned here: Adding rows into Cursor manually.
This is fine but MergeCursor aligns headers and cursor data one after the other.
Wanted to explore ways in which a final cursor can be achieved with the correct header and item positions.
You can use the library SectionedRecyclerViewAdapter to group your data into sections and add a header to each section.
First create a Section class:
class MySection extends StatelessSection {
String title;
List<String> list;
public MySection(String title, List<String> list) {
// call constructor with layout resources for this Section header, footer 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.title.setText(list.get(position));
}
#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();
// Add your Sections to the adapter
sectionAdapter.addSection(new MySection(headers[0], applesList));
sectionAdapter.addSection(new MySection(headers[1], bananasList));
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
With the example above you will have to work on how to convert your Cursor into List<String> but you can change the MySection class to receive a Cursor instead of a List<String>.
I am using RecyclerView that displays different categories list. Each row item also contains RecyclerView to show category items' list. Parent RecyclerView is populated with vertial LinearLayoutManager and child RecyclerViews are populated with GridLayoutManager (2 columns). Child RecyclerViews are not showing all category items except first 2 even it has many category items. All remaining category items are hiding for specific category. In other words, you can say child RecyclerView is not expanding as much as items it contains.
I found different solution here but no one worked. That's why posting again.
You can easily achieve it with only one RecyclerView using this library. See the image below:
First create a Section class:
class MySection extends StatelessSection {
String title;
List<String> list;
public MySection(String title, List<String> list) {
// call constructor with layout resources for this Section header, footer 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));
}
#Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new MyHeaderViewHolder(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 for each year
MySection section1 = new MySection("Categories 1", categories1DataList);
MySection section2 = new MySection("Categories 2", categories2DataList);
// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
switch(sectionAdapter.getSectionItemViewType(position)) {
case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
return 2;
default:
return 1;
}
}
});
recyclerView.setLayoutManager(glm);
recyclerView.setAdapter(sectionAdapter);
I had a scenario where I need to create a RecyclerView with 3 sections
The first section named as "First" should display 3 rows with each row containing TextView and EditText
Second section named as "Second" should contain 2 rows with each row displaying a single TextView
Third section named as "Third" should contain 4 rows with each row displaying a TextView with image . Is there a way in which can I achieve this using RecyclerView ? Can any one share me links or sample code fragments to achieve this functionality
Thanks in Advance
You can use the library SectionedRecyclerViewAdapter to group your data into sections.
First create a Section class:
class MyFirstSection extends StatelessSection {
String title;
List<String> list;
public MyFirstSection(String title, List<String> list) {
// call constructor with layout resources for this Section header, footer 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));
}
#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 for each year
MyFirstSection section1 = new MyFirstSection("First", firstDataList);
MySecondSection section2 = new MySecondSection("Second", secondDataList);
MyThirdSection section3 = new MyThirdSection("Third", thirdDataList);
// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);
sectionAdapter.addSection(section3);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
i want to show list as per image for that i am using recycler view and showing row its easy .but inside each row i want to showing many rows
say
i have 10 rows and each row has different row inside
so 1 row have 3 rows where as 2nd have 2 as on
so what is best way to do this
is it possible we can have one more listview inside that row ?
or inside onBindViewHolder i have to manually loop
and inflate layout
Edit :-
when i am trying this is always shuffles
#Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
for (int i = 0; i < position; i++) {
View c = ((Activity) mContext).getLayoutInflater().inflate(R.layout.row2, null);
// ((TextView) c.findViewById(R.id.mis)).setText(data.get(position) + "");
holder.inner.addView(c);
}
holder.n.setText(position+"");
holder.itemView.setTag(position);
}
image as follows
Yes you can use recyclerview inside recycler view just need to maintain separate adapter for that.
Or in this case you can also use expandable list view which will be much easier to use in this case.
If in your case, you don't have many rows, you can apply this:
Use NestedScrollview and add 2 RecyclerViews inside of it.
If you have specific number of rows like 2-3, it will be easy to implement.
Add layout_behavior to your RecyclerViews like below:
<android.support.v7.widget.RecyclerView
android:id="#+id/myRecyclerView"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
And wrap content for layout height is important.
android:layout_height="wrap_content"
And last, you should add this, so scroll works only for NestedScrollView
myRecyclerView.setNestedScrollingEnabled(false);
If you have many items use Single RecyclerView with multiple types of viewholders.
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int TYPE_MAIN = 0;
private final int TYPE_SUB = 1;
private ArrayList<Object> dataSet;
class ViewHolderMain extends RecyclerView.ViewHolder {
...
}
class ViewHolderSub extends RecyclerView.ViewHolder {
...
}
#Override
public int getItemViewType(int position) {
if(dataSet.get(position) instance of MainRowObject){
return TYPE_MAIN;
}else{
return TYPE_SUB;
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_MAIN: return new ViewHolderMain(...);
case TYPE_SUB: return new ViewHolderSub(...);
...
}
}
}
With the library SectionedRecyclerViewAdapter you can group your items in sections:
class MySection extends StatelessSection {
List<String> list;
public MySection(List<String> list) {
// call constructor with layout resource for this Section items
super(R.layout.section_item);
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));
}
}
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 per row
MySection row1Section = new MySection(data1List);
MySection row2Section = new MySection(data2List);
// Add your Sections to the adapter
sectionAdapter.addSection(row1Section);
sectionAdapter.addSection(row2Section);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
I have a json string which I break up into 4 datasets, each dataset has it's own RecyclerView.Adapter.
I want to output all adapters in the same RecyclerView ideally with StickyHeaders for each adapter.
I believe this can be done using ListViews via CommonsWare's CWAC-merge (https://github.com/commonsguy/cwac-merge)
Any ideas as to how this can, or should be done with RecyclerView? I am an amature developer so the less abstract the response the better ;-D
Thanks.
in new update of Recyclerview widget you can use MergeAdapter class
import androidx.recyclerview.widget.MergeAdapter
instantiate adapters:
private val mfirstAdapter = FirstAdapter()
private val mSecondAdapter = SecondAdapter()
// here is MergeAdapter class
private val adapter = MergeAdapter(mfirstAdapter , mSecondAdapter )
and then usage:
binding.recycler.adapter = adapter
how to update adapter: (using .submitList if use diffUtils, otherwise use notifyDataSetChaned())
mfirstAdapter.submitList(myDataArrayList.subList(0, 10))
mSecondAdapter.submitList(myDataArrayList.subList(10, list.size - 1))
You achieve that with only one adapter: SectionedRecyclerViewAdapter. With this library you can group your items into Sections and you can optionally add headers and/or footers to them.
First create a Section class to group your items:
class MySection extends StatelessSection {
String title;
List<String> list;
public MySection(String title, List<String> list) {
// call constructor with layout resources for this Section header, footer 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));
}
#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);
}
public void addRow(String item) {
this.list.add(item);
}
}
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
MySection section1 = new MySection("Header of section 1", section1List);
MySection section2 = new MySection("Header of section 2", section2List);
// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
I would go for this library: https://github.com/martijnvdwoude/recycler-view-merge-adapter
Check the documentation on the Github page for examples on how to use this in your code.
Extract from the readme:
RecyclerView myRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
// Create new merge adapter
RecyclerViewMergeAdapter mergeAdapter = new RecyclerViewMergeAdapter();
// Add any number of subadapters to merge adapter
MyRecyclerViewSubAdapter subAdapter1 = new MyRecyclerViewSubAdapter();
MyRecyclerViewSubAdapter subAdapter2 = new MyRecyclerViewSubAdapter();
mergeAdapter.addAdapter(subAdapter1);
mergeAdapter.addAdapter(subAdapter2);
// Set merge adapter to RecyclerView
myRecyclerView.setAdapter(mergeAdapter);
You can look at GroupAdapter
GroupAdapter.Builder builder = new GroupAdapter.Builder();
builder.add(firstAdapter);
builder.add(secondAdapter);
GroupAdapter groupAdapter = builder.build();
recyclerView.setAdapter(groupAdapter);
I made a class that do exactly what u want
https://gist.github.com/agustindev/e2faa7ef6a7b61cc0eafeeaf81e4e434
...
val multipleAdapter = MultipleAdapter()
val adapter1 = object: RecyclerView.Adapter<RecyclerView.ViewHolder>(){ ... }
val adapter2 = object: RecyclerView.Adapter<RecyclerView.ViewHolder>(){ ... }
val adapter3 = object: RecyclerView.Adapter<RecyclerView.ViewHolder>(){ ... }
multipleAdapter.addAdapter(adapter1)
multipleAdapter.addAdapter(adapter2)
multipleAdapter.addAdapter(adapter3)
recyclerView.adapter = multipleAdapter