您好,登錄后才能下訂單哦!
類似的功能可以看看:
使用RadioGroup實現底部導航欄
進入正題
效果圖:
注:PagerSlidingTabStrip為自定義控件,用于切換界面,此處不便貼出代碼
1.主界面xml布局中添加ViewPager控件:
<android.support.v4.view.ViewPager android:id="@+id/pager_view" android:layout_width="match_parent" android:layout_height="match_parent" />
2.主界面的activity代碼:
FrOrderList和FrFoodList分別為內部的fragment頁面
String[] tabs = {"堅果", "肉脯", "果凍"}; private MyPagerAdpater adpater; //為ViewPager添加Adpater adpater = new MyPagerAdpater(getSupportFragmentManager()); pagerView.setOffscreenPageLimit(4); pagerView.setAdapter(adpater); class MyPagerAdpater extends FragmentPagerAdapter { public MyPagerAdpater(FragmentManager manager) { super(manager); } @Override public Fragment getItem(int position) { switch (position){ case 0: return FrOrderList.newInstance( 0); case 1: return FrFoodList.newInstance( 1); case 2: return FrOrderList.newInstance( 2); } return null; } @Override public CharSequence getPageTitle(int position) { return tabs[position]; } @Override public int getCount() { return tabs.length; } }
3.內部fragment頁面:
其布局只是為了純顯示,所以只添加了一個textview
<TextView android:id="@+id/tv_content" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="32sp" android:textColor="#63B8FF" android:text="999"/>
activity代碼:
整個流程為:
public static FrFoodList newInstance(int mState) –》public void onCreate(@Nullable Bundle savedInstanceState) –》public View onCreateView –》public void initView() public class FrFoodList extends Fragment { TextView tvContent; private static String DATAKEY = "STATE"; private int mState = 0; public static FrFoodList newInstance(int mState) { FrFoodList frFoodList = new FrFoodList(); Bundle bundle = new Bundle(); bundle.putInt( DATAKEY, mState); frFoodList.setArguments(bundle); return frFoodList; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mState = getArguments().getInt( DATAKEY); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_food_list, null); tvContent = (TextView) view.findViewById(R.id.tv_content); initView(); return view; } public void initView(){ switch ( mState ){ case 0: tvContent.setText( "一大波堅果即將來襲~~~" ); break; case 1: tvContent.setText( "好吃又香,美味抵擋不住(^_^)" ); break; case 2: tvContent.setText( "浪漫氣息,粉嫩可愛(*_*)" ); break; } } }
FrOrderList的寫法和這個類似。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。