Loading a new ViewHolder based on click event - android

Within RecyclerView, any tips on loading a different viewholder onitemclick? i'm having trouble wrapping my head around loading viewholder 2 when item is clicked in viewholder 1 at that position. both viewholder 1 and 2 are a list of cards. so say for example. vh1 contains a sample hotel image. When you click that vh2 shows you contact details of that hotel - at that position, while retaining vh1 through out the other un-clicked contents.
I did ask a question like this before and i think it was a bit vague so people suggested i change information based onitem click. The problem is vh1 and 2 are different layouts completely so this wouldn't work for what i'm working on.
is this even possible?
Thanks

You can create another adapter for your RecyclerView. Create a new Java class, call it whatever you want. In your main activity, or the activity where you're setting your adapter, you can create a function like this:
public void setNewAdapter() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.your_recyclerview_id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
NewAdapter adapter = new NewAdapter(// any arguments);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
In your adapter, you can create a new instance of your activity. For example, if you're activity is called MainActivity, you can add this code in your onItemClick:
MainActivity mainActivity = new MainActivity();
mainActivity.setNewAdapter();
See if that works.

Related

how to get data from bottom to top from firebase?

I'm trying to get data from bottom to top (from the last item uploaded to the first item uploaded like Instagram) but I couldn't make this. I searched a lot and found this answer here.
I made it with firebaseRecyclerAdapter and worked fine but with custom Adapter I couldn't do it!
here is the method I used:
#Override
public ItemsRecyclerView getItem(int pos){
return super.getItem(getCount() - 1 - pos);
}
What you should be doing is reversing the recyclerview, forget about the method that you used above.
And do this when you set your recycler view:
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setReverseLayout(true);
manager.setStackFromEnd(true);
recycler_View.setLayoutManager(manager);
Possible solution:
try this:
Maybe try to reverse the list that you pass to your adapter like this:
//your list
List<model> your_list = new ArrayList()<>;
//before you pass it to the adapter do this
Collections.reverse(your_list);
//now your list is reversed, pass it to the adapter
Adapter adapter = new Adapter (your_list,.....,....);

Android : How to use recyclerView.findViewHolderForAdapterPosition inside Recycle View Cursor Adapter class

I am trying to get viewholder of a particular position from recycle view.
I had found to do so by doing this in MainActivity :
viewHolderRec = recyclerViewInstance.findViewHolderForAdapterPosition(vHID);
It works well inside my MainActivity.
But, when I tried to implement the same inside my recycle view cursor adapter like this :
I had changed the cursor to take recycleview :
In my MainActivity :
final RecyclerView recyclerView = findViewById(R.id.recycle_view);
LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(manager);
recyclerView.setNestedScrollingEnabled(true);
mCursorAdapter = new RecycleCursorAdapter(this, null, recyclerView);
recyclerView.setAdapter(mCursorAdapter);
In RecycleCursorAdapter :
RecyclerView recyclerViewInstance;
public RecycleCursorAdapter(Context context, Cursor cursor, RecyclerView recyclerView) {
super(context, cursor);
recyclerViewInstance = recyclerView;
}
#Override
public void onBindViewHolder(ChatCursorAdapter3.ViewHolder viewHolder, Cursor cursor) {
ChatListItem myListItem = ChatListItem.fromCursor(cursor);
int vHID = viewHolder.getAdapterPosition();
viewHolderRec = (ViewHolder) recyclerViewInstance.findViewHolderForAdapterPosition(vHID - 1);
}
But viewHolderRec is null.
I want to get the viewholder of the view which is above the current viewHolder.
I just want to know how can I get it work.
What I want is to use findViewHolderForAdapterPosition() method inside my recycle view cursor adapter.
Yes, I know that I can use
viewHolder.getOldPosition()
But It does not fulfill my needs at the time of scrolling the recycle view.
Thanks In Advance :-)
I had searched a lot and find that, Instead of using recyclerViewInstance.findViewHolderForAdapterPosition(vHID - 1);
I can find the viewholder of any position by storing the viewholders in a List<> like
List<ViewHolder> vList = new ArrayList<>();
and whenever i want the viewholder of any posstion than i can find by
int vHID = viewHolder.getAdapterPosition();
viewHolderRec = vList.get(vHID - 1);
As what i need to do.
But, I know this effects lots of memory when the list is very large.
So what I did is removing the viewholders from memory which are not visible right now.
For this, I get the height in display pixels(dp) and get the number of visible items by it. and remove all the other viewholders.
This trick works for me. May Be this will be helpful for someone so I posting this answer.

Setting different adapter in single RecyclerView in Android

