嘿,今天的你过的还好吗,今天分享复写RecyclerView的经历
根据官网可以看出 <灵活的视图,用于提供进入大型数据集的有限窗口。>
https://developer.android.google.cn/reference/android/support/v7/widget/RecyclerView
最开始就是引包,这个很简单,实在不会也可以看我之前文章
https://www.jianshu.com/p/a3c38e6ee0e5
然后开动,最重要的还是先引 一个组件在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"
/>
有了xml文件之后,我们需要一个将他的bean写出来
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;
}
}
那bean都有了我们还要一个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="血压:"
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>
<-这是圆形图片 ->
//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>
有了bean之后需要有一个adapter,那复写一个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);
}
}
}
最后就是直接在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);
//实例化
initview();
//网络操作
netWork();
//数据层
datalayer();
//点击事件
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+" 月: "+month+" 日:"+day+" 时: "+hour+" 分: "+min+" 秒: "+second +" 是上午吗? "+isAm+" 是24小时制吗? "+is24);
bp_shijian.setText(month + "-" + day + " " + hour + ":" + min);
}
}
这里我直接把整个代码贴出来了,方便自己日后复盘使用.
网友评论