本文實例為大家分享了RecyclerView實現橫向GridView效果展示的具體代碼,供大家參考,具體內容如下
成都創新互聯堅持“要么做到,要么別承諾”的工作理念,服務領域包括:做網站、成都網站設計、企業官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯網時代的榆次網站設計、移動媒體設計的需求,幫助企業找到有效的互聯網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!
要使用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));//設置布局管理器
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;
}
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創新互聯。
本文題目:基于RecyclerView實現橫向GridView效果
鏈接分享:http://www.yijiale78.com/article40/jdopeo.html
成都網站建設公司_創新互聯,為您提供虛擬主機、搜索引擎優化、建站公司、企業網站制作、小程序開發、響應式網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