一.作业目标
1,设计一个app的门户框架,需要实现3-4个tab切换效果;本功能要求需要的技术为:activity、xml、fragment,且在任一tab页中实现列表效果;
2,本功能的实现需要使用recycleview。
二.技术说明
制作顶部页面,中心显示“微信”字样,白底黑字
效果:
制作底部按钮页面,分成四个按钮“消息”“联系人”“发现”“我的”
效果:
结合两个页面形成初始页面
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/top" />
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</FrameLayout>
<include layout="@layout/bottom" />
</LinearLayout>
效果:
设置Fragment的xml文件的布局,gravity="center",文字居中,layout_gravity="center",使TextView居中。
效果:
初始化:将多个fragment添加入mainactivity.java
通过点击实现目标界面
public void onClick(View view) {//view
fragmenthide();
if(view.getId()==R.id.LinearLayout1) fragmentshow(fragment1);
if(view.getId()==R.id.LinearLayout2) fragmentshow(fragment2);
if(view.getId()==R.id.LinearLayout3) fragmentshow(fragment3);
if(view.getId()==R.id.LinearLayout4) fragmentshow(fragment4);
}
private void fragmentshow(Fragment fragment) {
FragmentTransaction transaction=fm.beginTransaction()
.show(fragment);
transaction.commit();
}
}
在tab1页面实现列表效果:
在tab1.xml中添加recycleview,并设置间距
创建item.xml文件设置内容
public void onClick(View view) {//view
fragmenthide();
if(view.getId()==R.id.LinearLayout1) fragmentshow(fragment1);
if(view.getId()==R.id.LinearLayout2) fragmentshow(fragment2);
if(view.getId()==R.id.LinearLayout3) fragmentshow(fragment3);
if(view.getId()==R.id.LinearLayout4) fragmentshow(fragment4);
}
private void fragmentshow(Fragment fragment) {
FragmentTransaction transaction=fm.beginTransaction()
.show(fragment);
transaction.commit();
}
}
创建adapter实现列表
在fragment中添加数据
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view=inflater.inflate(R.layout.tab1, container, false);
recyclerView = view.findViewById(R.id.recyclerview);
list =new ArrayList<>();
for(int i=0;i<9;i++) {
list.add("这是第"+i+"行数据");
}
adapter = new Myadapter(view.getContext(), list);
recyclerView.setAdapter(adapter);
LinearLayoutManager manager = new LinearLayoutManager(view.getContext());
manager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(manager);
return view;
}