我想在Tab中显示ListView(选择此选项卡后我希望ListView出现),所以我创建了Fragment类并在其中放置了ListView代码。现在的主要问题是将片段对象传递给tabListener。请在此处发布示例,或者甚至更好地展示如何在我的代码中执行此操作。无论如何,任何帮助将不胜感激。
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");
tabOne.setTabListener(new tabListener());
tabTwo.setTabListener(new tabListener());
actionBar.addTab(tabOne);
actionBar.addTab(tabTwo);
}
protected class tabListener implements ActionBar.TabListener {
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
public class FirstFragment extends ListFragment {
private ArrayList<Cinemas> cinema;
private CinemasAdapter cinemaAdapter;
private ListView list;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
cinema = new Handler().handle();
cinemaAdapter = new CinemasAdapter(MainActivity.this, R.layout.movie_data_row, cinema);
setListAdapter(cinemaAdapter);
}
@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);
}
}
}
答案 0 :(得分:3)
通常使用制表符创建活动时 选项卡内容很容易有单独的活动 但是,当这些活动需要时,事情会变得棘手 彼此互动。
所以解决方案是创建一个带有视图的tabactivity (而不是活动)作为选项卡内容。 要创建TAB,我们需要遵循以下几个步骤:
并查看这些链接,可以帮到你
http://joshclemm.com/blog/?p=59
和
http://www.edumobile.org/android/android-beginner-tutorials/tab-control/
和
答案 1 :(得分:1)
如果要打开tabTwo中的列表,则需要创建FirstFragment
类的实例,并在选择选项卡时将其添加到视图中。
在onTabSelected
内,如果它不为空,则创建FirstFragment
的新实例,并将其添加到您的activity_main文件中容器内的视图中。容器是视图内部的线性布局,具有应由片段占据的给定空间量。对于全屏片段,容器是根布局。您只需要为此布局指定ID。创建新片段后,通过调用transaction.attach();
来附加它。最终的代码块应该与此类似:
/** this example assumes that the fragment
* FirstFragment will be attached to tab 1
* which is at position = 0, and SecondFragment
* will be attached to tab 2, which is at
* position = 1. Also, the root layout of
* activity_main.xml has the id attribute of
* fragment_container
*/
FirstFragment firstFragment;
SecondFragment secondFragment;
@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
switch (tab.getPosition()) {
case 0:
if (firstFragment == null) {
firstFragment = new FirstFragment();
transaction.add(R.id.fragment_container,
firstFragment, "FIRST");
} else {
transaction.attach(firstFragment);
}
break;
case 1:
if (secondFragment == null) {
secondFragment = new SecondFragment();
transaction.add(R.id.fragment_container, secondFragment, "SECOND");
} else {
transaction.attach(secondFragment);
}
break;
}
}