其他分享
首页 > 其他分享> > Android --ViewPager组件的使用(含案例),滑动切换页面, PagerAdapter类

Android --ViewPager组件的使用(含案例),滑动切换页面, PagerAdapter类

作者:互联网

 PagerAdapter类

步骤:

1、Layout:主页面中调用 ViewPager,编写各滑动页面

2、Activity:把滑动的页面存到List并传到 适配器

3、适配器Adapter:继承 PagerAdapter 

 

1、Layout:主页面中调用 ViewPager,编写各滑动页面

activity_view_pager.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.viewpager.widget.ViewPager
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/vp"/>

</LinearLayout>
view_pager_layout_1.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:background="#ffaaffcc">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="layout1"/>

</LinearLayout>
view_pager_layout_2.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:background="#ffaaccff">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="layout2"/>

</LinearLayout>
view_pager_layout_3.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:background="#ffaaccff">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="layout2"/>

</LinearLayout>

2、Activity:把滑动的页面存到List并传到 适配器

package xyz.lushangg.t8;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

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

import xyz.lushangg.t8.R;
import xyz.lushangg.t8.viewPager.ViewPagerAdapter;

public class ViewPagerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);

        LayoutInflater lf = getLayoutInflater().from(this);
        View view1 = lf.inflate(R.layout.view_pager_layout_1,null);
        View view2 = lf.inflate(R.layout.view_pager_layout_2,null);
        View view3 = lf.inflate(R.layout.view_pager_layout_3,null);

        List<View> viewList=new ArrayList<>();
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);

        ViewPager viewPager = findViewById(R.id.vp);
        ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter(viewList);
        viewPager.setAdapter(viewPagerAdapter);


    }
}

3、适配器Adapter:继承 PagerAdapter 

package xyz.lushangg.t8.viewPager;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public class ViewPagerAdapter extends PagerAdapter {

    private List<View> mListView;

    public ViewPagerAdapter(List<View> mListView){
        this.mListView=mListView;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(mListView.get(position),0);
        return mListView.get(position);
    }



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

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

        container.removeView(mListView.get(position));
    }
}
结果展示:
滑动过程1页面:

 

 滑动成功后的页面:

 

 

 

标签:layout,mListView,--,ViewPager,import,Android,页面,pager,view
来源: https://www.cnblogs.com/technicist/p/15571969.html