我有ViewPager
可以显示四个或五个标签(使用" DO STUFF"和#34; DO STUFF2"按钮可切换)。我为FragmentStatePagerAdapter
使用了ViewPager
。
对于"表"仅限制表符,我使用FragmentTransaction.replace()
在其中添加了一个片段。
然而,当我切换到五个标签时,嵌套的片段被添加到BOTH" Squad"选项卡和"表"标签。我只想将它添加到"表"标签。我怎么能做到这一点?
片段持有我们的ViewPager:
public class GrandpaFragment extends Fragment implements View.OnClickListener {
private SectionPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
// Some irrelevant codes
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_grandpa, container, false);
mViewPager = (ViewPager) rootView.findViewById(R.id.pagerTournament);
mSectionsPagerAdapter = new SectionPagerAdapter(getChildFragmentManager());
mSectionsPagerAdapter.setViewPager(mViewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
btnInfo=(TextView)rootView.findViewById(R.id.btnInfo);
btnSquad=(TextView)rootView.findViewById(R.id.btnSquad);
btnTable=(TextView)rootView.findViewById(R.id.btnTable);
btnFixtures=(TextView)rootView.findViewById(R.id.btnFixtures);
btnStats=(TextView)rootView.findViewById(R.id.btnStats);
layoutMainTab=(LinearLayout)rootView.findViewById(R.id.layoutMainTab);
// Set onClickListener
btnInfo.setOnClickListener(this);
btnSquad.setOnClickListener(this);
btnTable.setOnClickListener(this);
btnFixtures.setOnClickListener(this);
btnStats.setOnClickListener(this);
setupTabButtonsText();
return rootView;
}
public void update(){
mSectionsPagerAdapter.updateAdapterData(5);
setupTabButtonsText();
}
public void update2(){
mSectionsPagerAdapter.updateAdapterData(4);
setupTabButtonsText();
}
private void setupTabButtonsText(){
if(mSectionsPagerAdapter.totalTabCount == 4){
layoutMainTab.setWeightSum(8);
btnSquad.setText("Table");
btnTable.setText("Fixture");
btnFixtures.setText("Stats");
btnStats.setVisibility(View.GONE);
}
else{
layoutMainTab.setWeightSum(10);
btnSquad.setText("Squad");
btnTable.setText("Table");
btnFixtures.setText("Fixture");
btnStats.setVisibility(View.VISIBLE);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnInfo:
mViewPager.setCurrentItem(0);
break;
case R.id.btnSquad:
mViewPager.setCurrentItem(1);
break;
case R.id.btnTable:
mViewPager.setCurrentItem(2);
break;
case R.id.btnFixtures:
mViewPager.setCurrentItem(3);
break;
case R.id.btnStats:
mViewPager.setCurrentItem(4);
break;
default:
break;
}
}
}
ViewPager中每个屏幕的片段:
public class PapaFragment extends Fragment implements View.OnClickListener{
public static final String ARG_SECTION_NUMBER = "section_number";
private Button button, button2;
public PapaFragment() {
}
public static PapaFragment newInstance(String sectionNumber) {
PapaFragment fragment = new PapaFragment();
Bundle args = new Bundle();
args.putString(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_papa, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getString(ARG_SECTION_NUMBER)));
button = (Button) rootView.findViewById(R.id.dostuff);
button.setOnClickListener(this);
button2 = (Button) rootView.findViewById(R.id.dostuff2);
button2.setOnClickListener(this);
return rootView;
}
@Override
public void onStart(){
super.onStart();
if(getArguments().getString(ARG_SECTION_NUMBER).equals("Table")) {
SonFragment f = SonFragment.newInstance("A", "B");
getChildFragmentManager().beginTransaction()
.replace(R.id.placeholder, f).commit();
}
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.dostuff){
((GrandpaFragment)getParentFragment()).update();
}
else{
((GrandpaFragment)getParentFragment()).update2();
}
}
}
我们的FragmentStatePagerAdapter:
public class SectionPagerAdapter extends FragmentStatePagerAdapter {
private static final int TOTAL_TAB_SQUAD = 5;
private static final int TOTAL_TAB_NO_SQUAD = 4;
private Context mContext;
public int totalTabCount = 4;
private ViewPager mViewPager;
private FragmentManager fm;
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
this.fm = fm;
}
public void setViewPager(ViewPager viewPager){
mViewPager = viewPager;
}
public void updateAdapterData(int count){
totalTabCount = count;
int currentItem = mViewPager.getCurrentItem();
mViewPager.setAdapter(this);
mViewPager.setCurrentItem(currentItem);
notifyDataSetChanged();
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position) {
case 0:
fragment = PapaFragment.newInstance("InfoDetail");
break;
case 1:
if(totalTabCount == TOTAL_TAB_SQUAD) {
fragment = PapaFragment.newInstance("Squad");
}
else {
fragment = PapaFragment.newInstance("Table");
}
break;
case 2:
if(totalTabCount == TOTAL_TAB_SQUAD) {
fragment = PapaFragment.newInstance("Table");
}
else {
fragment = PapaFragment.newInstance("Fixture");
}
break;
case 3:
if(totalTabCount == TOTAL_TAB_SQUAD) {
fragment = PapaFragment.newInstance("Fixture");
}
else {
fragment = PapaFragment.newInstance("Stats");
}
break;
case 4:
fragment = PapaFragment.newInstance("Stats");
break;
default:
break;
}
return fragment;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
return totalTabCount;
}
}
GrandpaFragment的Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layoutTournamentTab"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/layoutMainTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10" >
<TextView
android:id="@+id/btnInfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@color/colorPrimaryDark"
android:layout_margin="1dp"
android:gravity="center"
android:textColor="@android:color/white"
android:text="Info" />
<TextView
android:id="@+id/btnSquad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:background="@color/colorPrimaryDark"
android:layout_margin="1dp"
android:textColor="@android:color/white"
android:text="Squad"/>
<TextView
android:id="@+id/btnTable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:background="@color/colorPrimaryDark"
android:layout_margin="1dp"
android:textColor="@android:color/white"
android:text="Table" />
<TextView
android:id="@+id/btnFixtures"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:background="@color/colorPrimaryDark"
android:layout_margin="1dp"
android:textColor="@android:color/white"
android:text="Fixture" />
<TextView
android:id="@+id/btnStats"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:background="@color/colorPrimaryDark"
android:layout_margin="1dp"
android:textColor="@android:color/white"
android:text="Stats"/>
</LinearLayout>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pagerTournament"
android:layout_width="fill_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
PapaFragment的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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"/>
<Button
android:layout_below="@id/section_label"
android:id="@+id/dostuff"
android:text="do stuff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_below="@id/section_label"
android:layout_toRightOf="@id/dostuff"
android:id="@+id/dostuff2"
android:text="do stuff2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout android:id="@+id/placeholder"
android:layout_below="@id/dostuff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"/>
</RelativeLayout>