【Android笔记】 RecyclerView

布局






    
    

    


子布局

bar_bundle_item.xml



    
    
        
        
    
    
    
        
        
    
    
    
        
        
    





package com.example.meng.mes;


public class BarJudgeActivity extends AppCompatActivity {


    //捆号信息list
    private List 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

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  {

    private List 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) {
        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();
    }
}




layoutManager.setStackFromEnd(true);//列表再底部开始展示,反转后由上面开始展示
layoutManager.setReverseLayout(true);//列表翻转

实现Item添加和删除

颜色不准

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

    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的位置

    @Override
    public int getItemViewType(int position) {
        return position;
    }
BundlePhysicalInfoModel model = mBundlePhysicalInfoModelList.get(getItemViewType(i));

结语:
后续会持续更新哦,喜欢的话点赞关注一下吧!
Android知识相关视频
【Android知识】

你可能感兴趣的:(【Android笔记】 RecyclerView)