Android Studio Viewpager 案例

Android Studio Viewpager 案例

滑动布局:Viewpager

activityViewpager

import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplicationmeituan.R;

import java.util.ArrayList;
import java.util.List;

public class activityViewpager extends AppCompatActivity implements View.OnClickListener {
    private RadioGroup radioGroup;
    private ViewPager viewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.oderfour_list_view_pager);
        initView();
    }

    private void initView() {
        radioGroup = findViewById(R.id.rg);
        viewPager = findViewById(R.id.viewpager);
        viewPager.setAdapter(new adpterViewpager(getSupportFragmentManager(),setFragment()));
    }

    private List<Fragment> setFragment() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new MyOnefragment());
        fragmentList.add(new MyFourfragment());
        fragmentList.add(new MyThreefragment());
        return fragmentList;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.Radio1:
                showFragment(new MyOnefragment());
                break;
            case R.id.Radio2:
                showFragment(new MyOnefragment());
                break;
            case R.id.Radio3:
                showFragment(new MyOnefragment());
                break;
        }
    }
    /**
     * @param fragment 显示
     */
    private void showFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().show(fragment).commit();
    }

    /**
     * 隐藏
     */
    private void hideFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().hide(fragment).commit();
    }
}

adpterViewpager

package Fragment;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.List;

public class adpterViewpager extends FragmentPagerAdapter {
private List<Fragment>fragmentList;
    public adpterViewpager(@NonNull FragmentManager fm,List<Fragment>fragmentList) {
        super(fm);
        this.fragmentList=fragmentList;//返回它的对象
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

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

xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/aaaa"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#03A9F4" />

    <TextView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignBottom="@id/aaaa"
        android:background="@mipmap/jk1" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加"
        android:layout_alignParentRight="true"
        android:layout_marginTop="30dp"
        android:textSize="22dp"
        android:textColor="#FBFBFB"
        />

    <TextView
        android:id="@+id/text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="170dp"
        android:layout_marginTop="15dp"
        android:text="联系人"
        android:textColor="#FBFBFB"
        android:textSize="24dp" />


<!--    </RelativeLayout>-->

<!--
    <RelativeLayout
        android:id="@+id/bobo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/aaaa">

        <Button
            android:id="@+id/bobo_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:background="@drawable/oderfour_shape1"
            android:text="好友" />

        <Button
            android:id="@+id/bobo_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@id/bobo_1"

            android:background="@drawable/oderfour_shape1"
            android:text="分组" />

        <Button
            android:id="@+id/bobo_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@id/bobo_2"

            android:background="@drawable/oderfour_shape1"
            android:text="群聊" />

        <Button
            android:id="@+id/bobo_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@id/bobo_3"
            android:background="@drawable/oderfour_shape1"

            android:text="设备" />

    </RelativeLayout>
-->

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/rg"
        android:layout_below="@id/aaaa" />
<!--    android:layout_above="@id/bbbb"-->
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        >

        <RadioButton
            android:id="@+id/Radio1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="true"
            android:button="@null"
            android:drawableTop="@mipmap/map"
            android:gravity="center"
            android:paddingTop="12dp"
            android:text="消息"
            android:textColor="@color/black" />
        <View
            android:id="@+id/d1"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000"/>

        <RadioButton
            android:id="@+id/Radio2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:checked="true"
            android:button="@null"
            android:drawableTop="@mipmap/my"
            android:gravity="center"
            android:paddingTop="12dp"
            android:text="联系人"
            android:textColor="@color/black" />
        <View
            android:id="@+id/d2"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000"/>

        <RadioButton
            android:id="@+id/Radio3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawableTop="@mipmap/phone"
            android:gravity="center"
            android:button="@null"
            android:paddingTop="12dp"
            android:text="电话"
            android:textColor="@color/black" />
        <View
            android:id="@+id/d3"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000"/>
        <RadioButton
            android:id="@+id/Radio4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawableTop="@mipmap/sousuo"
            android:gravity="center"
            android:button="@null"
            android:paddingTop="12dp"
            android:text="搜索"
            android:textColor="@color/black" />

    </RadioGroup>



</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秃头的建国

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值