iT邦幫忙

2024 iThome 鐵人賽

DAY 13
0

介紹recyclerview

recyclerview是Listview的改良版,提供更靈活跟優化在布局檔內的限制,是製作滑動表單更優先的選擇。

Recyclerview UI

MainActivity
image
MainAdapter
image

Recyclereview的程式

Mainactivity 與listview的MainActivity大同小異,先宣告一下adapter跟recyclerview與建立arraylist
image
recyclerView.setLayoutManager(new LinearLayoutManager(this));
建立一個預設(垂直)滾動的LinearLayout布局在Activity上,如果想要改成水平滾動的可以改成new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)

recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
在每筆資料後增添的分隔線,如果想改成配合水平滾輪的分隔線可以用
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

MainAdapter一開始同樣要先建立一個arraylist存儲recyclerview產生的畫面
image
MainAdapter對於Recyclerview比較綁定viewholder來建立每筆資料的換面上的原件
image
recyclerview在建立新筆資料時,會先產生一個view
image
之後會在view上抓需要顯示的資料image
最後回傳總共產生了多少筆資料
image

成果展覽

最後還是展示一下我做的簡單例子

Ui

Mainactivity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="422dp"
        android:layout_height="725dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerviewAdapter

<?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"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <TextView
        android:id="@+id/textview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="TextView"
        android:gravity="center"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/textview2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="TextView"
        android:gravity="center"
        android:layout_weight="1"/>


</LinearLayout>

程式

Mainactivity

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    RecyclerviewAdapter listAdapter;
    static String[] data ={"abc","123","48763","hello world","apple"};
    ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for (int i = 0; i < 20; i++) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("number",String.format("%02d", i + 1));
            hashMap.put("data","data : "+String.valueOf(data[new Random().nextInt(5)]));
            arrayList.add(hashMap);
        }
        recyclerView = findViewById(R.id.recycleView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        listAdapter = new RecyclerviewAdapter(arrayList);
        recyclerView.setAdapter(listAdapter);
    }

}

RecyclerviewAdapter

class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.ViewHolder> {

    private ArrayList<HashMap<String, String>> arrayList;
    public RecyclerviewAdapter(ArrayList<HashMap<String, String>> arrayList) {
        this.arrayList = arrayList;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tx1, tx2;

        public ViewHolder(View itemView) {
            super(itemView);
            tx1 = itemView.findViewById(R.id.textview);
            tx2 = itemView.findViewById(R.id.textview2);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleviewitem, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.tx1.setText(arrayList.get(position).get("number"));
        holder.tx2.setText(arrayList.get(position).get("data"));
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }
}

上一篇
[day12]Adapter-Listview篇
下一篇
[day14]Gridview
系列文
深入Android 物件的認識與應用實踐30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言