前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android笔记】 RecyclerView

【Android笔记】 RecyclerView

原创
作者头像
程序员小何SS
发布2021-12-17 14:32:20
4110
发布2021-12-17 14:32:20
举报
文章被收录于专栏:Android理论

布局

代码语言:txt
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context=".BarJudgeActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:text="textView1"
        />
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerView">

    </android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

子布局

bar_bundle_item.xml

代码语言:txt
复制
<?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="wrap_content"
    android:orientation="vertical">
    <!--包装记录ID-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/perfPackId1"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />
        <TextView
            android:id="@+id/perfPackId2"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />
    </LinearLayout>
    <!--钢种-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/steelGrade1"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />
        <TextView
            android:id="@+id/steelGrade2"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />
    </LinearLayout>
    <!--包装根数-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/packRootCount1"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />
        <TextView
            android:id="@+id/packRootCount2"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />
    </LinearLayout>

</LinearLayout>
代码语言:txt
复制
package com.example.meng.mes;


public class BarJudgeActivity extends AppCompatActivity {


    //捆号信息list
    private List<BundleCodeInfoModel> bundleCodeInfoModelList = new ArrayList<>();

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

        //初始化数据
        initBundleCodeInfo();
        //通过findViewById拿到RecyclerView实例
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        //设置RecyclerView管理器
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //初始化适配器
        MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(bundleCodeInfoModelList);
        //设置适配器
        recyclerView.setAdapter(adapter);

    }
    
    private void initBundleCodeInfo()
    {
        BundleCodeInfoModel a1=new BundleCodeInfoModel(1,"50#",15);
        BundleCodeInfoModel a2=new BundleCodeInfoModel(1,"50#",15);
        BundleCodeInfoModel a3=new BundleCodeInfoModel(1,"50#",15);
        bundleCodeInfoModelList.add(a1);
        bundleCodeInfoModelList.add(a2);
        bundleCodeInfoModelList.add(a3);
    }
}

MyRecyclerViewAdapter.cs

代码语言:txt
复制
package com.example.meng.mes;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>  {

    private List<BundleCodeInfoModel> mBundleCodeInfoModel;

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView perfPackId1;
        TextView perfPackId2;
        TextView steelGrade1;
        TextView steelGrade2;
        TextView packRootCount1;
        TextView packRootCount2;
        public ViewHolder(View view){
            super(view);
            perfPackId1 =view.findViewById(R.id.perfPackId1);
            perfPackId2 =view.findViewById(R.id.perfPackId2);
            steelGrade1 =view.findViewById(R.id.steelGrade1);
            steelGrade2 =view.findViewById(R.id.steelGrade2);
            packRootCount1 =view.findViewById(R.id.packRootCount1);
            packRootCount2 =view.findViewById(R.id.packRootCount2);
        }
    }
    public MyRecyclerViewAdapter (List<BundleCodeInfoModel> BundleCodeInfoModel) {
        mBundleCodeInfoModel = BundleCodeInfoModel;
    }

    //创建view
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.bar_bundle_item, viewGroup, false);
        ViewHolder holder = new  ViewHolder(view);
        return holder;
    }
     //绑定数据
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        BundleCodeInfoModel bundleCodeInfoModel = mBundleCodeInfoModel.get(i);
        viewHolder.perfPackId1.setText("包装记录id:");
        viewHolder.perfPackId2.setText(String.valueOf(bundleCodeInfoModel.getPerfPackId()));
        viewHolder.steelGrade1.setText("钢种:");
        viewHolder.steelGrade2.setText(String.valueOf(bundleCodeInfoModel.getSteelGrade()));
        viewHolder.packRootCount1.setText("包装根数:");
        viewHolder.packRootCount2.setText(String.valueOf(bundleCodeInfoModel.getPackRootCount()));
    }

    //获得总条数
    @Override
    public int getItemCount() {
        return mBundleCodeInfoModel.size();
    }
}
代码语言:txt
复制
layoutManager.setStackFromEnd(true);//列表再底部开始展示,反转后由上面开始展示
layoutManager.setReverseLayout(true);//列表翻转

实现Item添加和删除

颜色不准

使用onBindViewHolder方法根据每个model的信息显示不同的背景色,发现背景色乱添加,并不是自己想要的

代码语言:txt
复制
    public void onBindViewHolder(@NonNull BundlePhysicalRecAdapter.ViewHolder viewHolder, int i) {
        BundlePhysicalInfoModel model = mBundlePhysicalInfoModelList.get(i);
        viewHolder.INSP_VALUE_NAME.setText(model.INSP_VALUE_NAME);
        xxx
        viewHolder.JUDGE_RESULT.setText(model.JUDGE_RESULT);
        if ("不合格".equals(model.JUDGE_RESULT)) {
               viewHolder.PhysicalItemLayout.setBackgroundColor(Color.RED);
        }
    }        

只有第一个model没问题,之后就乱了

正确的做法是重新获取int的位置

代码语言:txt
复制
    @Override
    public int getItemViewType(int position) {
        return position;
    }
代码语言:txt
复制
BundlePhysicalInfoModel model = mBundlePhysicalInfoModelList.get(getItemViewType(i));

结语:

后续会持续更新哦

相关视频

Android知识理论

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档