一、作业目标
1.根据课程内容设计一个app(微信)的门户框架,需要实现3-4个tab的切换效果;本功能要求需要的技术为:activity、xml、fragment;
2.在任一tab页实现列表效果;本功能的实现需要使用recycleview。
二、实现工具
Android Stdio
三、设计思路:
1.实现微信的界面设计:
将界面分为上中下三部分,分别用top.xml,tab.xml,bottom.xml文件设计,其中tab再分为4个页面tab1.xml,tab2.xml,tab3.xml,tab4.xml。
同时分别对应Fragment1,Fragment2,Fragment3,Fragment4 java文件。
最后创建main.xml整合到一起。
2.实现列表效果:
利用recycleview在tab2界面实现列表效果,将tab2界面内容替换成列表。
总体文件如下:
四、设计过程
首先设计top.xml:
<?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/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="40sp" />
</LinearLayout>
-
android:layout_width="wrap_content"
: 这个属性定义了视图的宽度。在这里,它被设置为 "wrap_content",表示视图的宽度将根据其内容自动调整,以适应文本内容的宽度。 -
android:layout_height="wrap_content"
: 这个属性定义了视图的高度。同样,它被设置为 "wrap_content",表示视图的高度将根据其内容自动调整,以适应文本内容的高度。 -
android:layout_weight="1"
: 这个属性通常用于在布局中进行权重分配。在这里,它被设置为 "1",表示这个视图在与其他视图共享空间时具有相等的权重, -
android:background="@color/black"
: 这个属性用于设置视图的背景颜色。它引用了一个颜色资源,@color/black
,表示该视图的背景颜色将是黑色。 -
android:gravity="center"
: 这个属性定义了文本在视图内的对齐方式,被设置为 "center",表示文本将水平和垂直居中对齐。 -
android:text="微信"
: 这个属性用于设置视图中显示的文本内容。文本内容被设置为 "微信",即视图将显示 "微信" 这个文本。 -
android:textColor="@color/white"
: 这个属性用于设置文本的颜色。它引用了一个颜色资源,@color/white
,表示文本颜色将是白色。 -
android:textSize="40sp"
: 这个属性定义了文本的大小。在这里,它被设置为 "40sp",表示文本将以40sp的大小显示。
top.xml界面显示如下:
其次设计中间部分tab.xml:
因为4个tab页面布局类似,这里只展示tab1页面设计代码及界面:
<?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:id="@+id/textView2"
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="50sp" />
</LinearLayout>
-
android:layout_width="wrap_content"
: 这是TextView
的属性之一,用于定义其宽度。在这里,它被设置为 "wrap_content",表示TextView
的宽度将根据其文本内容自动调整。 -
android:layout_height="wrap_content"
: 这是TextView
的属性之一,用于定义其高度。它被设置为 "wrap_content",表示TextView
的高度将根据其文本内容自动调整。 -
android:layout_gravity="center"
: 这是TextView
的属性之一,用于定义TextView
在其父容器中的对齐方式,设置为 "center",表示文本将在父容器中水平和垂直居中对齐。 -
android:layout_weight="1"
: 这是TextView
的属性之一,通常用于在布局中进行权重分配。在这里,它被设置为 "1",表示TextView
在与其他视图共享空间时具有相等的权重。 -
android:gravity="center"
: 这是TextView
的属性之一,用于定义文本在TextView
内的对齐方式。在这里,它被设置为 "center",表示文本将水平和垂直居中对齐。 -
android:text="这是聊天界面"
: 这是TextView
的属性之一,用于设置TextView
中显示的文本内容 -
android:textSize="50sp"
: 这是TextView
的属性之一,用于定义文本的大小。在这里,它被设置为 "50sp",表示文本将以50sp的大小显示。
tab1界面显示如下:
然后设计bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView11"
android:layout_width="115dp"
android:layout_height="109dp"
android:layout_weight="1"
app:srcCompat="@android:drawable/ic_menu_always_landscape_portrait" />
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="聊天"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView22"
android:layout_width="115dp"
android:layout_height="109dp"
android:layout_weight="1"
app:srcCompat="@android:drawable/ic_menu_call" />
<TextView
android:id="@+id/textView22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="联系人"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView33"
android:layout_width="match_parent"
android:layout_height="109dp"
android:layout_weight="1"
app:srcCompat="@android:drawable/ic_menu_compass" />
<TextView
android:id="@+id/textView33"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="位置"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout4"
android:layout_width="109dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView44"
android:layout_width="match_parent"
android:layout_height="109dp"
android:layout_weight="1"
app:srcCompat="@android:drawable/ic_menu_manage" />
<TextView
android:id="@+id/textView44"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="设置"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
-
android:orientation="horizontal"
: 这个属性用于设置LinearLayout
中子视图的排列方向。在这里,它被设置为 "horizontal",表示子视图将水平排列。 -
android:layout_weight="1"
: 这是内部LinearLayout
的属性之一,通常用于在布局中进行权重分配。在这里,它被设置为 "1",表示内部LinearLayout
在与其他视图共享空间时具有相等的权重。 -
android:orientation="vertical"
: 这个属性用于设置内部LinearLayout
中子视图的排列方向。在这里,它被设置为 "vertical",表示子视图将垂直排列。 -
ImageView
: 这是ImageView
元素的开始标签,表示一个图像视图。 -
android:id="@+id/imageView11"
: 这是ImageView
的属性之一,用于指定其唯一标识符,可以在代码中引用它。 -
android:layout_weight="1"
: 这是ImageView
的属性之一,通常用于在布局中进行权重分配,被设置为 "1",表示ImageView
在与其他视图共享空间时具有相等的权重。 -
app:srcCompat="@android:drawable/ic_menu_always_landscape_portrait"
: 这个属性用于设置ImageView
的图像资源。引用了一个Android内置的图像资源。
bottom.xml界面显示如下:
最后在main.xml整合,完成基础布局:
<?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/content1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</FrameLayout>
<include
android:id="@+id/content"
layout="@layout/bottom" />
</LinearLayout>
main.xml界面显示如下:
五、创建对应的java文件(Fragment类)
首先创建4个Frangment文件
因为Frangment文件代码类似,故只展示Fragment1代码:
package com.example.myfirstapp;
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) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.tab1, container, false);
}
}
-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
: 这是onCreateView
方法的开始。它是一个生命周期方法,用于创建和返回与Fragment
相关联的用户界面视图。 -
return inflater.inflate(R.layout.tab1, container, false)
: 这是onCreateView
方法的主体。它通过inflater
(一个用于将XML布局文件转换为视图对象的工具)加载了一个名为tab1
的布局文件,并将其与container
关联。
其次创建MainActivity文件,用以实现导航栏和Fragment布局的切换功能
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Fragment1 fragment;
Fragment fragment1,fragment2,fragment3,fragment4;
FragmentManager fm;
FragmentTransaction ft;
LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fragment1=new Fragment1();
fragment2=new Fragment2();
fragment3=new Fragment3();
fragment4=new Fragment4();
linearLayout1=findViewById(R.id.LinearLayout1);
linearLayout2=findViewById(R.id.LinearLayout2);
linearLayout3=findViewById(R.id.LinearLayout3);
linearLayout4=findViewById(R.id.LinearLayout4);
fm=getSupportFragmentManager();
initial();
fragmenthide();
fragmentshow(fragment1);
linearLayout1.setOnClickListener(this);
linearLayout2.setOnClickListener(this);
linearLayout3.setOnClickListener(this);
linearLayout4.setOnClickListener(this);
}
private void fragmenthide() {
FragmentTransaction ft=fm.beginTransaction()
.hide(fragment1)
.hide(fragment2)
.hide(fragment3)
.hide(fragment4);
ft.commit();
}
private void initial() {
FragmentTransaction ft=fm.beginTransaction()
.add(R.id.content1,fragment1)
.add(R.id.content1,fragment2)
.add(R.id.content1,fragment3)
.add(R.id.content1,fragment4);
ft.commit();
}
@Override
public void onClick(View view){
fragmenthide();
if(view.getId()==R.id.LinearLayout1){
fragmenthide();
fragmentshow(fragment1);
}
if(view.getId()==R.id.LinearLayout2){
fragmenthide();
fragmentshow(fragment2);
}
if(view.getId()==R.id.LinearLayout3){
fragmenthide();
fragmentshow(fragment3);
}
if(view.getId()==R.id.LinearLayout4){
fragmenthide();
fragmentshow(fragment4);
}
}
private void fragmentshow(Fragment fragment) {
FragmentTransaction transaction=fm.beginTransaction()
.show(fragment);
transaction.commit();
}
}
-
linearLayout1=findViewById(R.id.LinearLayout1);
,linearLayout2=findViewById(R.id.LinearLayout2);
,linearLayout3=findViewById(R.id.LinearLayout3);
,linearLayout4=findViewById(R.id.LinearLayout4);
: 这些行查找并获取XML布局文件中具有对应标识符的LinearLayout
视图,然后将它们分别分配给linearLayout1
、linearLayout2
、linearLayout3
和linearLayout4
变量。 -
fm=getSupportFragmentManager();
: 这一行获取FragmentManager
的实例,用于管理Fragment
的交互和显示。 -
initial();
: 这一行调用initial()
方法,它在布局中添加了四个Fragment
(Fragment1
、Fragment2
、Fragment3
和Fragment4
)。 -
fragmenthide();
: 这一行调用fragmenthide()
方法,它隐藏了所有的Fragment
。 -
fragmentshow(fragment1);
: 这一行调用fragmentshow()
方法,并显示Fragment1
,作为初始显示的Fragment
。 -
linearLayout1.setOnClickListener(this);
,linearLayout2.setOnClickListener(this);
,linearLayout3.setOnClickListener(this);
,linearLayout4.setOnClickListener(this);
: 这些行为linearLayout1
、linearLayout2
、linearLayout3
和linearLayout4
设置了点击监听器,当这些布局元素被点击时,onClick
方法会被调用。 -
private void fragmenthide() {
: 这是fragmenthide()
方法的开始,它用于隐藏所有的Fragment
。 -
FragmentTransaction ft=fm.beginTransaction()...
: 这一行开始了一个FragmentTransaction
事务,它用于执行界面操作。在这里,使用hide()
方法隐藏了四个不同的Fragment
:fragment1
、fragment2
、fragment3
和fragment4
。 -
ft.commit();
: 这一行提交了事务,使界面的隐藏操作生效。 -
private void initial() {
: 这是initial()
方法的开始,它用于初始化并添加四个不同的Fragment
到活动中。 -
FragmentTransaction ft=fm.beginTransaction()...
: 这一行开始了一个新的FragmentTransaction
事务,用于执行Fragment
操作。在这里,使用add()
方法将四个Fragment
添加到活动的布局中。 -
ft.commit();
: 这一行提交了事务,将Fragment
添加到活动的布局中。 -
public void onClick(View view)
: 这是onClick
方法的开始,它是一个点击事件处理方法。它会在用户点击布局元素时被调用,根据用户的点击来切换显示不同的Fragment
。 -
fragmenthide();
: 这一行再次调用fragmenthide()
方法,以确保在显示新的Fragment
之前隐藏所有其他Fragment
。 -
FragmentTransaction transaction=fm.beginTransaction()...
: 这一行开始了一个新的FragmentTransaction
事务,用于执行Fragment
操作。 -
transaction.commit();
: 这一行提交了事务,以显示特定的Fragment
。
最后结果如下:
六、实现列表效果
选择修改tab2页面:
在tab2页面加入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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="388dp"
android:layout_height="701dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
/>
</LinearLayout>
新建item.xml文件设计布局格式:
<?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/textView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textColor="@color/black"
android:textSize="35sp" />
</LinearLayout>
创建Myadapter类:
package com.example.myfirstapp;
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;
import java.util.zip.Inflater;
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);
Myholder myholder=new Myholder(view);
return myholder;
}
@Override
public void onBindViewHolder(@NonNull Myholder holder, int position) {
holder.textView.setText(list1.get(position));
}
@Override
public int getItemCount() {
return list1.size();
}
protected class Myholder extends RecyclerView.ViewHolder {
TextView textView;
public Myholder(@NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.textView6);
}
}
}
1. `onCreateViewHolder` 方法的参数 `parent` 是 `RecyclerView` 的父视图,`viewType` 是视图类型,通常在多视图类型的情况下使用。在这里,`viewType` 没有被使用。
2.`View view = LayoutInflater.from(context1).inflate(R.layout.item, parent, false);`:这一行通过从上下文 `context1` 中获取布局填充器,然后将 `item` 布局文件充气为视图对象,该视图将用作新的视图持有者的布局。
3.`Myholder myholder = new Myholder(view);`:接着,新的视图持有者对象 `myholder` 被创建,它将关联到视图对象 `view`。最后,`myholder` 对象被返回,表示新的视图持有者已创建。
4. `Myholder` 类包含一个 `TextView` 类型的成员变量 `textView`,用于引用 `item` 布局中的文本视图。
5.构造函数 `Myholder(@NonNull View itemView)` 接受一个视图 `itemView`,并调用父类的构造函数 `super(itemView)` 来设置视图持有者的根视图。
6.接着,通过 `itemView.findViewById(R.id.textView6)` 查找 `item` 布局中具有标识符 `R.id.textView6` 的文本视图,然后将它赋给 `textView` 成员变量,以便在 `onBindViewHolder` 方法中绑定数据到这个文本视图。
修改Fragment2文件:
package com.example.myfirstapp;
import android.annotation.SuppressLint;
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.*;
public class Fragment2 extends Fragment {
Context context;
List list;
RecyclerView recyclerView;
Myadapter myadapter;
@SuppressLint("MissingInflatedId")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tab2, container, false);
context = view.getContext();
recyclerView = view.findViewById(R.id.recycleView);
list = new ArrayList();
for(int i=0;i<9;i++){
list.add("这是第"+i+"行数据");}
myadapter = new Myadapter(context, list);
recyclerView.setAdapter(myadapter);
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
return view;
}
}
-
recyclerView = view.findViewById(R.id.recycleView);
: 这一行通过视图view
找到具有标识符R.id.recycleView
的RecyclerView
控件,并将其赋给recyclerView
变量,以便在后续操作中使用。 -
list = new ArrayList();
: 这一行创建一个名为list
的新数组列表(ArrayList
)以存储数据。这个列表将用于填充RecyclerView
。 -
myadapter = new Myadapter(context, list);
: 这一行创建了一个名为myadapter
的自定义适配器(Myadapter
),并将上下context
和数据列表list
传递给适配器。适配器用于将数据显示在RecyclerView
中。 -
recyclerView.setAdapter(myadapter);
: 这一行将适配器myadapter
设置为RecyclerView
的适配器,这将使RecyclerView
显示来自适配器的数据。 -
LinearLayoutManager manager = new LinearLayoutManager(context);
: 这一行创建了一个LinearLayoutManager
对象,用于配置RecyclerView
的布局管理器。这里的布局管理器是垂直线性布局管理器。 -
manager.setOrientation(LinearLayoutManager.VERTICAL);
: 这一行设置线性布局管理器的方向为垂直方向,以便RecyclerView
中的项目在垂直方向上排列。 -
recyclerView.setLayoutManager(manager);
: 这一行将线性布局管理器manager
分配给RecyclerView
,这将决定项目的排列方式。 -
return view;
: 这一行返回了包含RecyclerView
的视图,它将成为Fragment
2的用户界面。
最终结果:
七、作业总结
1、学会了如何使用AndroidStdio来完成一个app的门户框架,比如可以设计界面的xml文件,用于构建模块化的用户界面、在Android应用程序中可重用界面的组件Fragment。
2、还学会了如何在一个tab页面实现列表的效果,需要使用RecyclerView组件和自定义适配器Adapter来管理数据和视图之间的关联。
3、自定义了一个onclick函数,判断点击的view,然后执行view点击逻辑。但是判断view的id的switch-case报错需要常量表达式具体原因为:即使定义了变量为final,ADT or Gradle编译时也会将资源id(view id)做非final处理来避免项目资源id冲突,加快编译构建速度(取决于ADT和Gradle的版本)解决方式就是不用switch-case结构,用if-else结构。
八、gitee仓库链接
https://gitee.com/star-dawn/mobile-developmenthttps://gitee.com/star-dawn/mobile-development