他的作用是可以几个界面来回滑动
第一步: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="com.example.g160828_android09_widetpusl.GallerysActivity"> <android.support.v4.view.ViewPager android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/view_viewpager_2" ></android.support.v4.view.ViewPager> </LinearLayout>第二步,Java文件
package com.example.g160828_android09_widetpusl; import android.app.LocalActivityManager; import android.content.Intent; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; public class GallerysActivity extends AppCompatActivity { private ViewPager view_viewpager_2; //定义数组 private List<View> views = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gallerys); //得到控件id view_viewpager_2 = (ViewPager) findViewById(R.id.view_viewpager_2); //解析文件 LocalActivityManager localActivityManager = new LocalActivityManager(this, true); localActivityManager.dispatchCreate(savedInstanceState); Intent intent = new Intent(getApplicationContext(), ExpandableActivity.class); Intent inten = new Intent(getApplicationContext(), ListviewActivity1.class); //文件放到集合去 views.add(localActivityManager.startActivity("ExpandableActivity", intent).getDecorView()); views.add(localActivityManager.startActivity("ListviewActivity1", inten).getDecorView()); //设置适配器 view_viewpager_2.setAdapter(new my()); } //写方法继承pagerAdapter, class my extends PagerAdapter { @Override public int getCount() { return views.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } //重加两个方法 @Override public Object instantiateItem(ViewGroup container, int position) { View view = views.get(position); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // super.destroyItem(container, position, object); View view = views.get(position); container.removeView(view); } } }