Android开发 Fragment与RecyclerView的混合使用

Fragment与RecyclerView的混合使用
这篇是参考了这位仁兄的文章。所写的笔记;
要想使用Fragment与RecylerView的结合,首先知道Fragment是嵌入在Activity中的,而RecylerView是一个组件,它则嵌入在Fragment只有了解他们的关系才能更好的配置他们的代码:

  1. MainActivity.java(主要Activity的逻辑类)
  2. BlankFragment.java(嵌入在Activity的Fragment的逻辑类)
  3. RecyclerView_Myadapter.java(嵌入在Fragment的ReclyerView逻辑类)
  4. Bean.java(存储数据类)
  5. activity_main.xml(主要Activity的布局文件)
  6. fragemt_blank.xml(Fragment的布局文件)
  7. item_layout.xml(ReclyerView单个空格的布局文件)

Android开发 Fragment与RecyclerView的混合使用_第1张图片

MainActivity.java

package com.example.fragment;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import java.util.ArrayList;

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

BlankFragment.java

package com.example.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.RecoverySystem;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;


public class BlankFragment extends Fragment {
    private View root;
    private TextView textview;
    private Button button;
    private ArrayList<Bean> beanArrayList = new ArrayList<>();
    private RecyclerView recyclerview;
    private RecyclerView_Myadpter recyclerview_adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    //创建Fragemnt视图的方法
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (root == null){
            //获得布局文件
            root = inflater.inflate((R.layout.fragment_blank),container, false);
        }
        //初始化RecylerView组件的方法
        initRecyclerview();
        //初始化数据
        initData();
//        //这个部分是用于设置Fragemnt布局文件中控件的点击反应的*********************************************
//        textview = root.findViewById(R.id.tv);
//        button = root.findViewById(R.id.but);
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                textview.setText("修改");
//            }
//        });
//        //******************************************
        //返回设置完成的View文件用于显示
        return  root;
    }

    private void initData() {
        for (int i=0;i<30;i++){
            Bean bean = new Bean();
            bean.setId("ID:"+i);
            beanArrayList.add(bean);
        }
    }

    private void initRecyclerview() {
        //获得Recyclerview
        recyclerview = (RecyclerView)root.findViewById(R.id.recyclerview);
        //创建adapter类的对象
        recyclerview_adapter = new RecyclerView_Myadpter(getActivity(), beanArrayList);
        //将对象作为参数通过setAdapter方法设置给recylerview;
        recyclerview.setAdapter(recyclerview_adapter);
        //这步骤必须有,这是选择RecylerView的显示方式
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
    }
}

RecyclerView_Myadapter.java

package com.example.fragment;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class RecyclerView_Myadpter extends RecyclerView.Adapter<RecyclerView_Myadpter.myViewHodler> {
    private Context context;
    private ArrayList<Bean> beanArrayList;

    public RecyclerView_Myadpter(Context context, ArrayList<Bean> beanArrayList) {
        this.context = context;
        this.beanArrayList = beanArrayList;
    }

    @NonNull
    @Override
    public myViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //item_layout是Reclyerview每个格子的布局
        //获取recylerview格子的样式
        View itemview = View.inflate(context,R.layout.item_layout,null);
        //返回根据myViewHodler()处理完的新View
        return new myViewHodler(itemview);
    }

    @Override
    public void onBindViewHolder(@NonNull myViewHodler holder, int position) {
        Bean data = beanArrayList.get(position);
        holder.text.setText(data.getId());
    }

    @Override
    public int getItemCount() {
        return beanArrayList.size();
    }
    //用于根据布局文件生成View
    public class myViewHodler extends RecyclerView.ViewHolder{
        private TextView text;
        public myViewHodler(View itemview) {
            super(itemview);
            text = (TextView)itemview.findViewById(R.id.item_tv);
        }
    }
}

Bean.java

package com.example.fragment;

public class Bean {
    private String id;

    public Bean(String id) {
        this.id = id;
    }

    public Bean() {

    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

activity_main.xml


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <fragment
        android:name="com.example.fragment.BlankFragment"
        android:layout_width="200dp"
        android:layout_height="400dp"
        android:layout_gravity="center_horizontal"
        android:id="@+id/fg"/>





LinearLayout>

fragemt_blank.xml


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

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

item_layout.xml


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/item_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />

androidx.constraintlayout.widget.ConstraintLayout>

你可能感兴趣的:(android,java,android,studio)