一、目标
设计一个app的门户框架,实现3-4个tab切换效果,并在任意tab实现列表效果
二、实现过程
首先实现多个tab以及tab之间的切换
1.布局
仿照微信的UI布局可以看出,一个tab页面大体分为三个部分顶部标题,中间主题,底部按钮,所以需要三种XML文件分别显示。
1.)制作顶部页面
选择一个LinearLayout布局器存放textView组件,调整好想要的效果。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/black"
android:gravity="center"
android:text="微信"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
效果如下:
2.)制作底部按钮界面
先使用一个水平方向的LinearLayout布局器,再在其中设置四个小的垂直方向的LinearLayout布局器,每个布局器中imageButton对应每个图标,textView对应文字(以一个按钮为例,其余三个只需要重复三次即可)
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/ImageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@android:drawable/btn_star"
tools:srcCompat="@android:drawable/ic_dialog_email"
android:contentDescription="TODO" />
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="消息"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
效果如下
3.)中间部分需要四个tab分别实现不同的显示效果(以其中一个为例)
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="这是消息界面"
android:textSize="50dp" />
</LinearLayout>
效果如下:
2.压缩
使准备好的xml文件按照预期输出(以一个tab为例)
1.)首先创建一个新的Fragment,与对应tab链接
package com.example.myapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1, container, false);
}
}
2.)写MainActivity.java文件
(1)加载fragment:首先获取四个fragment对象,然后定义initial()方法,将这四个fragment加载到activity_main文件的FrameLayout中去。
fragment1=new Fragment1();
fragment2=new Fragment2();
fragment3=new Fragment3();
fragment4=new Fragment4();
(2)找到响应区:我们需要对底部四个控件进行点击事件的监听,因此在onCreate()方法找到响应区。
linearLayout1.setOnClickListener(this);
linearLayout2.setOnClickListener(this);
linearLayout3.setOnClickListener(this);
linearLayout4.setOnClickListener(this);
(3)定义事件启动函数:MainActivity实现接口View.OnClickListener,默认会对界面进行全屏监听,这里我们只需要对四个控件进行监听。fragmentshow()显示界面。
private void fragmentshow(Fragment f) {
FragmentTransaction ft= fm.beginTransaction()
.show(f);
ft.commit();
}
(4)控制tab变换,重写onClick方法:自定义 void onClick(View v)函数通过点击来显示不同界面内容。一共有四个tab,我们获取每个tab的id,根据id显示不同的界面。要实现此功能,需要先将所有界面都隐藏,再通过if条件来匹配,将id对应的界面展示出来,并修改图片路径显示第二张图片。函数fragmenthide()实现隐藏所有界面功能。代码如下:
public void onClick(View view) {
fragmenthide();
if(view.getId()==R.id.linearLayout1) fragmentshow(fragment1);
else if (view.getId()==R.id.linearLayout2) {
fragmentshow(fragment2);
}
else if (view.getId()==R.id.linearLayout3) {
fragmentshow(fragment3);
}
else fragmentshow(fragment4);
}
3.实现列表
这里对第二个页面进行替换,因此对tab2.xml进行修改。删除原来的TextView,找到一个Recyclerview拖入Component Tree,并修改属性值。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
创建一个新的xml (item.xml),作为内容容器填充到recyclerview中去
<?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="wrap_content">
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="TextView"
android:textColor="@color/black"
android:textSize="35sp" />
</LinearLayout>
对Fragment2.java中onCreateView()方法进行修改
package com.example.myapplication;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Fragment2 extends Fragment {
RecyclerView recyclerView;
List list;
Myadapter myadapter;
Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.tab2,container,false);
recyclerView=view.findViewById(R.id.recyclerview);
context=view.getContext();
list= new ArrayList();
for (int i = 0; i < 8; i++) {
list.add("这是第" + i + "个联系人");
myadapter = new Myadapter(context, list);
recyclerView.setAdapter(myadapter);
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(manager);//上下左右
}
return view;
}
}
创建Myadapter类
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class Myadapter extends RecyclerView.Adapter <Myadapter.myholder>{
Context context1;
List <String>list1;
public Myadapter(Context context, List list) {
context1=context;
list1=list;
}
@NonNull
@Override
public myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view =
LayoutInflater.from(context1).inflate(R.layout.item,parent,false) ;
//Inflater inflater=new inflater();
//inflater.inflate()不是要这个压缩
myholder myholder=new myholder(view);
return myholder;
}
@Override
public void onBindViewHolder(@NonNull Myadapter.myholder holder, int position) {
holder.textView.setText(list1.get(position));
}
@Override
public int getItemCount() {
return list1.size();
}
public class myholder extends RecyclerView.ViewHolder{//类的嵌套,会调用子类对象
TextView textView;
public myholder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView5);
}
}
}
最终效果
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- haog.cn 版权所有 赣ICP备2024042798号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务