这是我的应用(带片段): 在左侧有一个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)
{
}
}
}
Spiele和Tabelle-Class加载到片段中并按我想要的方式运行。 如果我点击一个类别(左侧片段),右侧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;
Spiele此时空白。 Tabelle展示了一个基本的tabelle。像图像一样: 我又说了一遍。我第一次在左侧片段中选择一个项目时,它会按照我的意愿行事。但是如果第二次点击#Verein + 0 - Punkte就会消失而且它是一个空白页... beacuse tabelle不要再加载......
答案 0 :(得分:5)
我认为问题是onCreateView
第二次加载时没有被调用,因为它已经被创建了。
移动附加片段的代码
到onActivityCreated()