本文介绍了里面ViewPager ListFragments的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在写我的第一个基于片段应用并运行到我无法与API或解决#1一些沉重的问题。

我使用的 viewPager 的两个列表之间滑动。每个列表都有的的按钮,创建一个新的列表元素(类似原生的Andr​​oid闹钟应用)。目前调试按钮返回一条错误消息。

问题是:


  • FragmentList一个返回FragmentList乙
  • 调试消息
  • FragmentListρB返回没有调试信息

      ... //主类
    公共类DemoApp扩展FragmentActivity实现ActionBar.TabListener {@覆盖
    保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);的setContentView(R.layout.main);//设置操作栏。
    最后的动作条动作条= getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);PageAdapter mPageAdapter =新PageAdapter(getSupportFragmentManager());
    mViewPager =(ViewPager)findViewById(R.id.pager);
    mViewPager.setAdapter(mPageAdapter);mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
        @覆盖
        公共无效使用onPageSelected(INT位置){
            actionBar.setSelectedNavigationItem(位置);
        }
    });的for(int i = 0; I< mPageAdapter.getCount();我++){
        actionBar.addTab(actionBar.newTab()的setText(mPageAdapter.getPageTitle(I))setTabListener(本));
    }
    }
    ...


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

 公共类PageAdapter扩展FragmentStatePagerAdapter {私人最终诠释NUMBER_PAGES = 2;公共PageAdapter(FragmentManager FM){
    超(FM);
}@覆盖
公共片段的getItem(INT位置){
    如果(位置== 0){
        返回新FoodListFragment();
    }否则如果(位置== 1){
        返回新LocationListFragment();
    }其他{
        抛出新抛出:IllegalArgumentException(无效的页面位置:+位置);
    }
}
@覆盖
公众诠释的getCount(){
    返回NUMBER_PAGES;
}...}

FoodListFragment剪断(另一个列表看起来除了调试输出类似):

 公共类FoodListFragment扩展ListFragment实现LoaderCallbacks<&光标GT; {
/ *
 *(非Javadoc中)
 *
 * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
 * /
@覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
    super.onActivityCreated(savedInstanceState);    //添加列表头的按钮创建一个新的列表项
    视图V = getActivity()getLayoutInflater()膨胀(R.layout.list_header,空)。
    getListView()addHeaderView(五)。...
    setListAdapter(getSomeAdapter());    //获取列表标题按钮
    的ImageButton createItem中=(的ImageButton)getActivity()findViewById(R.id.create_new_entry)。    createItem.setOnClickListener(新View.OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            Log.e(LOG_TAG的onclick FoodListFragment);
        }
    });}

main.xml中:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID /包装
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>< android.support.v4.view.ViewPager
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID /寻呼机
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    工具:上下文=。DemoApp/>< / LinearLayout中>


解决方案

我发现的bug。在每个列表我打电话相同的标题视图,相同的标题按钮。 OnClick方法不能明确分配

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>
解决方案

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

这篇关于里面ViewPager ListFragments的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:41