将主列表中的列表传递到选项卡Fragment RecyclerView

时间:2018-03-31 18:37:41

标签: java android android-activity android-recyclerview fragment

我的活动有4个标签,每个标签都有RecyclerView。我在MainActivity中获取Json数据,然后将其转换为List。

此处的挑战是将列表传递给每个标签中每个RecyclerView中的每个Fragment

2 个答案:

答案 0 :(得分:0)

最简单的方法是使用ViewModel这样:

视图模型:

public class SharedViewModel extends ViewModel {
    private List<Item> list_1= ArrayList<Item>();
    private List<Item> list_2= ArrayList<Item>();

    public void setList_1(List<Item> list) {
        list_1 = list
    }

    public List<Item> getList_1() {
        return list_1 ;
    }

    public void setList_2(List<Item> list) {
        list_2 = list
    }

    public List<Item> getList_2() {
        return list_2 ;
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity {

    SharedViewModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_acticity);
        model = ViewModelProviders.of(this).get(SharedViewModel.class);

        // Getting your lists here
        model.setList_1(list_1);
        model.setList_2(list_2);

    }
}

Fragment_1:

public class Fragment_1 extends Fragment {

    private SharedViewModel model;
    List<Item> list_1 = new ArrayList<>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
        list_1 = model.getList_1();
    }
}

Fragment_2:

public class Fragment_2 extends Fragment {

    private SharedViewModel model;
    List<Item> list_2 = new ArrayList<>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
        list_2 = model.getList_2();
    }
}

但是,更好的方法更复杂。更多信息Guide to App Architecture

答案 1 :(得分:0)

您可以考虑在BoradcastReceiver中使用Fragment。因此,当更新片段列表时,您将从MainActivity发送广播,并且将在片段中接收广播。

您只需在BroadcastReceiver中注册Fragment即可。

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get extra data included in the Intent
        // You might consider sending a list to be passed to the adapter of the RecyclerView. 
        String data = intent.getStringExtra("data");
        // Update your RecyclerView with data. 
    }
};

Fragment功能内的onResume中注册接收器。

@Override
protected void onResume() {
    // Register to receive messages.
    // We are registering an observer (mMessageReceiver) to receive Intents
    // with actions named "update-frag-1-recyclerview".
    LocalBroadcastManager.getInstance(this).registerReceiver(
            mMessageReceiver, new IntentFilter("update-frag-1-recyclerview"));
    super.onResume();
}

并在onPause的{​​{1}}功能中取消注册接收器。

Fragment

现在,从您的@Override protected void onPause() { // Unregister since the Fragment is paused. LocalBroadcastManager.getInstance(this).unregisterReceiver( mMessageReceiver); super.onPause(); } 收到数据并且您现在可以将数据传递到片段时,只需相应地发送广告即可被片段接收。我正在展示一个。

Activity

当您正在收听来自Intent intent = new Intent("update-frag-1-recyclerview"); // You can also include some extra data. intent.putExtra("data", "This is some data!"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 的{​​{1}}事件时,这会触发onReceive中的Fragment1功能。你也可以为其他片段做同样的事情。请记住,我只发送一个update-frag-1-recyclerview的数据。您可以考虑发送一个列表以及一个额外的列表以及在Fragment1中收到的意图。

希望有所帮助!