I'm trying to customize android studio's master/detail flow template for my project.
When running this template on a tablet, the master list of items (the RecyclerView) appears on left pane and a blank container appears on the right pane which is considered to be replaced with the detail fragment.
Blank detail pane at startup
I want to change this behavior so that instead of the blank container, detail of the first item of the RecyclerView to be appeared on the right pane at app start without clicking.
I tried to add the following code with no success:
Within onBindViewHolder method:
if (position == selectedPosition) {
holder.mView.setSelected(true);
}
Within onClick(View v) method:
selectedPosition = holder.getAdapterPosition();
I read so many posts in stackoverflow but didn't find the answer.
Can anybody suggest a solution?
Whatever you want to show on detailed panel, Make a function which takes position from ArrayList of your recyclerview and add details as per that position. And then call that function while adding adapter to recyclerview in Activty followed by holder.mView.setSelected(true). make these two custom functions in adapter and call them from your activity. it will work for sure.
Related
I have one Activity which contains viewpager inside and each viewpager tab contains one fragment . only one of these fragment has one recyclerview (which has inside cardView)and of course one adapter for the recyclerview . by clicking on floating action button which is in Main Activtiy layout another Activity open that contains two edit texts one for device id and one for device name and one button called Add .by clicking the add button it should add the device to the database and update adapter using notifyItemInserted(position) .... the problem here it add the device to the database immediately but the adapter not update the view immediately . it update after i scroll through the app using viewpager tabs or when i start the main activity from the beginning .... there is no error in the code and i have search for answer but i couldn't find anything to solve this issue ..
anyone face like this problem please advice
thanks
Try swapadapter() method for the same.
void swapAdapter (Adapter adapter,
boolean removeAndRecycleExistingViews)
Swaps the current adapter with the provided one. It is similar to setAdapter(Adapter) but assumes existing adapter and the new adapter uses the same RecyclerView.ViewHolder and does not clear the RecycledViewPool.
Have you tried to call your SetUpRecyclerView in onActivityResult() which I asume your are using. That is what I use when adding elements to My RecyclerViews.
When using FragmentPagerAdapter, Your pager keeps two tabs in memory, that's why you have to scroll for the changes to take effect. But for me calling my SetupRecyclerView works just fine.
I have a view pager with two tabs in my app (A and B). The first tab i.e A has a listview. Clicking on any of the items in the list opens a new fragment (let's call it C) with another list. The tab B also has a listview.
The list in tab B and the list contained in the fragment C have the same type of items. That is each item of both list is of the same class. I have used a recycler view with a custom adapter to create the list. Since both lists in tab B and fragment c are of the same object type I have used the same adapter, namely (CarListAdapter.class) for this purpose.
The problem I am facing is that when I open fragment C then go back to tab A and then right swipe so that tab B is visible, clicking on an item in tab B causes null pointer exception, reason being that the list in fragment CarListFragment.class still has the data from fragment C and not tab B.(However the list is rendered properly, it is only the click that is not working properly)
Since two instance of this adapter are present I expected this to work fine. If however I use two different adapters with exact same code like (CarListAdapterC.class and CarListAdapterB.class) the code works fine.
I am stuck, can someone please explain why this is happening?
I have encountered same problem when using view pager and recycler view. I have searched allot and as i know it's an issue in recycler view adapter.
As you may know view pager load one more next page by default every time it shows a page.and here is where adapter shows the problem.The different instances of a same adapter that you create point to same place.because of that it still has other pages items.
I know it's not a good idea but i have created another adapter same as the first one with different name and used it for the next list and it worked.try to separate your list's adapters.let me know if it works.
I finally found the solution, it was really stupid on my part. I had static fields in RecyclerView Adapter. Removing them worked like a charm.
I created a navigation drawer project in android studio and created the separate fragments for my each item in the list view in drawer. However, now the items in the list are not static ones. I have a map in which i have some values based on which i have to choose whether to show a particular item or not.
I tried to change the call to setAdapter method based on values in map. However, it breaks the code when clicked on a particular item because in my fragments i do setOnAttached with a certain position which is hard coded such as 0,1,2 .. My question is how can I change all of this to show the corresponding fragment when any item in drawer is clicked.
Is it possible to know that this fragment should be attached to the dynamically calculated position based on the list view that I get at run time ?
public void onAttach(Activity activity) {
((MyActivity)activity).onSectionAttached(4);
}
Thanks
I am trying to make an application that has the following design.
Login->Dashboard
Dashboard has a #navigationdrawer having a listview ,of which every item opens a fragment.
Dashboard has a view pager of two pages.
Page 1 : listview
Page2: search bar.
Till now this is in place.
Problem statement :
The #page2 is constant it has to appear on every fragment ,but #page1 changes for every fragment . Can someone help me achieve this.
I am not getting how to do it.
I think you should use search menu item in Dashboard's ActionBar instead of fragment and display it using viewpager. Check following link to make search interface.
http://developer.android.com/guide/topics/search/search-dialog.html
We are trying to create an Android application that is based on ViewPager that receive during runtime instructions to add and remove pages. Almost like a tabbed browser experience, that you can remove the current tab, or remove a specific tab.
Following Google documentation we use FragmentStatePagerAdapter, that is intended to be used in situations when there are a large number of pages, working more like a list view.
But when we try to:
- Remove a page that is not on the screen
- and create a new fragment object from the same class
- and on the same position of the page removed
We noticed that the Android platform recover the dead page and display that to the user. The new object that we just created simply does not run “onCreate”, “onCreateView” or “onActivityCreated”.
We are looking to ways to fix this issue forcing the platform to use our new fragment object from the same class. Any ideas?
We found out that if we destroy the current page the platform indeed destroyed the page and created a new object from the same class. Here is a small example replicating the problem and this behavior.
Source: http://dl.dropbox.com/u/8333012/SimpleAdapter/SimplePager.zip
Video: http://www.youtube.com/watch?v=-oaXXwU8PSI&hd=1
From this project when you touch the TextView on the first page it was designed to remove the second page (which is green) to a new blue page. You will see that even doing that from the first page the second page remains green. But when you press the back Android button on the second page (green) and touch the TextView the second page created will be the right blue color.
When you are dealing with ListView's and change the underlying data of your adapter you call notifyDataSetChanged() and any View reflecting the data set will refresh itself. Thats the way you should go with fragment pager adapter too.
In your case you do not notify the adapter. However, in case of FragmentPagerAdapter/FragmentStatePagerAdapter it does not make a difference because those adapters ignore notifyDataSetChanged() "by default". To make it work override the getItemPosition() in your adapter implementation
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
And as already said, after you add/remove fragments call (in your showOtherPage())
mAdapter.notifyDataSetChanged();