Android Studio 学习记录:第四天

一、回顾与总结
在前三天的学习中,我逐步掌握了 Android Studio 的项目结构、布局设计(线性布局、相对布局、ConstraintLayout)、基本 UI 组件的使用(TextView、Button、EditText、ImageView)、事件处理机制以及简单的数据存储(SharedPreferences)。这些知识让我对 Android 开发有了更深入的理解,并能够实现简单的交互功能。今天,我将继续深入学习 Android 开发的核心内容,包括 Fragment 的使用和 RecyclerView 的实现。


二、学习目标

• 深入学习 Fragment 的使用:掌握 Fragment 的生命周期、创建和管理方法,以及与 Activity 的交互。

• 学习 RecyclerView 的使用:掌握 RecyclerView 的基本用法,实现列表数据的动态显示。

• 理解 Fragment 和 RecyclerView 的结合使用:通过实际案例,实现一个包含 Fragment 和 RecyclerView 的简单应用。


三、Fragment 的使用

(一)Fragment 的概念
Fragment 是一种可以在 Activity 中复用的 UI 片段,类似于一个“迷你 Activity”。它可以有自己的布局和生命周期,并且可以与 Activity 交互。

(二)Fragment 的生命周期
Fragment 的生命周期与 Activity 类似,但更加复杂。主要生命周期方法包括:

onAttach(Context context):Fragment 与 Activity 关联时调用。

onCreate(Bundle savedInstanceState):创建 Fragment 时调用。

onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState):创建 Fragment 的视图时调用。

onActivityCreated(Bundle savedInstanceState):Activity 的onCreate()方法执行完成后调用。

onStart()onResume()onPause()onStop()onDestroyView()onDestroy()onDetach():与 Activity 的生命周期方法类似。

(三)创建 Fragment

• 创建 Fragment 类:

   public class MyFragment extends Fragment {
       @Nullable
       @Override
       public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
           // 加载布局文件
           return inflater.inflate(R.layout.fragment_my, container, false);
       }
   }

• 创建 Fragment 的布局文件:

   <!-- res/layout/fragment_my.xml -->
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:padding="16dp">

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="This is a Fragment" />
   </LinearLayout>

• 在 Activity 中添加 Fragment:

• 静态添加:通过 XML 布局文件直接添加。

     <fragment
         android:name="com.example.MyFragment"
         android:id="@+id/my_fragment"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
     ```



• 动态添加:通过代码动态添加 Fragment。

```java
     FragmentManager fragmentManager = getSupportFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
     MyFragment myFragment = new MyFragment();
     fragmentTransaction.add(R.id.fragment_container, myFragment);
     fragmentTransaction.commit();
     ```



• Fragment 与 Activity 的交互:

• Fragment 可以通过`getActivity()`方法获取与之关联的 Activity。

• Activity 可以通过`FragmentManager`获取 Fragment 的实例。


(四)今日收获

• 理解了 Fragment 的概念和生命周期。

• 掌握了 Fragment 的创建方法,包括静态添加和动态添加。

• 学会了 Fragment 与 Activity 的交互方式。


---



四、RecyclerView 的使用


(一)RecyclerView 的概念
RecyclerView 是 Android 中用于显示列表数据的组件,它比传统的`ListView`更灵活、更高效,支持多种布局方式(如线性布局、网格布局等)。


(二)RecyclerView 的基本使用

• 添加依赖:
在`build.gradle`文件中添加 RecyclerView 的依赖:

```gradle
   implementation 'androidx.recyclerview:recyclerview:1.3.0'

• 创建 RecyclerView 的布局:

   <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/recyclerView"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

• 创建适配器(Adapter):
适配器用于将数据绑定到 RecyclerView 的每个条目中。

   public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
       private List<String> dataList;

       public MyAdapter(List<String> dataList) {
           this.dataList = dataList;
       }

       @NonNull
       @Override
       public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
           View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
           return new ViewHolder(view);
       }

       @Override
       public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
           holder.textView.setText(dataList.get(position));
       }

       @Override
       public int getItemCount() {
           return dataList.size();
       }

       public static class ViewHolder extends RecyclerView.ViewHolder {
           TextView textView;

           public ViewHolder(@NonNull View itemView) {
               super(itemView);
               textView = itemView.findViewById(R.id.textView);
           }
       }
   }

• 创建条目布局文件:

   <!-- res/layout/item_layout.xml -->
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:padding="16dp">

       <TextView
           android:id="@+id/textView"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Item" />
   </LinearLayout>

• 在 Activity 中设置 RecyclerView:

   public class MainActivity extends AppCompatActivity {
       private RecyclerView recyclerView;
       private MyAdapter adapter;
       private List<String> dataList;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);

           recyclerView = findViewById(R.id.recyclerView);
           recyclerView.setLayoutManager(new LinearLayoutManager(this));

           dataList = new ArrayList<>();
           for (int i = 0; i < 50; i++) {
               dataList.add("Item " + i);
           }

           adapter = new MyAdapter(dataList);
           recyclerView.setAdapter(adapter);
       }
   }

(三)今日收获

• 理解了 RecyclerView 的基本概念和优势。

• 掌握了 RecyclerView 的基本使用方法,包括适配器的创建和数据绑定。

• 学会了如何实现简单的列表显示。


五、Fragment 和 RecyclerView 的结合使用
为了巩固所学知识,我尝试开发了一个简单的应用,该应用包含一个 Fragment,Fragment 中使用 RecyclerView 显示列表数据。

(一)项目结构

MainActivity:主 Activity,用于加载 Fragment。

MyFragment:包含 RecyclerView 的 Fragment。

MyAdapter:RecyclerView 的适配器。

• 布局文件:activity_main.xmlfragment_my.xmlitem_layout.xml

(二)实现代码

MainActivity

   public class MainActivity extends AppCompatActivity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);

           if (savedInstanceState == null) {
               getSupportFragmentManager().beginTransaction()
                   .add(R.id.fragment_container, new MyFragment())
                   .commit();
           }
       }
   }

MyFragment

   public class MyFragment extends Fragment {
       private RecyclerView recyclerView;
       private MyAdapter adapter;
       private List<String> dataList;

       @Nullable
       @Override
       public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
           View view = inflater.inflate(R.layout.fragment_my, container, false);

           recyclerView = view.findViewById(R.id.recyclerView);
           recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

           dataList = new ArrayList<>();
           for (int i = 0; i < 50; i++) {
               dataList.add("Item " + i);
           }

           adapter = new MyAdapter(dataList);
           recyclerView.setAdapter(adapter);

           return view;
       }
   }

• 布局文件:

activity_main.xml

     <FrameLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/fragment_container"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
     ```



• `fragment_my.xml`:

```xml
     <androidx.recyclerview.widget.RecyclerView
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/recyclerView"
         android:layout_width="match_parent"
         android:layout_height="match_parent
posted @ 2025-03-16 13:00  马瑞鑫03  阅读(25)  评论(0)    收藏  举报