怎么在Android中自定義View和TabLayout實現(xiàn)頂部標(biāo)簽滑動效果?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)與策劃設(shè)計,溆浦網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:溆浦等地區(qū)。溆浦做網(wǎng)站價格咨詢:18982081108
主布局: fragment_message.xml
item布局: item_table_msg.xml
適配器:MessagePagerAdapter
public class MessagePagerAdapter extends FragmentPagerAdapter { private final List> mFragmentList = new ArrayList<>(); private Context mContext; public MessagePagerAdapter(Context mContext,FragmentManager fm) { super(fm); this.mContext = mContext; } public void addFlag(Pair msgTabBeanPair){ mFragmentList.add(msgTabBeanPair); } @Override public Fragment getItem(int position) { return mFragmentList.get(position).first; } @Override public int getCount() { return mFragmentList.size(); } public View getTabView(int position, ViewGroup mGroup){ View view; view = LayoutInflater.from(mContext).inflate(R.layout.view_table_msg,mGroup,false); ImageView img = view.findViewById(R.id.iv_msg_tab); TextView tv = view.findViewById(R.id.tv_msg_tab); img.setImageResource(mFragmentList.get(position).second.getResId()); tv.setText(mFragmentList.get(position).second.getTitle()); return view; } }
Fragment中進(jìn)行設(shè)置:
mMessagePagerAdapter = new MessagePagerAdapter(getActivity(),getActivity().getSupportFragmentManager()); mMessagePagerAdapter.addFlag(new Pair<>(MsgFragmentFactory.getInstance().getMsgCommListFragment(),new MsgTabBean("通訊錄",R.drawable.ic_msg_comm_list))); mMessagePagerAdapter.addFlag(new Pair<>(MsgFragmentFactory.getInstance().getMsgCrowdFragment(),new MsgTabBean("群聊",R.drawable.ic_msg_crowd))); mMessagePagerAdapter.addFlag(new Pair<>(MsgFragmentFactory.getInstance().getMsgConverFragment(),new MsgTabBean("消息",R.drawable.ic_msg_conver))); mMessagePagerAdapter.addFlag(new Pair<>(MsgFragmentFactory.getInstance().getMsgFindFragment(),new MsgTabBean("發(fā)現(xiàn)",R.drawable.ic_msg_find))); mFgMgViewpager.setAdapter(mMessagePagerAdapter); mFgMgTab.setupWithViewPager(mFgMgViewpager); for (int index =0 ;index關(guān)于怎么在Android中自定義View和TabLayout實現(xiàn)頂部標(biāo)簽滑動效果問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
本文名稱:怎么在Android中自定義View和TabLayout實現(xiàn)頂部標(biāo)簽滑動效果
本文網(wǎng)址:http://weahome.cn/article/johhsc.html