zoukankan      html  css  js  c++  java
  • 安卓开发笔记(二十五):ViewPager的简单介绍和使用

    • 首先我们来看看运行之后的效果:

    然后我们也不多说废话了,下面是这个项目所需要的全部代码,很多博主写这个都不把代码写完,因此笔者自己也琢磨了一会儿才把这个弄出来,感觉很烦,但我肯定会把代码写全的。我这里一共引入了三个界面,三个布局的xml我都会写的。

    1.MainActivity.java

    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    
    import java.util.ArrayList;
    
    public class MainActivity extends AppCompatActivity {
        private ViewPager vpager_one;
        private ArrayList<View> aList;
        private MyPagerAdapter mAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            vpager_one = (ViewPager) findViewById(R.id.vpager_one);
    
            aList = new ArrayList<View>();
            LayoutInflater li = getLayoutInflater();
            aList.add(li.inflate(R.layout.view_one,null,false));
            aList.add(li.inflate(R.layout.view_two,null,false));
            aList.add(li.inflate(R.layout.view_three,null,false));
            mAdapter = new MyPagerAdapter(aList);
            vpager_one.setAdapter(mAdapter);
        }
    }

    2.MypagerAdapter.java

    这个是ViewPager的适配器,和listview所需要的适配器差不多,是我们创建的新类。

    import android.support.v4.view.PagerAdapter;
    import android.view.View;
    import android.view.ViewGroup;
    
    import java.util.ArrayList;
    
    public class MyPagerAdapter extends PagerAdapter {
    
        private ArrayList<View> viewLists;
    
        public MyPagerAdapter() {
        }
    
        public MyPagerAdapter(ArrayList<View> viewLists) {
            super();
            this.viewLists = viewLists;
        }
    
        @Override
        public int getCount() {
            return viewLists.size();
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewLists.get(position));
            return viewLists.get(position);
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewLists.get(position));
        }
    }

    3.activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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.support.v4.view.ViewPager
            android:id="@+id/vpager_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    
    </android.support.constraint.ConstraintLayout>

    4.view_one

    <?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="#FFBA55"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个Page"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold" />
    
    </LinearLayout>

    5.view_two

    <?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="#FFBA55"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二个Page"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold" />
    
    </LinearLayout>

    6.view_three

    <?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="#FFBA55"
        android:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第三个Page"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

    得解,就这么简单。我真是不知道其他博主为啥不把代码写全,尤其是每个viewpager的布局,连说都不说下有三个布局,真实让人头大。

  • 相关阅读:
    Codeforces 177G2 Fibonacci Strings KMP 矩阵
    Codeforces Gym100187C Very Spacious Office 贪心 堆
    Codeforces 980F Cactus to Tree 仙人掌 Tarjan 树形dp 单调队列
    AtCoder SoundHound Inc. Programming Contest 2018 E + Graph (soundhound2018_summer_qual_e)
    BZOJ3622 已经没有什么好害怕的了 动态规划 容斥原理 组合数学
    NOIP2016提高组Day1T2 天天爱跑步 树链剖分 LCA 倍增 差分
    Codeforces 555C Case of Chocolate 其他
    NOIP2017提高组Day2T3 列队 洛谷P3960 线段树
    NOIP2017提高组Day2T2 宝藏 洛谷P3959 状压dp
    NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序
  • 原文地址:https://www.cnblogs.com/geeksongs/p/10743932.html
Copyright © 2011-2022 走看看