溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android RecyclerView的簡(jiǎn)單使用

發(fā)布時(shí)間:2020-10-08 14:32:18 來源:腳本之家 閱讀:127 作者:zhu6201976 欄目:移動(dòng)開發(fā)

本文實(shí)例為大家分享了Android RecyclerView使用的具體代碼,供大家參考,具體內(nèi)容如下

package com.itheima74.recyclerview; 
 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.GridLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
 
import java.util.ArrayList; 
 
/** 
 * RecyclerView 
 * 1.增強(qiáng)版的listview 
 * 2.官方推薦 
 * 3.使用步驟: 
 * a.build.gradle文件中添加依賴compile 'com.android.support:recyclerview-v7:25.2.0' 
 * b.xml布局中引入,注意完整包名+類名 
 * c.具體見代碼 
 */ 
public class MainActivity extends AppCompatActivity { 
 
 ArrayList<Fruit> mFruitList; 
 private RecyclerView recyclerview; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 
 initView(); 
 initData(); 
 initAdapter(); 
 } 
 
 /** 
 * 初始化數(shù)據(jù)適配器 
 */ 
 private void initAdapter() { 
 recyclerview.setAdapter(new FruitAdapter()); 
 } 
 
 /** 
 * 初始化控件 
 */ 
 private void initView() { 
 recyclerview = (RecyclerView) findViewById(R.id.recyclerview); 
 
 // 設(shè)置為L(zhǎng)inearLayoutManager布局方式,線性布局 
 // LinearLayoutManager layoutManager = new LinearLayoutManager(this); 
 // 兩種方式,水平或垂直排布,默認(rèn)垂直排布 
 // layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); 
 // recyclerview.setLayoutManager(layoutManager); 
 
 // 設(shè)置為GridLayoutManager布局方式,3列排布 
 GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3); 
 recyclerview.setLayoutManager(gridLayoutManager); 
 } 
 
 /** 
 * 初始化數(shù)據(jù),水果集合 
 */ 
 private void initData() { 
 mFruitList = new ArrayList<>(); 
 mFruitList.add(new Fruit("apple", R.drawable.apple_pic)); 
 mFruitList.add(new Fruit("banana", R.drawable.banana_pic)); 
 mFruitList.add(new Fruit("cherry", R.drawable.cherry_pic)); 
 mFruitList.add(new Fruit("grape", R.drawable.grape_pic)); 
 mFruitList.add(new Fruit("mango", R.drawable.mango_pic)); 
 mFruitList.add(new Fruit("orange", R.drawable.orange_pic)); 
 mFruitList.add(new Fruit("pear", R.drawable.pear_pic)); 
 mFruitList.add(new Fruit("pineapple", R.drawable.pineapple_pic)); 
 mFruitList.add(new Fruit("strawberry", R.drawable.strawberry_pic)); 
 mFruitList.add(new Fruit("watermelon", R.drawable.watermelon_pic)); 
 } 
 
 class FruitAdapter extends RecyclerView.Adapter<ViewHolder> { 
 
 // 返回ViewHolder對(duì)象 
 @Override 
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
 return new ViewHolder(View.inflate(getApplicationContext(), R.layout.listview_item, null)); 
 } 
 
 // 刷新界面,為ViewHolder兩個(gè)屬性設(shè)置內(nèi)容 
 @Override 
 public void onBindViewHolder(ViewHolder holder, int position) { 
 Fruit fruit = mFruitList.get(position); 
 holder.iv.setImageResource(fruit.imageId); 
 holder.tv.setText(fruit.name); 
 } 
 
 @Override 
 public int getItemCount() { 
 // FruitAdapter是MainActivity的內(nèi)部類,可以直接使用外部類的成員變量 
 return mFruitList.size(); 
 } 
 } 
 
 static class ViewHolder extends RecyclerView.ViewHolder { 
 ImageView iv; 
 TextView tv; 
 
 ViewHolder(View itemView) { 
 super(itemView); 
 iv = (ImageView) itemView.findViewById(R.id.iv); 
 tv = (TextView) itemView.findViewById(R.id.tv); 
 } 
 } 
 
 
} 

package com.itheima74.recyclerview; 
 
/** 
 * Created by My on 2017/3/2. 
 */ 
 
class Fruit { 
 String name; 
 int imageId; 
 
 Fruit(String name, int imageId) { 
 this.name = name; 
 this.imageId = imageId; 
 } 
} 

復(fù)制代碼 代碼如下:
<img src="http://img.blog.csdn.net/20170303085928987?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh2NjIwMTk3Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""> 

效果圖:

Android RecyclerView的簡(jiǎn)單使用

Android RecyclerView的簡(jiǎn)單使用

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI