从setTabListener调用片段

时间:2014-09-02 06:03:32

标签: java android android-fragments

我有相同的碎片:

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类型。如何解决这个问题?

0 个答案:

没有答案