【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>
    <!--包裝根數(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知識】

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末馅袁,一起剝皮案震驚了整個濱河市汗销,隨后出現(xiàn)的幾起案子大溜,更是在濱河造成了極大的恐慌钦奋,老刑警劉巖付材,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異富寿,居然都是意外死亡页徐,警方通過查閱死者的電腦和手機变勇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來链患,“玉大人锣险,你說我怎么就攤上這事芯肤⊙伦桑” “怎么了署拟?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長馒铃。 經(jīng)常有香客問我区宇,道長议谷,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上昼钻,老公的妹妹穿的比我還像新娘。我一直安慰自己碗淌,他們只是感情好亿眠,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著憔购,像睡著了一般。 火紅的嫁衣襯著肌膚如雪屎飘。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天,我揣著相機與錄音怨规,去河邊找鬼。 笑死掰烟,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播先馆,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼铣减!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起魄梯,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤魏烫,失蹤者是張志新(化名)和其女友劉穎稀蟋,沒想到半個月后退客,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體萌狂,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡霹琼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了泊窘。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瓜贾。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖胃夏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情答恶,我是刑警寧澤裕坊,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布映企,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏得问。R本人自食惡果不足惜囤攀,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望宫纬。 院中可真熱鬧焚挠,春花似錦、人聲如沸漓骚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至噩斟,卻和暖如春曹锨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背剃允。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工沛简, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人斥废。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓椒楣,卻偏偏與公主長得像,于是被迫代替她去往敵國和親营袜。 傳聞我的和親對象是個殘疾皇子撒顿,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容