ListFragments inside ViewPager

天大地大妈咪最大 提交于 2020-01-14 17:55:11

问题


I'm writing my first fragment-based app and running into some heavy problems which i couldn't solve with the API or Stackoverflow.

I am using a viewPager to swipe between two lists. Each list has a header button to create a new list element (similar to the native android alarm app). The button returns currently an error message for debugging.

The problem is:

  • FragmentList A returns the debug message for FragmentList B
  • FragmentList B returns no debug message

     ... // 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));
    }
    }
    ...
    

My custom PageAdapter will create two list framgent objects:

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 snipped (The other list looks similar except the debug output) :

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");
        }
    });

}

main.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>

回答1:


I've found the bug. In each list i was calling the same header view and same header button. The onClick method could not be clearly assigned



来源:https://stackoverflow.com/questions/15189316/listfragments-inside-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!