你好,感謝你的幫助!ActionBarSherlock片段顯示不同於傳遞的數據
我正試圖用ActionBarSherlcok構建一個應用程序。當我點擊標籤時,我希望同一個片段實例化,但是基於我點擊的標籤的動態數據。 由於某種原因它不斷向我顯示錯誤的數據,雖然傳遞了正確的參數值(我使用斷點和手錶進行了驗證)。
我讀過關於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頁」。 我認爲它與預加載的片段或片段刷新有關,但在這一點上我很無能...
再次感謝您的幫助!
所以你應該將getActivity()轉換爲MainActivity。無論如何它的權利。 – 2013-04-05 08:21:53
Emil,改變你的建議,並試圖改變爲'getSherlockActivity()'不會編譯錯誤:'mViewPager無法解析或它不是字段'。順便說一句,我在運行時沒有'pos'的值問題。它是正確的。我的問題是顯示的文本與pos的值不一致。 – 2013-04-06 17:33:34