【Android】ViewPager的使用

server/2025/2/28 5:39:00/

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"tools:targetApi="31"><activityandroid:name=".MainActivity"android:exported="true"android:launchMode="singleTop"android:taskAffinity=""android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"android:hardwareAccelerated="true"android:windowSoftInputMode="adjustResize"android:background="@android:color/white"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

MainActivity.java

import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;import com.cib.myapplication.adapters.AppFragmentPagerAdapter;
import com.cib.myapplication.fragments.HomeFragment;
import com.cib.myapplication.fragments.MyFragment;
import com.cib.myapplication.fragments.WalletFragment;
import com.cib.myapplication.fragments.WealthFragment;
import com.cib.myapplication.widgets.CustomRadioButton;
import com.cib.myapplication.widgets.CustomViewPager;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {public CustomViewPager mViewPager;private RadioGroup mainTab;private ArrayList<Fragment> mainFraments = new ArrayList<>();private ArrayList<RadioButton> tabBars = new ArrayList<>();public HomeFragment homePageFragment;public WealthFragment wealthFragment;public WalletFragment walletFragment;public MyFragment myFragment;private AppFragmentPagerAdapter mAdapter;public static CustomRadioButton homeBar, wealthBar, walletBar, accountBar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_main);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});//findmViewPager = findViewById(R.id.activity_content);mainTab = findViewById(R.id.main_tab_views);//find tab barhomeBar = this.findViewById(R.id.bottom_bar_home);wealthBar = this.findViewById(R.id.bottom_bar_wealth);walletBar = this.findViewById(R.id.bottom_bar_wallet);accountBar = this.findViewById(R.id.bottom_bar_account);tabBars.add(homeBar);tabBars.add(wealthBar);tabBars.add(walletBar);tabBars.add(accountBar);//view pagerhomePageFragment = new HomeFragment();wealthFragment = new WealthFragment();walletFragment = new WalletFragment();myFragment = new MyFragment();mainFraments.add(homePageFragment);mainFraments.add(wealthFragment);mainFraments.add(walletFragment);mainFraments.add(myFragment);mAdapter = new AppFragmentPagerAdapter(getSupportFragmentManager(), mainFraments);mViewPager.setAdapter(mAdapter);mainTab.setOnCheckedChangeListener((group, checkedId) -> {int index = group.indexOfChild(group.findViewById(checkedId));Log.d("'QDLog'", "index " + index);mViewPager.setCurrentItem(index);});ViewPager.OnPageChangeListener listener = new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageSelected(int position) {int currentItem = mViewPager.getCurrentItem();tabBars.get(currentItem).setChecked(true);}@Overridepublic void onPageScrollStateChanged(int state) {}};mViewPager.addOnPageChangeListener(listener);}
}

AppFragmentPagerAdapter.java


import android.view.ViewGroup;import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;import java.util.ArrayList;
import java.util.List;/*** 实现Fragment ViewPager适配器,用于App主体4个TAB页滑动切换*/
public class AppFragmentPagerAdapter extends FragmentStatePagerAdapter {private List<Fragment> mFragmentList = new ArrayList<>();private FragmentManager fm;public AppFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {super(fm);this.fm = fm;this.mFragmentList.addAll(fragmentList);}@Overridepublic Fragment getItem(int position) {return mFragmentList == null ? null : mFragmentList.get(position);}@Overridepublic int getCount() {return mFragmentList == null ? 0 : mFragmentList.size();}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {// 保证ViewPager切换过程中,page不会被销毁// super.destroyItem(container, position, object);}public int getItemPosition(Object object) {return POSITION_NONE;}@Overridepublic Object instantiateItem(ViewGroup container, int position) {return super.instantiateItem(container, position);}public void setFragmentList(List<Fragment> fragmentList) {this.mFragmentList.clear();this.mFragmentList.addAll(fragmentList);notifyDataSetChanged();}public void clearFragment() {if (this.mFragmentList != null) {FragmentTransaction fragmentTransaction = fm.beginTransaction();for (Fragment f : this.mFragmentList) {fragmentTransaction.remove(f);}fragmentTransaction.commit();fm.executePendingTransactions();}}
}

HomeFragment.java

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;import com.cib.myapplication.R;public class HomeFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_home, container, false);}
}

