Android replication recyclerview

Time:2021-11-27
Hey, how are you today? Today, share the experience of copying recyclerview

According to the official website, it can be seen that < flexible view is used to provide a limited window into large data sets. >
https://developer.android.google.cn/reference/android/support/v7/widget/RecyclerView

The first is the introduction package. This is very simple. You can also see my previous articles
https://www.jianshu.com/p/a3c38e6ee0e5

Then start, the most important thing is to first introduce a component in XML

<androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/bp_baise"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="30dp"
        android:id="@+id/bp_rv"
        />

Once we have the XML file, we need a bean to write it out

package com.example.new_home_acyivity.bean;

public class BPBean {
    private String xueyatime;
    private String xueyaValue;

    public String getXueyatime() {
        return xueyatime;
    }

    public void setXueyatime(String xueyatime) {
        this.xueyatime = xueyatime;
    }

    public String getXueyaValue() {
        return xueyaValue;
    }

    public void setXueyaValue(String xueyaValue) {
        this.xueyaValue = xueyaValue;
    }
}

We also need a layout file of recyclerview

<?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="73dp"
    android:gravity="center"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/xueyatime_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="11-19 12:36:56"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:layout_marginRight="30dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                Android: text = "blood pressure:"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/xueya_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:text="98%"
                android:textColor="#000000" />
        </LinearLayout>

        < - this is a circular picture - >
        //implementation 'de.hdodenhof:circleimageview:3.1.0'
        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/headportrait_cimg"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="30dp"
            android:src="@color/btncolor2" />

    </RelativeLayout>


</LinearLayout>

After having a bean, you need to have an adapter. Copy an adapter

package com.example.new_home_acyivity.adapter;

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 com.example.gymsysandroid.R;
import com.example.new_home_acyivity.bean.BPBean;

import java.util.List;

public class BPAdapter extends RecyclerView.Adapter<BPAdapter.ViewHolder> {
    private Context context;
    private List<BPBean> listBP;

    public BPAdapter(Context context, List<BPBean> listBP) {
        this.context = context;
        this.listBP = listBP;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.xueyatime_tv.setText(listBP.get(position).getXueyatime());
        holder.xueya_tv.setText(listBP.get(position).getXueyaValue());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView xueyatime_tv;
        private TextView xueya_tv;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            xueyatime_tv = itemView.findViewById(R.id.xueyatime_tv);
            xueya_tv = itemView.findViewById(R.id.xueya_tv);
        }
    }
}

Finally, it is directly referenced in the activity

package com.example.new_home_acyivity;

public class BPAcyivity extends BaseActivity {
    private Calendar calendars;

    private Button bp_daiciceliang;
    private Button bp_shishiceliang;
    private TextView bp_shijian;
    private RecyclerView bp_rv;
    private List<BPBean> mBPBeanList = new ArrayList<>();
    private BPAdapter mBPAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_b_p);
        manager = CommandManager.getInstance(this);
        //Instantiation
        initview();
        //Network operation
        netWork();
        //Data layer
        datalayer();
        //Click event
        clicksed();
    }

    private void netWork() {
        MediaType JSON = MediaType.parse("application/json;charset=utf-8");;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("LoginKey", "....");
            jsonObject.put("SystemID", ".");
            jsonObject.put("AppType", "..");
            jsonObject.put("MemberID", ".");
            jsonObject.put("Type", ".");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(JSON,String.valueOf(jsonObject));
        okHttpUtil.postOkhttpRequest(url,requestBody, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.i("ryanliu", "onFailure: " + e.toString());
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String reposes = response.body().string();
                Log.i("ryanliu",reposes + "");
                try {
                    JSONObject jsonObject = new JSONObject(reposes);
                    String data = jsonObject.optString("Data");
                    JSONObject jsonObject1 = new JSONObject(data);
                    String healthIfno = jsonObject1.optString("HealthIfno");
                    JSONArray jsonArray=new JSONArray(healthIfno);
                    for (int i = 0;i<jsonArray.length();i++){
                        jsonObject1=new JSONObject(jsonArray.get(i).toString());
                        Log.i("ryanliu", String.valueOf(jsonObject1));
                        BPBean b = new BPBean();
                        b.setXueyatime(jsonObject1.getString("AddTime"));
                        b.setXueyaValue(jsonObject1.getString("MeasureValue"));
                        mBPBeanList.add(b);
                    }
                    handler.sendEmptyMessage(1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

  
    }

    private void datalayer() {
//        for (int i = 0; i < 10; i++){
//            BPBean bpBean = new BPBean();
//            bpBean.setXueyatime("11-19 12:36:" + i);
//            bpBean.setXueyaValue(i + "/222");
//            mBPBeanList.add(bpBean);
//        }
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what==1){
                    mBPAdapter.notifyDataSetChanged();
                }
            }
        };

        mBPAdapter = new BPAdapter(this,mBPBeanList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        bp_rv.setLayoutManager(linearLayoutManager);
        bp_rv.setAdapter(mBPAdapter);
    }

    private void initview() {
        bp_rv = findViewById(R.id.bp_rv);
        bp_shijian = findViewById(R.id.bp_shijian);
        bp_shishiceliang = findViewById(R.id.bp_shishiceliang);
        bp_daiciceliang = findViewById(R.id.bp_daiciceliang);


        calendars = Calendar.getInstance();
        calendars.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String year = String.valueOf(calendars.get(Calendar.YEAR));
        String month = String.valueOf(calendars.get(Calendar.MONTH));
        String day = String.valueOf(calendars.get(Calendar.DATE));
        String hour = String.valueOf(calendars.get(Calendar.HOUR));
        String min = String.valueOf(calendars.get(Calendar.MINUTE));
        String second = String.valueOf(calendars.get(Calendar.SECOND));
        Boolean isAm = calendars.get(Calendar.AM_PM) == 1 ? true : false;
        Boolean is24 = DateFormat.is24HourFormat(this) ? true : false;
        //Log. I ("MD", "year:" + year + "month:" + month + "day:" + day + "hour:" + hour + "minute:" + min + "second +" is it morning? "+ ISAM +" is it 24-hour system? "+ is24);
        bp_shijian.setText(month + "-" + day + " " + hour + ":" + min);
    }
}

Here I directly posted the whole code, which is convenient for me to use again in the future

No updates or frequent updates are serious life