這是我的應用程序(帶有片段): 在左側有一個ListView,您可以在其中選擇。 如果您選擇了右側的加載片段並將Tabs添加到ActionBar。 存在對的代碼:片段中的片段不刷新
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class SpieleUndTabelleFragment extends SherlockFragment
{
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
ActionBar bar = getSherlockActivity().getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setHomeButtonEnabled(true);
mViewPager = new ViewPager(getSherlockActivity());
mViewPager.setId(R.id.pager);
mTabsAdapter = new TabsAdapter(getSherlockActivity(), mViewPager);
mTabsAdapter.removeAllTabs();
mTabsAdapter.addTab(bar.newTab().setText("Alle Spiele"), Spiele.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Tabelle"), Tabelle.class, null);
return mViewPager;
}
public static 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 activity, ViewPager pager)
{
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.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();
}
public void removeAllTabs()
{
mActionBar.removeAllTabs();
notifyDataSetChanged();
}
@Override
public int getCount()
{
return mTabs.size();
}
@Override
public Fragment getItem(int position)
{
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
public void onPageSelected(int position)
{
mActionBar.setSelectedNavigationItem(position);
}
public void onPageScrollStateChanged(int state)
{
}
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
Object tag = tab.getTag();
for (int i = 0; i < mTabs.size(); i++)
{
if (mTabs.get(i) == tag)
{
mViewPager.setCurrentItem(i);
}
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
}
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
}
}
}
的免費遊戲和Tabelle級加載到片段和運行像我想。 如果我點擊一個類別(左邊的片段)正確的Tabs與Spiele和Tabelle-Calss加載並運行。 但是,如果我點擊左側的其他類別的第二次,操作欄中的選項卡將被刪除,並且新的一個加載。但是,我的問題。 Tabelle和Spiele-Tabs不會再次加載!我一直都是空白頁面!因爲上課再次犯規負荷......
這裏是我刷新與標籤右邊片段代碼:
SpieleUndTabelleFragment fragment = new SpieleUndTabelleFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.item_detail_container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
任何人可以幫助我嗎? 謝謝!
---------------- 更新: ---------------------
我使用getFragmetManager()
,因爲我有一個setOnItemListener()
與內部類。如果我想用getSupportFragmentManager()
eclipse說它不支持它。 這裏是onCreateView()
從類代碼:
View view = inflater.inflate(R.layout.waehle_mannschaft, container, false);
ListView myList = (ListView) view.findViewById(R.id.list);
myList.setDivider(null);
final String[] listContent = {"News", "Gästebuch", "Herren 1", "Herren 2", "Herren 3", "Damen 1", "Damen 2", "männl. A", "männl. B1", "männl. B2", "männl. C", "männl. D", "E-Jugend"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1, listContent);
myList.setAdapter(adapter);
final View v2 = view;
myList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView av, View v, int position, long id)
{
Toast.makeText(v2.getContext(), listContent[position], Toast.LENGTH_LONG).show();
if(twoPanels)
{
SpieleUndTabelleFragment fragment = new SpieleUndTabelleFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.item_detail_container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
else
{
Intent myIntent = new Intent(v2.getContext(), SpieleUndTabelleActivity.class);
startActivity(myIntent);
}
}
});
return view;
免費遊戲就是在這個時候空白。表格顯示了一個基本的表格。像圖片: 我再說一遍。我第一次在左邊的片段上選擇一個項目,它就是我想要的。但如果再次點擊#Verein + 0 - Punkte消失,這是一個空白頁...因爲tabelle不要再次加載...
確保您使用'getFragmentManager()'?不應該編譯。應該是'getSupportFragmentManager()'。 「Spiele」和「Tabelle」中發生了什麼? 'SpieleUndTabelleFragment'很好。還請添加您的'SherlockListFragment'子類的基礎知識。 –