CustomRadioButton.java


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;import androidx.appcompat.widget.AppCompatRadioButton;import com.cib.myapplication.R;/*** 自定义底部Button*/
public class CustomRadioButton extends AppCompatRadioButton {private int mDrawableSize; // XML文件中设置的大小public CustomRadioButton(Context context) {this(context, null, 0);}public CustomRadioButton(Context context, AttributeSet attrs) {this(context, attrs, 0);}public CustomRadioButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);Drawable drawableLeft = null, drawableTop = null, drawableRight = null, drawableBottom = null;TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.CustomRadioButton);mDrawableSize = tArray.getDimensionPixelSize(R.styleable.CustomRadioButton_drawablesize, 50);drawableTop = tArray.getDrawable(R.styleable.CustomRadioButton_drawabletop);drawableBottom = tArray.getDrawable(R.styleable.CustomRadioButton_drawableBottom);
//        int attrCount = tArray.getIndexCount();
//        for (int i = 0; i < attrCount; i++) {
//            int attr = tArray.getIndex(i);
//            switch (attr) {
//                case R.styleable.CustomRadioButton_drawablesize:
//                    mDrawableSize = tArray.getDimensionPixelSize(R.styleable.CustomRadioButton_drawablesize, 50);
                    Log.d("appfw", "mDrawableSize:" + mDrawableSize);
//                    break;
//                case R.styleable.CustomRadioButton_drawabletop:
//                    drawableTop = tArray.getDrawable(attr);
//                    break;
//                case R.styleable.CustomRadioButton_drawableBottom:
//                    drawableBottom = tArray.getDrawable(attr);
//                    break;
//                default:
//                    break;
//            }
//        }tArray.recycle();setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);}public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {if (left != null) {left.setBounds(0, 0, mDrawableSize, mDrawableSize);}if (right != null) {right.setBounds(0, 0, mDrawableSize, mDrawableSize);}if (top != null) {top.setBounds(0, 0, mDrawableSize, mDrawableSize);}if (bottom != null) {bottom.setBounds(0, 0, mDrawableSize, mDrawableSize);}setCompoundDrawables(left, top, right, bottom);}}

CustomViewPager.java


import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;import androidx.viewpager.widget.ViewPager;public class CustomViewPager extends ViewPager {public CustomViewPager(Context context) {super(context);}public CustomViewPager(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {try {return super.onInterceptTouchEvent(ev);} catch (Exception ex) {ex.printStackTrace();}return false;}@Overridepublic boolean onTouchEvent(MotionEvent ev) {try {return super.onTouchEvent(ev);} catch (Exception ex) {ex.printStackTrace();}return false;}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!-- App5.0为支持划动切换页面变更Content view为ViewPager--><com.cib.myapplication.widgets.CustomViewPagerandroid:id="@+id/activity_content"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white" /><!-- 自定义标签栏 --><includeandroid:id="@+id/main_tab_views"layout="@layout/bottom_bar_ver5"android:layout_width="match_parent"android:layout_height="50dp"android:layout_alignParentBottom="true" /><RelativeLayoutandroid:id="@+id/rl_older_layout"android:layout_width="match_parent"android:layout_height="56dp"android:layout_alignParentBottom="true"><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:layout_alignParentBottom="true"android:layout_marginBottom="55dp"android:background="@color/devideline_bg" /></RelativeLayout></RelativeLayout><!--    <include--><!--        layout="@layout/bindkeyboard_num"--><!--        android:visibility="gone" />--><!--    <com.cib.qdzg.widgets.NumKeyBoard--><!--        android:id="@+id/num_keyboard_index"--><!--        android:layout_width="match_parent"--><!--        android:layout_height="match_parent"--><!--        android:visibility="gone" />--><!--    <include--><!--        layout="@layout/input"--><!--        android:visibility="gone" />--><!--            <ImageView--><!--                android:id="@+id/iv_advertPic"--><!--                android:layout_width="wrap_content"--><!--                android:layout_height="wrap_content"--><!--                android:visibility="gone" />--></FrameLayout>

bottom_bar_ver5.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"xmlns:radio="http://schemas.android.com/apk/res-auto"android:id="@+id/main_tab"android:layout_width="match_parent"android:layout_height="50dp"android:background="@color/white"android:orientation="horizontal"><com.cib.myapplication.widgets.CustomRadioButtonandroid:id="@+id/bottom_bar_home"style="@style/bottom_bar_item"android:checked="true"android:text="@string/tabbar_home_title"radio:drawablesize="20dp"radio:drawabletop="@drawable/bottom_bar_home_ver5" /><com.cib.myapplication.widgets.CustomRadioButtonandroid:id="@+id/bottom_bar_wealth"style="@style/bottom_bar_item"android:text="@string/tabbar_wealth_title"radio:drawablesize="20dp"radio:drawabletop="@drawable/bottom_bar_wealth_ver5" /><com.cib.myapplication.widgets.CustomRadioButtonandroid:id="@+id/bottom_bar_wallet"style="@style/bottom_bar_item"android:text="@string/tabbar_wallet_title"radio:drawablesize="20dp"radio:drawabletop="@drawable/bottom_bar_wallet_ver5" /><com.cib.myapplication.widgets.CustomRadioButtonandroid:id="@+id/bottom_bar_account"style="@style/bottom_bar_item"android:text="@string/tabbar_account_title"radio:drawablesize="20dp"radio:drawabletop="@drawable/bottom_bar_account_ver5" /></RadioGroup>

fragment_home.xml,fragment_my.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:gravity="center"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Home Page"android:textSize="24sp"/>
</LinearLayout>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources><color name="purple_200">#FFBB86FC</color><color name="purple_500">#FF6200EE</color><color name="purple_700">#FF3700B3</color><color name="teal_200">#FF03DAC5</color><color name="teal_700">#FF018786</color><color name="black">#FF000000</color><color name="white">#FFFFFFFF</color><color name="bottom_bar_text_normal">#555555</color><color name="bottom_bar_text_checked">#5047FF</color><color name="transparent">#00000000</color><color name="devideline_bg">#E5E5E5</color>
</resources>

strings.xml

<resources><string name="app_name">MyApplication</string><string name="tabbar_home_title">首页</string><string name="tabbar_wealth_title">财富</string><string name="tabbar_scan_title">扫一扫</string><string name="tabbar_wallet_title">钱包</string><string name="tabbar_account_title">我的</string>
</resources>

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="CustomRadioButton"><attr name="drawablesize" format="dimension" /><attr name="drawabletop" format="reference" /><attr name="drawableBottom" format="reference" /></declare-styleable><!-- Bottom start --><style name="bottom_bar_item"><item name="android:textSize">12sp</item><item name="android:textColor">@drawable/bottom_bar_text_color</item><item name="android:layout_width">fill_parent</item><item name="android:layout_height">fill_parent</item><item name="android:layout_weight">1</item><item name="android:gravity">center</item><item name="android:paddingTop">5dp</item><item name="android:paddingBottom">5dp</item><item name="android:drawablePadding">1.0dip</item><item name="android:button">@null</item><item name="android:background">@null</item><item name="android:clickable">true</item></style>
</resources>

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools"><!-- Base application theme. --><style name="AppTheme" parent="BaseTheme"></style><style name="BaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar"><item name="android:windowNoTitle">true</item><item name="android:windowFullscreen">true</item><item name="android:windowTranslucentNavigation">true</item><item name="android:windowTranslucentStatus">true</item><item name="android:windowDrawsSystemBarBackgrounds">false</item><item name="android:windowBackground">@drawable/bg_splash_layer</item></style>
</resources>

在这里插入图片描述

demo download:
demo


http://www.ppmy.cn/server/171224.html

相关文章

【Go】十七、grpc 服务的具体功能编写

服务的具体编写 获取品牌信息的基础逻辑 我们为了便于测试&#xff0c;可以先把方法写成下面这样&#xff1a; type GoodsServer struct {proto.UnimplementedGoodsServer }之后再 test/brands.go 中进行编写测试代码&#xff1a; // 创建客户端 var brandClient proto.Goo…

ESP32移植Openharmony外设篇(9)NB-IOT

NB-IOT&#xff08;窄带物联网&#xff09; 模块介绍 NB-IoT&#xff08;Narrowband Internet of Things&#xff09;是一种低功耗广域物联网&#xff08;LPWAN&#xff09;技术&#xff0c;专为低功耗、低数据速率和大规模连接的物联网应用而设计。它采用窄带宽信道和低复杂…

QtPropertyBrowser实现属性管理中的下拉框

#include <QtWidgets> #include <QtPropertyBrowser>class Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent nullptr) : QWidget(parent) {// 创建属性浏览器QtTreePropertyBrowser *browser new QtTreePropertyBrowser(this);// 创建枚举属性管…

盲视观测者效应:认知的量子诗学 AI回复盲人双缝实验

&#x1f30c; **《盲视观测者效应&#xff1a;认知的量子诗学》** ### **一、盲视者的波函数坍缩** 当盲人"观察"双缝实验时&#xff1a; - 他的视觉皮层正在用触觉重构量子态 - 指尖的震动频率 ≈ 光子的概率波函数 - 导盲杖的敲击声 新的观测暴力系…

图数据库Neo4j面试内容整理-索引(Index)

索引(Index) 是数据库中用来提高查询性能的技术,特别是在处理大量数据时,索引能够大大加速查询操作。在 Neo4j 这样的图数据库中,索引也起着非常重要的作用,尤其是在图中查找节点时,使用索引可以避免全图扫描,从而提高查询效率。 1. Neo4j 中的索引概念

蓝耘科技上线 DeepSeek 满血版,500万tokens免费送

&#x1f31f; 嗨&#xff0c;我是Lethehong&#xff01;&#x1f31f; &#x1f30d; 立志在坚不欲说&#xff0c;成功在久不在速&#x1f30d; &#x1f680; 欢迎关注&#xff1a;&#x1f44d;点赞⬆️留言收藏&#x1f680; &#x1f340;欢迎使用&#xff1a;小智初学…

MySQL自启动失败(MySQL不能开机自启)解决方案_MySQL开机自启疑难杂症解决,适用Win11Win10

问题描述&#xff08;MySQL 开机自启失败&#xff09; 本文解决方法&#xff0c;在 windows10 、 windows11 系统中均可使用。 win11 安装 MySQL 后&#xff0c;不能开机自启。 在服务中&#xff0c;手动启动服务后&#xff0c;可正常使用&#xff0c;一点异常都没有。 或者…

C++ 顺序容器--vector容器详解

元素保存在连续的内存空间中。插入元素或者删除元素通常需要线性时间&#xff0c;当这些操作在尾部执行时&#xff0c;实际运行时间为摊还常量时间。随机访问某个元素的复杂度为常量时间。 1 vector 概述 vector 在<vector>头文件中被定义为一个带有2个类型参数的类模板…