I have three ListFragment is one activity. For the first listFragment I have created a ListFragment and inflate a layout with some listItem easily:
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getResources.getStringArray(R.string.baal)));
The item of the second listFragment will change according to the selected item of the first ListFragment and in the similar way The item of the third listFragment will change according to the selected item of the second ListFragment.
The first ListFragment is static as it won't depend on any other ListFragment. So I simply inflate a layout with some item for it.
But The items of second and third listFragments need to be changed frequently. So I need to inflate it in run-time with new listitems every time on click event. So I think the second and third LisFragment needs to be create dynamically by inflating every-time with new list items. How can I achieve that? I am new in Fragment and I need my concept on dynamic UI clear. Thanks in advance.
You can, for example, try to clear your adapters of second and third fragments and then add all necessary items, this way you would not need to inflate the whole fragments, but instead only the content would change. Also depending on adapter types you would need to call notifyDataSetChanged or not (if it is ArrayAdapter for example).
UPD: On your main item click event you can write some code like this
YourAdapter1 adapter1 = getFirstAdapter();
adapter1.clear();
adapter1.addAll(newItem1Collection);
YourAdapter2 adapter2 = getSecondAdapter();
adapter2.clear();
adapter2.addAll(newItem2Collection);
where YourAdapter1 and YourAdapter2 should extend ArrayAdapter, this will also automatically invoke notifyDataSetChange.
I suggest you use tabs, adding tabs dynamically for each selection, and allowing the user to swipe back to cancel a selection, something like breadcrumbs navigation.
You can see the related question here:
Android - How to create tabs on demand using existing layout?
Related
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.
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 ViewPager in my activity which contains 5 fragments,I
fixed a menu on toolbar which used as filter, on click on menu
item I need to change views on all fragments,
I used to clear all the fragments in ViewPager and attached new instances of fragments,this displays correct views but while creating new
views I used to show a progress bar on screen but It's not showing as
we set, it calls after the change of all views.
The thing I doubt was is this correct method to change all views by replacing fragments with new instances Or there is any other method to change data in fragments respective to my context
Thank you
Edit:
clearing adapter for create new views
viewPagerAdapter.fragmentArrayList.clear();
viewPagerAdapter.fragmentTitle.clear();
viewPagerAdapter.notifyDataSetChanged();
If you want to refresh all your fragment at once you can setAdapter of ViewPager again than it will refresh your all fragments like this;
mViewPager.setAdapter(yourAdapter);
//If you want set current postion for adapter
mViewPager.setCurrentItem(theCurrentPage);
You are rightfully doubting whether destroying and recreating all your fragments is a good method if you only want to filter the list of fragments that your ViewPager is showing. It amounts to unnecessarily wasting considerable resources.
Instead you either manipulate your fragment list (and fragment title list?) and then call notifyDataSetChanged() or implement a Filter as explained here.
So you go:
viewPagerAdapter.fragmentArrayList.remove(index);
or
viewPagerAdapter.fragmentArrayList.remove(fragment);
and then you say:
viewPagerAdapter.notifyDataSetChanged();
Concerning the progress bar, where in your XML layout is it located? Within the view pager? Then move it outside. Somewhere else? Then show us your XML and the code creating and showing it.
Is it possible to build one fragment with a listview and a textview inside, so i dont need to add a listfragment and and a DetailFragment. Just one fragment. Because I have only space for one fragment in my activity.
Thanks for youranswers
You don't need to use a List-Activity/Fragment to be able to show a listview. It simply makes it a bit easier for you with functions like getList();
You can just make any normal Activity/Fragment, give your ListView an ID and just fetch it with findViewById(); like you'd do with any other view.
I've implemented Sherlock action bar tabs today, so my tab handling class extends SherlockActivity implements ActionBar.TabListener.
I start some empty layout, and then each tab has it's own layout, and it works fine.
First, on my first tab I need a list (but can't extend listview obviously). I'm using Strings
and then string array
<string-array name="my_keys">
<item>#string/mytab_mymonitor</item>
<item>#string/mytab_mymessaging</item>
<item>#string/mytab_information</item>
</string-array>
So first, how to populate listview from strings using adapter?
I tried this, but it crashes my app:
myKeys = getResources().getStringArray(R.array.my_keys);
ListView mListView = (ListView) findViewById(R.id.lvMyList);
mListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myKeys));
Second question here is, as I'll have 5 tabs with lot of data processing, is it normal to have everything within "Tab.Listener" activity, or could I somehow use multiple classes / activities while my tabs would still be on place?
The key is to use Tabs with a ViewPager and Fragments. That way you could put any type of content you need (e.g. you could use a ListFragment).
Check out this code snippet I've written in another answer.