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

时间:2013-04-05 07:51:52

标签: java android android-fragments actionbarsherlock android-viewpager

您好,感谢您的帮助!

我正在尝试使用ActionBarSherlcok构建一个应用程序。当我单击选项卡时,我希望实例化相同的片段,但基于我单击的选项卡使用动态数据。 出于某种原因它不断向我显示错误的数据,尽管传递了正确的参数值(我使用断点和监视验证了它)。 correct parameter value but wrong data

我读过SimpleOnPageChangeListener,getCurrentItem,当然还有Fragments Tutorial。 我依靠SwipeyTabs示例来创建它,并构建了一个简短的演示来显示我的问题:

这是我的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);
        }
    }
}

这是我的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;
    }
}

我的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>

和我的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>

最后我的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();
    }
}

如果您要玩它,您会看到点击标签“片段2”有时会显示“第1页”,有时会显示“第3页”,有时会显示“第2页”。 我认为它与预加载片段或片段刷新有关,但此时我无能为力......

再次感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

尝试改变这一点:

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

对此:

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

答案 1 :(得分:0)

我发现了问题所在。 我浏览了ActionBarSherlock网站并查看了示例代码。实施与我的不同,但有一点是站起来的。 以下是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);
}

以下是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;
    }
}

如您所见,我完全放弃使用pos。 谢谢大家的帮助!