dependencies {
compile 'com.android.support:recyclerview-v7:26.1.0'
// okhttp
compile 'com.squareup.okhttp3:okhttp:3.3.0'
// 解析Gson
compile 'com.google.code.gson:gson:2.4'
// 图片加载Glide
compile 'com.github.bumptech.glide:glide:3.7.0'
}
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public final String goods_url = "http://365jia.cn/uploads/";
public final int TYPE_ONE = 0;
public final int TYPE_TWO = 1;
List data;
Context context;
private MyLongItemClick onLongItemClick;
public MyAdapter(List data, Context context) {
this.data = data;
this.context = context;
}
@Override
public int getItemViewType(int position) {
// 根据集合内图片个数设置多条目
int size = data.get(position).getPics().size();
Log.d("MyAdapter-----", "size:" + size);
if (size == 1) {
return TYPE_ONE;
} else if (size == 3) {
return TYPE_TWO;
}
return 0;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// 根据viewType选择多条目初始化布局
if (viewType == TYPE_ONE) {
Log.d("MyAdapter", "条目一");
View view = View.inflate(context, R.layout.layout_recycler_1, null);
return new MyViewHolder(view);
} else if (viewType == TYPE_TWO) {
Log.d("MyAdapter", "条目二");
View view1 = View.inflate(context, R.layout.layout_recycler_2, null);
return new MyViewHolder1(view1);
}
return null;
}
private static final String TAG = "MyAdapter----";
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof MyViewHolder) {
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onLongItemClick.onItemLongClick(v, position);
return true;
}
});
Log.d(TAG, "onBindViewHolder: " + ((MyViewHolder) holder).title1);
((MyViewHolder) holder).title1.setText(data.get(position).getTitle());
Glide.with(context).load(goods_url + data.get(position).getPics().get(0)).into(((MyViewHolder) holder).image1);
//-----------------------------------------------------------------------------------
//属性动画透明
((MyViewHolder) holder).image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//属性动画透明
ObjectAnimator alpha = ObjectAnimator.ofFloat(((MyViewHolder) holder).image1, "alpha", 1, 0, 1);
alpha.setDuration(2000);
alpha.start();
}
});
//-----------------------------------------------------------------------------------------------
}
if (holder instanceof MyViewHolder1) {
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onLongItemClick.onItemLongClick(v, position);
return true;
}
});
((MyViewHolder1) holder).title2.setText(data.get(position).getTitle());
// 拼接图片地址
Glide.with(context).load(goods_url + data.get(position).getPics().get(0)).into(((MyViewHolder1) holder).image2_1);
Glide.with(context).load(goods_url + data.get(position).getPics().get(1)).into(((MyViewHolder1) holder).image2_2);
Glide.with(context).load(goods_url + data.get(position).getPics().get(2)).into(((MyViewHolder1) holder).image2_3);
}
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
public final TextView title1;
public final ImageView image1;
public MyViewHolder(View itemView) {
super(itemView);
title1 = itemView.findViewById(R.id.recy_1_title);
image1 = itemView.findViewById(R.id.recy_1_image);
}
}
class MyViewHolder1 extends RecyclerView.ViewHolder {
private final TextView title2;
private final ImageView image2_1;
private final ImageView image2_2;
private final ImageView image2_3;
public MyViewHolder1(View itemView) {
super(itemView);
image2_1 = itemView.findViewById(R.id.recy_2_image1);
title2 = itemView.findViewById(R.id.recy_2_title);
image2_2 = itemView.findViewById(R.id.recy_2_image2);
image2_3 = itemView.findViewById(R.id.recy_2_image3);
}
}
//长按监听
public interface MyLongItemClick {
void onItemLongClick(View view, int postion);
}
//外部调用
public void setOnLongItemClick(MyLongItemClick onLongItemClick) {
this.onLongItemClick = onLongItemClick;
}
}
public class MainActivity extends AppCompatActivity implements IMainView {
private RecyclerView mMyrecycler;
private MyAdapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
PresenterImpl presenter = new PresenterImpl();
presenter.showGoodsToView(new ModleImpl(),this);
// 创建布局管理器
LinearLayoutManager layoutManager = new LinearLayoutManager(this );
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// 设置recyclerview
mMyrecycler.setLayoutManager(layoutManager);
}
@Override
public void showGoodsList(final List data) {
Toast.makeText(this, "data.size():" + data.size(), Toast.LENGTH_SHORT).show();
myadapter = new MyAdapter(data,this);
mMyrecycler.setAdapter(myadapter);
// 设置长按点击
myadapter.setOnLongItemClick(new MyAdapter.MyLongItemClick() {
@Override
public void onItemLongClick(View view, final int postion) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("确定要删除?");
builder.setNegativeButton("取消", null);
builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
data.remove(postion);
myadapter.notifyDataSetChanged();
}
});
builder.show();
}
});
// 添加分割线
mMyrecycler.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
}
private void initView() {
mMyrecycler = (RecyclerView) findViewById(R.id.myrecycler);
}
}
自定义点击事件
// 内部调用
public interface HistroyRecycListener{
void onItemClick(int position);
}
// 外部调用
public void setOnRecycListener(HistroyRecycListener histroyRecycListener){
this.histroyRecycListener = histroyRecycListener;
}