BaseRecyclerViewAdapterHelper 是用于对RecycleView的数据绑定以及定义事件的一系列超级简单的一个adapter,下面我们就来学习他的用法
标准配置
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
dependencies {
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:VERSION_CODE'
}
可能由于我的网络环境有问题,这种配置方式我的gradle并不能成功,于是就采用了下载源码的方式,具体方法:
public class Message {
private String title; //这里声明两个text,我们的item里面也只有两个TextView
private String message;
public Message(String title, String message) {
this.title = title;
this.message = message;
}
public Message(){
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public class QuickAdapter extends BaseQuickAdapter<Message,BaseViewHolder>{
//这个是构造,用来初始化要输入的数据
public QuickAdapter(List data) {
super(R.layout.item, data);
}
@Override
protected void convert(BaseViewHolder helper, Message item) {
//将每一个item的数据绑定到对应的TextView上
helper.setText(R.id.title,item.getTitle())
.setText(R.id.message,item.getMessage());
}
}
我们用CardView来作为item
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="50dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_marginTop="@dimen/dp_10"
android:elevation="10dp"
app:cardCornerRadius="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
LinearLayout>
android.support.v7.widget.CardView>
activity_main.xml的布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
tools:context="com.example.rcycleadapterdemo.activity.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android.support.v7.widget.RecyclerView>
LinearLayout>
MainAcitivy的内容
public class MainActivity extends AppCompatActivity {
private List list = new ArrayList<>();
private QuickAdapter quickAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<10;i++){
list.add(new Message("haha","heheheheheheh"));
}
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycleview);
GridLayoutManager manager = new GridLayoutManager(this,1);
recyclerView.setLayoutManager(manager);
quickAdapter = new QuickAdapter(list);
recyclerView.setAdapter(quickAdapter);
// quickAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN); //指定缩放动画
//自定义动画
quickAdapter.openLoadAnimation(new BaseAnimation() {
@Override
public Animator[] getAnimators(View view) {
return new Animator[]{
ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1),
ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)
};
}
});
//添加点击事件
recyclerView.addOnItemTouchListener(new OnItemClickListener() {
@Override
public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this,"点击了"+position,Toast.LENGTH_SHORT).show();
}
});
}
}