最近项目中一个需求需要嵌套使用Fragment,于是乎研究了一凡了,Fragment嵌套使用用到了getChildFragmentManager()方法,这个是support v4 jar中的提供方法。自己抽时间写了一个简单的Demo跟大家分享,示例中使用到了GitHub上一个非常流行的库 PagerSlidingTabStrip,它提供了在ViewPager 不同page之间导航的指示器功能。官方地址:https://github.com/astuetz/PagerSlidingTabStrip。
先看看Demo 的效果:
点击Next进入第二个Child Fragment:
这里只贴一下主要的代码,完整代码大家可以下载下来后自己去研究。
MainActivity 源代码:
package com.ricky.nestfragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import com.astuetz.PagerSlidingTabStrip;
import com.ricky.nestfragment.fragment.CommonFragment;
import com.ricky.nestfragment.fragment.ContainerFragment;
/**
*
* @author Ricky Feng
*
*/
public class MainActivity extends BaseFragmentActivity {
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
private int current_position = 0;
@Override
protected void setupContentView() {
setContentView(R.layout.main);
}
@Override
protected void findViewById() {
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
}
@Override
protected void setListener() {
// TODO Auto-generated method stub
}
@Override
protected void processLogic() {
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
.getDisplayMetrics());
pager.setPageMargin(pageMargin);
// tabs.setBackgroundColor(Color.parseColor("#FFF4842D"));
tabs.setIndicatorColor(Color.parseColor("#FFF4842D"));
tabs.setViewPager(pager);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("position", current_position);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
current_position = savedInstanceState.getInt("position");
}
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
"Top New Free", "Trending" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public int getCount() {
return TITLES.length;
}
@Override
public Fragment getItem(int position) {
current_position = position;
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ContainerFragment();
break;
default:
fragment = new CommonFragment(TITLES[position]);
break;
}
return fragment;
}
}
}
CommonFragment源代码如下:
package com.ricky.nestfragment.fragment;
import com.ricky.nestfragment.R;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class CommonFragment extends Fragment {
private String title;
public CommonFragment(String title){
this.title = title;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_common, null);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
tv_title.setText(title);
return view;
}
}
最重要的是ContainerFragment 源代码如下:
package com.ricky.nestfragment.fragment;
import com.ricky.nestfragment.R;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Fragment容器,用来嵌套其他子Fragment
* @author Ricky Feng
*
*/
public class ContainerFragment extends Fragment implements OnClickListener {
private static final String TAG = ContainerFragment.class.getSimpleName();
private int mCurrentlyShowingFragment = 0;
private Button bt_show;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_container, null);
bt_show = (Button) view.findViewById(R.id.bt_show);
bt_show.setOnClickListener(this);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
if (savedInstanceState == null) {
getChildFragmentManager().beginTransaction().replace(R.id.fl_chilid, new ChildFragmentOne("Child Fragment One")).commit();
mCurrentlyShowingFragment = 0;
} else {
mCurrentlyShowingFragment = savedInstanceState.getInt("currently_showing_fragment");
}
adjustButtonText();
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_show:
if (mCurrentlyShowingFragment == 0) {
showNextScreen();
} else {
showPreviousScreen();
}
break;
default:
break;
}
}
private void showNextScreen() {
mCurrentlyShowingFragment = 1;
getChildFragmentManager().beginTransaction().replace(R.id.fl_chilid, new ChildFragmentTwo("Child Fragment Two")).addToBackStack(null).commit();
adjustButtonText();
}
private void showPreviousScreen() {
mCurrentlyShowingFragment = 0;
getChildFragmentManager().popBackStackImmediate();
adjustButtonText();
}
private void adjustButtonText() {
if(mCurrentlyShowingFragment == 0){
bt_show.setText("Next");
} else {
bt_show.setText("Previous");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("currently_showing_fragment", mCurrentlyShowingFragment);
super.onSaveInstanceState(outState);
}
@Override
public void onPause() {
Log.e(TAG, "onPause");
super.onPause();
}
}
今天就先写到这里了,有问题的同学可以留言讨论!
完整Demo下载地址:http://download.csdn.net/detail/fx_sky/7600087