本文共 6651 字,大约阅读时间需要 22 分钟。
ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we’ll implement a ViewPager that swipes through three views with different images and texts.
Android中的ViewPager允许用户在数据页面之间左右翻转。 在我们的Android ViewPager应用程序中,我们将实现一个ViewPager,该控件可在具有不同图像和文本的三个视图之间滑动。
Android ViewPager widget is found in the support library and it allows the user to swipe left or right to see an entirely new screen.
在支持库中找到了Android ViewPager小部件,它允许用户向左或向右滑动以查看全新的屏幕。
Today we’re implementing a ViewPager by using Views and PagerAdapter. Though we can implement the same using too, but we’ll discuss that in a later tutorial.
今天,我们通过使用Views和PagerAdapter来实现ViewPager 。 尽管我们也可以使用实现相同的功能,但是我们将在以后的教程中进行讨论。
The ViewPager uses a PagerAdapter whose job is to supply views to the MainActivity similar to what a ListAdapter does for a .
ViewPager使用PagerAdapter,其工作是向MainActivity提供视图,类似于ListAdapter对所做的操作。
The activity_main.xml consists solely of the ViewPager as shown below.
activity_main.xml仅由ViewPager组成,如下所示。
activity_main.xml
activity_main.xml
The MainActivity.java is given below.
MainActivity.java在下面给出。
MainActivity.java
MainActivity.java
package com.journaldev.viewpager;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setAdapter(new CustomPagerAdapter(this)); }}
The role of the MainActivity in the above code is to just reference the ViewPager
and set the CustomPagerAdapter
that extends the PagerAdapter
.
上面的代码中MainActivity的作用是仅引用ViewPager
并设置CustomPagerAdapter
来扩展PagerAdapter
。
Before we discuss the CustomPagerAdapter
class, let’s look into the ModelObject
class.
在讨论CustomPagerAdapter
类之前,让我们研究一下ModelObject
类。
ModelObject.java
ModelObject.java
package com.journaldev.viewpager;public enum ModelObject { RED(R.string.red, R.layout.view_red), BLUE(R.string.blue, R.layout.view_blue), GREEN(R.string.green, R.layout.view_green); private int mTitleResId; private int mLayoutResId; ModelObject(int titleResId, int layoutResId) { mTitleResId = titleResId; mLayoutResId = layoutResId; } public int getTitleResId() { return mTitleResId; } public int getLayoutResId() { return mLayoutResId; }}
The above lists all the pages of the ViewPagers. There are three pages with their respective layouts.
上面的列出了ViewPagers的所有页面。 共有三页,分别有各自的布局。
The layout of a single page is given below.
单个页面的布局如下。
view_blue.xml
view_blue.xml
The remaining two pages have similar layouts and are given in the source code of this project.
其余两个页面具有相似的布局,并在该项目的源代码中给出。
CustomPagerAdapter.java
CustomPagerAdapter.java
package com.journaldev.viewpager;import android.content.Context;import android.support.v4.view.PagerAdapter;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class CustomPagerAdapter extends PagerAdapter { private Context mContext; public CustomPagerAdapter(Context context) { mContext = context; } @Override public Object instantiateItem(ViewGroup collection, int position) { ModelObject modelObject = ModelObject.values()[position]; LayoutInflater inflater = LayoutInflater.from(mContext); ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false); collection.addView(layout); return layout; } @Override public void destroyItem(ViewGroup collection, int position, Object view) { collection.removeView((View) view); } @Override public int getCount() { return ModelObject.values().length; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public CharSequence getPageTitle(int position) { ModelObject customPagerEnum = ModelObject.values()[position]; return mContext.getString(customPagerEnum.getTitleResId()); }}
isViewFromObject
method InstantiateItem :在这种情况下,我们使用枚举并为特定的枚举值关联的布局充气。 然后,将新膨胀的布局添加到PagerAdapter维护的ViewGroup(视图集合)中,然后返回该布局。 此方法返回的对象稍后也将用作isViewFromObject
方法中的第二个参数 instantiateItem
method isViewFromObject :此方法检查特定对象是否属于给定位置,这很简单。 如前所述,第二个参数的类型为Object,并且与instantiateItem
方法的返回值相同。 The image below shows the app in action.
下图显示了正在运行的应用程序。
This brings an end to ViewPager in android example tutorial. You can download the Android ViewPager Project from the below link.
这结束了Android示例教程中的ViewPager。 您可以从下面的链接下载Android ViewPager项目。
翻译自:
转载地址:http://fqlzd.baihongyu.com/