首页 >> 大全

Android移动开发第一次作业

2023-12-14 大全 26 作者:考证青年

移动开发第一次作业

目录

一、作业目标

在本次作业中,我们需要设计和开发一个符合以下要求App的门户页面:

在其中一个标签页中展示一个列表效果 二、技术说明

在本次实验中,我们主要采用以下技术:

:官网指南 XML布局:官网指南 :官网指南 :官网指南 :官网指南 三、项目主要文件说明

Java文件说明: 布局文件说明: 四、关键代码解析 .xml

定义了一个包含底部导航栏的线性布局。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
······android:id="@+id/id_tab_home"android:layout_width="0dp"android:layout_height="match_parent"android:layout_gravity="center"android:layout_weight="1"android:gravity="center"android:orientation="vertical"><ImageViewandroid:id="@+id/tab_iv_home"android:layout_width="match_parent"android:layout_height="wrap_content"android:adjustViewBounds="true"android:scaleType="centerCrop"android:src="@mipmap/ml23_16_9_removebg_preview" /><TextViewandroid:id="@+id/text_home"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="首页"android:textSize="25sp" />LinearLayout><LinearLayout······android:text="探索" />······        LinearLayout><LinearLayout······   android:text="消息" />······        LinearLayout><LinearLayout······android:text="我的" />······LinearLayout>LinearLayout>

.xml

定义了一个使用作为根元素的布局。


<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:layout_width="match_parent"android:layout_height="match_parent"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recylerview"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />
androidx.constraintlayout.widget.ConstraintLayout>

.xml

这个布局的目的是在左侧显示一个列表,右侧显示对应列表项的图片。

通过:属性设置了宽度的比例,使得占据了父容器的2/9宽度,占据了父容器的7/9宽度。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:id="@+id/list_view"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="2"android:divider="@android:color/darker_gray"android:dividerHeight="1dp" /><ImageViewandroid:id="@+id/image_view"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="7"android:scaleType="fitCenter" />LinearLayout>

item.xml

创建一个水平布局,其中包含一个和一个。图像显示在左侧,而两个文本视图在图像的右侧,并且垂直居中对齐 一个垂直方向排列的,宽度为208dp,高度为。这个用于包含两个元素。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:id="@+id/imageView"android:layout_width="58dp"android:layout_height="match_parent"android:adjustViewBounds="true"android:scaleType="centerCrop"android:src="@mipmap/ml19" /><LinearLayoutandroid:layout_width="208dp"android:layout_height="match_parent"android:layout_gravity="center"android:layout_weight="1"android:gravity="center"android:orientation="vertical"><TextViewandroid:id="@+id/textView21"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:text="TextView"android:textColor="@color/black"android:textSize="20sp" /><TextViewandroid:id="@+id/textView22"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center|left"android:text="TextView"android:textColor="#48514D"android:textSize="30sp" />LinearLayout>LinearLayout>

top.xml


<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/linearLayout2"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#CA8C8C"><TextViewandroid:id="@+id/textView3"android:layout_width="0dp"android:layout_height="wrap_content"android:gravity="center"android:text="还没有想好名字"android:textColor="@color/white"android:textSize="30sp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />androidx.constraintlayout.widget.ConstraintLayout>

.xml

创建一个垂直布局,其中包含了一个顶部布局、一个内容显示区域()和一个底部布局。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><includeandroid:id="@+id/id_top"layout="@layout/top" /><FrameLayoutandroid:id="@+id/content1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="#ECEEEE">FrameLayout><includeandroid:id="@+id/id_buttom"layout="@layout/buttom" />LinearLayout>

_安卓开发作业源码_安卓开发作业

.java

该适配器类的功能是将数据从列表(list1和list2)绑定到的每个列表项中,以便在列表中显示相应的文本内容 ()方法:将数据绑定到中的视图元素上。 ()方法:返回列表(list1)的大小,即适配器应该显示的项数。类:自定义的内部类,继承自.。

package com.example.myapplicationhomework;
import ···public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Myholder> {List<String> list1, list2;Context context1;public MyAdapter(List list_1, List list_2, Context context) {list1 = list_1;list2 = list_2;context1 = context;}@NonNull@Overridepublic 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;}public void onBindViewHolder(@NonNull Myholder holder, int position) {holder.textView1.setText(list1.get(position));holder.textView2.setText(list2.get(position));}@Overridepublic int getItemCount() {return list1.size();}class Myholder extends RecyclerView.ViewHolder {TextView textView1, textView2;public Myholder(@NonNull View itemView) {super(itemView);textView1 = itemView.findViewById(R.id.textView21);textView2 = itemView.findViewById(R.id.textView22);}}
}

