您好,登錄后才能下訂單哦!
Android中怎么使用ViewPager+Fragment實現(xiàn)滑動菜單Tab效果?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
前臺代碼(activity_main.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:background="@android:color/white" android:baselineAligned="false" android:gravity="center_vertical" android:orientation="horizontal" android:paddingBottom="5dp" android:paddingTop="5dp"> <!--detail Tab--> <TextView android:id="@+id/item_detail" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" android:text="@string/detail_tab" android:textColor="@color/main_tab_text_color" android:textSize="20dp"/> <!--category report Tab--> <TextView android:id="@+id/item_category_report" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" android:text="@string/category_report_tab" android:textColor="@color/main_tab_text_color" android:textSize="20dp"/> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/mainViewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout>
主界面代碼(MainActivity.java):
package com.hyl.acccountbookdemo; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * @programName: MainActivity.java * @programFunction: Recording of income and expenditure * @createDate: 2018/09/25 * @author: AnneHan * @version: * xx. yyyy/mm/dd ver author comments * 01. 2018/09/25 1.00 AnneHan New Create */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView item_detail, item_category_report; private ViewPager vp; private OneFragment oneFragment; private TwoFragment twoFragment; private List<Fragment> mFragmentList = new ArrayList<Fragment>(); private FragmentAdapter mFragmentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); mFragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), mFragmentList); vp.setOffscreenPageLimit(2);//ViewPager的緩存為2幀 vp.setAdapter(mFragmentAdapter); vp.setCurrentItem(0);//初始設(shè)置ViewPager選中第一幀 item_detail.setTextColor(Color.parseColor("#1ba0e1")); //ViewPager的監(jiān)聽事件 vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { /*此方法在頁面被選中時調(diào)用*/ changeTextColor(position); } @Override public void onPageScrollStateChanged(int state) { /*此方法是在狀態(tài)改變的時候調(diào)用,其中arg0這個參數(shù)有三種狀態(tài)(0,1,2)。 arg0==1的時辰默示正在滑動, arg0==2的時辰默示滑動完畢了, arg0==0的時辰默示什么都沒做。*/ } }); } /** * 初始化布局View */ private void initViews() { item_detail = (TextView) findViewById(R.id.item_detail); item_category_report = (TextView) findViewById(R.id.item_category_report); item_detail.setOnClickListener(this); item_category_report.setOnClickListener(this); vp = (ViewPager) findViewById(R.id.mainViewPager); oneFragment = new OneFragment(); twoFragment = new TwoFragment(); //給FragmentList添加數(shù)據(jù) mFragmentList.add(oneFragment); mFragmentList.add(twoFragment); } /** * 點擊頭部Text 動態(tài)修改ViewPager的內(nèi)容 */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.item_detail: vp.setCurrentItem(0, true); break; case R.id.item_category_report: vp.setCurrentItem(1, true); break; } } public class FragmentAdapter extends FragmentPagerAdapter { List<Fragment> fragmentList = new ArrayList<Fragment>(); public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) { super(fm); this.fragmentList = fragmentList; } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } } /** * 由ViewPager的滑動修改頭部導航Text的顏色 * @param position */ private void changeTextColor(int position) { if (position == 0) { item_detail.setTextColor(Color.parseColor("#1ba0e1")); item_category_report.setTextColor(Color.parseColor("#000000")); } else if (position == 1) { item_category_report.setTextColor(Color.parseColor("#1ba0e1")); item_detail.setTextColor(Color.parseColor("#000000")); } } }
需要多少個Fragment,便創(chuàng)建多少個,這里只舉例寫一個,其它相同
建立Fragment(fragment_one.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/detail_tab" android:textSize="25sp"/> </LinearLayout>
Fragment界面代碼(OneFragment.java):
package com.hyl.acccountbookdemo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * @programName: OneFragment.java * @programFunction: * @createDate: 2018/09/25 * @author: AnneHan * @version: * xx. yyyy/mm/dd ver author comments * 01. 2018/09/25 1.00 AnneHan New Create */ public class OneFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_one, container, false); } }
strings.xml:
<string name="detail_tab">明細</string> <string name="category_report_tab">類別報表</string>
colors.xml:
<color name="main_tab_text_color">#000000</color>
看完上述內(nèi)容,你們掌握Android中怎么使用ViewPager+Fragment實現(xiàn)滑動菜單Tab效果的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。