之前我介紹了RecyclerView的簡單使用,那麼如何在RecyclerView中自定義佈局呢,就像ListView那樣,下面我給大家介紹一下,講說和RecyclerView的簡單使用那篇博客差不多,但是代碼不同,大牛請繞過!
第一步:創建一個空的Activity項目,導入support-v7這個類庫,用Androidstudio的童鞋們可直接右擊項目Project Structure中的Dependencies中直接從網上添加。然後在佈局文件中添加其組件,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.oak.learnrecyclerview.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
第二步:創建顯示在RecyclerView中的條目自定義佈局文件:list_cell.xml,代碼如下:
<?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="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_title"
android:text="title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_content"
android:text="content"/>
</LinearLayout>
第三步:在MainActivity中進行一些初始化設置,在對這個組件進行操作的時候要先findViewById拿到這個組件,然後使用setLayoutManager這個方法設置組件的佈局方式(必須設置,否則無法顯示內容),然後爲其設置適配器setAdapter方法,new出一個適配器,我們需要重寫三個方法,分別是onCreateViewHolder:用於將View返回給RecyclerView組件的、onBindViewHolder:用於在View綁定上後對View進行一些數據上的操作、getItemCount:用來返回顯示多少條信息的。在這三個方法中最重要的就是onCreateViewHolder這個方法了,它需要返回一個ViewHolder,所以我們需要創建一個MyViewHolder類並繼承support-v7包中的RecyclerView.ViewHolder,並在裏面對外公開一個方法,用來在onBindViewHolder方法中獲取到這個View對其數據操作。具體代碼如下:
package com.oak.learnrecyclerview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new RecyclerView.Adapter() {
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tv_title;
private TextView tv_content;
public MyViewHolder(View itemView) {
super(itemView);
tv_title = (TextView) itemView.findViewById(R.id.tv_title);
tv_content = (TextView) itemView.findViewById(R.id.tv_content);;
}
public TextView getTv_title() {
return tv_title;
}
public TextView getTv_content() {
return tv_content;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
return new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.list_cell,null));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder myHolder = (MyViewHolder) holder;
myHolder.getTv_title().setText("title:item"+position);
myHolder.getTv_content().setText("content:item"+position);
}
@Override
public int getItemCount() {
return 100;
}
});
}
}
效果圖:
By:Brycen Liu