当前位置: 代码迷 >> Android >> Android ViewPager 上下滑动2-1
  详细解决方案

Android ViewPager 上下滑动2-1

热度:120   发布时间:2016-05-01 11:47:28.0
Android ViewPager 左右滑动2-1
public class PagerActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initViewPager();
    }
   
    private void initViewPager()
    {
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
       
        View view1 = LayoutInflater.from(this).inflate(R.layout.index_1, null);
        View view2 = LayoutInflater.from(this).inflate(R.layout.index_2, null);
        View view3 = LayoutInflater.from(this).inflate(R.layout.index_3, null);
       
        ArrayList<View> views = new ArrayList<View>();
        views.add(view1);
        views.add(view2);
        views.add(view3);
       
        MYPagerAdapter adapter = new MYPagerAdapter();
        adapter.setViews(views);
        viewPager.setAdapter(adapter);
    }
}

public class MYPagerAdapter extends PagerAdapter
{
    private ArrayList<View> views;
   
    public void setViews(ArrayList<View> views)
    {
        this.views = views;
    }
   
    @Override
    public int getCount()
    {
        return views.size();
    }
   
    @Override
    public boolean isViewFromObject(View arg0, Object arg1)
    {
        return arg0 == arg1;
    }
   
    @Override
    public void destroyItem(View container, int position, Object object)
    {
       
        ((ViewPager) container).removeView(views.get(position));
    }
   
    @Override
    public Object instantiateItem(View container, int position)
    {
        ((ViewPager) container).addView(views.get(position));
        return views.get(position);
    }
}


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

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

        <requestFocus />
    </android.support.v4.view.ViewPager>

</RelativeLayout>
  相关解决方案