先上个效果图:![这里写图片描述](https://img-my.csdn.net/uploads/201609/11/1473600843_9480.gif)
首先是主页面,下面是导航栏,上面一个线性布局(这个无所谓,FrameLayout也可以)用于加载Fragment。下面是代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context="com.example.leon.willnote_03.MainActivity"
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="@+id/replace"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/id_click_weather"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="天气"
android:gravity="center"
android:layout_margin="7dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/id_click_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="内容"
android:gravity="center"
android:layout_margin="7dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/id_click_info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="信息"
android:gravity="center"
android:layout_margin="7dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
然后是三个Fragment和对应的布局,先以一个简单的WeatherFragment为例,布局文件就是线性布局里面加一个ViewPager,WeatherFragment.java里也就是重写onCreateView。
public class WeatherFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.weather_layout,container,false);
return view;
}
}
另一个InfoFragment也同样,但是ContentFragment就稍微复杂点了,因为他里面要嵌套ViewPager,所以要先定义ViewPager和存视图的数组ArrayList<View>,然后往ArrayList<View>里加入我们的内容子布局:内容子布局1,内容子布局2。但是这个工作不能再这里做,而是通过构造函数把ArrayList<View>传进来,因为如果在这里做每次切换到别的Fragment再切回来就会再次往ArrayList<View>加入两个布局。接下来就是在onCreateView中通过id找到viewPager,然后设置适配器。
public class ContentFragment extends Fragment {
private ViewPager viewPager;
private ArrayList<View> pagerList;
ContentFragment(ArrayList<View> pagerList) {
this.pagerList = pagerList;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_layout,container,false);
viewPager = (ViewPager) view.findViewById(R.id.id_Pager);
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return pagerList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(pagerList.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(pagerList.get(position));
return pagerList.get(position);
}
});
return view;
}
}
最后在主活动里设置初始页面和导航栏的监听。直接上代码
public class MainActivity extends FragmentActivity implements View.OnClickListener {
private LinearLayout click_info;
private LinearLayout click_content;
private LinearLayout click_weather;
private Fragment weatherFragment;
private Fragment contentFragment;
private Fragment infoFragment;
private ArrayList<View> pagerList = new ArrayList<View>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater inflater = getLayoutInflater();
View noteView = inflater.inflate(R.layout.note_layout,null);
View scheduleView = inflater.inflate(R.layout.shcedule_layout,null);
pagerList.add(noteView);
pagerList.add(scheduleView);
setDefaultFragment();
setOnclickListener();
}
public void setOnclickListener() {
click_content = (LinearLayout) findViewById(R.id.id_click_content);
click_info = (LinearLayout) findViewById(R.id.id_click_info);
click_weather = (LinearLayout) findViewById(R.id.id_click_weather);
click_content.setOnClickListener(this);
click_info.setOnClickListener(this);
click_weather.setOnClickListener(this);
}
@Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
switch (view.getId()) {
case R.id.id_click_weather :
if (weatherFragment == null) {
weatherFragment = new WeatherFragment();
}
transaction.replace(R.id.replace,weatherFragment);
break;
case R.id.id_click_content :
if (contentFragment == null) {
contentFragment = new ContentFragment(pagerList);
}
transaction.replace(R.id.replace,contentFragment);
break;
case R.id.id_click_info :
if (infoFragment == null) {
infoFragment = new InfoFragment();
}
transaction.replace(R.id.replace,infoFragment);
break;
}
transaction.commit();
}
public void setDefaultFragment() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
contentFragment = new ContentFragment(pagerList);
transaction.replace(R.id.replace,contentFragment);
transaction.commit();
}
}
本小白在寝室捣鼓了两天才搞完,另外,代码里不合理或者可以优化的地方,欢迎大家指点!