列表视图和标签

时间:2014-09-21 13:52:05

标签: android tabs android-listfragment

我想在不同的标签下创建一个列表视图。这就是我为FragmentA课所做的。

public class FragmentA extends ListFragment {
String [] names = {"Theo","Theo","Theo","Theo","Theo"};
public FragmentA() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_a, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);



    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1,names);
    setListAdapter(adapter);
 }

}

我正在onActivityCreated方法中创建一个示例列表视图。最后,对于选项卡,我使用以下代码。

  public class MainActivity extends ActionBarActivity implements TabListener {
  ActionBar actionBar;
  ViewPager pager;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    pager = (ViewPager)findViewById(R.id.pager);
    pager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            actionBar.setSelectedNavigationItem(arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });

     ActionBar.Tab tab1=actionBar.newTab();
     tab1.setText("Tab 1");
     tab1.setTabListener(this);

     ActionBar.Tab tab2=actionBar.newTab();
     tab2.setText("Tab 2");
     tab2.setTabListener(this);

     ActionBar.Tab tab3=actionBar.newTab();
     tab3.setText("Tab 3");
     tab3.setTabListener(this);

     actionBar.addTab(tab1);
     actionBar.addTab(tab2);
     actionBar.addTab(tab3);
   }

   @Override
   public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

   }

  @Override
  public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub
    pager.setCurrentItem(arg0.getPosition());
 }

 @Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

  }
 }
   class MyAdapter extends FragmentPagerAdapter{

    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        Fragment fragment = null;
        if(arg0==0){
            fragment = new FragmentA();
        }
        if(arg0==1){
            fragment = new FragmentB();
        }
        if(arg0==2){
            fragment = new FragmentC();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

}

编译器不允许我运行应用程序,因为它给我以下错误..

Type mismatch: cannot convert from FragmentA to Fragment
Change type of fragment to fragmentA.

如果我在FragmentA类中注释setListAdapter(适配器),一切都很好。为什么会这样?

1 个答案:

答案 0 :(得分:1)

变化:

import android.app.ListFragment;

import android.support.v4.app.ListFragment;

您使用了API级别11的ListFragment,但您必须使用支持库中的ListFragment才能使用viewpager。因为它FragmentManager也来自支持库(getSupportFragmentManager)所以它不能理解除了它自己的库之外的任何fragment