简体   繁体   English

如何从另一个片段更新片段的Lis​​tView?

[英]How to update the ListView of a Fragment from another Fragment?

How can I communicate a listview of a ListFragment after an event of a Fragment inside another Fragment ? 如何在另一个Fragment中的Fragment事件之后传达ListFragmentlistview

In the ListFragment (fragment A) I have a method to refresh the ListView . ListFragment (片段A)中,我有一个刷新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) 但是,我需要在Fragment内点击一个Button后刷新它,它是另一个FragmentFragment (片段b)

Its like Fragment A (listFragment) | 它像Fragment A(listFragment)| Fragment B (detailview) (fragment C - child fragment of B) Fragment B(detailview)(片段C - B的子片段)

How can I do it? 我该怎么做?

You can access another Fragment by its tag : 您可以通过其标记访问另一个Fragment

 // 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: Fragment的标记在附加到容器布局时设置:

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. 因此,当您单击Button ,找到要通过其标记刷新的Fragment ,然后调用您的刷新方法。

IF you are using a ViewPager , this is how to get the Fragments tag: 如果您使用的是ViewPager ,这是获取Fragments标记的方法:

   /**
     * 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;
    }

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. 为组件创建一个侦听器接口,以侦听FragmentC中的按钮单击事件。 For example: 例如:

    public interface FragmentCButtonListener { public void onButtonClicked(); public interface FragmentCButtonListener {public void onButtonClicked(); } }

  2. Add a method in FragmentC to allow listener registration. 在FragmentC中添加一个方法以允许侦听器注册。 FragmentC will keep track of the listener, and call the listener callback as soon as the button is clicked. FragmentC将跟踪监听器,并在单击按钮后立即调用监听器回调。 For example: 例如:

    public class FragmentC extends Fragment { FragmentCButtonListener myListener; 公共类FragmentC扩展Fragment {FragmentCButtonListener myListener; public void registerListener (FragmentCButtonListener listener) { myListener = listener; 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. FragmentA应该实现FragmentCButtonListener接口,将自己注册为FragmentC的监听器,并在收到FragmentC的回调时刷新列表视图。 For example: 例如:

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

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

    } }

Please note, I assume the FragmentA has a reference to FragmentC in the sample. 请注意,我假设FragmentA在样本中引用了FragmentC。 If not, just make sure the container class of all fragments registers itself as the listener of FragmentC. 如果没有,只需确保所有片段的容器类将自己注册为FragmentC的侦听器。 The container class can as FragmentA to update listview once it receives callback from FragmentC. 一旦收到FragmentC的回调,容器类就可以像FragmentA一样更新listview。

follow these steps We have two fragments called AddFragment and 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!!!). 按照这些步骤我们有两个名为AddFragmentListFragment片段,在第一个片段上添加一个项目时,你希望更新的列表显示在列表片段上(这是什么样的巫术!!!)。

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 步骤1在AddFragment的类级别上创建一个侦听器接口,该方法将由另一个人(ListFragment)实现并创建接口类型变量

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 步骤2在同一个AddFragment类的类级别创建register方法并设置listenter变量

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 …. ” 第3步任何事件反刍时按钮点击或大吼大叫你的应用程序(这被认为是android中的粗暴事件:-))检查监听器对象meAddListener变量并调用界面,在莎士比亚的简言之,它意味着“for thou who implement ye interface and brought the method within ur class shelter, I shall give u my utmost privilege and blessing to …. ” “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. 步骤4在ListFragment上实现AddFragment的界面,没有回头只是去实现它的方法。 Within that method u just call abracadabra to repopulate ur list or any sort of updatable android view object… and ur done 在那个方法中你只需要调用abracadabra来重新填充你的列表或任何类型的可更新的android视图对象...并且你做了

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 检查一下,多一点享受java魔法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM