Android RecyclerView(三)学习BRVAH使RecyclerView变得简单

在这里插入图片描述

这是啥

学习了这章你对RecyclerView的基本使用没有任何压力

  1. Recycler View的简单适配器
  2. item事件
  3. 添加动画
  4. 添加头部或者尾部
如何使用
1.引入框架

在工程的build.gradle(project下)中添加

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

在项目的build.gradle(app下)中的dependencies添加

compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
2.创建我们的MeAdapter.class继承BaseQuickAdapter<Module,BaseViewHolder>
package com.yuexia.recyclerview;

import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * Created by syt98 on 2019/1/9.
 */

public class MeAdapter extends BaseQuickAdapter<User,BaseViewHolder>{

    public MeAdapter(int layoutResId, @Nullable List<User> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, User item) {
        helper.setImageResource(R.id.iv_user_img,item.getImg());
        helper.setText(R.id.tv_user_name,item.getName());
    }
}
3.然后到我们的活动中添加
//使用框架的RecyclerView
MeAdapter meAdapter=new MeAdapter(R.layout.item_user,users);
//添加到控件
recyclerView.setAdapter(meAdapter);

其中包括布局管理器都是照常使用

		//创建线性布局管理器
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        //设置为垂直布局
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);

        //设置水平布局
//        linearLayoutManager.setOrientation(OrientationHelper.HORIZONTAL);
        //设置网格布局管理器每行四个Item
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4, OrientationHelper.VERTICAL, false);
        //设置瀑布式布局
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL);

唯一不同的是适配器的配置简单以及优化。

item事件
1.item点击事件

先把meAdapter变成全局变量 ctrl+alt+f 然后enter键

meAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Log.d(TAG, "onItemClick: ");
        Toast.makeText(MainActivity.this, "点击了" + users.get(position).getName(), Toast.LENGTH_SHORT).show();
    }
});
2.item长按事件
meAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        Log.d(TAG, "onItemLongClick: ");
        Toast.makeText(MainActivity.this, "长按了"+users.get(position).getName(), Toast.LENGTH_SHORT).show();
        return false;
    }
});
再看一下我们的子控件点击事件和长按事件

先在MeAdpater.class中添加事件

@Override
protected void convert(BaseViewHolder helper, User item) {
    helper.setImageResource(R.id.iv_user_img,item.getImg())
            .setText(R.id.tv_user_name,item.getName())
            .addOnClickListener(R.id.iv_user_img)
            .addOnLongClickListener(R.id.iv_user_img);
}

再在我们的活动中编写监听方法

//子控件的点击事件
meAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        Log.d(TAG, "onItemChildClick: ");
        Toast.makeText(MainActivity.this, "点击了"+users.get(position).getName()+"的图片", Toast.LENGTH_SHORT).show();
    }
});
//子控件的长按事件
meAdapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
    @Override
    public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
        Log.d(TAG, "onItemChildLongClick: ");
        Toast.makeText(MainActivity.this, "长按了"+users.get(position).getName()+"的图片", Toast.LENGTH_SHORT).show();
        return false;
    }
});
添加动画
1.默认是有五种,框架自带的动画
//Animation
/**
 * Use with {@link #openLoadAnimation}
 */
public static final int ALPHAIN = 0x00000001;
/**
 * Use with {@link #openLoadAnimation}
 */
public static final int SCALEIN = 0x00000002;
/**
 * Use with {@link #openLoadAnimation}
 */
public static final int SLIDEIN_BOTTOM = 0x00000003;
/**
 * Use with {@link #openLoadAnimation}
 */
public static final int SLIDEIN_LEFT = 0x00000004;
/**
 * Use with {@link #openLoadAnimation}
 */
public static final int SLIDEIN_RIGHT = 0x00000005;
2.添加动画
meAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
3.动画默认执行一次,重复执行添加如下代码
meAdapter.isFirstOnly(false);
添加头部或者尾部
1.添加头部,尾部
meAdapter.addHeaderView(getView());
meAdapter.addFooterView(getView());
public View getView() {
    View view = LayoutInflater.from(this).inflate(R.layout.item_user_header, null, false);
    return view;
}

在这里插入图片描述
是不是有了头部,为什么我明明只改了个颜色和名称,它却填充了整个宽度?
看我Android 自定义View(inflate()模式)

付:

下次我会介绍 上拉刷新下拉加载 以及 拖动滑动删除
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值