欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)自動(dòng)變換大小的組件ViewPager2

 更新時(shí)間:2023年03月20日 10:04:43   作者:Dormiveglia-flx  
這篇文章主要介紹了Android實(shí)現(xiàn)自動(dòng)變換大小的組件ViewPager2,ViewPager2最顯著的特點(diǎn)是基于RecyclerView實(shí)現(xiàn),RecyclerView是目前Android端最成熟的AdapterView解決方案

ViewPager2的概念

ViewPager2是一個(gè)翻頁視圖組件

ViewPager2能做什么

  • 支持垂直方向的滑動(dòng)且實(shí)現(xiàn)極其簡(jiǎn)單。
  • 完全支持RecyclerView的相關(guān)配置功能。
  • 支持多個(gè)PageTransformer。
  • 支持DiffUtil,局部數(shù)據(jù)刷新和Item動(dòng)畫。
  • 支持模擬用戶滑動(dòng)與禁止用戶操作。

ViewPager2的用法

因?yàn)閂iewPager2是基于RecyclerView的,所以在使用上與RecyclerView的使用基本一致

ViewPager2常用的API

 1. setAdapter()   為viewpager2設(shè)置是配置
 2. setOrientation()  設(shè)置視圖翻頁的方向,可以設(shè)置垂直方向,也可以設(shè)置水平方向。
 3. setPageTransformer() 設(shè)置翻頁的動(dòng)畫

舉個(gè)簡(jiǎn)單的例子,adapter部分的代碼省略了

第一步: activity_main.xml

// 第一步: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_pager"/>
</LinearLayout>

第二步:創(chuàng)建適配器的視圖

// 第二步:創(chuàng)建適配器的視圖
<!-- ViewPager2要求每頁的寬高都必須是match_parent -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/iv_pic"
        android:layout_width="match_parent"
        android:layout_height="360dp"
        android:scaleType="fitCenter" />
    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

第三步: 創(chuàng)建適配器adapter

// 第三步:創(chuàng)建適配器adapter
public class viewpagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    // 聲明一個(gè)上下文對(duì)象
    private Context mContext; 
    // 聲明一個(gè)商品列表,用于渲染adapter
    private List<GoodsInfo> mGoodsList = new ArrayList<GoodsInfo>(); 
    // 函數(shù)構(gòu)造
    public viewpagerAdapter(Context context, List<GoodsInfo> goodsList) {
        mContext = context;
        mGoodsList = goodsList;
    }
    // 創(chuàng)建列表項(xiàng)的視圖持有者
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup vg, int viewType) {
        // 根據(jù)布局文件item_mobile.xml生成視圖對(duì)象
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_mobile, vg, false);
        return new ItemHolder(v);
    }
    // 綁定列表項(xiàng)的視圖持有者
    public void onBindViewHolder(RecyclerView.ViewHolder vh, final int position) {
        ItemHolder holder = (ItemHolder) vh;
        holder.iv_pic.setImageResource(mGoodsList.get(position).pic);
        holder.tv_desc.setText(mGoodsList.get(position).desc);
    }
    // 定義列表項(xiàng)的視圖持有者
    public class ItemHolder extends RecyclerView.ViewHolder {
        public ImageView iv_pic; // 聲明列表項(xiàng)圖標(biāo)的圖像視圖
        public TextView tv_desc; // 聲明列表項(xiàng)描述的文本視圖
        public ItemHolder(View v) {
            super(v);
            iv_pic = v.findViewById(R.id.iv_pic);
            tv_desc = v.findViewById(R.id.tv_desc);
        }
    }
}

第四步:書寫MainAcvitivity.java,調(diào)用ViewPager的API

//第四步:書寫MainAcvitivity.java,調(diào)用ViewPager的API
public class MainActivity extends AppCompatActivity {
    private List<GoodsInfo> viewPagerList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        // 從布局文件中獲取翻頁視圖
        ViewPager2 viewPager2 = findViewById(R.id.view_pager);
        // 構(gòu)建適配器
        viewpagerAdapter vpa = new viewpagerAdapter(viewPagerList);
        // 設(shè)置翻頁視圖的排列方向?yàn)樗椒较?
        viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        // 為翻頁視圖添加適配器
        viewPager2.setAdapter(vpa);
    }
    private void initData(){
            GoodsInfo g1 = new GoodsInfo("123", R.drawable.cloudy);
            viewPagerList.add(g1);
            GoodsInfo g2 = new GoodsInfo("456", R.drawable.moon);
            viewPagerList.add(g2);
            GoodsInfo g3 = new GoodsInfo("789", R.drawable.sunny);
            viewPagerList.add(g3);
    }
}

