RecylerView实现流布局StaggeredGridLayoutManager使用详解
作者:流水潺媛 发布时间:2023-04-30 20:51:38
标签:RecylerView,流布局
本文实例为大家分享了RecylerView实现流布局的具体代码,供大家参考,具体内容如下
第一步:添加依赖
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
因为加载的是网络图片,所以采用的是Glide图片加载框架。
第二步:recylerview布局 (刷新需要添加SwipeRefreshLayout)
<?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.example.wangchang.testpalette.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recylerview"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
第三步:RecylerView初始化
recyclerView = (RecyclerView) findViewById(R.id.recylerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
recyclerView.setAdapter(adapter = new DemoAdapter());
adapter.replaceAll(getData());
Demo中:
final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,
StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mMeizhiListAdapter = new MeizhiListAdapter(getContext(), mMeizhiList);
mRecyclerView.setAdapter(mMeizhiListAdapter);
mRecyclerView.addOnScrollListener(getOnBottomListener(layoutManager));//滚动预先加载数据
mMeizhiListAdapter.setOnMeizhiTouchListener(getOnMeizhiTouchListener());//点击事件
这里主要是 recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); 设置StaggeredGridLayoutManager布局管理器,参数分别是列数,方向。
第四步:适配器的实现
package com.hsy.utils.utilsdemo.adapter;
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;
import com.hsy.utils.utilsdemo.R;
import com.hsy.utils.utilsdemo.module.home.Daily;
import com.hsy.utils.utilslibrary.utils.GlideUtils;
import java.util.List;
/**
* Created by drakeet on 6/20/15.
*/
public class MeizhiListAdapter
extends RecyclerView.Adapter<MeizhiListAdapter.ViewHolder> {
public static final String TAG = "MeizhiListAdapter";
private List<Daily> mList;
private Context mContext;
private OnMeizhiTouchListener mOnMeizhiTouchListener;
public MeizhiListAdapter(Context context, List<Daily> meizhiList) {
mList = meizhiList;
mContext = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_meizhi, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
Daily meizhi = mList.get(position);
int limit = 48;
String text = "";
if (meizhi != null && meizhi.getTitle() != null) {
text = meizhi.getTitle().length() > limit ? meizhi.getTitle().substring(0, limit) +
"..." : meizhi.getTitle();
}
viewHolder.meizhi = meizhi;
viewHolder.titleView.setText(text);
viewHolder.card.setTag(meizhi.getDesc());
//加载图片
GlideUtils.displayImage(mContext, meizhi.getImgUrl(), viewHolder.meizhiView);
}
@Override
public void onViewRecycled(ViewHolder holder) {
super.onViewRecycled(holder);
}
@Override
public int getItemCount() {
return mList != null ? mList.size() : 0;
}
public void setOnMeizhiTouchListener(OnMeizhiTouchListener onMeizhiTouchListener) {
this.mOnMeizhiTouchListener = onMeizhiTouchListener;
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView meizhiView;
TextView titleView;
View card;
Daily meizhi;
public ViewHolder(View itemView) {
super(itemView);
card = itemView;
meizhiView = (ImageView) itemView.findViewById(R.id.meizhi);
titleView = (TextView) itemView.findViewById(R.id.title);
meizhiView.setOnClickListener(this);
card.setOnClickListener(this);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mOnMeizhiTouchListener.onTouch(v, meizhiView, card, meizhi);
}
}
}
每个布局的样式item_layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp">
<LinearLayout
android:id="@+id/meizhi_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical">
<!-- android:background="@drawable/bg_card"-->
<ImageView
android:id="@+id/meizhi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:text="Title" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
下面来说实现动态设置图片大小方式
ivImage.getContext()).getWindowManager().getDefaultDisplay().getWidth();
ViewGroup.LayoutParams params = ivImage.getLayoutParams();
//设置图片的相对于屏幕的宽高比
params.width = width/3;
params.height = (int) (200 + Math.random() * 400) ;
ivImage.setLayoutParams(params);
这里是获取屏幕宽度,因为是三列,所以设置图片宽度取屏幕三分之一。
demo源码:参考UtilsDemo中的消息
来源:https://blog.csdn.net/Imshuyuan/article/details/72877829


猜你喜欢
- 在Java SE中,对IO流与集合的操作在应用中比较重要。接下来,我以一个小型项目的形式,演示IO流、集合等知识点在实践中的运用。该项目名称
- Demo环境JDK8Maven3.6.3springboot2.4.3redis_version:6.2.1仓库地址https://gite
- 问题提出:一般在生产环境上,日志的级别是INFO以上,但有时候程序出现问题(如SQL报错),少量日志不能尽快定位问题,这时候可以动态修改日志
- Java IO中File的使用是比较频繁的,在文件的上传和删除中都会用到的。比如我们在写管理系统的时候有可能会用到图片的上传,和删除。那么我
- 文件的上传与下载(一)在实现文件上传和下载之前我们需要做一些准备工作,在Apache官网去下载文件上传下载的两个组件,下载链接这里给出:co
- 本文实例为大家分享了Java实现聊天室界面的具体代码,供大家参考,具体内容如下服务器端:package Server; impor
- Android用SharedPreferences实现登录注册注销功能前言本文用SharedPreferences本地缓存账号信息来实现登录
- Android指定SnackBar在屏幕的位置Snackbar 常以一个小的弹出框的形式,出现在手机屏幕下方或者桌面左下方,并且是在屏幕所有
- name和value属性的区别从源码可以得知,name是value的别名,value也是name的别名。两者的作用是一致的,name指定Fe
- 1.先下载微信分享的jar包放在lib目录下,并且添加依赖,清单文件添加<activity
- 本文实例讲述了Android基于OpenGL在GLSurfaceView上绘制三角形及使用投影和相机视图方法。分享给大家供大家参考,具体如下
- 目录RemoveSubstringReplaceSpiltJoinAppendRemoveRemove(int startIndex) 删除
- 之前讲到了自定义Adapter传递给ListView时,因为ListView的View回收,需要注意当ListView列表项中包含有带有状态
- 本文实例讲述了C#获取远程XML文档的方法。分享给大家供大家参考,具体如下:private XDocument GetXDocument(s
- java多态性多态分两种:(1) 编译时多态(设计时多态):方法重载。(2) 运行时多态:J
- 详解java中保持compareTo和equals同步摘要 : 介绍重写equlas()和comparable接口,两者进行不相同的判断。从
- 方法一:实现Comparator接口,并重写compare方法实体类代码:import java.util.Comparator;/** *
- 图像滤波在opencv中可以有多种实现形式自定义滤波如使用3×3的掩模:对图像进行处理.使用函数filter2D()实现#include&l
- 本文实例讲述了C#序列化与反序列化的方法。分享给大家供大家参考。具体分析如下:把“对象”转换为“字节序列”的过程称为对象的序列化。 
- 首先:我们要建一个web项目接着: 我们先来导入struts的xml文件第一步:右击你的项目名,鼠标到MyEclipse会看到一个add s