1. 程式人生 > >android中RecyclerView控件的使用

android中RecyclerView控件的使用

使用 ava wrap mage sad ann inflate getitem odin

1、RecyclerView控件不在標準的庫裏面,需要先引入,引入比較簡單,點擊控件右邊的下載按鈕即可

技術分享圖片

2、先添加一個新聞實體類,用來為新聞列表提供數據,news.java:

package com.example.chenrui.common;

public class News {

    private String title;
    private int pic;

    public News(String title, int pic) {
        this.title = title;
        this.pic = pic;
    }

    
public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPic() { return pic; } public void setPic(int pic) { this.pic = pic; } }

3、在res/drawable/xhdpi目錄下引用幾個圖片,做為新聞圖片的資源

技術分享圖片

4、添加一個Layout XML File,做為列表項的內容,news_item.xml:

<?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:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="100dp"> <ImageView android:id="@+id/newsPic" android:layout_width="120dp" android:layout_height="80dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:adjustViewBounds="false" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/newsTitle" android:layout_width="0dp" android:layout_height="wrap_content" android:text="TextView" app:layout_constraintLeft_toRightOf="@+id/newsPic" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="@+id/newsPic" /> </android.support.constraint.ConstraintLayout>

5、添加一個適配器,這個適配器用於為RecyclerView指定使用的Layout數據項模板,以及對應的數據操作,NewsAdapter.java:

package com.example.chenrui.app1;

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.ImageView;
import android.widget.TextView;

import com.example.chenrui.common.News;

import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<News2Adapter.ViewHolder> {

    private List<News> newsList;

    public News2Adapter(List<News> newsList) {
        this.newsList = newsList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item,viewGroup,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        News news = newsList.get(i);
        viewHolder.newsImage.setImageResource(news.getPic());
        viewHolder.newsTitle.setText(news.getTitle());
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView newsImage;
        TextView newsTitle;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            newsImage = itemView.findViewById(R.id.newsPic);
            newsTitle = itemView.findViewById(R.id.newsTitle);
        }
    }
}

6、接下來就是主體Activity了

activity_main.xml

<?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=".Main13Activity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.chenrui.app1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.example.chenrui.common.News;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

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

        List<News> newsList = new ArrayList();
        newsList.add(new News("新聞標題1",R.drawable.o1));
        newsList.add(new News("新聞標題2",R.drawable.o2));
        newsList.add(new News("新聞標題3",R.drawable.o3));
        newsList.add(new News("新聞標題4",R.drawable.o4));
        newsList.add(new News("新聞標題5",R.drawable.o5));
        NewsAdapter newsAdapter = new NewsAdapter(newsList);

        RecyclerView view = findViewById(R.id.list1);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        view.setLayoutManager(layoutManager);
        view.setAdapter(newsAdapter);
    }
}

到此一個簡單的RecyclerView控件示例就完成了,顯示效果如下:

技術分享圖片

android中RecyclerView控件的使用