Recyclerview keep scrolling position after changing layout - android

I am using recycle view in my application. I have one button above recycle view in relative layout for changing layout manager. It first displays grid layout manager and when a button is clicked it displays Linearlayoutmanager. While I am changing layout manager, recycle view again starts from the first position.
Can anyone help me to keep the position as it is when making the change in layout manager dynamically?

You have to remember the firstVisibleItem and scroll to that position after changing the LayoutManager it works fine I just tried it out by myself:
E.g. Button click to change LayoutManager:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = ((LinearLayoutManager) rcView.getLayoutManager()).findFirstVisibleItemPosition();
if(rcView.getLayoutManager() instanceof GridLayoutManager) {
rcView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
} else {
rcView.setLayoutManager(new GridLayoutManager(MainActivity.this, 3));
}
rcView.scrollToPosition(i);
}
});

Related

how to apply onTouchEvent in recyclerview child inside the child?

I had made a navigation drawer which is having header footer and list. I wonder how to apply touch event in specific navigation drawer header image and footer. I'm having navigation drawer with Header which contain Image and name of the user then in between list of items and then footer.
All the xml file(Header, CustomRow and footer) are in recyclerView,
so i want basically functionality to click header image,customrom and
footer as well.
If you are using recyclerview you must have ViewHolder as well. Yes? Your problem is fixed.
You can define clickListeners within ViewHolder
Taking an example -
class AddedProductHolder extends RecyclerView.ViewHolder {
View mCardContainer;
AddedProductHolder(View itemView) {
super(itemView);
mContainer = itemView.findViewById(R.id.conatiner);
// Define clickListener here...
mCardContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do your stuff.. If you need position of item clicked.. user getAdapterPosition()
}
});
}
}

Why is Recycler view data on the row is gone when i click on the same row's child view?

I have created a recycler view view which will have cardView rows,in the card view i have put a layout,on click of it a childview will open, when i click on the layout, child view is coming but the data which is already populated on the card is dissappearing.And when I scroll down the child view itself disappears and the data on the card is populated again.I am not able to find a solution for this from a long time, please help me with this.This is how I am adding the child view in the adapter
#Override
public void onClick(View view) {
mAdapter = new ViewOrderAdapter(orderList);
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// view = inflater.inflate(R.layout.cancelledcardview, null);
final RelativeLayout parent = (RelativeLayout) inflater.inflate(R.layout.cancelledcardview, null);
Log.i("TAG", "onclickmethod" + parent);
final View child = inflater.inflate(R.layout.vieworder_layout, null);
recyclerView = (RecyclerView)child. findViewById(R.id.recycler_view);
Log.i("TAG", "onclickmethod" + recyclerView);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(mAdapter);
parent.addView(child);
ImageView cancelButton= (ImageView)child.findViewById(R.id.cancelbutton);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("TAG", "onclickmethodcancel button" );
parent.removeView(child);
}
});
prepareMovieData();
cv.addView(parent);
}
You have to maintain the click state. when you scroll the list the onBind method is called again and it populates the old card again.
I Suggest
-create 1 more variable(isClicked) in your model and set it true when you click it.
-In your onBind method of adapter you have to again write the code for creating this child view.
Or you can use ExpandableListView this will handle the state more gracefully.

Custom ItemDecoration not drawn on last view of RecyclerView

