学习了这章你对RecyclerView的基本使用没有任何压力
在工程的build.gradle(project下)中添加
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
在项目的build.gradle(app下)中的dependencies添加
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
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());
}
}
//使用框架的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);
唯一不同的是适配器的配置简单以及优化。
先把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();
}
});
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;
}
});
//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;
meAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
meAdapter.isFirstOnly(false);
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()模式)