2013-01-19 113 views
3

我是新使用ViewPagersFragments在一起。我的應用程序在啓動時有一個ListView,這會使用戶根據傳遞的ID來查看詳細信息視圖。我遇到的問題是,細節視圖與列表視圖有不同的佈局。 ViewPager處於初始屏幕的佈局,所以當使用ViewPager時,佈局保留原始佈局,該佈局基本上只是底部的狀態欄,當用戶處於詳細視圖時應該消失。ViewPager帶有不同佈局的片段

這是我到目前爲止有:

main.xml中(啓動時的初始佈局,狀態欄)

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
> 
     <!-- Footer --> 
     <TextView 
       android:id="@+id/status" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
     /> 
     <!-- Footer --> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_above="@id/status 
    /> 

</RelativeLayout> 

Main.class

public class Main extends SherlockFragmentActivity 
{ 
    private static int NUMBER_OF_PAGES; 
    private ViewPager mViewPager; 
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter; 
    private static List<Fragment> fragments; 

    @Override 
    public void onCreate(final Bundle icicle) 
    {  
     super.onCreate(icicle); 

     setContentView(R.layout.main); 

     mViewPager = (ViewPager)findViewById(R.id.viewpager); 
     mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager()); 
     mViewPager.setAdapter(mMyFragmentPagerAdapter); 

     NUMBER_OF_PAGES = 5; 

     fragments = new ArrayList<Fragment>(); 

     fragments.add(new ListingFragment()); //initial screen that just a ListView 

     fragments.add(DetailsFragment.newInstance(1)); 
     fragments.add(DetailsFragment.newInstance(2)); 
     fragments.add(DetailsFragment.newInstance(3)); 
     fragments.add(DetailsFragment.newInstance(4)); 
    } 

    private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {    

     public MyFragmentPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int index) { 

      return fragments.get(index); 
     } 

     @Override 
     public int getCount() { 

      return NUMBER_OF_PAGES; 
     } 
    } 
} 

Details.xml(沒有狀態欄的細節視圖)

<?xml version="1.0" encoding="utf-8"?> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    > 

     <ListView android:id="@android:id/list" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
     /> 

    </RelativeLayout> 

DetailsFragment.class

public class DetailsFragment extends SherlockFragmentActivity 
{ 
    private int detailId; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState);   
    } 

    @Override 
    public void onActivityCreated(final Bundle icicle) 
    {  
     super.onActivityCreated(icicle); 
    } 

    public static DetailsFragment newInstance(int id) { 

     DetailsFragment lf = new DetailsFragment(); 
     Bundle bundle = new Bundle(); 
     bundle.putInt("id", id); 
     lf.setArguments(bundle); 
     return lf; 
    } 

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

     View view = inflater.inflate(R.layout.details, container, false); 

     detailId = getArguments().getInt("id"); 

     return view; 
    } 
} 

回答

0

的ViewPager是對INTIAL屏幕的佈局,所以使用 ViewPager當佈局保留了inital佈局,這基本上是 只是一種狀態如果在詳細視圖中用戶爲 ,則應該消失。

然後不要把它放在主佈局文件中。如果狀態與第一個片段相關,那麼它屬於它的視圖,特別是如果DetailsFragment不需要顯示狀態TextView

所以從main.xml中刪除狀態TextView並將其添加到在ListingFragment使用的佈局文件。如果ListingFragment擴展ListFragment然後創建一個包含與ID(android:id="@android:id/list")+狀態TextView一個ListView佈局文件和膨脹在onCreateView這個佈局文件。

+0

就是這樣。不過,這應該對我來說非常明顯。 :) 謝謝! –

+0

http://stackoverflow.com/q/18030296/1503130,請給我一些指導@Luksprog – Prateek

相關問題