I am using MergeAdapter to combine 2 adapters It works fine .
Problem is for 1 adapter I need LinearLayoutManager with single column and for second adapter I need GridLayoutManager to show items in 2 columns . Is there any way to do it ?
I found a working solution(added below) by setting it to GridLayoutManager and span size based on position . I just want to know is there a better way to do this .
Set GridLayoutManager to RecyclerView and span size based on position .
val layoutManager = GridLayoutManager(requireContext(), 2)
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
if(position==0){
return 2 //no of colums it show occupy
}
return 1
}
}
recyclerView.layoutManager = layoutManager
In this case if position in mergeadapter is 0 then the only 1 column will be show come else 2 columns will be shown .
Related
I'm trying to achieve the following list item formation within a recycler or grid view:
1
1 1
1
1 1
1
where 1 is a single view holder (list item). My list items all have the same width and height.
I've attempted to use a recyclerview with a grid layout manager as seen here:
recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
But this will strictly display 2 columns.
I've tried using a StaggeredGridLayoutManager also with my recyclerView as seen below:
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
But this only stretches my list items to fit a 2 column list, similar to the GridLayoutManager.
I've also tried to change the width of 1 in 3 list views in my adapter, but the width of one view does not effect the position of another.
The current setup looks like this:
adapterPre = new CategoryListAdapter(this, getContext(),GeneralUtils.isLoggedIn());
RecyclerView premadeLists = view.findViewById(R.id.practise_recycler);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
gridLayoutManager.setSpanSizeLookup(new CustomSpanSizeLookup());
premadeLists.setLayoutManager(gridLayoutManager);
premadeLists.setHasFixedSize(false);
premadeLists.setAdapter(adapterPre);
CustomSpanSizeLookup:
class CustomSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
#Override
public int getSpanSize(int position) {
return position % 2 == 0 ? 2 : 1;
}
}
You have to use a GridLayoutManager with 2 spans and create a custom GridLayoutManager.SpanSizeLookup:
class CustomSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
#Override
public int getSpanSize(int position) {
/* one in three items will occupy the whole row,
* two in three items will take up half a row and can
* therefore be placed next to each other.
*/
return position % 3 == 0 ? 2 : 1;
}
}
You can set it on the GridLayoutManager with the method:
layoutManager.setSpanSizeLookup(new CustomSpanSizeLookup());
I am thinking of a way to implement a view using RecyclerView like this image where the header row is on the left side and the content under that header on the right. Any ideas or pointers on how to go about this.
If someone else gets here looking for an answer/ solution, here's what I did. I used default GridLayoutManager using child RecyclerView in the parent RecyclerView. Parent RV is using GridLayoutManager while child RV uses LinearLayoutManager. For parent RV, using multiple viewType, position 0 or position %2 == 0 returns HEADER_TYPE while the rest returns CARD_TYPE. Using this
mGridLayoutManager = GridLayoutManager(activity, 3)
mGridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return when (enquiryAdapter.getItemViewType(position)) {
HEADER_TYPE -> 1
CARD_TYPE -> 2
else -> -1
}
}
}
recyclerView.layoutManager = mGridLayoutManager
I changed the spanSize. For position that returns CARD_TYPE, I inflate another RV which takes list to layout the content view. I am not sure this is the best solution but that seems to be working for me. I will gladly accept any solution working better/simpler than this.
I am new to android i have developed an application in which i have to display images horizontal view on single activity i have done using staggered recycleview but am getting like this.
But i want to design like this as part of the activity.
You can easily do that with GridLayoutManager. Use SpanSizeLookup to control your row/column . For your case you have to use HORIZONTAL orientation. SpanSizeLookup will help you to control your rows in each column in HORIZONTAL GridLayoutManager.
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), NUM_OF_ROW, LinearLayoutManager.HORIZONTAL, reverseOrder);
GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
// for position 0 use only one row. for this position it will take all rows
if (position == 0) {
return NUM_OF_ROW;
}
return 1;
}
};
Here is blog post about different Layout Manager implementation.
I have uploaded a repo on Github about different LayoutManager usage like LinearLayoutManager, GridLayoutManager, StaggeredGridLayoutManager and some advance RecyclerView usage like swipe, Drag and Drop. You can also check that
here is what i use for arranging items horizontally provided you're using RecyclerView:
int numberOfColumns = 3; //edit as you want pls
final GridLayoutManager gridLayoutManager = new GridLayoutManager(this.getContext(), numberOfColumns);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(adapter);
I might be off your solution, perdon if so, but pasting some of your code might help better.
I need a recycler view with different number of columns in a row. For that I have used a gridlayout manager with dynamic span size. Here is my code:
rvSubCat.layoutManager = StaggeredGridLayoutManager(4, 1)
val viewTreeObserver = rvSubCat.getViewTreeObserver()
viewTreeObserver.addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener { calculateSize() })
categoryAdapter = CategoryAdapter(categoryList, true, this)
rvSubCat.adapter = categoryAdapter
private fun calculateSize() {
val spanCount = Math.floor((rvSubCat.getWidth() / SizeUtils.dp2px(sColumnWidth.toFloat())).toDouble()).toInt()
(rvSubCat.getLayoutManager() as StaggeredGridLayoutManager).spanCount = spanCount
}
But the desired result is not good. It is fixing the number of columns in every row. I need the number of columns should depend upon the total width of the row items.
Need this (Desired Result):
Getting This
Best light weight library i use
compile 'com.xiaofeng.android:flowlayoutmanager:1.2.3.2'
Benifit is you dont need to change your existing code, you just need to change recyclerview's LayoutManager like
recyclerView.setLayoutManager(new FlowLayoutManager());
You are all set, Happy coding :)
Update You know already how to populate recycler view. But verify by following
private void setAdapter() {
recyclerView = (RecyclerView) fragment_view.findViewById(R.id.rv);
recyclerView.setLayoutManager(new FlowLayoutManager());
myRecyclerAdapter = new AdapterOrders(getActivity(), list);
recyclerView.setAdapter(myRecyclerAdapter);
}
Call myRecyclerAdapter.notifyDataSetChanged(); after list is change from any source.
What you need is flow Layout. I think these library will help you solve the purpose :
https://github.com/nex3z/FlowLayout
https://github.com/ApmeM/android-flowlayout
Adjusts according to size of texts. Hope this helps !
How can we change the spanCount of RecyclerView dynamically?
My code create 3 column of cards in RecyclerView:
StaggeredGridLayoutManager mFavouratesLayoutManager;
mFavouratesLayoutManager = new StaggeredGridLayoutManager(3,
StaggeredGridLayoutManager.VERTICAL);
I'm trying to make the adapter like:
one element at top row
2 elements in second row
3 items below that
StaggeredGridLayoutManager does not support that. You can change the span count but it will effect every row. Your only option is to have full rows w/ SGLM.
If you use GridLayoutManager, you can achieve that by setting span count to 6, then providing a SpanSizeLookup that returns
6 for first item
3 for items 1 and 2
2 for items 3 4 5
Etc.
You can try this as StaggeredGridLayoutManager does not support that so use Gridlayoutmanager.
GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
if (position == 1)
return 6;
else
return 2;
}
});