RecyclerView万能适配器
导入依赖
项目gradle:
maven {
url ‘https://jitpack.io’
}
应用gradle:
implementation ‘com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.42’
implementation ‘com.android.support:recyclerview-v7:28.0.0’
适配器
public class MyAdapter extends BaseQuickAdapter<Food.DataBean, BaseViewHolder> {
int layoutResId;
List<Food.DataBean> data;
public MyAdapter(int layoutResId, @Nullable List<Food.DataBean> data) {
super(layoutResId, data);
this.layoutResId=layoutResId;
this.data=data;
}
@Override
protected void convert(BaseViewHolder helper, Food.DataBean item) {
//找控件ID
View image = helper.getView(R.id.item_pic);
// helper.setText(R.id.item_title,item.getTitle());
//填充数据
Glide.with(mContext).load(item.getPic()).into((ImageView) image);
helper.setText(R.id.item_title,item.getTitle())
.addOnClickListener(R.id.item_pic) //给图片设置点击事件
.addOnClickListener(R.id.item_title); //给文本设置点击事件
}
}
MainActivity:
package com.example.day8;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
MyAdapter myAdapter;
List<Food.DataBean> lists=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
//请求数据
OkGo.<String>get("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1")
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
List<Food.DataBean> data = new Gson().fromJson(response.body(), Food.class).getData();
lists.addAll(data);
myAdapter.notifyDataSetChanged();
}
});
//点击事件
myAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "点击了"+position, Toast.LENGTH_SHORT).show();
}
});
//条目点击事件
myAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
int id = view.getId();
switch (id){
case R.id.item_pic:
Toast.makeText(MainActivity.this, "点击了子图片"+position, Toast.LENGTH_SHORT).show();
break;
case R.id.item_title:
Toast.makeText(MainActivity.this, "点击了子文本"+position, Toast.LENGTH_SHORT).show();
break;
}
}
});
}
private void init() {
recyclerView=findViewById(R.id.recyclerview);
myAdapter=new MyAdapter(R.layout.layout_item,lists);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
QQ聊天
适配器
public class QQAdapter extends BaseQuickAdapter<Message, BaseViewHolder> {
public QQAdapter(@Nullable List<Message> data) {
super(data);
//设置类型
setMultiTypeDelegate(new MultiTypeDelegate<Message>() {
@Override
protected int getItemType(Message message) {
return message.getType();
}
});
//设置多布局
getMultiTypeDelegate()
.registerItemType(Message.LEFT, R.layout.layout_left)
.registerItemType(Message.RIGHT,R.layout.layout_right);
}
@Override
protected void convert(BaseViewHolder helper, Message item) {
int type = helper.getItemViewType();
switch (type){
case Message.LEFT:
helper.setText(R.id.left_message,item.getMessage());
break;
case Message.RIGHT:
helper.setText(R.id.right_message,item.getMessage());
break;
}
}
}
Message类:
package com.example.day8.QQ;
public class Message {
public static final int LEFT=0;
public static final int RIGHT=1;
Integer pic;
String message;
int type;
public Integer getPic() {
return pic;
}
public void setPic(Integer pic) {
this.pic = pic;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Message() {
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Message( String message, int type) {
this.message = message;
this.type = type;
}
}
MainActivity类:
public class QQActivity extends AppCompatActivity {
RecyclerView recyclerView;
EditText message;
List<Message> lists=new ArrayList<>();
QQAdapter adapter;
LinearLayoutManager linearLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qq);
recyclerView=findViewById(R.id.recyclerview);
message=findViewById(R.id.text_message);
adapter=new QQAdapter(lists);
recyclerView.setAdapter(adapter);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
lists.add(new Message("Asu",Message.LEFT));
lists.add(new Message("Qsu",Message.LEFT));
adapter.notifyDataSetChanged();
}
public void click(View view) {
String s = message.getText().toString();
lists.add(new Message(s,Message.RIGHT));
adapter.notifyDataSetChanged();
linearLayoutManager.scrollToPosition(lists.size()-1);
}
}