How to refresh a Fragment in android - android

I am using three tabs and these tabs having list view, on these tabs i am adding new list items in tabs. After Creating a list the new list item is not added when i scroll the tabs then it shows the list item

when you add item in listview call one method (make it by own) in your adapter
public void refreshWishlistAdapter() {
itemList.clear();
itemList.addAll(dbHelper.getallWishlistItems());
itemsAdapter.notifyDataSetChanged();
}

Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
Your_Fragment_TAG is the name you gave your fragment when you created it
This code is for support library.

Related

Why my app crash when i refreshing the fragment inside the same fragment on button click listner

I want refresh my fragment when I click on button which inside in this same fragment. I tried some code but when I click on button my app automatically crash.
I try these codes πŸ‘‡
Fragment frg =null;
frg=getActivity().getSupportFragmentManager().findFragmentByTag(getFragmentTag(4));
final FragmentTransaction ft =
getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
And I try this for getting the tags of fragment because I'm using ViewPager.
And I'm Using Androidx Libraries.
My code is this for getting tag. πŸ‘‡
public String getFragmentTag(int pos){
return "android:switcher:"+R.id.view_pager_main+":"+pos;
}
I solved my problem just using thisπŸ‘‡
getActivity().getSupportFragmentManager().beginTransaction().detach(FragmenViews.this).attach(FragmentViews.this).commit();

Added fragment scrolled down

I have two fragments the first fragment contains list of linear layouts and the whole fragment is in scroll view the second fragment is added and the first is hidden on choosing item from the first.
The problem is the second fragment is created scrolled down if the first fragment was scrolled down.
I tried the ways to force second fragment to be scrolled to (0,0) but failed.
the code used to add the second fragment
public void setActionOnClick(String id) {
CommentFragment frag = new CommentFragment();
Bundle bundle = new Bundle();
bundle.putString("id", id);
bundle.putString("TAG", TAG_NEWS_STORY);
((MainActivity) getActivity()).setCurrentTag(TAG_NEWS_STORY);
frag.setArguments(bundle);
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.add(R.id.main_content, frag, TAG_NEWS_STORY);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
on Attach of second fragment the first fragment is hidden.
I don't want to use fragmentTransaction.replace because there are calls to the api which I don't want to reload.
this is an old question but. probably your fragment place_hoder is inside an scrollview.
just check if R.id.main_content is in a scrollview or not.
you should add the ScrollView to the fragment-layout instead of instantiating the fragment inside one
Before this:
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
Add code to remove old fragment:
getSupportFragmentManager().beginTransaction().remove(yourOldFragment).commitAllowingStateLoss();

Fragment not replacing properly in second time android

I am having FragmentDetail.java file which consist of recylerview,when touch on recylerview the same fragment FragmentDetail.java is called,Same process when i repeat 4 5 times by touching two simultaneously recyler view items. It does not show the back fragment immediately.Instead it shows the fragment of recyler view items.First Fragmet contains recylerview on which click ProductMenu calls,and on Product menu recyler view click FragmentDetail.java calls.So know what my problem is that on back press fragments are not getting replace properly.Below is my code :-
FirstFragment
list_recycler.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), list_recycler, new ClickListener() {
#Override
public void onClick(View view, int position) {
Fragment fragment = new ProductMenu();
fragmentManager = getActivity().getSupportFragmentManager();
fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out, R.anim.slide_enter, R.anim.slide_exit);
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
ProductMenu.java
Also contains the same code.In spite of fragment FragmentDetail fragment = new FragmentDetail();
FragmentDetail.java
FragmentDetail fragment = new FragmentDetail();
ProductsBean productsBean = arr_related_product.get(position);
fragmentManager = getActivity().getSupportFragmentManager();
//final int newBackStackLength = fragmentManager.getBackStackEntryCount() +1;
fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out, R.anim.slide_enter, R.anim.slide_exit);
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
what do you mean by
:
Same process when i repeat 4 5 times by touching two simultaneously recyler view items. --> ?
At first instance i would suggest do
fragmentTransaction.add(R.id.container_body, fragment);
instead of
fragmentTransaction.replace(R.id.container_body, fragment);
Let me know if it works ,also try to explain your problem little more with help of code

How can I programmatically add multiple list fragments to a single activity?

Apologies as I am an Android novice. I'm trying to programmatically add multiple list fragments to a single activity, but when I do, only one is displayed. How can I display multiple lists in a single action?
The end goal is to read a set of data from an API and categorize it into multiple lists in my application. I would like the data to be horizontally scrolling list fragments, but since that's an additional complication I've started with simple ListFragments. My code looks like this:
activity_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
ItemActivity:
public class ItemActivity extends FragmentActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager manager = getSupportFragmentManager();
ItemListFragment fragment1 = new ItemListFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragmentContainer, fragment1);
ItemListFragment fragment2 = new ItemListFragment();
transaction.add(R.id.fragmentContainer, fragment2);
transaction.commit();
}
}
ItemListFragment:
public class ItemListFragment extends ListFragment {
List<String> items = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
items.add("One");
items.add("Two");
items.add("Three");
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
setListAdapter(adapter);
}
}
If you want multiple lists in one Activity, here's what I usually do:
Because every ListView is using independent scrolling view, I usually split the screen for every ListView I have. Example: with 2 ListView i split the height of the screen 50/50 so every ListView have 50% portion of the screen.
If I have to dynamically add ListView to the screen, I use cwac merge adapter to merge the adapter and display it in a single ListView
Another alternative you can just use ViewPager to display ListFragment. This would achieve what you want, which is having multiple listviews in a single activity.
Why wouldnt you do it this way?
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
// work here to change Activity fragments (add, remove, etc.). Example here of adding.
fragmentTransaction.add (R.id.myFrame, myFrag);
fragmentTransaction.commit ()
i found this code, so as just one is displayed, commit each transaction in your code.
FragmentManager manager = getSupportFragmentManager();
ItemListFragment fragment1 = new ItemListFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragmentContainer, fragment1).commit();
transaction = manager.beginTransaction(); // might be unnescessary
ItemListFragment fragment2 = new ItemListFragment();
transaction.add(R.id.fragmentContainer, fragment2).commit();
if this throws some sort of error, you might need to start another transaction for adding the second fragment.

Can't change a fragment in ViewPager

I'm trying to change a fragment inside a ViewPager:
public void change() {
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager, mapa);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
mSectionsPagerAdapter.notifyDataSetChanged();
}
R.id.pager is my ViewPager id.
mapa is a inflated fragment.
If I call change(), the fragment I want to replace will disappear but the new fragment will not be load.
The view pager uses an Adapter to change the fragments it displays. Just like a ListView uses its adapter to get views to populate the list.
You have to extend FragmentPagerAdapter, create a new instance of it and set it to the pager with this line:
mViewPager.setAdapter(fragPagerAdapter);

Categories

Resources