我有相同的碎片:
public class Movies extends Fragment {
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.movies_at_now, container, false);
listView = (ListView) view.findViewById(R.id.listView);
MovieAdapter adapter = new MovieAdapter(getActivity(), initData());
listView.setAdapter(adapter);
return view;
}
private List<MovieItem> initData () {
List<MovieItem> list = new ArrayList<MovieItem>();
list.add(new MovieItem("M1");
list.add(new MovieItem("M2");
list.add(new MovieItem("M3");
return list;
}
}
如何在另一个类中使用 setTabListener 将此片段设置为标签的内容?
感谢。
UPD:
主要活动中的setTabListener :
tab.setTabListener(new MyTabListener());
MyTabListener.java 源代码:
public class MyTabListener implements ActionBar.TabListener {
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
if( tab.getPosition() == 1) {
Movies frag = new Movies();
ft.replace(R.id.tmpListView, frag);
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
//ft.remove(frag);
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
因此,frag
返回非Fragment类型。如何解决这个问题?