TabLayout 中各个 tab 布局一致,怎么解决控件的复用问题?

发布于 2022-09-02 10:21:02 字数 3889 浏览 39 评论 0

看完布局和代码再说具体问题吧

图片描述

        ArrayList<FrameLayout> frameLayouts = new ArrayList<>();
        MyOnItemClickListener onItemClickListener = new MyOnItemClickListener();
        FrameLayout frameLayout1 = (FrameLayout) getLayoutInflater().inflate(R.layout.fragment_my, null);
        kuwoAdapter1 = new SimpleAdapter(MainActivity.this, list1, R.layout.listview_item, new String[]{"id", "name", "artist"},
                new int[]{R.id.txtMusicId, R.id.txtMusicName, R.id.txtMusicArtist});
        ((ListView) frameLayout1.findViewById(R.id.lvShow)).setAdapter(kuwoAdapter1);
        ((ListView) frameLayout1.findViewById(R.id.lvShow)).setOnItemClickListener(onItemClickListener);
        swipeRefreshLayout1 = setSwipeRefreshLayout((SwipeRefreshLayout) frameLayout1.findViewById(R.id.swipeLayout));
        getDate(1);
        frameLayouts.add(frameLayout1);

        FrameLayout frameLayout2 = (FrameLayout) getLayoutInflater().inflate(R.layout.fragment_my, null);
        kuwoAdapter2 = new SimpleAdapter(MainActivity.this, list2, R.layout.listview_item, new String[]{"id", "name", "artist"},
                new int[]{R.id.txtMusicId, R.id.txtMusicName, R.id.txtMusicArtist});
        ((ListView) frameLayout2.findViewById(R.id.lvShow)).setAdapter(kuwoAdapter2);
        ((ListView) frameLayout2.findViewById(R.id.lvShow)).setOnItemClickListener(onItemClickListener);
        swipeRefreshLayout2 = setSwipeRefreshLayout((SwipeRefreshLayout) frameLayout2.findViewById(R.id.swipeLayout));
        getDate(2);
        frameLayouts.add(frameLayout2);

        FrameLayout frameLayout3 = (FrameLayout) getLayoutInflater().inflate(R.layout.fragment_my, null);
        kuwoAdapter3 = new SimpleAdapter(MainActivity.this, list3, R.layout.listview_item, new String[]{"id", "name", "artist"}, new int[]{R.id.txtMusicId, R.id.txtMusicName, R.id.txtMusicArtist});
        ((ListView) frameLayout3.findViewById(R.id.lvShow)).setAdapter(kuwoAdapter3);
        ((ListView) frameLayout3.findViewById(R.id.lvShow)).setOnItemClickListener(onItemClickListener);
        swipeRefreshLayout3 = setSwipeRefreshLayout((SwipeRefreshLayout) frameLayout3.findViewById(R.id.swipeLayout));
        getDate(3);
        frameLayouts.add(frameLayout3);

        FrameLayout frameLayout4 = (FrameLayout) getLayoutInflater().inflate(R.layout.fragment_my, null);
        kuwoAdapter4 = new SimpleAdapter(MainActivity.this, list4, R.layout.listview_item, new String[]{"id", "name", "artist"}, new int[]{R.id.txtMusicId, R.id.txtMusicName, R.id.txtMusicArtist});
        ((ListView) frameLayout4.findViewById(R.id.lvShow)).setAdapter(kuwoAdapter4);
        ((ListView) frameLayout4.findViewById(R.id.lvShow)).setOnItemClickListener(onItemClickListener);
        swipeRefreshLayout4 = setSwipeRefreshLayout((SwipeRefreshLayout) frameLayout4.findViewById(R.id.swipeLayout));
        getDate(4);
        frameLayouts.add(frameLayout4);

        FrameLayout frameLayout5 = (FrameLayout) getLayoutInflater().inflate(R.layout.fragment_my, null);
        kuwoAdapter5 = new SimpleAdapter(MainActivity.this, list5, R.layout.listview_item, new String[]{"id", "name", "artist"}, new int[]{R.id.txtMusicId, R.id.txtMusicName, R.id.txtMusicArtist});
        ((ListView) frameLayout5.findViewById(R.id.lvShow)).setAdapter(kuwoAdapter5);
        ((ListView) frameLayout5.findViewById(R.id.lvShow)).setOnItemClickListener(onItemClickListener);
        swipeRefreshLayout5 = setSwipeRefreshLayout((SwipeRefreshLayout) frameLayout5.findViewById(R.id.swipeLayout));
        getDate(5);
        frameLayouts.add(frameLayout5);

总是重复这段代码感觉很恶心,有没有优雅的方式,具体的代码在这里

自己摸索的,还望大神指点。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名。 2022-09-09 10:21:03

用TabLayout+ViewPager+Fragment解决

列表封装到Fragment中

顾挽 2022-09-09 10:21:03

哈哈,你早该用 https://github.com/hackware19... 了。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文