简体   繁体   English

ActionBarSherlock片段显示与传递的数据不同的数据

[英]ActionBarSherlock fragment showing different data than passed

Hello and thank you for trying to help! 您好,感谢您的帮助!

I'm trying to build an app with ActionBarSherlcok. 我正在尝试使用ActionBarSherlcok构建应用程序。 When I click the tabs, I would like the same fragment to instantiate but with dynamic data based on the tab I clicked. 当我单击选项卡时,我希望实例化相同的片段,但是基于我单击的选项卡具有动态数据。 For some reason it keeps showing me the wrong data , though the correct parameter value is passed (I verified it using breakpoints and watches). 由于某种原因,尽管传递了正确的参数值(我使用断点和监视对其进行了验证),但它始终向我显示错误的数据 正确的参数值但数据错误

I've read about SimpleOnPageChangeListener, getCurrentItem and of course the Fragments Tutorial . 我已经阅读了有关SimpleOnPageChangeListener,getCurrentItem以及“ 片段教程”的内容 I relied on SwipeyTabs example to create this, and built a short demo to show my problem here: 我依靠SwipeyTabs示例来创建此示例,并构建了一个简短的演示以在此处显示我的问题:

This is my MainActivity.java 这是我的MainActivity.java

package il.co.gilead.testdynamicfragments;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class MainActivity extends SherlockFragmentActivity {

    public static ViewPager mViewPager;
    private TabsAdapter mTabsAdapter;
    Integer intNumOfPages = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);
        setContentView(mViewPager);
        final ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mTabsAdapter = new TabsAdapter(this, mViewPager);
        for (int i=1; i<=intNumOfPages; i++) {
            mTabsAdapter.addTab(bar.newTab().setText("Fragment "+i), 
                TestFrag.class, null);
        }
    }
}

This is my TestFrag.java 这是我的TestFrag.java

package il.co.gilead.testdynamicfragments;

import com.actionbarsherlock.app.SherlockFragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TestFrag extends SherlockFragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_test, container, false);
        TextView tv = (TextView) v.findViewById(R.id.textView1);
        Integer pos = (MainActivity.mViewPager.getCurrentItem() + 1);
        tv.setText("Page " + pos.toString());
        return v;
    }
}

My activity_main.xml 我的activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/pager" />

</RelativeLayout>

and my test_frag.xml 和我的test_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

Finally my TabsAdapter 最后是我的TabsAdapter

public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener{
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo{
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args){
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity fa, ViewPager pager) {
        super(fa.getSupportFragmentManager());
        mContext = fa;
        mActionBar = fa.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args){
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public void onPageScrollStateChanged(int state) {    
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
        Object tag = tab.getTag();
        for (int i = 0; i<mTabs.size(); i++){
            if (mTabs.get(i) == tag){
                mViewPager.setCurrentItem(i);
            }
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }
}

If you'll play around with it, you'll see that clicking tab "Fragment 2" sometimes shows "Page 1" and sometimes shows "Page 3" and sometimes shows "Page 2". 如果您会玩转它,您会发现单击选项卡“片段2”有时会显示“页面1”,有时会显示“页面3”,有时会显示“页面2”。 I think it has something to do with pre-loading the fragments, or fragment refresh, but at this point I am clueless... 我认为这与预加载片段或刷新片段有关,但在这一点上我一无所知...

Thanks again for your help! 再次感谢你的帮助!

Try to change this: 尝试更改此:

Integer pos = (MainActivity.mViewPager.getCurrentItem() + 1);

To this: 对此:

Integer pos = (getActivity().mViewPager.getCurrentItem() + 1);

I found out what the problem was. 我发现了问题所在。 I browsed through ActionBarSherlock website and looked at the sample code. 我浏览了ActionBarSherlock网站,并查看了示例代码。 The implementation was different than mine, but one thing stood up. 实施不同于我的实施,但有一件事情站了起来。 Here are the code changes in MainActivity.java: 这是MainActivity.java中的代码更改:

for (int i=1; i<=intNumOfPages; i++)
{
    Bundle args = new Bundle();
    args.putInt("num", i);
mTabsAdapter.addTab(bar.newTab().setText("Fragment "+i), TestFrag.class, args);
}

Here is the addition to TestFrag.java: 这是TestFrag.java的附加内容:

public class TestFrag extends SherlockFragment {
    Integer intPageNum;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null)
            intPageNum = args.getInt("num");
        else
            intPageNum = 1;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_test, container, false);
        TextView tv = (TextView) v.findViewById(R.id.textView1);
        tv.setText("Page " + intPageNum.toString());
        return v;
    }
}

As you can see, I dropped the use of pos altogether. 如您所见,我完全放弃了pos的使用。 Thank you all for trying to help! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM