欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【达内课程】ViewPager

程序员文章站 2022-06-09 20:21:41
...

【达内课程】ViewPager
创建3个类似的布局,分别命名为

page_one,page_two,page_three

<LinearLayout 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"
    tools:context=".ui.Main3Activity"
    android:gravity="center"
    android:background="#006400">

    <TextView
        android:id="@+id/tv_page_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Page 01"
        android:textColor="@android:color/white"
        android:textSize="18dp"/>

</LinearLayout>

Activity

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.example.xx.myapplication2.R;

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

public class Main3Activity extends AppCompatActivity {
    private ViewPager viewPager;
    private PagerAdapter adapter;
    private List<View> pages;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        pages = new ArrayList<>();
        pages.add(getLayoutInflater().inflate(R.layout.page_one,null));
        pages.add(getLayoutInflater().inflate(R.layout.page_two,null));
        pages.add(getLayoutInflater().inflate(R.layout.page_three,null));

        viewPager = findViewById(R.id.viewpager);
        adapter = new InnerPagerAdapter();
        viewPager.setAdapter(adapter);
    }

    private class InnerPagerAdapter extends PagerAdapter{
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //添加第position个view对象,并添加到容器container中,并返回
            View view = pages.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //将第position个view从container中移除走
            View view = pages.get(position);
            container.removeView(view);
        }

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

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

效果图
【达内课程】ViewPager

栗子

基于这个栗子做的
https://blog.csdn.net/u010356768/article/details/78998922

xml

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="#e5e5e5">

        <Button
            android:id="@+id/btn_home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="首页"
            android:drawableTop="@drawable/home_selector"
            android:textColor="@drawable/text_selector"
            android:drawablePadding="5dp"
            android:background="@null"
            />

        <Button
            android:id="@+id/btn_location"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="位置"
            android:drawableTop="@drawable/location_selector"
            android:textColor="@drawable/text_selector"
            android:drawablePadding="5dp"
            android:background="@null"/>

        <Button
            android:id="@+id/btn_cart"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="购物车"
            android:drawableTop="@drawable/cart_selector"
            android:textColor="@drawable/text_selector"
            android:drawablePadding="5dp"
            android:background="@null"/>

        <Button
            android:id="@+id/btn_user"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="我的"
            android:drawableTop="@drawable/user_selector"
            android:textColor="@drawable/text_selector"
            android:drawablePadding="5dp"
            android:background="@null"/>
    </LinearLayout>
</RelativeLayout>

activity

public class RadioButtonActivity extends FragmentActivity implements ViewPager.OnPageChangeListener{
    private Button btn_home;
    private Button btn_location;
    private Button btn_cart;
    private Button btn_user;
    private ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);

        btn_home = findViewById(R.id.btn_home);
        btn_location = findViewById(R.id.btn_location);
        btn_cart = findViewById(R.id.btn_cart);
        btn_user = findViewById(R.id.btn_user);
        viewPager = findViewById(R.id.viewpager);

        InnerClassOnClickListener innerClassOnClickListener = new InnerClassOnClickListener();
        btn_home.setOnClickListener(innerClassOnClickListener);
        btn_location.setOnClickListener(innerClassOnClickListener);
        btn_cart.setOnClickListener(innerClassOnClickListener);
        btn_user.setOnClickListener(innerClassOnClickListener);
        btn_home.setEnabled(false);

        InnerFragmentPagerAdapter adapter = new InnerFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(this);
    }

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

    }

    @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
                select(btn_home);
                break;
            case 1:
                select(btn_location);
                break;
            case 2:
                select(btn_cart);
                break;
            case 3:
                select(btn_user);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private class InnerFragmentPagerAdapter extends FragmentPagerAdapter{

        public InnerFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position){
                case 0:
                    fragment = new OneFragment();
                    break;
                case 1:
                    fragment = new TwoFragment();
                    break;
                case 2:
                    fragment = new ThreeFragment();
                    break;
                case 3:
                    fragment = new FourFragment();
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }
    }



    private class InnerClassOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_home:
                    select(btn_home);
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.btn_location:
                    select(btn_location);
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.btn_cart:
                    select(btn_cart);
                    viewPager.setCurrentItem(2);
                    break;
                case R.id.btn_user:
                    select(btn_user);
                    viewPager.setCurrentItem(3);
                    break;
            }
        }
    }

    private void select(Button button){
        btn_home.setEnabled(true);
        btn_location.setEnabled(true);
        btn_cart.setEnabled(true);
        btn_user.setEnabled(true);

        button.setEnabled(false);

        Toast.makeText(RadioButtonActivity.this,button.getText(),Toast.LENGTH_SHORT).show();
    }
}

其中的fragment

public class OneFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //通过xml布局得到需要显示的view对象
        View view = inflater.inflate(R.layout.page_one,null);
        //返回view对象
        return view;
    }
}

效果图
【达内课程】ViewPager

相关标签: viewpager