我尝试在其中创建包含ListFragment的标签。我尝试了几种不同的方法,但它们都不起作用。我不知道如何正确设置ListFragment obkect的容器。 这是java代码。
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 :(得分:0)
1)创建一个FragmentManager对象。
2)在此对象上使用方法beginTransaction()
来创建FragmentTransaction对象。
3)在add(int containerViewId, Fragment fragment, String tag)
对象上添加方法FragmentTransaction
的片段。
在此方法中,您引用了容器的id,新的ListFragment
(或您已编写的类)和标记(以便轻松获取对ListFragment
的引用需要)。
请注意,您必须更改xml布局。您可以添加带有ID的FrameLayout
。然后,将此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>