android - ViewPager 中的 ListFragments

标签 android android-fragments android-viewpager android-pageradapter

我正在编写我的第一个基于 fragment 的应用程序,遇到了一些我无法使用 API 或 Stackoverflow 解决的严重问题。

我正在使用 viewPager 在两个列表之间滑动。每个列表都有一个 header 按钮来创建一个新的列表元素(类似于 native android 闹钟应用程序)。该按钮当前返回一条用于调试的错误消息。

问题是:

  • FragmentList A 返回 FragmentList B 的调试信息
  • FragmentList B 没有返回任何调试信息

     ... // The main class
    public class DemoApp extends FragmentActivity implements ActionBar.TabListener {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.main);
    
    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
    PageAdapter mPageAdapter = new PageAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mPageAdapter);
    
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });
    
    for (int i = 0; i < mPageAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab().setText(mPageAdapter.getPageTitle(i)).setTabListener(this));
    }
    }
    ...
    

我的自定义 PageAdapter 将创建两个列表 fragment 对象:

public class PageAdapter extends FragmentStatePagerAdapter {

private final int NUMBER_PAGES = 2;

public PageAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    if (position == 0) {
        return new FoodListFragment();
    } else if (position == 1){
        return new LocationListFragment();
    } else {
        throw new IllegalArgumentException("Invalid page position: " + position);
    }
}


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

...

}

FoodListFragment 被截断(除了调试输出,其他列表看起来很相似):

public class FoodListFragment extends ListFragment implements LoaderCallbacks<Cursor> {
/*
 * (non-Javadoc)
 * 
 * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
 */
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // add a list header with the button to create a new list item
    View v = getActivity().getLayoutInflater().inflate(R.layout.list_header, null);
    getListView().addHeaderView(v);

...
    setListAdapter(getSomeAdapter());

    // get the list header button
    ImageButton createItem = (ImageButton) getActivity().findViewById(R.id.create_new_entry);

    createItem.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e(LOG_TAG, "onclick FoodListFragment");
        }
    });

}

主.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".DemoApp" />

</LinearLayout>

最佳答案

我发现了错误。在每个列表中,我调用相同的标题 View 和相同的标题按钮。 onClick方法无法明确赋值

关于android - ViewPager 中的 ListFragments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15189316/

相关文章:

android - 防止 fragment 自定义动画在屏幕旋转时播放?

Android setUserVisibleHint 永远不会被调用?

android - 从 ViewPager 中的 Activity 更新 fragment

android - Android后台服务弹出对话框

Android:从DialogFragment回调 fragment ?

android - 调用 fragment 和 Activity 之间的委托(delegate)但未触发可见性更改

android - 无法更改 fragment 的容器 ID

Android SQLite 查找速度非常慢

java - Comparable 不排序对象

android - 解码大型 base64 字符串