iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 16
0
Mobile Development

Android Studio 菜鳥筆記本系列 第 16

Android Studio 菜鳥筆記本-Day 16-元件介紹-RecyclerView

  • 分享至 

  • xImage
  •  

大家好,今天我要分享RecyclerView的使用方法,那我們就開始吧。

UI設置

在activity_main.xml新增RecyclerView元件

<?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/recycle"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>

recyclerview_textview.xml,RecyclerView的列表內容的外觀都會依照此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">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="TextView"
        android:textSize="30sp"/>
</LinearLayout>

設置資料

新增在RecyclerView裡呈現的內容

 String[] cal ={"January","February","March","April","May","June","July","August",
    "September","October","November","December"};
    private ArrayList<String> mitem=new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerV();
        for (int i=0; i < cal.length; i++){
            mitem.add(cal[i]);
        }
    }

完整程式碼

package com.example.test;

import ...
public class MainActivity extends AppCompatActivity {
    RecyclerAdapter adapter;
    String[] cal ={"January","February","March","April","May","June","July","August",
    "September","October","November","December"};
    private ArrayList<String> mitem=new ArrayList<>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerV();
        for (int i=0; i < cal.length; i++){
            mitem.add(cal[i]);
        }
    }
    //設置RecyclerView
    private void RecyclerV() {
        RecyclerView recycle =findViewById(R.id.recycle);
        recycle.setLayoutManager(new LinearLayoutManager(this));
        recycle.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        adapter=new RecyclerAdapter(this,mitem);
        recycle.setAdapter(adapter);
    }
}

創建Adapter

package com.example.test;

import ...
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private List<String> mitem;
    private LayoutInflater inflater;
    RecyclerAdapter(Context context,List<String> item){
        this.inflater=LayoutInflater.from(context);
        this.mitem=item;
}
    //取得控制元件
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title=itemView.findViewById(R.id.title);
        }
    }
    //連接recyclerview_textview.xml
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view =inflater.inflate(R.layout.recyclerview_textview,parent,false);
        return new ViewHolder(view);
    }
    //連結剛才在ViewHolder宣告過的元件,就可以從mitem中取出你要的值
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.title.setText(mitem.get(position));
    }
    //取得顯示數量的大小,return 陣列長度
    @Override
    public int getItemCount() {
        return mitem.size();
    }
}

成果圖
https://ithelp.ithome.com.tw/upload/images/20200919/20129408A1iudUSuXp.jpg


上一篇
Android Studio 菜鳥筆記本-Day 15-功能介紹-Spinner
下一篇
Android Studio 菜鳥筆記本-Day 17-ItemTouchHelper使用方法
系列文
Android Studio 菜鳥筆記本30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言