适配器
public class CrosseTalkAdapter extends RecyclerView.Adapter {
//定义两种常量 表示两种条目类型
public static final int TYPE_LEFT_IMAGE = 0;
public static final int TYPE_RIGHT_IMAGE = 1;
private List<Bean.DataBean> list;
private Context context;
private OnLongItemClickListener mLongItemClickListener;
public CrosseTalkAdapter(List<Bean.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
public interface OnLongItemClickListener {
void onItemLongClick(int i);
}
public void setLongItemClickListeners(OnLongItemClickListener longItemClickListener) {
this.mLongItemClickListener = longItemClickListener;
}
/**
* 创建viewholder
*
* @param parent
* @param viewType
* @return
*/
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = null;
RecyclerView.ViewHolder holder = null;
switch (viewType) {
case TYPE_LEFT_IMAGE:
//条目布局
view = LayoutInflater.from(context).inflate(R.layout.oneimage, null);
holder = new TextViewHolder(view);
break;
case TYPE_RIGHT_IMAGE:
view = LayoutInflater.from(context).inflate(R.layout.threeimage, null);
holder = new ImageViewHolder(view);
break;
}
return holder;
}
/**
* 绑定数据
*
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
Bean.DataBean dataBean = list.get(position);
if (holder instanceof TextViewHolder) {
String imgUrls = dataBean.getThumbnail_pic_s();
String[] split = imgUrls.split("\\|");
((TextViewHolder) holder).title_rightlayout.setText(dataBean.getTitle());
Glide.with(context).load(dataBean.getThumbnail_pic_s()).into(((TextViewHolder) holder).img_right);
//长按
((TextViewHolder) holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (mLongItemClickListener != null){
mLongItemClickListener.onItemLongClick(position);
}
return true;
}
});
}
//在里面设置各自的适配器 和显示的布局
if (holder instanceof ImageViewHolder) {
String imgUrls = dataBean.getThumbnail_pic_s();
String[] split = imgUrls.split("\\|");
String imgUrls1 = dataBean.getThumbnail_pic_s02();
String[] split1 = imgUrls1.split("\\|");
String imgUrls2 = dataBean.getThumbnail_pic_s03();
String[] split2 = imgUrls2.split("\\|");
Glide.with(context).load(split[0]).into(((ImageViewHolder) holder).img1);
Glide.with(context).load(split1[0]).into(((ImageViewHolder) holder).img2);
Glide.with(context).load(split2[0]).into(((ImageViewHolder) holder).img3);
((ImageViewHolder) holder).title_zhong.setText(dataBean.getTitle());
//长按
((ImageViewHolder) holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (mLongItemClickListener != null){
mLongItemClickListener.onItemLongClick(position);
}
return true;
}
});
}
}
/**
* 条目数量
*
* @return
*/
@Override
public int getItemCount() {
return list.size();
}
//删除
public void deleteItem(int poistion){
list.remove(poistion);
notifyItemRemoved(poistion);
}
/**
* 获取不同的条目类型
*
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
//&& imgUrls.isEmpty()
if ( list.get(position).getThumbnail_pic_s02() == null || list.get(position).getThumbnail_pic_s03() == null ) {
return TYPE_LEFT_IMAGE;
} else {
return TYPE_RIGHT_IMAGE;
}
}
private class TextViewHolder extends RecyclerView.ViewHolder {
private final TextView title_rightlayout;
private final ImageView img_right;
public TextViewHolder(View itemView) {
super(itemView);
title_rightlayout = itemView.findViewById(R.id.textview1);
img_right = itemView.findViewById(R.id.image1);
}
}
private class ImageViewHolder extends RecyclerView.ViewHolder {
private final TextView title_zhong;
private final ImageView img1;
private final ImageView img2;
private final ImageView img3;
public ImageViewHolder(View itemView) {
super(itemView);
title_zhong = itemView.findViewById(R.id.textview2);
img1 = itemView.findViewById(R.id.image2);
img2 = itemView.findViewById(R.id.image3);
img3 = itemView.findViewById(R.id.image4);
}
}
}
主页面
public class MainActivity extends AppCompatActivity {
private RecyclerView mRv;
private CrosseTalkAdapter crosseTalkAdapter ;
private List<Bean.DataBean> listBeans = new ArrayList<>();
private String url ="http://www.xieast.com/api/news/news.php?page=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//隐藏原有标题
//getSupportActionBar().hide();
initView();
crosseTalkAdapter = new CrosseTalkAdapter(listBeans, this);
mRv.setAdapter(crosseTalkAdapter);
crosseTalkAdapter.setLongItemClickListeners(new CrosseTalkAdapter.OnLongItemClickListener() {
@Override
public void onItemLongClick(int i) {
showAlertDialog(i);
}
});
OkHttpUtils.getInstance().doGet(url, new OnNetListener() {
@Override
public void onSuccess(String result) {
Bean bean = new Gson().fromJson(result, Bean.class);
List<Bean.DataBean> data = bean.getData();
/* MyAdapter myAdapter = new MyAdapter(MainActivity.this, data);
mRv.setAdapter(myAdapter);*/
crosseTalkAdapter = new CrosseTalkAdapter(data, MainActivity.this);
mRv.setAdapter(crosseTalkAdapter);
}
@Override
public void onFailed(Exception e) {
}
});
}
private void showAlertDialog(final int i) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示:");
builder.setMessage("请问你要删除吗?");
builder.setIcon(R.mipmap.ic_launcher_round);
//点击对话框以外的区域是否让对话框消失
builder.setCancelable(false);
//设置正面按钮
builder.setPositiveButton("是的", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
crosseTalkAdapter.deleteItem(i);
crosseTalkAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
private void initView() {
mRv = findViewById(R.id.recycle);
mRv.setLayoutManager(new LinearLayoutManager(this));
}
}
接口回调
public interface OnNetListener {
void onSuccess(String result);
void onFailed(Exception e);
}
get请求
public void doGet(String url, final OnNetListener onNetListener) {
//创建Request
final Request request = new Request.Builder()
.url(url)
.build();
//发送请求
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
mHandler.post(new Runnable() {
@Override
public void run() {
onNetListener.onFailed(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//拿到服务器返回的数据
final String string = response.body().string();
mHandler.post(new Runnable() {
@Override
public void run() {
onNetListener.onSuccess(string);
}
});
}
});
}