布局
<?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>
<!--包裝根數(shù)-->
<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);
//初始化數(shù)據(jù)
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;
}
//創(chuàng)建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;
}
//綁定數(shù)據(jù)
@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("包裝根數(shù):");
viewHolder.packRootCount2.setText(String.valueOf(bundleCodeInfoModel.getPackRootCount()));
}
//獲得總條數(shù)
@Override
public int getItemCount() {
return mBundleCodeInfoModel.size();
}
}
layoutManager.setStackFromEnd(true);//列表再底部開始展示蓝翰,反轉(zhuǎn)后由上面開始展示
layoutManager.setReverseLayout(true);//列表翻轉(zhuǎn)
實現(xiàn)Item添加和刪除
顏色不準
使用onBindViewHolder方法根據(jù)每個model的信息顯示不同的背景色诞帐,發(fā)現(xiàn)背景色亂添加停蕉,并不是自己想要的
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));
結(jié)語:
后續(xù)會持續(xù)更新哦,喜歡的話點贊關注一下吧灿意!
Android知識相關視頻
【Android知識】