6

我想爲此創建一個滑動應用程序我在Android中使用ViewPager。當我運行下面的代碼時,它會成功運行並打開一個藍色的Fragment,但刷卡不起作用。你能告訴我爲什麼嗎?刷卡不工作在Android ViewPager使用FragmentPagerAdapter

這是我的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

爲什麼使用該片段。實例化方法,而不僅僅是'new redColor()'? – Bhargav

+0

我已經嘗試過新的redColor(),但現在仍然有效。 –

回答

0

你instantiateFrags功能是有點奇怪 - 通常你應該從內聲明片段PagerAdapter,然後使用activity的onCreate方法實例化PagerAdapter本身。您的PagerAdapter代碼可能是類似以下內容:

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; 
    } 
} 

然後在主活動的onCreate方法初始化PagerAdapter(我使用了一個TabLayout這裏):

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進行標籤佈局和尋呼機:

<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"/> 
+0

首先,我不想在頁面上添加標籤,我只是想要一個全屏頁面滑動像:畫廊中的圖像刷卡。第二:我已經嘗試過新的redColor();在getItem()方法,但仍然無法正常工作.. –