中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在ViewPager中使用Fragment實現側滑導航欄

發布時間:2021-05-18 15:33:13 來源:億速云 閱讀:155 作者:Leah 欄目:開發技術

這期內容當中小編將會給大家帶來有關怎么在ViewPager中使用Fragment實現側滑導航欄,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

Activity:

package com.example.administrator.android006;
 
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
 
import com.example.administrator.android006.Fragment.fragment1;
import com.example.administrator.android006.Fragment.fragment2;
import com.example.administrator.android006.Fragment.fragment3;
import com.example.administrator.android006.Fragment.fragment4;
 
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends FragmentActivity implements View.OnClickListener {
 
    //頂部4個按鈕
    private LinearLayout main_home_layout,main_msg_layout,main_pal_layout,main_me_layout;
    private ViewPager main_mViewPager;
    //ViewPager的適配器
    private FragmentPagerAdapter mAdapter;
    //4個Fragment碎片的集合
    private List<Fragment> mFragments = new ArrayList<>();
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //初始化,加載碎片
        initView();
        initAdapter();
    }
 
    public void initAdapter(){
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }
 
            @Override
            public int getCount() {
                return mFragments.size();
            }
        };
        main_mViewPager.setAdapter(mAdapter);
        main_mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
 
            }
 
            @Override
            public void onPageSelected(int position) {
                //重置ImageView的顏色
                resetImg();
                //設置選中時的圖片
                switch (position) {
                    case 0:
                        ((ImageView) main_home_layout.findViewById(R.id.main_home_img))
                                .setImageResource(R.drawable.home_black);
                        break;
                    case 1:
                        ((ImageView) main_msg_layout.findViewById(R.id.main_msg_img))
                                .setImageResource(R.drawable.msg_black);
                        break;
                    case 2:
                        ((ImageView) main_pal_layout.findViewById(R.id.main_pal_img))
                                .setImageResource(R.drawable.pal_black);
                        break;
                    case 3:
                        ((ImageView) main_me_layout.findViewById(R.id.main_me_img))
                                .setImageResource(R.drawable.me_black);
                        break;
                }
            }
 
            @Override
            public void onPageScrollStateChanged(int state) {
 
            }
        });
    }
 
    //重置ImageView的圖片
    protected void resetImg(){
        ((ImageView) main_home_layout.findViewById(R.id.main_home_img))
                .setImageResource(R.drawable.home_gray);
        ((ImageView) main_msg_layout.findViewById(R.id.main_msg_img))
                .setImageResource(R.drawable.msg_gray);
        ((ImageView) main_pal_layout.findViewById(R.id.main_pal_img))
                .setImageResource(R.drawable.pal_gray);
        ((ImageView) main_me_layout.findViewById(R.id.main_me_img))
                .setImageResource(R.drawable.me_gray);
    }
 
    public void initView(){
        main_home_layout = findViewById(R.id.main_home_layout);
        main_msg_layout = findViewById(R.id.main_msg_layout);
        main_pal_layout = findViewById(R.id.main_pal_layout);
        main_me_layout = findViewById(R.id.main_me_layout);
        main_mViewPager = findViewById(R.id.main_mViewPager);
 
        fragment1 vp_fr1 = new fragment1();
        fragment2 vp_fr2 = new fragment2();
        fragment3 vp_fr3 = new fragment3();
        fragment4 vp_fr4 = new fragment4();
        mFragments.add(vp_fr1);
        mFragments.add(vp_fr2);
        mFragments.add(vp_fr3);
        mFragments.add(vp_fr4);
        main_home_layout.setOnClickListener(this);
        main_msg_layout.setOnClickListener(this);
        main_pal_layout.setOnClickListener(this);
        main_me_layout.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //點擊首頁時,設置ViewPager的下標為0
            case R.id.main_home_layout:
                main_mViewPager.setCurrentItem(0);
                break;
            //點擊消息時,設置ViewPager的下標為1
            case R.id.main_msg_layout:
                main_mViewPager.setCurrentItem(1);
                break;
            //點擊好友時,設置ViewPager的下標為2
            case R.id.main_pal_layout:
                main_mViewPager.setCurrentItem(2);
                break;
            //點擊我時,設置ViewPager的下標為3
            case R.id.main_me_layout:
                main_mViewPager.setCurrentItem(3);
                break;
        }
    }
}

.xml文件中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        >
        <LinearLayout
            android:id="@+id/main_home_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            >
            <ImageView
                android:id="@+id/main_home_img"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/home_black"
                android:scaleType="fitXY"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="首頁"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/main_msg_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            >
            <ImageView
                android:id="@+id/main_msg_img"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/msg_gray"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="消息"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/main_pal_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            >
            <ImageView
                android:id="@+id/main_pal_img"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/pal_gray"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="好友"
                />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/main_me_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            >
            <ImageView
                android:id="@+id/main_me_img"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/me_gray"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我"
                />
        </LinearLayout>
    </LinearLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/main_mViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
    </android.support.v4.view.ViewPager>
</LinearLayout>

這個是ViewPager中的其中一個Fragment:

public class fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1,container,false);
    }
}

其Fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="我是Fragment1"
        />
 
</android.support.constraint.ConstraintLayout>

上述就是小編為大家分享的怎么在ViewPager中使用Fragment實現側滑導航欄了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

剑河县| 洛南县| 辽阳县| 白水县| 靖西县| 永登县| 莎车县| 新田县| 河南省| 武夷山市| 上虞市| 淳化县| 海盐县| 舟曲县| 长阳| 忻州市| 辽源市| 广丰县| 蓬莱市| 三门峡市| 固始县| 缙云县| 乌兰浩特市| 绍兴县| 石楼县| 桑日县| 永胜县| 贵阳市| 龙岩市| 乐都县| 偃师市| 北宁市| 田林县| 沙河市| 花垣县| 张家口市| 香格里拉县| 台北县| 锡林浩特市| 宁晋县| 资溪县|