0

我嘗試了第一次在android中的碎片,我使用FragmentPagerAdapter爲有限數量的碎片,在這種情況下3.唯一的問題是,當我刷卡時,它會進入第二個和第三個頁面,但每次顯示第一個片段的佈局,而不是另一個。FragmentActivity沒有顯示片段的正確佈局

MainActivity.java:

package com.example.swipetabtest; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v4.view.ViewPager.OnPageChangeListener; 
import android.widget.Toast; 

public class MainActivity extends FragmentActivity { 
    FragmentPagerAdapter adapterViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager); 
     adapterViewPager = new MyPagerAdapter(getSupportFragmentManager()); 
     vpPager.setAdapter(adapterViewPager); 
    } 

    public static class MyPagerAdapter extends FragmentPagerAdapter { 
    private static int NUM_ITEMS = 3; 

     public MyPagerAdapter(FragmentManager fragmentManager) { 
      super(fragmentManager); 
     } 

     // Returns total number of pages 
     @Override 
     public int getCount() { 
      return NUM_ITEMS; 
     } 

     // Returns the fragment to display for that page 
     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
      case 0: 
       return FirstFragment.newInstance(0, "Page # 1"); 
      case 1: 
       return SecondFragment.newInstance(1, "Page # 2"); 
      case 2: 
       return ThirdFragment.newInstance(2, "Page # 3"); 
      default: 
       return null; 
      } 
     } 

    } 

} 

FirstFragment.java

package com.practice.swipetabtest; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class FirstFragment extends Fragment { 
    // Store instance variables 
    private String title; 
    private int page; 

    // newInstance constructor for creating fragment with arguments 
    public static FirstFragment newInstance(int page, String title) { 
     FirstFragment fragmentFirst = new FirstFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("someInt", page); 
     args.putString("someTitle", title); 
     fragmentFirst.setArguments(args); 
     return fragmentFirst; 
    } 

    // Store instance variables based on arguments passed 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     page = getArguments().getInt("someInt", 0); 
     title = getArguments().getString("someTitle"); 
    } 

    // Inflate the view for the fragment based on layout XML 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_first, container, false); 
     return view; 
    } 
} 

SecondFragment.java

package com.practice.swipetabtest; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class SecondFragment extends Fragment { 
    // Store instance variables 
    private String title; 
    private int page; 

    // newInstance constructor for creating fragment with arguments 
    public static FirstFragment newInstance(int page, String title) { 
     FirstFragment fragmentFirst = new FirstFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("someInt", page); 
     args.putString("someTitle", title); 
     fragmentFirst.setArguments(args); 
     return fragmentFirst; 
    } 

    // Store instance variables based on arguments passed 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     page = getArguments().getInt("someInt", 1); 
     title = getArguments().getString("someTitle"); 
    } 

    // Inflate the view for the fragment based on layout XML 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_second, container, false); 
     return view; 
    } 
} 

ThirdFragment.java

package com.practice.swipetabtest; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class ThirdFragment extends Fragment { 
    // Store instance variables 
    private String title; 
    private int page; 

    // newInstance constructor for creating fragment with arguments 
    public static FirstFragment newInstance(int page, String title) { 
     FirstFragment fragmentFirst = new FirstFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("someInt", page); 
     args.putString("someTitle", title); 
     fragmentFirst.setArguments(args); 
     return fragmentFirst; 
    } 

    // Store instance variables based on arguments passed 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     page = getArguments().getInt("someInt", 2); 
     title = getArguments().getString("someTitle"); 
    } 

    // Inflate the view for the fragment based on layout XML 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_third, container, false); 
     return view; 
    } 
} 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/vpPager" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </android.support.v4.view.ViewPager> 
</LinearLayout> 

fragment_first.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:text="this is the first fragment" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

fragment_second.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:text="this is the second fragment" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

fragment_third.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:text="hey look a third fragment" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

我試圖檢查是否正確的佈局是否正在充氣每個片段和開關語句套管到正確的片段,但他們看起來也沒關係,我也...

但它始終顯示第一個片段的佈局。

回答

3

的問題是,如果你看一看到您FirstFragment,SecondFragment和ThirdFragment類instantiateFragment方法,你實際上是在他們三個人都創造FirstFragment的新實例。

要調用:

 FirstFragment fragmentFirst = new FirstFragment(); 

在所有三個你上課時,你應該調用的:

 SecondFragment fragmentSecond = new SecondFragment(); 

 ThirdFragment fragmentThird = new ThirdFragment(); 

取決於哪個類,你都在

+0

我知道這是明顯的事情...從FirstFragment.java複製和粘貼,而不是重命名返回正確的類型...謝謝你這工作:: ) – user2405469

+0

@ user2405469不客氣!複製和廢物是更合適的描述。我只是從來沒有做過複製/粘貼代碼或XML,正是因爲一些不好的經歷,當我失去了幾個小時試圖解決某些問題時才發現它是一個簡單的複製/粘貼問題。 – Matej

+0

@Matej ...你是對的,在我的編碼經驗中儘早採納這個建議,並擺脫這個習慣:) – user2405469

0
@Override 
     public Fragment getItem(int position) { 
      switch (position) 
      { 
       Fragment frag=null; 
     if(position==0) 
      frag=new FirstFragment(); 
     if(position==1) 
      frag=new SecondFragment(); 
     if(position==2) 
      frag=new ThirdFragment(); 

     return frag; 
      } 
+0

無法訪問的代碼。 –

+0

@DoctororDrive ^是對的......因爲我們正在返回一些東西,因此反正會這樣工作......? – user2405469

+0

我承認我的錯誤,並對此非常抱歉..... –