i am building an app on android and am using several recyclerviews.
in the first page that i used a recyclerview, everything worked flawlessly and my custom ItemDecoration (divider) was applied to all views.
then i started a new page in which i use the same divider on a similar RecyclerView, but on the last list item the bottom divider is not present.
seeing that it worked perfectly in one place, i don't believe the issue is with the custom ItemDecoration class, nor with the xml. I also tried using the same viewholder for the one that worked, but still the last divider was not drawn.
here is the code where is set up my adapter in the problematic recyclerview:
private void setUpPracticesList() {
lstPractices = (RecyclerView) getActivity().findViewById(R.id.lstPractices);
lstPractices.setLayoutManager(new LinearLayoutManager(getActivity()));
RecyclerView.Adapter<PracticeHolder> adapter = new RecyclerView.Adapter<PracticeHolder>() {
String[] titles = getResources().getStringArray(R.array.practices_names);
int[] imageIds = {R.drawable.consultation_meeting_icon,
R.drawable.monitoring_meeting_icon,
R.drawable.parent_lectures_icon,
R.drawable.parent_support_groups_icon,
R.drawable.staff_courses_icon};
#Override
public PracticeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.kidsense_practices_list_item, parent, false);
return new PracticeHolder(v);
}
#Override
public void onBindViewHolder(PracticeHolder holder, int position) {
holder.setName(titles[position]);
holder.setImage(imageIds[position]);
}
#Override
public int getItemCount() {
return titles.length;
}
};
lstPractices.setAdapter(adapter);
lstPractices.setHasFixedSize(true);
lstPractices.addItemDecoration(new DividerItemDecoration(getActivity(), null));
}
does anyone have any ideas?
I have found the solution:
what i have found is that a recyclerview will not show a divider at the bottom of the last view if there is no more space left in the recyclerview.
in my case, the problem was that my recyclerview was in a layout that had a height of "wrap_content", meaning that there was no extra space left under the recyclerview to display the divider. as soon as i rearranged my layout so that my recyclerview had more space underneath with a parent layout height of "match_parent" the final divider was displayed.

PagerAdapter returns wrong position for Multi View Pager

I am implementing caurosel like view pager by adjusting page margin. I am able to implement like below
but i am facing problem with PagerAdapter
#Override
public Object instantiateItem(ViewGroup container, int position) {
View view= inflater.inflate(R.layout.mylayout, null);
//
Button btn=(Button)view.findViewById(R.id.button1);
btn.setTag(position);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("position",v.getTag()+"");
}
}
return imgView;
}
Though I am setting Tags, I am able to get correct position only for center page (Selected item), but when I click on left side page's button it returns wrong value.
I have searched about this issue lot, I could not find the solution so far.
looking for your help.
Thanks
You can use RecyclerView
LinearLayoutManager layoutManager = ...
recyclerView.setLayoutManager(layoutManager);
//when you want horizontal
layoutManager.setOrientation(context,LinearLayoutManager.HORIZONTAL,false);
//when you want vertical
layoutManager.setOrientation(context,LinearLayoutManager.VERTICAL,false);
There was no issues with PagerAdapter. Actual issue was due to a third party library which i used as wrapper to animate the views.

How to scroll recylcerView and bottom button simultaneously

I have a recylcer view having grid images and I have a button at the bottom of the recycler view. This button should be at the bottom of the recyclerview, not the bottom of the parent.
I tried code for recyclerView. But the button stays as parent bottom not scrolling along with the recycler view.
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/hello_blank_fragment"/>
I have found the solution.
I can create a separate view with Button and can add in the recyclerView Adapter.But there will be one problem - Since My Layout manager is GridLayout manager and having span of 2, I have to change the span to 1 when the current view is Button.
The changing of span when the view is Button is below:
final GridLayoutManager mGridManager = new GridLayoutManager(getActivity(),2);
mGridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
return adapter.isPositionHeader(position) ? mGridManager.getSpanCount() : 1;
}
});
recyclerView.setLayoutManager(mGridManager);
recyclerView.setAdapter(adapter);
This code I had to add in recyclerView Adapter, this below code means when button is the view return true, otherwise return false
public boolean isPositionHeader(int position) {
if(ADD_SOURCE_BUTTON == getItemViewType(position)){
return true;
} else {
return false;
}
}
Am not sure this is a good method, but it worked for me.
Try putting RecyclerView inside a ScrollView and place the button below RecyclerView. It will work fine now. Earlier it had some issues of scrolling but now its fixed by google I think and we can place RecyclerView inside scroll view, but am not sure its a good method, but it will work.

Categories

Resources