1

I try to create tabs with ListFragment in it. I've tried several different appoaches but neither of them works. I don't know how to set container for ListFragment obkect properly. Here is java code.

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

          }
        } 
}

And activity_main.XML file:

<?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>

Thank you for reading, any help will be appreciated!

4

1 回答 1

0

1) 创建一个FragmentManager对象。

2)beginTransaction()在这个对象上使用方法来创建一个FragmentTransaction对象。

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

在此方法中,您放置了对容器 id 的引用、一个新的ListFragment(或您编写的类)和一个标签(如果需要,可以轻松获得对容器的引用ListFragment)。

请注意,您必须更改您的 xml 布局。您可以添加一个FrameLayout带有 id 的。然后,将此 id 赋予对象的方法add()FragmentTransaction

编辑

代码示例:

公共类 ContentActivity 扩展 Activity {

  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>
于 2013-03-18T18:49:29.330 回答