有沒有發(fā)現(xiàn),這個(gè)和recycleView的寫法一摸一樣。

ViewPager2與fragment結(jié)合使用

第一步:activity_main.xml視圖

// 第一步:activity_main.xml視圖  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_pager"/>
</LinearLayout>

第二步:創(chuàng)建fragment所需要的視圖fragment_blank.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".BlankFragment">
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/mTextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:textSize="36sp"
        android:gravity="center"/>
</FrameLayout>

第三步:fragment所需的代碼

public class BlankFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    String mTextString = "xxx";
    View rootView;
    public static BlankFragment newInstance(String param1) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTextString = getArguments().getString(ARG_PARAM1);
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_blank, container, false);
        }
        initView();
        return rootView;
    }
    private void initView() {
        TextView textView = rootView.findViewById(R.id.mTextView);
        textView.setText(mTextString);
    }
}

第四步:創(chuàng)建承載fragment所需要的適配器

public class MyFragmentAdapter extends FragmentStateAdapter {
    List<Fragment> fragments = new ArrayList<>();
    public MyFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragments) {
        super(fragmentManager, lifecycle);
        this.fragments = fragments;
    }
    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }
    @Override
    public int getItemCount() {
        return fragments.size();
    }
}

第五步:書寫MainAcvitivity.java,調(diào)用ViewPager的API

public class MainActivity extends AppCompatActivity {
    ViewPager2 viewPager2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initPage();
    }
    private void initPage() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(BlankFragment.newInstance("fragment1"));
        fragments.add(BlankFragment.newInstance("fragment2"));
        fragments.add(BlankFragment.newInstance("fragment3"));
        fragments.add(BlankFragment.newInstance("fragment4"));
        viewPager2 = findViewById(R.id.myViewPager);
        viewPager2.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(),
                getLifecycle(),fragments));
    }
}

ViewPager2與導(dǎo)航欄配合使用

代碼簡(jiǎn)寫,只寫相關(guān)的部分

// activity_main.xml 寫上用到的兩個(gè)組件TabLayout與ViewPager2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/mTabLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1">
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Monday" />
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tuesday" />
        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Wednesday" />
    </com.google.android.material.tabs.TabLayout>
    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/myViewPager"
        android:background="@color/purple_500"
        >
    </androidx.viewpager2.widget.ViewPager2>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    ViewPager2 viewPager2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPage();
    }
    private void initPage() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(BlankFragment.newInstance("fragment1"));
        fragments.add(BlankFragment.newInstance("fragment2"));
        fragments.add(BlankFragment.newInstance("fragment3"));
        fragments.add(BlankFragment.newInstance("fragment4"));
        viewPager2 = findViewById(R.id.myViewPager);
        viewPager2.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(),
                getLifecycle(),fragments));
		//綁定使用
		new TabLayoutMediator(findViewById(R.id.mTabLayout),viewPager2,new TabLayoutMediator.TabConfigurationStrategy(){
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch (position){
                    case 0:
                        tab.setText("1");
                        break;
                    case 1:
                        tab.setText("2");
                        break;
                    case 2:
                        tab.setText("3");
                        break;
                }
            }
        }).attach();
    }
}

到此這篇關(guān)于Android實(shí)現(xiàn)自動(dòng)變換大小的組件ViewPager2的文章就介紹到這了,更多相關(guān)Android ViewPager2內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android設(shè)計(jì)登錄界面、找回密碼、注冊(cè)功能

    Android設(shè)計(jì)登錄界面、找回密碼、注冊(cè)功能

    這篇文章主要為大家詳細(xì)介紹了Android設(shè)計(jì)登錄界面的方法,Android實(shí)現(xiàn)找回密碼、注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android編程解析XML文件的方法詳解【基于XmlPullParser】

    Android編程解析XML文件的方法詳解【基于XmlPullParser】

    這篇文章主要介紹了Android編程解析XML文件的方法,結(jié)合實(shí)例形式分析了Android基于XmlPullParser解析xml文件的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2017-07-07
  • Android學(xué)習(xí)筆記(二)App工程文件分析

    Android學(xué)習(xí)筆記(二)App工程文件分析

    之前寫過一篇關(guān)于安卓環(huán)境配置以及第一個(gè)app的制作過程,下面我們來進(jìn)一步,分析下APP工程文件
    2014-07-07
  • 最新評(píng)論