使用FragmentPagerAdapter在Android ViewPager中滑动不工作

时间:2015-09-13 15:02:36

标签: android android-fragments android-viewpager swipe fragmentpageradapter

我想为此创建一个滑动应用程序,我在Android中使用ViewPager。当我运行下面的代码时,它会成功运行并打开一个蓝色的Fragment,但滑动不起作用。你能告诉我为什么吗?

这是我的Activity

package app.learn.com.learnapp;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import android.os.Bundle;

import java.util.List;
import java.util.Vector;

import menulistFrag.blueColor;
import menulistFrag.greenColor;
import menulistFrag.redColor;


public class MenuActivityList1 extends FragmentActivity {

    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_activity_list1);
        viewPager = (ViewPager)findViewById(R.id.pager);

        instantiateFrags();



    }
    public  void instantiateFrags(){
        List<Fragment> frags = new Vector<Fragment>();
        frags.add(Fragment.instantiate(this, redColor.class.getName()));
        frags.add(Fragment.instantiate(this, greenColor.class.getName()));
        frags.add(Fragment.instantiate(this, blueColor.class.getName()));
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(),frags);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0);
    }
}

这是我的PagerAdapter

package app.learn.com.learnapp;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

import menulistFrag.blueColor;
import menulistFrag.greenColor;
import menulistFrag.redColor;

/**
 * Created by root on 13/9/15.
 */

public class PagerAdapter extends FragmentPagerAdapter {
    List<Fragment> fragments;
    public PagerAdapter(FragmentManager fm,List<Fragment> fragLists){
        super(fm);
        fragments =fragLists;
    }
    @Override
    public Fragment getItem(int position) {
   return fragments.get(position);
    }

    @Override
    public int getCount() {
        return 3;

    }
}

我有三个片段:

  • redColor.java

  • greenColor.java

  • blueColor.java

这是我的布局文件:

<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"
    android:orientation="vertical">
<android.support.v4.view.ViewPager 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"
    android:id="@+id/pager">

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

0 个答案:

没有答案