iT邦幫忙

2021 iThome 鐵人賽

DAY 12
0
Mobile Development

刮掉Web Development的我,與撿到的Android Development系列 第 12

[Lesson12] RecyclerView

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rec"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

recyclerview_row:

<?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="100dp"
    android:orientation="horizontal"
    android:padding="10dp">

    <TextView
        android:id="@+id/animalText"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:textSize="20sp"/>
</LinearLayout>

MyRecyclerViewAdapter:

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

    private List<String> mDataList;
    private LayoutInflater mInflater;

    // context跟arrayList就是透過這個傳進來
    MyRecyclerViewAdapter(Context context, List<String> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mDataList = data;
    }

    // 用inflater載入recyclerview_row,並傳入viewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
        return new ViewHolder(view);
    }

    // 尋找textView的Id
    public class ViewHolder extends RecyclerView.ViewHolder  {
        TextView animalText;

        ViewHolder(View itemView) {
            super(itemView);
            animalText = itemView.findViewById(R.id.animalText);
        }
    }

    // 用viewHolder綁定資料
    // position是mDataList的第幾個
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String s = mDataList.get(position);
        holder.animalText.setText(s);
    }

    // 確定mDataList有幾個
    @Override
    public int getItemCount() {
        return mDataList.size();
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity  {
    MyRecyclerViewAdapter adapter;

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

        // 要傳入recycleView的資料
        String[] animal = {"cat","dog","horse","sheep"};
        ArrayList<String> arrayList = new ArrayList<>();
        for (String str:animal) {
            arrayList.add(str);
        }

        // 先用ID尋找哪個是你recycleView的ID
        RecyclerView rec = findViewById(R.id.rec);

        //設定排序用LinearLayout的排法
        rec.setLayoutManager(new LinearLayoutManager(this));

        //this是你現在在哪個activity(context)
        //arrayList長度是4,畫面就會顯示4個recyclerview_row.xml
        adapter = new MyRecyclerViewAdapter(this, arrayList);

        //接下來設定recyclview要綁上的是剛宣告的adapter,adapter的作用是處理那4筆資料該如何顯示
        rec.setAdapter(adapter);
    }
}

謝謝大家願意花時間閱讀,小弟弟我在此鞠躬/images/emoticon/emoticon41.gif


上一篇
[Lesson11] SQLite
下一篇
[Lesson13] OkHttp
系列文
刮掉Web Development的我,與撿到的Android Development30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言