【Android 基础 4.5】RecyclerView

2018-11-16 13:31:21

RecyclerView is a subclass of ViewGroup and is a more resource-efficient way to display scrollable lists. Instead of creating a View for each item that may or may not be visible on the screen, RecyclerView creates a limited number of list items and reuses them for visible content.

What you’ll learn

  • How to use the RecyclerView class to display items in a scrollable list.
  • How to dynamically add items to the RecyclerView as they become visible through scrolling.
  • How to perform an action when the user taps a specific item.

The relationship

在这里插入图片描述

I’m using another sample for this codelab, so i stored data in Room.

Layout

list item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/text_view_style"
        android:background="@android:color/holo_blue_light"/>

</LinearLayout>

main layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="moe.leer.roomwordsample.MainActivity"
    android:background="@color/colorScreenBackground"
    tools:showIn="@layout/activity_main">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        tools:listitem="@layout/recyclerview_item"/>
</android.support.constraint.ConstraintLayout>

Adapter

ViewHolder

ViewHolder create the item view from XML layout.

  class WordViewHolder extends RecyclerView.ViewHolder {
    final TextView textView;

    WordViewHolder(@NonNull View itemView) {
      super(itemView);
      textView = itemView.findViewById(R.id.textViewItem);
    }
  }

In onCreateViewHolder, we create a view holder:

  @Override
  public WordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    return new WordViewHolder(inflater.inflate(R.layout.recyclerview_item, parent, false));
  }

Bind view

In WordListAdapter, we override onBindViewHolder. In this method we init the data of the item view(like texts or images. etc.).

  @Override
  public void onBindViewHolder(@NonNull final WordViewHolder wordViewHolder, final int i) {
    if (words != null) {
      wordViewHolder.textView.setText(words.get(i).getWord());
    } else {
      wordViewHolder.textView.setText("No word");
    }
  }

Create RecyclerView in Activity

To use recycler view in activity, we should appoint its adapter and layout manager.

    val adapter = WordListAdapter(this)
    wordRecyclerView.adapter = adapter
    wordRecyclerView.layoutManager = LinearLayoutManager(this)

Add click listener

RecyclerView doesn’t has a click listener by default, we can implement it by ourselves.

Define a interface

The clicked position is useful for us, so let the interface get position for us.

In WordListAdapter add:

  interface OnClickListener {
    void onClick(int pos);
//    void onLongClick(int pos); if long click is needed
  }

Add click listener

at onBindViewHolder:

    wordViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        clickListener.onClick(wordViewHolder.getAdapterPosition());
      }
    });

Usage

In activity:

adapter.setClickListener { pos ->
   Toast.makeText(this, "You clicked ${adapter.getWordAtPosition(pos).word}", 	
                  Toast.LENGTH_SHORT).show()
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值