On button click I want to change list view to gridview as on shopping-cart pages but my layout looks like link shown below.
//For displaying the row as linear list view
case R.id.ivGrid:
ivList.setVisibility(View.VISIBLE);
ivGrid.setVisibility(View.GONE);
LinearLayoutManager llm = new LinearLayoutManager(context);
rcvProducts.setLayoutManager(llm);
break;
//For displaying the row as gridview
case R.id.ivList:
ivList.setVisibility(View.GONE);
ivGrid.setVisibility(View.VISIBLE);
GridLayoutManager glm = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL, false);
rcvProducts.setLayoutManager(glm);
break;
GRIDview
LISTview
Just use a GridLayoutManager and change the span count from 1 to 2 and back again.
view.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
GridLayoutManager layoutManager = (GridLayoutManager) grid.getLayoutManger();
layoutMananger.setSpanCount(layoutManager.getSpanCount() == 2 ? 1 : 2);
}
})
Maintain 2 adapters, one for ListView and one for GridView.
On button click, change the adapter to RecyclerView.
Listview.setAdapter(gridAdapter) (or) ListView.setAdapter(listAdapter)
Related
I want to display items on Recyclerview if columns 3 select from back-end RecyclerView first three items on first row and four one in second row, if columns 4 first four item on first row and five in second row.
how can arrange above list in RecyclerView
recyclerViewLineItem.setHasFixedSize(false);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerViewLineItem.setLayoutManager(linearLayoutManager);
Use GridLayoutManager class if number of columns are more than 1
GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, 2/*column count*/);
recyclerView.setLayoutManager(gridLayoutManager);
& for horizontal scrolling, use this
new GridLayoutManager(getActivity(), 1 /*column count*/, GridLayoutManager.HORIZONTAL, false)
This is actually very easy. You need to use GridLayoutManager instead of the usual LinearLayoutManager.
Inside GridLayoutManager, you can choose how many columns you want depending upon the number of items. Let me show you:
int spanCount = 0 ; //This is the number of column you need, set to 0 at first
if(yourItemCount == 3){
spanCount = 3; //set to 3 columns
};
if(yourItemCount == 5){
spanCount = 4; //set to 4 columns, and the 5th item will be at the second row
}
//now pass the spanCount to the GridLayoutManager
GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, spanCount);
recyclerView.setLayoutManager(gridLayoutManager);
Hope it helps.
Cheers!
You can try passing position from your adapter and in your activity/fragment you can create a method which display data according to the values passed.
private void CheckColumn(RecyclerView recyclerView) {
int count = recyclerView.getChildCount();
for (int i = 0; i < count; i++) {
View view = recyclerView.getChildAt(i);
// Your code to show grid items here
}
}
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 to implement a list that has some card which scroll horizontally (scroll one item at a time like facebook friend suggestion list).
Use RecyclerView & set layout manager to that like this
recyclerView.setLayoutManager(new LinearLayoutManager(this) {
#Override
public boolean canScrollVertically() {
return false;
}
});
recyclerView.setAdapter(adapter);
I hope this will help you.
for set horizontal RecyclerView use :
LinearLayoutManager mLayoutManager = new LinearLayoutManager(Context, RecyclerView.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
and for showing horizontal scrollBar in RecyclerView , in xml layout :
android:scrollbars="horizontal"
and with this line you can set custom scrollBar:
android:scrollbarThumbHorizontal="#drawable/recycler_scroll"
I am using recyclerview with staggeredGridLayoutManager.
What i am trying to do is to show one header item in first row. then i want to show 2 items in each below row. But i am unable to do so.
Here is code that i used.
recyclerView = (RecyclerView)findViewById(R.id.recycler);
staggeredGridLayoutManager = new StaggeredGridLayoutManager(1,1);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
And these are the results
Help me with this. thanks
You could use GridLayoutManager.
GridLayoutManager manager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
manager.setSpanSizeLookup(
new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
// 2 column size for first row
return (position == 0 ? 2 : 1);
}
});
Here we are creating a GridLayoutManager with 2 grid columns. Then only for the first row, we are setting the span size to 2.
I want to use the grid view in which first image is constant and on the click want to launch addProductActivity for all user , and after that the images from server will be displayed. How to achieve this please help.
You can achieve this by recyclerview where you can give layout manager as gridlayout manager :
RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.fridID);
Adapter gridAdapter = new Adapter(yourProductArray, getContext(), this, this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
mRecyclerView.setLayoutManager(gridLayoutManager);
mRecyclerView.setAdapter(gridAdapter);
and at the recyclerview Adapter when position is zero in onBindViewHolder(Adapter.ViewHolder holder, int position), always show the constant image
You can set onClickListener to the ImageView and when the position is zero and onClickListener is triggered show the add product screen
when a new product is added add the product to the yourProductArray and use gridAdapter.notifyDataSetChanged()