基于RecyclerView實現(xiàn)橫向GridView效果
本文實例為大家分享了RecyclerView實現(xiàn)橫向GridView效果展示的具體代碼,供大家參考,具體內(nèi)容如下
要使用RecyclerView,首先要在build.gradle文件中添加依賴compile 'com.android.support:appcompat-v7:24.1.0'
效果圖

布局如下
<?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.dxx.recycleviewtestdemo.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="200dp" android:layout_margin="20dp"/> </RelativeLayout>
使用方法:
package com.dxx.recycleviewtestdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));//設(shè)置布局管理器
rv.setAdapter(new MyRVAdapter(this));
}
}
其Adapter要繼承RecyclerView.Adapter,在Adapter中藥先定義ViewHolder,并繼承RecyclerView.ViewHolder;如:
public class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
}
ImageView rv_item_image;
TextView rv_item_tv;
}
在onCreateViewHolder進行初始化操作,在onBindViewHolder中對各種事件進行處理,getItemCount返回的是 RecyclerView的長度,其布局與代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="18dp"
android:layout_marginBottom="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/rv_item_image"
android:layout_width="82dp"
android:layout_height="82dp"
android:scaleType="centerCrop"
android:src="@drawable/shiqikuangsan"/>
<TextView
android:id="@+id/rv_item_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:textSize="15sp"/>
</LinearLayout>
package com.dxx.recycleviewtestdemo;
import android.content.Context;
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;
/**
* Created by duxiaxing on 2016/7/27.
*/
public class MyRVAdapter extends RecyclerView.Adapter<MyRVAdapter.ViewHolder> {
private Context context;
public MyRVAdapter(Context context){
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_rv_item,parent,false);
ViewHolder holder = new ViewHolder(view);
holder.rv_item_image = (ImageView) view.findViewById(R.id.rv_item_image);
holder.rv_item_tv = (TextView) view.findViewById(R.id.rv_item_tv);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.rv_item_tv.setText(position + "");
}
@Override
public int getItemCount() {
return 9;
}
public class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
}
ImageView rv_item_image;
TextView rv_item_tv;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Compose Column列表不自動刷新問題
這篇文章主要介紹了Android Compose Column列表數(shù)據(jù)更新列表不刷新的問題,總的來說這并不是一道難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過程。希望通過這道題能給你帶來一種解題優(yōu)化的思路2023-01-01
Android四大組件之廣播BroadcastReceiver詳解
Android開發(fā)的四大組件分別是:活動(activity),用于表現(xiàn)功能;服務(wù)(service),后臺運行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個應(yīng)用中存儲和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫,本篇著重介紹廣播組件2021-11-11
app 請求服務(wù)器json數(shù)據(jù)實例代碼
下面小編就為大家分享一篇app 請求服務(wù)器json數(shù)據(jù)實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

