How to update the ListView of a Fragment from another Fragment?

喜欢而已 提交于 2019-12-06 05:15:10

问题


How can I communicate a listview of a ListFragment after an event of a Fragment inside another Fragment?

In the ListFragment (fragment A) I have a method to refresh the ListView. But also, I need to refresh it after a click of a Button inside a Fragment, wich is child of another Fragment (fragment b)

Its like Fragment A (listFragment) | Fragment B (detailview) (fragment C - child fragment of B)

How can I do it?


回答1:


You can access another Fragment by its tag:

 // find your fragment
 YourFragment f = (YourFragment) getSupportFragmentManager().findFragmentByTag("yourFragTag");

 // update the list view
 f.updateListView(); 

The tag of your Fragment is set when it is attached to a container layout:

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frameBuy, YourFragment.newInstance(), "yourFragTag").commit();

So when you click your Button, find the Fragment you want to refresh by its tag, and then call your refresh method.

IF you are using a ViewPager, this is how to get the Fragments tag:

   /**
     * Gets the fragment tag of a fragment at a specific position in the viewpager.
     *
     * @param pos the pos
     * @return the fragment tag
     */
    public String getFragmentTag(int pos){
        return "android:switcher:"+R.id.yourViewPagerId+":"+pos;
    }



回答2:


You can do it with a few simple steps:

  1. Create a listener interface for component to listen to the button click event from FragmentC. For example:

    public interface FragmentCButtonListener { public void onButtonClicked(); }

  2. Add a method in FragmentC to allow listener registration. FragmentC will keep track of the listener, and call the listener callback as soon as the button is clicked. For example:

    public class FragmentC extends Fragment { FragmentCButtonListener myListener; public void registerListener (FragmentCButtonListener listener) { myListener = listener; } }

  3. FragmentA should implement FragmentCButtonListener interface, register itself as a listener to FragmentC, and refresh the list view when it receives the callback from FragmentC. For example:

    public class FragmentC extends Fragment implements FragementCButtonListener { FragmentC fragmentC; public void onCreate() { fragment = new FragmentC(); fragment.registerListener (this); }

    public void onButtonClicked() {
        //refresh the list view
    }
    

    }

Please note, I assume the FragmentA has a reference to FragmentC in the sample. If not, just make sure the container class of all fragments registers itself as the listener of FragmentC. The container class can as FragmentA to update listview once it receives callback from FragmentC.




回答3:


follow these steps We have two fragments called AddFragmentand ListFragment, and upon adding an item on first fragment you want the updated list be shown on list fragment (what sort of sorcery is this!!!).

Step 1 create the listener interface on class level of AddFragment with a method that is going to be implemented by the other guy (ListFragment ) and create Interface type variable

public class AddFragment extends Fragment{
 //listener varriable

 //listener
  public interface OnCategoryAddedListener{
    public void refreshList();
}
 private static OnCategoryAddedListener meAddListener;
}

Step 2 create register method on class level of the same AddFragment class and set listenter variable

public class AddFragment extends Fragment{


public void registerListener(OnCategoryAddedListener listener)
{
    meAddListener = listener;
}
}

Step 3 upon any event cud be button click or yelling at ur application(that is considered rude event in android :-) ) check for listener object meAddListener variable and call the interface, in a Shakespeare’s nutshell it means “for thou who implement ye interface and brought the method within ur class shelter, I shall give u my utmost privilege and blessing to …. ”

Step 4 On ListFragment implement the AddFragment’s interface,no turning back just go implement its method. Within that method u just call abracadabra to repopulate ur list or any sort of updatable android view object… and ur done

public class ListFragment extends Fragment implements AddFragment.OnCattegoryAddedListener{
//refer to AddFragment
AddFragment addFragment;

//once the fragment is within the memory scope u instantiate AddFragment
//and register listener with AddFragment context which inherently implement OnCategoryAddedListener

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        categoryAddFragment = new CategoryAddFragment();
        categoryAddFragment.registerListener(this);

    }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    fillList();

}

public void fillList() {

    ArrayAdapter<String>   catArrayAdapter = new ArrayAdapter<>(context,android.R.layout.simple_list_item_1,getItems());
    setListAdapter(catArrayAdapter);

}
//le interface method u can count on to refresh ur list content
public void refreshList(){
fillList();
}

check this out for a little more enjoy the java magic



来源:https://stackoverflow.com/questions/22386109/how-to-update-the-listview-of-a-fragment-from-another-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!