2013-03-18 143 views
1

我嘗試在其中創建帶有ListFragment的選項卡。我已經嘗試了幾種不同的應用程序,但它們都不起作用。我不知道如何正確設置ListFragment的容器。 這裏是java代碼。如何正確設置ListFragment?

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     ActionBar.Tab tabOne = actionBar.newTab().setText("ONE"); 
     ActionBar.Tab tabTwo = actionBar.newTab().setText("TWO"); 

     tabParkCinema.setTabListener(new tabListener()); 
     tab28Cinema.setTabListener(new tabListener()); 

     actionBar.addTab(tabOne); 
     actionBar.addTab(tabTwo); 

     } 

    protected class tabListener implements ActionBar.TabListener { 

     ParkFragment firstFragment; 

     @Override 
     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 

     } 

     @Override 
     public void onTabSelected(Tab tab, FragmentTransaction ft) { 
      switch (tab.getPosition()){ 
      case 0: 
       if (firstFragment == null){ 
        firstFragment = new ParkFragment(); 
        System.out.println("initialized"); 
        ft.add(R.id.cont, firstFragment,"FIRST"); 
       } 
       else{ 
        ft.attach(firstFragment); 
       } 
       break; 

      case 1: 
       if (firstFragment == null){ 
        firstFragment = new ParkFragment(); 
        ft.add(R.id.cont,firstFragment,"SECOND"); 
       } 
       else{ 
        ft.attach(firstFragment); 
       } 
       break; 
      } 
     } 

     @Override 
     public void onTabReselected(Tab tab, FragmentTransaction ft) { 

     } 
    }; 

    public class ParkFragment extends ListFragment { 
     private ArrayList<Cinemas> cinema; 
     private CinemasAdapter cinemaAdapter; 
     private View v; 
     //private ListView list; 

      @Override 
      public View onCreateView(LayoutInflater in, ViewGroup gr, Bundle savedInstanceState) { 
      v = in.inflate(R.id.listing1, gr,false); 
      super.onActivityCreated(savedInstanceState); 
      cinema = new Handler().handle(); 
      cinemaAdapter = new CinemasAdapter(MainActivity.this, R.layout.movie_data_row, cinema); 
      setListAdapter(cinemaAdapter); 
      return gr; 
      } 

      @Override 
      public void onListItemClick(ListView l, View v, int position, long id) { 
       Cinemas movie = cinemaAdapter.getItem(position); 
       Intent intent = new Intent (MainActivity.this, More.class); 
       intent.putExtra("Cinemas", movie); 
       intent.putExtra("data", movie.getBitmap()); 

       Bundle translateBundle = 
         ActivityOptions.makeCustomAnimation(MainActivity.this, 
         R.anim.slide_in_left, R.anim.slide_out_left).toBundle(); 
       startActivity (intent, translateBundle); 

      } 
     } 
} 

而且activity_main.xml中的文件:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/cont" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 
      <ListView 
       android:id = "@+id/listing1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" >  
     </ListView> 
</LinearLayout> 

謝謝您的閱讀,任何幫助將不勝感激!

回答

0

1)創建一個FragmentManager對象。

2)在此對象上使用方法beginTransaction()創建一個FragmentTransaction對象。

3)用FragmentTransaction對象上的方法add(int containerViewId, Fragment fragment, String tag)添加片段。

在此方法中,您將對容器的id,新的ListFragment(或已寫入的類)和標記(如果需要,可輕鬆獲得對ListFragment的引用)的引用。

請注意,您必須更改您的xml佈局。您可以添加一個帶有ID的FrameLayout。然後,將此ID提供給FragmentTransaction對象的方法add()

編輯

的代碼示例:

公共類ContentActivity延伸活動{

public void onCreate(Bundle savedInstanceState) { 

    //your action bar stuff 

    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.parkfragment, new ParkFragment(), "locations"); 
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
    fragmentTransaction.commit(); 
} 

public class ParkFragment extends ListFragment { 
    //your methods 
} 

protected class tabListener implements ActionBar.TabListener { 
    //your methods 
} 
} 

你的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:baselineAligned="false" 
    android:orientation="vertical" > 

    <FrameLayout 
     android:id="@+id/parkfragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 


</LinearLayout> 
+0

你可以給ListView的FrameLayout的例子嗎? – 2013-03-18 19:14:14

+0

非常感謝您的幫助! :) – 2013-03-19 05:12:42

+0

切換到第二個選項卡後,發生以下情況:( http://s018.radikal.ru/i528/1303/0d/8eeb9e964cab.jpg – 2013-03-19 08:02:09