移动开发-app

一、作业目标

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>

1. android:layout_width="wrap_content": 这个属性定义了视图的宽度。在这里,它被设置为 "wrap_content",表示视图的宽度将根据其内容自动调整,以适应文本内容的宽度。

2. android:layout_height="wrap_content": 这个属性定义了视图的高度。同样,它被设置为 "wrap_content",表示视图的高度将根据其内容自动调整,以适应文本内容的高度。

3. android:layout_weight="1": 这个属性通常用于在布局中进行权重分配。在这里,它被设置为 "1",表示这个视图在与其他视图共享空间时具有相等的权重,

4. android:background="@color/black": 这个属性用于设置视图的背景颜色。它引用了一个颜色资源,@color/black,表示该视图的背景颜色将是黑色。

5. android:gravity="center": 这个属性定义了文本在视图内的对齐方式,被设置为 "center",表示文本将水平和垂直居中对齐。

6. android:text="微信": 这个属性用于设置视图中显示的文本内容。文本内容被设置为 "微信",即视图将显示 "微信" 这个文本。

7. android:textColor="@color/white": 这个属性用于设置文本的颜色。它引用了一个颜色资源,@color/white,表示文本颜色将是白色。

8. 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>

1. android:layout_width="wrap_content": 这是 TextView 的属性之一,用于定义其宽度。在这里,它被设置为 "wrap_content",表示 TextView 的宽度将根据其文本内容自动调整。

2. android:layout_height="wrap_content": 这是 TextView 的属性之一,用于定义其高度。它被设置为 "wrap_content",表示 TextView 的高度将根据其文本内容自动调整。

3. android:layout_gravity="center": 这是 TextView 的属性之一,用于定义 TextView 在其父容器中的对齐方式,设置为 "center",表示文本将在父容器中水平和垂直居中对齐。

4. android:layout_weight="1": 这是 TextView 的属性之一,通常用于在布局中进行权重分配。在这里,它被设置为 "1",表示 TextView 在与其他视图共享空间时具有相等的权重。

5. android:gravity="center": 这是 TextView 的属性之一,用于定义文本在 TextView 内的对齐方式。在这里,它被设置为 "center",表示文本将水平和垂直居中对齐。

6. android:text="这是聊天界面": 这是 TextView 的属性之一,用于设置 TextView 中显示的文本内容

7. 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();
    }
}

1. 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 变量。

2. fm=getSupportFragmentManager();: 这一行获取 FragmentManager 的实例,用于管理Fragment的交互和显示。

3. initial();: 这一行调用 initial() 方法,它在布局中添加了四个Fragment(Fragment1、Fragment2、Fragment3 和 Fragment4)。

4. fragmenthide();: 这一行调用 fragmenthide() 方法,它隐藏了所有的Fragment。

5. fragmentshow(fragment1);: 这一行调用 fragmentshow() 方法,并显示 Fragment1,作为初始显示的Fragment。

6. linearLayout1.setOnClickListener(this);, linearLayout2.setOnClickListener(this);, linearLayout3.setOnClickListener(this);, linearLayout4.setOnClickListener(this);: 这些行为 linearLayout1、linearLayout2、linearLayout3 和 linearLayout4 设置了点击监听器,当这些布局元素被点击时,onClick 方法会被调用。

7. private void fragmenthide() {: 这是 fragmenthide() 方法的开始,它用于隐藏所有的Fragment。

8. FragmentTransaction ft=fm.beginTransaction()...: 这一行开始了一个 FragmentTransaction 事务,它用于执行界面操作。在这里,使用 hide() 方法隐藏了四个不同的Fragment:fragment1、fragment2、fragment3 和 fragment4。

9. ft.commit();: 这一行提交了事务,使界面的隐藏操作生效。

10. private void initial() {: 这是 initial() 方法的开始,它用于初始化并添加四个不同的Fragment到活动中。

11. FragmentTransaction ft=fm.beginTransaction()...: 这一行开始了一个新的 FragmentTransaction 事务,用于执行Fragment操作。在这里,使用 add() 方法将四个Fragment添加到活动的布局中。

12. ft.commit();: 这一行提交了事务,将Fragment添加到活动的布局中。

13. public void onClick(View view): 这是 onClick 方法的开始,它是一个点击事件处理方法。它会在用户点击布局元素时被调用,根据用户的点击来切换显示不同的Fragment。

14. fragmenthide();: 这一行再次调用 fragmenthide() 方法,以确保在显示新的Fragment之前隐藏所有其他Fragment。

15. FragmentTransaction transaction=fm.beginTransaction()...: 这一行开始了一个新的 FragmentTransaction 事务,用于执行Fragment操作。

16. 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 的视图,它将成为Fragment2的用户界面。
 

最后结果:

总结:

通过这次实践,我收获了些许感想。首先,门户框架的设计是关键。作为一个社交类应用,微信的门户框架需要具有良好的用户界面和交互设计。在设计过程中,我注意到底部导航栏、顶部搜索栏和中间的功能页是常见的设计模式。底部导航栏方便用户快速切换功能页,搜索栏则提供了便捷的搜索操作。

其次,实现列表效果需要使用合适的控件和技术。在我的实践中,我选择使用 RecyclerView 控件来展示列表数据。RecyclerView 提供了灵活的布局管理和动画效果,能够高效地处理大量的数据项。同时,为了与列表数据进行绑定和管理,我使用了适配器模式来创建 RecyclerView 的适配器。

在列表项的设计上,我注重了用户体验。根据微信的样式和需求,我设计了列表项的布局,包括头像、昵称、消息内容和时间等元素。我还使用数据绑定技术,将列表数据和布局进行绑定,实现动态的数据展示效果。

gitee 仓库:

翁梓策/666

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值