配合RecyclerView使用的适配器
导入依赖
项目的gradle
allprojects {
repositories {
……
maven { url “https://jitpack.io” }
}
}
模块的gradle
implementation ‘com.alibaba:fastjson:1.2.55’
基本使用
FoodQuikAdapter类
mport android.widget.ImageView;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import java.util.List;
/***
第一个泛型 :数据源Bean类
第二个泛型 :BaseViewHolder
*/
public class FoodQuikAdapter extends BaseQuickAdapter
/***
*
/***
*
MainActivity类
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.fastjson.JSON;
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 {
private RecyclerView rv_food;
private FoodQuikAdapter foodQuikAdapter;
private List list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
getData("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
}
private void initView() {
rv_food = findViewById(R.id.rv_food);
list = new ArrayList<>();
foodQuikAdapter = new FoodQuikAdapter(R.layout.food_item_layout, list);
rv_food.setAdapter(foodQuikAdapter);
rv_food.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
}
private void getData(String url) {
OkGo.get(url).execute(new StringCallback() {
@Override
public void onSuccess(Response response) {
FoodBean foodBean = JSON.parseObject(response.body(), FoodBean.class);
list.addAll(foodBean.getData());
foodQuikAdapter.notifyDataSetChanged();
}
});
}
}
点击事件
private void listener() {
//点击事件
foodQuikAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, list.get(position).getTitle(), Toast.LENGTH_SHORT).show();
}
});
//长点击事件
foodQuikAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
list.remove(position);
foodQuikAdapter.notifyDataSetChanged();
return true;
}
});
}
子控件的点击事件
//适配器
/***
*
* @param helper holder
* @param item 每一行的数据
*/
@Override
protected void convert(BaseViewHolder helper, FoodBean.DataBean item) {
helper.setText(R.id.tv_food, item.getTitle())
//给指定控件添加点击事件
.addOnClickListener(R.id.tv_food)
.addOnClickListener(R.id.iv_food);
Glide.with(mContext).load(item.getPic()).into((ImageView) helper.getView(R.id.iv_food));
}
//Activity
//子控件的点击事件
foodQuikAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
switch (view.getId()) {
case R.id.tv_food:
Toast.makeText(MainActivity.this, "TextView的点击事件 -->" + list.get(position).getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.iv_food:
Toast.makeText(MainActivity.this, "ImageView的点击事件 -->" + list.get(position).getTitle(), Toast.LENGTH_SHORT).show();
break;
}
}
});
多布局
FoodQuickAdapter类
public FoodQuikAdapter(@Nullable List
super(data);
//设置布局类型
setMultiTypeDelegate(new MultiTypeDelegate
@Override
protected int getItemType(FoodBean.DataBean dataBean) {
return Integer.parseInt(dataBean.getId()) % 2;
}
});
getMultiTypeDelegate()
.registerItemType(0, R.layout.food_item_layout_left)
.registerItemType(1, R.layout.food_item_layout_right);
}
/***
*
* @param helper holder
* @param item 每一行的数据
*/
@Override
protected void convert(BaseViewHolder helper, FoodBean.DataBean item) {
switch (helper.getItemViewType()) {
case 0:
helper.setText(R.id.tv_food_left, item.getTitle())
//给指定控件添加点击事件
.addOnClickListener(R.id.tv_food_left)
.addOnClickListener(R.id.iv_food_left);
Glide.with(mContext).load(item.getPic()).into((ImageView) helper.getView(R.id.iv_food_left));
break;
case 1:
helper.setText(R.id.tv_food_right, item.getTitle())
//给指定控件添加点击事件
.addOnClickListener(R.id.tv_food_right)
.addOnClickListener(R.id.iv_food_right);
Glide.with(mContext).load(item.getPic()).into((ImageView) helper.getView(R.id.iv_food_right));
break;
}
}