package
com.example.yk_moni1.base;
import
android.content.Context;
import
android.support.v7.widget.RecyclerView;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.ImageView;
import
android.widget.TextView;
import
com.bumptech.glide.Glide;
import
com.example.yk_moni1.MyGson;
import
com.example.yk_moni1.R;
import
java.util.List;
/**
* Created by John on 2018/5/30 0030.
*/
public class
MyBase
extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
private
Context
context
;
//上下文
private
List<MyGson.DataBean>
list
;
//集合
private static final int
IMG_ONE
=
0
;
//布局是一张图片的
private static final int
IMG_THREE
=
1
;
//布局是三张图片的
public
MyBase(Context context, List<MyGson.DataBean> list) {
this
.
context
= context;
this
.
list
= list;
}
/**
*
* @param
parent
* @param
viewType
* @return
* 加载布局 创建ViewHolder
*
*/
@Override
public
RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int
viewType) {
int
viewType1 = getItemViewType(viewType);
if
(viewType1 ==
IMG_ONE
){
View view = View.
inflate
(
context
, R.layout.
myabse_img1
,
null
);
MyViewHolder holder =
new
MyViewHolder(view);
return
holder;
}
else
{
View view = View.
inflate
(
context
, R.layout.
myabse_img3
,
null
);
MyViewHolder2 holder2 =
new
MyViewHolder2(view);
return
holder2;
}
}
//绑定数据
@Override
public void
onBindViewHolder(RecyclerView.ViewHolder holder,
final int
position) {
int
itemViewType = getItemViewType(position);
if
(itemViewType ==
IMG_ONE
){
MyViewHolder holder1 = (MyViewHolder) holder;
holder1.
txt_title1
.setText(
list
.get(position).getBrief());
holder1.
txt_time11
.setText(
list
.get(position).getDate());
holder1.
txt_laiy11
.setText(
list
.get(position).getSource());
//下载图片
List<MyGson.DataBean.LbimgBean> lbimg =
list
.get(position).getLbimg();
String src = lbimg.get(
0
).getSrc();
Glide.
with
(
context
).load(src).into(holder1.
img_tu11
);
}
if
(itemViewType ==
IMG_THREE
){
MyViewHolder2 holder2 = (MyViewHolder2) holder;
holder2.
txt_title31
.setText(
list
.get(position).getBrief());
holder2.
txt_time31
.setText(
list
.get(position).getDate());
holder2.
txt_laiy31
.setText(
list
.get(position).getSource());
//下载图片
List<MyGson.DataBean.LbimgBean> lbimg =
list
.get(position).getLbimg();
String src = lbimg.get(
0
).getSrc();
String src1 =
list
.get(position).getMiniimg().get(
0
).getSrc();
String src2 =
list
.get(position).getMiniimg02().get(
0
).getSrc();
Glide.
with
(
context
).load(src).into(holder2.
img_tu31
);
Glide.
with
(
context
).load(src1).into(holder2.
img_tu32
);
Glide.
with
(
context
).load(src2).into(holder2.
img_tu33
);
}
//设置条目长按点击事件
holder.
itemView
.setOnLongClickListener(
new
View.OnLongClickListener() {
@Override
public boolean
onLongClick(View view) {
if
(
icallBack
!=
null
){
icallBack
.onLongClick(
position
);
}
return false
;
}
});
}
//得到条目的数量
@Override
public int
getItemCount() {
return
list
.size();
}
//判断确定的条目 应该是什么布局
@Override
public int
getItemViewType(
int
position) {
if
(position%
3
==
0
){
return
IMG_ONE
;
}
else
{
return
IMG_THREE
;
}
}
public class
MyViewHolder
extends
RecyclerView.ViewHolder{
private final
ImageView
img_tu11
;
private final
TextView
txt_title1
;
private final
TextView
txt_time11
;
private final
TextView
txt_laiy11
;
public
MyViewHolder(View itemView) {
super
(itemView);
//获取资源ID
img_tu11
= itemView.findViewById(R.id.
img_tu11
);
txt_title1
= itemView.findViewById(R.id.
txt_title11
);
txt_time11
= itemView.findViewById(R.id.
txt_time11
);
txt_laiy11
= itemView.findViewById(R.id.
txt_laiy11
);
}
}
public class
MyViewHolder2
extends
RecyclerView.ViewHolder{
private final
ImageView
img_tu31
;
private final
ImageView
img_tu32
;
private final
ImageView
img_tu33
;
private final
TextView
txt_title31
;
private final
TextView
txt_time31
;
private final
TextView
txt_laiy31
;
public
MyViewHolder2(View itemView) {
super
(itemView);
//获取资源ID
img_tu31
= itemView.findViewById(R.id.
img_tu31
);
img_tu32
= itemView.findViewById(R.id.
img_tu32
);
img_tu33
= itemView.findViewById(R.id.
img_tu33
);
txt_title31
= itemView.findViewById(R.id.
txt_title3
);
txt_time31
= itemView.findViewById(R.id.
txt_time3
);
txt_laiy31
= itemView.findViewById(R.id.
txt_laiy3
);
}
}
//1.定义接口
public interface
IcallBack{
void
onLongClick(
int
position);
//条目长按事件
}
//2.声明接口对象
private
IcallBack
icallBack
;
//3.提供外部调用的方法
public void
setOnLongClick(IcallBack icallBack){
this
.
icallBack
= icallBack;
}
//删除数据
public void
removeItem(
int
position){
list
.remove(position);
notifyItemRemoved(position);
}
}