android RecyclerView

 

布局

<?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

<?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>

 

 

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

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

 

 

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));

 

posted @ 2018-12-12 16:59  富坚老贼  阅读(140)  评论(0编辑  收藏  举报