2015-11-04 193 views
0

我對我正在使用的android應用程序的viewpager有一個問題。我有DrawerLayout調用碎片。其中一個片段是HomeFragment,其中包含2個可單擊的文本視圖。我希望HomeFragment切換到另一個具有可滑動視圖的Fragment(ParkingTutorialFragment)。我正在使用的代碼切換到ParkingTutorialFragment,但可滑動視圖似乎沒有工作,因爲我刷卡時沒有任何反應。下面是該片段和適配器代碼:片段中的ViewPager從另一個片段調用Android

import android.app.ActionBar; 
import android.app.ActionBar.Tab; 
import android.app.Activity; 
import android.app.FragmentTransaction; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.ActionBarActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class ParkingTutorialFragment extends Fragment { 
    private ViewPager viewPager; 
    private int page; 
    private String title; 
    public static ParkingTutorialFragment newInstance(int page, String title) { 
     ParkingTutorialFragment parkingFragment = new ParkingTutorialFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("someInt", page); 
     args.putString("someTitle", title); 
     parkingFragment.setArguments(args); 
     return parkingFragment; 
    } 

    // 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 rootView = inflater.inflate(R.layout.parking_tutorial_page, container, false);  
      return rootView; 
     } 

    } 

請注意:

page = getArguments().getInt("someInt", 0); 

上面一行返回null,我不知道爲什麼,如果任何人也可以告訴我,爲什麼它不能檢索值「someInt」的關鍵,如果任何人也能指出這一點我將不勝感激

import java.util.LinkedHashMap; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.ViewPager; 
import android.content.Intent; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

//Fragment for the home page 
public class HomeFragment extends Fragment { 
    private TypedArray tutorial_icons; 
    private TextView tutorial_1; 
    private ViewPager viewpager; 
    FragmentPagerAdapter adapterViewPager; 


    public HomeFragment() { 

    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.home, container, false); 
     viewpager = (ViewPager) rootView.findViewById(R.id.tabs_pager); 
     adapterViewPager = new TabsPagerAdapter(getFragmentManager()); 
     viewpager.setAdapter(adapterViewPager); 
     tutorial_1 = (TextView) rootView.findViewById(R.id.tutorial_item_1); 
     tutorial_1.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View view) 
      { 
       FragmentManager fragmentManager2 = getFragmentManager(); 
       FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction(); 
       ParkingTutorialFragment parkingFrag = new ParkingTutorialFragment(); 
       fragmentTransaction2.addToBackStack("Back"); 
       fragmentTransaction2.hide(HomeFragment.this); 
       fragmentTransaction2.add(android.R.id.content, parkingFrag); 
       fragmentTransaction2.commit(); 
      } 
     }); 

     return rootView; 
    } 
} 

下面是適配器代碼:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.app.FragmentStatePagerAdapter; 

public class TabsPagerAdapter extends FragmentPagerAdapter { 

    private static int NUM_ITEMS = 3; 

    public TabsPagerAdapter(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: // Fragment # 0 - This will show FirstFragment 
      return ParkingTutorialFragment.newInstance(0, "Page # 1"); 
     case 1: // Fragment # 0 - This will show FirstFragment different title 
      return new OffersFragment(); 
     default: 
      return ParkingTutorialFragment.newInstance(0, "Page # 1"); 
     } 
    } 

    // Returns the page title for the top indicator 
    @Override 
    public CharSequence getPageTitle(int position) { 
     return "Page " + position; 
    } 

} 

流量是HomeFragment - >按按鈕 - >轉到ParkingTutorialFragment(應該包含有swipeview) 如果有人可以幫助我實現所需的功能,明確從.getInt方法的空指針,它將非常感激。謝謝。

回答

0

如果我已經正確理解你,這將做到這一點。更新你的代碼在按鈕點擊。

FragmentManager fragmentManager2 = getFragmentManager(); 
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction(); 
fragmentTransaction2.addToBackStack("Back"); 
fragmentTransaction2.hide(HomeFragment.this); 
fragmentTransaction2.add(android.R.id.content, ParkingTutorialFragment.newInstance(0, "Page # 1")); 
fragmentTransaction2.commit(); 
相關問題