gpt4 book ai didi

android - 如何在Tab中显示ListView?

转载 作者:搜寻专家 更新时间:2023-11-01 08:05:04 25 4
gpt4 key购买 nike

我想在Tab中显示ListView(选择这个tab后我希望ListView出现),所以我创建了Fragment类并在其中放置了ListView代码。现在的主要问题是将 fragment 对象传递给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);
}
}

}

最佳答案

如果您想在 tabTwo 中打开列表,您需要创建 FirstFragment 类的实例,并在您选择选项卡时将其添加到您的 View 中。

onTabSelected 中,创建一个新的 FirstFragment 实例(如果它不为 null),并将其添加到 activity_main 中容器内的 View 中文件。容器是 View 内的线性布局,具有应由 fragment 占用的给定空间量。在全屏 fragment 的情况下,容器是根布局。您只需要为此布局指定一个 id。创建新 fragment 后,通过调用 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;
}
}

关于android - 如何在Tab中显示ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15471370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com