TabLayout 中各个 tab 布局一致,怎么解决控件的复用问题?
看完布局和代码再说具体问题吧
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用TabLayout+ViewPager+Fragment解决
列表封装到Fragment中
哈哈,你早该用 https://github.com/hackware19... 了。