Swipe not Working in android ViewPager using FragmentPagerAdapter
Asked Answered
O

1

6

I want to create a swipe application for this I am using ViewPager in Android. When I run the code below, it runs successfully and a blue colored Fragment is opened, but swipe is not working on this. Can you tell me why?

This is the my 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);
    }
}

This is my 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;

    }
}

I have three fragments:

  • redColor.java

  • greenColor.java

  • blueColor.java

And this is my layout file:

<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>
Onus answered 13/9, 2015 at 15:2 Comment(2)
why use the fragment. instantiate method and not just do new redColor()?Badger
i already tried new redColor() but that still now worked.Onus
B
0

Your instantiateFrags function is a bit strange - normally you should declare the fragments from within the PagerAdapter, and then instantiate the PagerAdapter itself from with the activity's onCreate method. Your code for the PagerAdapter could be something like the following:

public class PagerAdapter extends FragmentPagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs){
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                blueColor tab1 = new blueColor();
                return tab1;
            case 1:
                greenColor tab2 = new greenColor();
                return tab2;
            case 2:
                redColor tab3 = new redColor();
                return tab3;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}

And then in your onCreate method in the main activity initialise the PagerAdapter (I have used a TabLayout here):

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Blue"));
tabLayout.addTab(tabLayout.newTab().setText("Green"));
tabLayout.addTab(tabLayout.newTab().setText("Red"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
PagerAdapter adapter = new PagerAdapter(
        getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(
        new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }

});

xml for the tab layout and pager:

 <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:theme="@style/YOUR_THEME_HERE"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tab_layout"/>
Belostok answered 13/9, 2015 at 16:29 Comment(1)
first I dont want to add tabs on page, i just want a full screen page swiping like: swiping of images in gallery. second: i have already tried new redColor(); in getItem() method but that still not working..Onus

© 2022 - 2024 — McMap. All rights reserved.