不同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,

相关内容

热门资讯

透视线上!wpk数据工具,cl... 透视线上!wpk数据工具,cloudpoker辅助透视原来真的有挂,微扑克教程(有挂详情)-哔哩哔哩...
十分钟了解(wpk专用)外挂透... 十分钟了解(wpk专用)外挂透明挂辅助app(辅助挂)德州论坛(2022已更新)(哔哩哔哩),亲,有...
科普攻略!wepoke辅助(辅... 科普攻略!wepoke辅助(辅助挂)外挂辅助器(2025已更新)(哔哩哔哩)1、很好的工具软件,可以...
2分钟科普!陕西欢喜麻将辅牌器... 2分钟科普!陕西欢喜麻将辅牌器(辅助挂)外挂透视辅助脚本(2021已更新)(哔哩哔哩);人气非常高,...
记者揭秘!wpk数据透视,德州... 记者揭秘!wpk数据透视,德州透视原来是真的有挂,2024新版技巧(有挂介绍)-哔哩哔哩;(需添加指...
2分钟了解(Wepoke脚本)... 《2分钟了解(Wepoke脚本)软件透明挂辅助挂(辅助挂)黑科技教程(2025已更新)(哔哩哔哩)》...
盘点一款!微扑克app发牌规律... 盘点一款!微扑克app发牌规律(辅助挂)透视辅助工具(2023已更新)(哔哩哔哩)1、微扑克appa...
2分钟普及!决战卡五星挂,畅游... 1、2分钟普及!决战卡五星挂,畅游麻将有挂,力荐教程(真实有挂)2、进入游戏-大厅左侧-新手福利-激...
透视辅助!约局吧是正规的,we... 此外,数据分析德州(wepoke苹果版)辅助神器app还具备辅助透视行为开挂功能,通过对客户wepo...
一分钟揭秘(德扑ai智能)外挂... 一分钟揭秘(德扑ai智能)外挂透明挂辅助软件(透视)透视辅助(2020已更新)(哔哩哔哩)是一款可以...