RecyclerView使用总结
遇到的异常:
java.lang.ExceptionInInitializerError静态块初始化异常
NetworkOnMainThreadException访问网线不能在主线程中进行
我的参考资料:
http://www.oschina.net/question/1764894_246656?fromerr=ZElwOffs
老师给的源代码
recyclerView 和cartView的导入方法
别人的都是:
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
但是我的自动添加是这样的:
compile 'com.android.support:recyclerview-v7:24.0.0 -beat'
compile 'com.android.support:cardview-v7:24.0.0 -beat'
但是就算这样也是不成功的
我的只能这样写:
compile 'com.android.support:recyclerview-v7:21.+'
compile 'com.android.support:cardview-v7:21.+'
RecyclerView的使用
1页面布局
<android.support.v7.widget.RecyclerView
android:id="@+id/rcView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
2初始化控件
RecyclerView rcView;
rcView = (RecyclerView) this.findViewById(R.id.rcView);
3给它数据(图片、文字等等)
List<Integer> imageUrls;
Integer[] imgs ={
R.drawable.happy1,R.drawable.happy2,R.drawable.happy3,R.drawable.happy4,
R.drawable.happy5,R.drawable.happy6,R.drawable.happy7,R.drawable.happy8
};
imageUrls = new ArrayList<>();
imageUrls.addAll(Arrays.asList(imgs));
4适配器
public class MyApdater extends RecyclerView.Adapter<MyApdater.MyViewHolder> {
private Context context;
private LayoutInflater inflater;
private List<Integer> imageList;
private OnItemClickListener itemClickListener;
public interface OnItemClickListener{
void onItemClick(View view, int position);
}
public void setOnItemClickListener(OnItemClickListener itemClickListener){
this.itemClickListener = itemClickListener;
}
public MyApdater(Context context, List<Integer> imageList) {
this.context = context;
this.imageList = imageList;
inflater = LayoutInflater.from(context);
setHasStableIds(true);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(inflater.inflate(R.layout.list_item,parent,false));
}
@Override
public int getItemCount() {
return imageList.size();
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
int e = imageList.get(position);
holder.img_item.setImageResource(e);
// x.image().bind(holder.img_item,imageList.get(position),options);
/*if(itemClickListener != null){
holder.img_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// int pos = holder.getPosition(); //获取当前Item的postion
itemClickListener.onItemClick(holder.img_item,position);
}
});
}*/
}
class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView img_item;
public MyViewHolder(View itemView) {
super(itemView);
img_item = (ImageView) itemView.findViewById(R.id.img_item);
}
}
}
5素材xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:orientation="horizontal"
card_view:cardBackgroundColor="#EEEEEE"
card_view:cardCornerRadius="5dp"> <ImageView
android:src="@drawable/happy1"
android:id="@+id/img_item"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </android.support.v7.widget.CardView>
6在MainActivity中使用适配器
MyApdater myApdater;
myApdater = new MyApdater(this,imageUrls);
rcView.setAdapter(myApdater);
7设置RecyclerView的动画、显示等效果
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
rcView.setItemAnimator(new DefaultItemAnimator());
//rcView.addItemDecoration();
rcView.setLayoutManager(layoutManager);
附:MainActivity全代码
package com.firefly.recyclerview; import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class MainActivity extends ActionBarActivity {
RecyclerView rcView;
MyApdater myApdater;
List<Integer> imageUrls;
Integer[] imgs ={
R.drawable.happy1,R.drawable.happy2,R.drawable.happy3,R.drawable.happy4,
R.drawable.happy5,R.drawable.happy6,R.drawable.happy7,R.drawable.happy8
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
init();
} private void init() { rcView = (RecyclerView) this.findViewById(R.id.rcView);
//GridLayoutManager layoutManager = new GridLayoutManager(this,2);
//LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
rcView.setItemAnimator(new DefaultItemAnimator());
//rcView.addItemDecoration();
rcView.setLayoutManager(layoutManager);
myApdater = new MyApdater(this,imageUrls);
rcView.setAdapter(myApdater);
/* myApdater.setOnItemClickListener(new MyApdater.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, position + " click", Toast.LENGTH_SHORT).show();
imageUrls.remove(position);
myApdater.notifyItemRemoved(position);
}
});*/ } protected void initData()
{
imageUrls = new ArrayList<>();
imageUrls.addAll(Arrays.asList(imgs));
} }
RecyclerView使用总结的更多相关文章
- RecyclerView使用大全
RecylerView介绍 RecylerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,这一点从它的名字recyler ...
- 带你实现开发者头条APP(五)--RecyclerView下拉刷新上拉加载
title: 带你实现开发者头条APP(五)--RecyclerView下拉刷新上拉加载 tags: -RecyclerView,下拉刷新,上拉加载更多 grammar_cjkRuby: true - ...
- 安卓易学,爬坑不易——腾讯老司机的RecyclerView局部刷新爬坑之路
针对手游的性能优化,腾讯WeTest平台的Cube工具提供了基本所有相关指标的检测,为手游进行最高效和准确的测试服务,不断改善玩家的体验.目前功能还在免费开放中. 点击地址:http://wetest ...
- Android Studio开发RecyclerView遇到的各种问题以及解决(二)
开发RecyclerView时候需要导入别人的例子,我的是从github导入的,下载下github的压缩包之后解压看你要导入的文件是priject还是Module.(一般有app文件夹的大部分是pro ...
- Android Studio开发RecyclerView遇到的各种问题以及解决(一)
以前一直在用ListView,,,最近才看RecyclerView发现好强大.RecyclerView前提是Android版本在5.0以上,本人以前用的是eclipse只支持到4.4.索性就安装一个A ...
- Android的Kotlin秘方(II):RecyclerView 和 DiffUtil
作者:Antonio Leiva 时间:Sep 12, 2016 原文链接:http://antonioleiva.com/recyclerview-diffutil-kotlin/ 如你所知,在[支 ...
- Android RecyclerView 实现支付宝首页效果
Android RecyclerView 实现支付宝首页效果 [TOC] 虽然我本人不喜欢支付宝的,但是这个网格本身其实还是不错的,项目更新中更改了一个布局为网格模式,类似支付宝.(估计是产品抄袭的= ...
- Android开发学习之路-RecyclerView滑动删除和拖动排序
Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...
- 打造android偷懒神器———RecyclerView的万能适配器
转载请注明出处谢谢:http://www.cnblogs.com/liushilin/p/5720926.html 很不好意思让大家久等了,本来昨天就应该写这个的,无奈公司昨天任务比较紧,所以没能按时 ...
- 安卓v7支持包下的ListView替代品————RecyclerView
RecyclerView这个控件也出来很久了,相信大家也学习的差不多了,如果还没学习的,或许我可以带领大家体验一把这个艺术般的控件. 项目已经同步至github:https://github.com/ ...
随机推荐
- [转载]学习VC MFC开发必须了解的常用宏和指令————复习一下
1.#include指令 包含指定的文件 2.#define指令 预定义,通常用它来定义常量(包括无参量与带参量),以及用来实现那些“表面似和善.背后一长串”的宏,它本身并不在编译过程中进行,而 ...
- ArcGIS AddIN异常之:object reference not set to an instance of an object
异常出现在 frmDownload frd = new frmDownload(); frd.ShowDialog(); 在ArcMap中能正常弹出窗体,点击按钮时显示此异常:object refer ...
- hdu Remainder
这道题是道很明显的bfs题.因为对数论没什么研究 ,所以这道题目里的两个关键点并不知道,看了别人的题解才知道 . 1.为避免取模后出现负数,采用:x%y=(x%y+y)%y 2.全部采用对m*k取模后 ...
- 手动创建oem
[oracle@std bin]$ /u02/app/product//db_1/bin/emca -config dbcontrol db -repos create STARTED EMCA at ...
- Java可读取操作系统的配置
/** * Java获取操作系统的配置环境 * @throws Exception */ @Test public void testPro() throws Exception { Properti ...
- html第二天CSS
- 怎么启动或停止mysql服务
在linux下, 启动mysql用 service mysql start 停止用 service mysql stop 在windows下, 启动用 net start mysql 停止 ...
- SVN :This XML file does not appear to have any style information associated with it.
SVN :This XML file does not appear to have any style information associated with it. The document tr ...
- 给RecyclerView实现的GridView加上HeaderView和FooterView
给RecyclerView设置布局管理器 GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); 写适配器,添加子项 ...
- Python之调用函数
Python之调用函数 Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数 abs,它接收一个参数. 可以直接从Python的官方网站查 ...