android viewpage中加入framment碎片

1.创建适配器

public class PageAdapters extends FragmentPagerAdapter {
    private List<Fragment> fragmentList;

    public PageAdapters(FragmentManager fm, int behavior,List<Fragment> fragmentList) {
        super(fm, behavior);
        this.fragmentList = fragmentList;
    }
    @Override
    public Fragment getItem(int position) {

        return fragmentList.get(position);
    }
    @Override
    public int getCount() {
        return fragmentList.size();
    }
}

2.主界面

public class ClientMainActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    // 底部菜单4个Linearlayout
    private LinearLayout ll_home;
    private LinearLayout ll_address;
    private LinearLayout ll_friend;
    private LinearLayout ll_setting;

    // 底部菜单4个ImageView
    private ImageView iv_home;
    private ImageView iv_address;
    private ImageView iv_friend;
    private ImageView iv_setting;

    // 底部菜单4个菜单标题
    private TextView tv_home;
    private TextView tv_address;
    private TextView tv_friend;
    private TextView tv_setting;
    //4个fragment
    private ContactFragment contactFragment;
    private MessageFragment messageFragment;
    private OtherFragment otherFragment;
    private MineFragment mineFragment;
    // 中间内容区域
    private ViewPager viewPager;

    private List<Fragment> fragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_main);
        initView(); //控件初始化
        initEvent();// 初始化底部按钮事件
    }


    @Override
    public void onClick(View v) {
        // 点击后,fragment切换
        switch (v.getId()) {
            case R.id.ll_home:
                viewPager.setCurrentItem(0);
                break;
            case R.id.ll_address:
                viewPager.setCurrentItem(1);
                break;
            case R.id.ll_friend:
                viewPager.setCurrentItem(2);
                break;
            case R.id.ll_setting:
                viewPager.setCurrentItem(3);
                break;
            default:
                break;
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void initEvent() {
        // 设置按钮监听
        ll_home.setOnClickListener(this);
        ll_address.setOnClickListener(this);
        ll_friend.setOnClickListener(this);
        ll_setting.setOnClickListener(this);

        //设置ViewPager滑动监听
        viewPager.setOnPageChangeListener(this);
    }

    private void initView() {
        // 底部菜单4个Linearlayout
        this.ll_home =  findViewById(R.id.ll_home);
        this.ll_address =  findViewById(R.id.ll_address);
        this.ll_friend =  findViewById(R.id.ll_friend);
        this.ll_setting =  findViewById(R.id.ll_setting);
        // 底部菜单4个ImageView
        this.iv_home = findViewById(R.id.iv_home);
        this.iv_address = findViewById(R.id.iv_address);
        this.iv_friend =  findViewById(R.id.iv_friend);
        this.iv_setting = findViewById(R.id.iv_setting);
        // 底部菜单4个菜单标题
        this.tv_home = findViewById(R.id.tv_home);
        this.tv_address = findViewById(R.id.tv_address);
        this.tv_friend = findViewById(R.id.tv_friend);
        this.tv_setting = findViewById(R.id.tv_setting);
        // 中间内容区域ViewPager
        this.viewPager = findViewById(R.id.vp_content);

        //初始化4个fragment
        messageFragment = new MessageFragment();
        contactFragment = new ContactFragment();
        otherFragment = new OtherFragment();
        mineFragment = new MineFragment();

        fragments = new ArrayList<>();
        fragments.add(messageFragment);
        fragments.add(contactFragment);
        fragments.add(otherFragment);
        fragments.add(mineFragment);

        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new PageAdapters(fragmentManager,0,fragments));
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(0);
    }

}

2.1主界面布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="联系人"
        android:textSize="20dp"
        android:textColor="#ffffff"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#ffffff"
        android:layout_weight="1"/>

    <LinearLayout
        android:id="@+id/ll_home"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/iv_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/message" />

        <TextView
            android:id="@+id/tv_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:text="首页"
            android:textColor="#1B940A"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_address"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/iv_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/contact" />

        <TextView
            android:id="@+id/tv_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:text="通讯录"
            android:textColor="#000000"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_friend"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/iv_friend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/others" />

        <TextView
            android:id="@+id/tv_friend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:text="朋友"
            android:textColor="#000000"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_setting"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/iv_setting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/mine" />

        <TextView
            android:id="@+id/tv_setting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:text="我的"
            android:textColor="#000000"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

3.四个fragment碎片,只列举一个其余的类似创建就行

public class ContactFragment extends Fragment {
    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.page_02,container,false);

        return view;
    }
}

对应fargment布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="Page1"
        android:textSize="25dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

**仅供参考学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值