how to set navigation drawer custom list view in android - android

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

Related

click on listview, android studio

I would like to click on an item of a listview to show another listview of other items, what can I do?
in my application I have a main Activity, where I have inserted a bottom view navigation, when you click on an item in the bottom view menu, the corresponding fragment appears in the main activity.
my listview is in the first fragment, and in the same fragment the next screen must appear, after the click in the listview.
do I have to replace the fragment of the listview with another one or can I replace only the listview? how?
Thanks for the attention
Instead of those options (replacing the fragment / list view), you could use the onItemSelected method to repopulate the list view object with the new list. Since you already know how to do this, it would seem the simplest option. A simple switch statement would suffice.

make first item of recyclerview selected in master/detail flow

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.

Android: Visible viewpager tab contains arraylist data of previous tab

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.

Integrating viewpager with a listview

I have a listview, clicking on one of the item in listview takes me to some fragment (different item may take to different fragment). Once a new fragment is opened I want to swipe to open the the next item in list instead of going back and clicking on the next item. I am thinking of using ViewPagers but not sure how to open the next item in the list.
You can create an Activity which has a ViewPager and Fragments with desired info as its children. On a list item click, take the position info and pass it to the Activity via Bundle. And inside the Activity after setting the ViewPager, you can show the desired Fragment with ViewPager's setCurrentItem(int item) function.

How do I refresh connected fragments accordingly?

I'm making a simple notepad app. In one fragment there's a list view of added notes and when some note is selected the other fragment shows the details of that note. Now if user clicks delete button a note is deleted, but how do I refresh the list view and detail view accordingly?
For refreshing the ListView actually what you have to refresh is the underlaying Adapter.
This would be the workflow of your scenario:
Remove the selected item
Call notifyDataSetChanged()
Set another item as the current one, for example the previous note or the first one.
And that's it.

Categories

Resources