.java:

该类的主要功能是创建一个显示列表数据的,并将其与布局文件进行绑定 ()方法:这是一个自定义方法,用于为列表数据集合(list1和list2)添加数据。()方法:在视图被销毁时调用。

package com.example.myapplicationhomework;
import ···public class Fragment1 extends Fragment {private Fragment1Binding fragment1Binding;ArrayList<String> list1 = new ArrayList<>();ArrayList<String> list2 = new ArrayList<>();@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {fragment1Binding = Fragment1Binding.inflate(inflater, container, false);View view = fragment1Binding.getRoot();setList();MyAdapter myAdapter = new MyAdapter(list1, list2, view.getContext());fragment1Binding.recylerview.setAdapter(myAdapter);LinearLayoutManager manager = new LinearLayoutManager(view.getContext());manager.setOrientation(RecyclerView.VERTICAL);fragment1Binding.recylerview.setLayoutManager(manager);return view;}private void setList() {for (int i = 1; i <= 11; i++) {list1.add("六边形战士:马龙");}list2.add("2022世锦赛男团冠军");list2.add("2021东京奥运会男单冠军");list2.add("2022东京奥运会男团冠军");list2.add("2020世界乒联总决赛冠军");list2.add("2019世锦赛男单冠军");list2.add("2019世锦赛男双冠军");list2.add("2018世锦赛男团冠军");list2.add("2017世锦赛男单冠军");list2.add("2016世锦赛男团冠军");list2.add("2016巴西奥运会男单冠军");list2.add("2016巴西奥运会男团冠军");}@Overridepublic void onDestroyView() {super.onDestroyView();fragment1Binding = null;}
}

.java

主要功能是管理四个的显示和隐藏,并通过底部导航栏按钮切换不同的。 ()方法:用于显示指定的。()方法:用于初始化。()方法:用于隐藏所有的。()方法:底部导航栏按钮的点击事件。根据不同的点击事件,调用()方法显示相应的。

package com.example.myapplicationhomework;import ···
public class MainActivity1Binding extends AppCompatActivity implements View.OnClickListener {Fragment fragment1, fragment2, fragment3, fragment4;private ActivityMain1Binding activityMain1Binding;FragmentManager fragmentManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);activityMain1Binding = ActivityMain1Binding.inflate(getLayoutInflater());View view = activityMain1Binding.getRoot();setContentView(view);fragmentManager = getSupportFragmentManager();fragment1 = new Fragment1();fragment2 = new Fragment2();fragment3 = new Fragment3();fragment4 = new Fragment4();initial();fragmentshow(fragment1);activityMain1Binding.idButtom.idTabHome.setOnClickListener(this);activityMain1Binding.idButtom.idTabExplore.setOnClickListener(this);activityMain1Binding.idButtom.idTabMessage.setOnClickListener(this);activityMain1Binding.idButtom.idTabMy.setOnClickListener(this);}private void fragmentshow(Fragment fragment) {fragmenthide();FragmentTransaction ft = fragmentManager.beginTransaction().show(fragment);ft.commit();}private void initial() {FragmentTransaction ft = fragmentManager.beginTransaction().add(activityMain1Binding.content1.getId(), fragment1).add(activityMain1Binding.content1.getId(), fragment2).add(activityMain1Binding.content1.getId(), fragment3).add(activityMain1Binding.content1.getId(), fragment4);ft.commit();}private void fragmenthide() {FragmentTransaction ft = fragmentManager.beginTransaction().hide(fragment1).hide(fragment2).hide(fragment3).hide(fragment4);ft.commit();}@Overridepublic void onClick(View view) {if (view == activityMain1Binding.idButtom.idTabHome)fragmentshow(fragment1);else if (view == activityMain1Binding.idButtom.idTabExplore)fragmentshow(fragment2);else if (view == activityMain1Binding.idButtom.idTabMessage)fragmentshow(fragment3);else if (view == activityMain1Binding.idButtom.idTabMy)fragmentshow(fragment4);}
}

五、效果展示

六、完整源码地址

Gitee完整源码

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了