ViewPager与片段活动中的FragmentList一起使用

时间:2012-12-31 12:09:48

标签: android

这是我的第一个问题,所以我会尽可能精确地尝试。我正在尝试使用Fragment Activity,Fragment List和View Pager创建一个应用程序。当活动默认加载时,片段列表应显示在左侧,视图寻呼机显示在右侧(详细视图)。当我单击其中一个片段列表按钮时,我想在详细视图(右侧)中加载webview。

我已经使用Fragment List实现了Fragment Activity。现在,如何将View Pager添加到此处,以便在应用程序加载时显示它..

    public class MainActivity extends FragmentActivity implements McDonaldsListener {

  @Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

McDonaldsFragment countries
  =(McDonaldsFragment)getSupportFragmentManager()
                        .findFragmentById(R.id.countries);

countries.setCountryListener(this);

Fragment f=getSupportFragmentManager().findFragmentById(R.id.details);


  countries.enablePersistentSelection();

  }

 @Override
  public void onCountrySelected(McDonalds c) {
String url=getString(c.url);


  ((DetailsFragment)getSupportFragmentManager()
                        .findFragmentById(R.id.details))
                        .loadUrl(url);                            


  }
 }

还有其他支持课程,如果有需要,我会发布。

希望有人可以建议我一些解决方案。

编辑: 这是我试图实现但非常有用的工作....给出实例化例外:(

public class ViewPagerExample extends android.support.v4.app.FragmentActivity        implements McDonaldsListener {
private MyAdapter mAdapter;
private ViewPager mPager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAdapter = new MyAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);


    McDonaldsFragment mc
    =(McDonaldsFragment)getSupportFragmentManager()
                          .findFragmentById(R.id.countries);

  mc.setCountryListener(this);

  Fragment f=getSupportFragmentManager().findFragmentById(R.id.details);

    mc.enablePersistentSelection();
}

public void onCountrySelected(McDonalds c) {
  String url=getString(c.url);


    ((DetailsFragment)getSupportFragmentManager()
                          .findFragmentById(R.id.details))
                          .loadUrl(url);                            


}

public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return new DetailFragment();
        case 1:
            return new ImageFragment(R.drawable.ic_launcher);
        case 2:
            return new ImageFragment(R.drawable.ic_launcher);

        default:
            return null;
        }
    }
  }
 }

1 个答案:

答案 0 :(得分:0)

让我们试试这个peter kuterna view pager http://blog.peterkuterna.net/2011/09/viewpager-meets-swipey-tabs.html