I have MenuActivity which has RecyclerView and ViewPager. This ViewPager has 3 pages which use three different Fragment.
FragmentOne,FragmentTwo, FragmentThree, All these Fragments use RecyclerView data.
I want to use three different ArrayList need to be set in RecyclerView Adapter, which is depends on what Fragment user is viewing.
One solution is to put RecyclerView in all three fragment and get update data in Fragment. I just want to know if it is possible to set Adapter data in MenuActivity based on what Fragment is called.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
itemList = new ArrayList<>();
prepareMenuData();
vegadapter = new MenuItemAdapter(getContext(),vegItemList);
nonvegadapter = new MenuItemAdapter(getContext(),nonvegItemList);
dessertAdapter = new MenuItemAdapter(getContext(),dessertItemList);
recyclerView.setAdapter(vegadapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
return rootView;
}
prepareMenuData();
for (MenuItem item:itemList)
{
if(item.getCategory().equals("VEG"))
{
vegItemList.add(item);
vegadapter.notifyDataSetChanged();
}
if(item.getCategory().equals("NONVEG"))
{
nonvegItemList.add(item);
nonvegadapter.notifyDataSetChanged();
}
if(item.getCategory().equals("DESSERTVEG"))
{
dessertItemList.add(item);
dessertAdapter.notifyDataSetChanged();
}
}
This code does not know when to set vegadapter,nonvegadapter etc.
Please suggest.
Thanks
Deepak
Technically yes you can, but whether thats good practice is another question.
You'll have to have the RecyclerView in each Fragment regardless but you can get the data for the adapter from the Activity. Fragments have access to their Activity, thats how the Android lifecycle works so from the Fragment you could call ((MenuActivity) getActivity()).getMenuList(); and then pass the result to that Fragments adapter. This will however couple the Fragment with the Activity which isn't good practice.
Given the fact that it looks like your data isn't dynamic what I would personally do is make your MenuItem class implement Parcelable, then when you create your Fragments you can pass through your ArrayList of MenuItems as an argument, this way your Fragment is independent of your Activity.

Android RecyclerView infinity scrolling: how to add more items to dataset

I have implemented my RecyclerView and even added an onscrolllistener to support infinity scrolling and now I'm stuck with a, hopefully, easy problem: How can I add the newly loaded data to the existing dataset?
My current approach: I create a new array with the length of the existing dataset + the length of the newly loaded data. I System.arraycopy my existing dataset and add the new content with a for-loop.
This works but the list is always reset (scrolls back to the top) and I assume my way to add additional content is overly complicated/wrong, though the tutorials I have looked at seem to pass over this "detail".
Update: I'm currently calling "scrollToPosition" on the UI-Thead after the data has been loaded, but I doubt this is the correct way of doing this or am I wrong?
You shouldn't be adding stuff to your dataset, you will sooner or later run out of memory. What you can do is return a big number (I used Short.MAX_VALUE) item in getItemCount inside your adapter and in the method that requests a view for postion you should do position % list.size();
It is not a truly endless RecyclerView this way, but good enough. I will paste some code tomorrow, I don't have it here now :/
I think you have to add items inside your adapter. Let`s say
class Adapter extends Recycler.Adapter<Recycler.ViewHolder>{
List<YourCustomObject> list;
public Adapter(){
list = new ArrayList<>();
}
public void addItem(YourCustomObject item){
list.add(item);
notifyItemDateSetChanged(); //This method for adapter to notice that list size have been changed
}
// Here your views
}
There is implementation of Your fragment or Activity where you retrieve data from internet.Let` say
class MainActivity extends AppCompactActivity{
Adapter adapter = new Adapter();
List<YourCustomObjects> objects;
public void onCreateView(){
//////// Something yours
}
public void onLoadMore(){
///// Your operation to retrieve data and init it to your list objects
for(YourCustomObject object : objects){
adapter.addItem(object);
}
}
}

ExpandableListView adding groups inside another group

I am having an ExpandableListView inside a fragment which is part of a tab.
So inside - onCreateView(...) of fragment class I do :
ExpandableListView expandableList = (ExpandableListView) view
.findViewById(R.id.groupList);
parent.add(getString(Group1));
parent.add(getString(Group2));
ArrayList<MyObject> groupList1 = new ArrayList<MyObject>();
ArrayList<MyObject> groupList2 = new ArrayList<MyObject>();
childs.add(groupList1);
childs.add(groupList2);
MyAdapter adapter = new MyAdapter(getActivity(), parent,
childs);
Now when I open both the groups and then switch to tab by doing :
transaction.replace(R.id.realtabcontent, new MyFragment(),
"MyFragment");
And again come back to this tab by doing the same, the last group gets added with groups with child item.
Note : This only happens when we switch tab using replace. When we open for the first time launching the activity this works fine.
Kindly let me know what might be going wrong here. Let me know if I need to provide more code. Thanks in advance!

Categories

Resources