package com.jereh.biyingapplication.adapter;第三步:现在已经有了适配器,布局中也已经有了组件,剩下要做的就是在java代码中把他们关联起来并给添加数据以及对各种事件做监听了:
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.huxq17.swipecardsview.BaseCardAdapter;
import com.jereh.biyingapplication.entity.CardImg;
import com.jereh.biyingapplication.*;
import java.util.List;
/**
* Created by zhangdi on 2016/8/31.
*/
public class MyAdapter extends BaseCardAdapter {
private List<CardImg> datas;
private Context context;
public MyAdapter(List<CardImg> datas, Context context) {
this.datas = datas;
this.context = context;
}
/**
* 获取卡片的数量
*
* @return
*/
@Override
public int getCount() {
if (datas.size()<1){
return 0;
}else {
return datas.size();
}
}
/**
* 获取卡片view的layout id
*
* @return
*/
@Override
public int getCardLayoutId() {
return R.layout.card_item;
}
/**
* 将卡片和数据绑定在一起
*
* @param position 数据在数据集中的位置
* @param cardview 要绑定数据的卡片
*/
@Override
public void onBindData(int position, View cardview) {
if (datas == null || datas.size() == 0) {
return;
}
ImageView imageView =
(ImageView) cardview.findViewById(R.id.lv_img);
CardImg img = datas.get(position);
Glide.with(context)
.load(img.getImg())
.crossFade()
.centerCrop()
.into(imageView);
}
/**
* 获取可见的cardview的数目,默认是3
* @return
*/
@Override
public int getVisibleCardCount() {
return datas.size();
}
}
swipeCardsView =下面就是定义各种事件的方法以及关联适配器展示界面的方法:(其中mList是数据)
(SwipeCardsView)view.findViewById(R.id.swipCardsView);
//设置滑动监听
swipeCardsView
.setCardsSlideListener(new SwipeCardsView.CardsSlideListener() {
@Override
public void onShow(int index) {
Log.i("test showing index = ", "" + index);
}
@Override
public void onCardVanish(int index, SwipeCardsView.SlideType type) {
String orientation = "";
switch (type) {
case LEFT:
orientation = "向左飞出";
break;
case RIGHT:
orientation = "向右飞出";
break;
}
}
@Override
public void onItemClick(View cardImageView, int index) {
}
});
/**至此这个组件已经完全配置完成,简单吧,希望我的笔记能对大家有所帮助,更希望大家能在自己的APP中实现,谢谢
* 卡片向左边飞出
*/
public void doLeftOut() {
swipeCardsView.slideCardOut(SwipeCardsView.SlideType.LEFT);
}
/**
* 卡片向右边飞出
*/
public void doRightOut() {
swipeCardsView.slideCardOut(SwipeCardsView.SlideType.RIGHT);
}
/**
* 显示cardsview
*/
private void show() {
if (adapter == null) {
adapter = new MyAdapter(mList, getActivity());
swipeCardsView.setAdapter(adapter);
} else {
swipeCardsView.notifyDatasetChanged(mList.indexOf(mList));
}
}
原文:http://www.cnblogs.com/zhangdiIT/p/5829589.html