不同Fragment中RecyclerView需要执行不同的操作。
创始人
2025-01-09 00:34:29
0

我们首先需要在FragmentManager中为每个Fragment创建一个tag,然后在Activity中为每个tag创建相应的RecyclerView。在Fragment中,我们可以通过onAttach和onDetach方法来获取Activity中相应的RecyclerView,并根据需要执行不同的操作。

下面是一个示例代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    
    private RecyclerView fragment1RecyclerView;
    private RecyclerView fragment2RecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取各个Fragment的tag
        String tag1 = "fragment1";
        String tag2 = "fragment2";

        // 创建RecyclerView
        fragment1RecyclerView = findViewById(R.id.fragment1RecyclerView);
        fragment2RecyclerView = findViewById(R.id.fragment2RecyclerView);

        // 将RecyclerView添加到FragmentManager中
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment1 fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();
        ft.add(R.id.fragmentContainer, fragment1, tag1);
        ft.add(R.id.fragmentContainer, fragment2, tag2);
        ft.commit();
    }

    // 通过tag获取对应的RecyclerView
    public RecyclerView getRecyclerView(String tag) {
        if (tag.equals("fragment1")) {
            return fragment1RecyclerView;
        } else if (tag.equals("fragment2")) {
            return fragment2RecyclerView;
        }
        return null;
    }
}

Fragment1.java

public class Fragment1 extends Fragment {

    private RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, container, false);
        recyclerView = ((MainActivity) getActivity()).getRecyclerView("fragment1");
        // 在这里执行Fragment1需要的操作
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        recyclerView = null;
    }
}

Fragment2.java

public class Fragment2 extends Fragment {

    private RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,

相关内容

热门资讯

aapoker挂!aapoke... aapoker挂!aapoke辅助工具存在吗(透视)原本是真的有挂(详细辅助解密教程);小薇(透视辅...
wepoke辅助!微扑克俱乐部... wepoke辅助!微扑克俱乐部管理(透视)果然真的有挂(详细辅助解密教程)1)wepoke辅助辅助挂...
aapoker辅助工具!wpk... aapoker辅助工具!wpk俱乐部有外挂吗(透视)本然是有挂(详细辅助AI教程);人气非常高,ai...
wepoke有挂!wpk微扑克... wepoke有挂!wpk微扑克免费辅助(透视)往昔存在有挂(详细辅助普及教程)是一款可以让一直输的玩...
aapoker有外挂!微扑克算... aapoker有外挂!微扑克算正规平台吗(透视)起初存在有挂(详细辅助介绍教程)您好,aapoker...
微扑克全自动机器人!微扑克代打... 您好,微扑克全自动机器人这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家...
wepoke真的有挂!hm3德... wepoke真的有挂!hm3德州辅助怎么购买(透视)往昔真的是有挂(详细辅助插件教程);是一款可以让...
微扑克wpk透视辅助!扑克世界... 微扑克wpk透视辅助!扑克世界牌局设置(透视)果然是有挂(详细辅助科技教程)微扑克wpk透视辅助辅助...
wpk透视辅助工具!德州之星a... wpk透视辅助工具!德州之星app辅助器怎么用(透视)素来是真的有挂(详细辅助揭秘教程)是一款可以让...
微扑克ai辅助!微扑克线上代打... 微扑克ai辅助!微扑克线上代打(透视)往昔真的是有挂(详细辅助2025新版教程)是一款可以让一直输的...