重中之重 需要关联一个library包
1.导依赖
compile 'com.android.support:cardview-v7:26.0.0-alpha1'
2.mainactivity布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#cd4827">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="探索"
android:textColor="#fff"
android:textSize="17sp" />
<ImageView
android:id="@+id/notify_change"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right|center_vertical"
android:padding="16dp"
android:src="@drawable/download" />
</FrameLayout>
<com.stone.card.library.CardSlidePanel
android:id="@+id/image_slide_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
card:bottomMarginTop="38dp"
card:itemMarginTop="10dp"
card:yOffsetStep="13dp" />
</LinearLayout>
3.卡片辅助类
/**
* 卡片数据装载对象
*
* @author xmuSistone
*/
public class CardDataItem {
public String imagePath;
public String userName;
public int likeNum;
public int imageNum;
}
4.MainActivity 本人在fragment里用的
public class FragmentThree extends BaseFragmetActivity<MovePresenter> implements IMoveView {
private CardSlidePanel.CardSwitchListener cardSwitchListener;
List<String> list=new ArrayList<>();
List<String> liststr=new ArrayList<>();
private List<CardDataItem> dataList = new ArrayList<>();
private MyRecyAdapter adapter;
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = View.inflate(getActivity(), R.layout.fragment_three, null);
initView();
presenter.showmoveData();
return view;
}
@Override
public void initCreate() {
presenter = new MovePresenter(this);
}
@Override
public void getmoveData(List<Movebean.RetBean.ListBean> movebean) {
for (int i = 0; i < movebean.size(); i++) {
list.add(movebean.get(i).getPic());
liststr.add(movebean.get(i).getTitle());
}
}
private void initView() {
final CardSlidePanel slidePanel = (CardSlidePanel) view.findViewById(R.id.image_slide_panel);
// 1. 左右滑动监听
cardSwitchListener = new CardSlidePanel.CardSwitchListener() {
@Override
public void onShow(int index) {
Log.d("Card", "正在显示-" + dataList.get(index).userName);
}
@Override
public void onCardVanish(int index, int type) {
Log.d("Card", "正在消失-" + dataList.get(index).userName + " 消失type=" + type);
}
};
slidePanel.setCardSwitchListener(cardSwitchListener);
// 2. 绑定Adapter
slidePanel.setAdapter(new CardAdapter() {
@Override
public int getLayoutId() {
return R.layout.card_item;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public void bindView(View view, int index) {
Object tag = view.getTag();
ViewHolder viewHolder;
if (null != tag) {
viewHolder = (ViewHolder) tag;
} else {
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}
viewHolder.bindData(dataList.get(index));
}
@Override
public Object getItem(int index) {
return dataList.get(index);
}
@Override
public Rect obtainDraggableArea(View view) {
// 可滑动区域定制,该函数只会调用一次
View contentView = view.findViewById(R.id.card_item_content);
View topLayout = view.findViewById(R.id.card_top_layout);
View bottomLayout = view.findViewById(R.id.card_bottom_layout);
int left = view.getLeft() + contentView.getPaddingLeft() + topLayout.getPaddingLeft();
int right = view.getRight() - contentView.getPaddingRight() - topLayout.getPaddingRight();
int top = view.getTop() + contentView.getPaddingTop() + topLayout.getPaddingTop();
int bottom = view.getBottom() - contentView.getPaddingBottom() - bottomLayout.getPaddingBottom();
return new Rect(left, top, right, bottom);
}
});
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
prepareDataList();
slidePanel.getAdapter().notifyDataSetChanged();
}
}, 500);
// 3. notifyDataSetChanged调用
view.findViewById(R.id.notify_change).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appendDataList();
slidePanel.getAdapter().notifyDataSetChanged();
}
});
}
private void prepareDataList() {
for (int i = 0; i < list.size(); i++) {
CardDataItem dataItem = new CardDataItem();
// dataItem.userName = names[i];
dataItem.userName = liststr.get(i);
// dataItem.imagePath = imagePaths[i];
dataItem.imagePath = list.get(i);
dataItem.likeNum = (int) (Math.random() * 10);
dataItem.imageNum = (int) (Math.random() * 6);
dataList.add(dataItem);
}
}
private void appendDataList() {
for (int i = 0; i < list.size(); i++) {
CardDataItem dataItem = new CardDataItem();
dataItem.userName = "From Append";
// dataItem.imagePath = imagePaths[8];
dataItem.imagePath = list.get(i);
dataItem.likeNum = (int) (Math.random() * 10);
dataItem.imageNum = (int) (Math.random() * 6);
dataList.add(dataItem);
}
}
class ViewHolder {
ImageView imageView;
View maskView;
TextView userNameTv;
TextView imageNumTv;
TextView likeNumTv;
public ViewHolder(View view) {
imageView = (ImageView) view.findViewById(R.id.card_image_view);
maskView = view.findViewById(R.id.maskView);
userNameTv = (TextView) view.findViewById(R.id.card_user_name);
imageNumTv = (TextView) view.findViewById(R.id.card_pic_num);
likeNumTv = (TextView) view.findViewById(R.id.card_like);
}
public void bindData(CardDataItem itemData) {
Glide.with(getActivity()).load(itemData.imagePath).into(imageView);
userNameTv.setText(itemData.userName);
imageNumTv.setText(itemData.imageNum + "");
likeNumTv.setText(itemData.likeNum + "");
}
}
}
5.还有一个辅助布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/card_item_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/card_top_layout"
android:layout_width="match_parent"
android:layout_height="230dp"
android:background="@drawable/top">
<ImageView
android:id="@+id/card_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<View
android:id="@+id/maskView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:clickable="true" />
<TextView
android:id="@+id/card_pic_num"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_margin="5dp"
android:background="#5f000000"
android:drawableLeft="@drawable/card_photot"
android:drawablePadding="4dp"
android:gravity="center"
android:paddingLeft="6dp"
android:paddingRight="4dp"
android:text="6"
android:textColor="#fff" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/card_bottom_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bottom"
android:paddingTop="10dp">
<TextView
android:id="@+id/card_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="叶琪琪 23"
android:textColor="#111"
android:textSize="18sp" />
<TextView
android:id="@+id/card_other_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/card_user_name"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="16dp"
android:text="其它 6km4"
android:textColor="#888" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="3dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/card_left1"
android:drawablePadding="2dp"
android:gravity="center_horizontal"
android:text="0"
android:textColor="#999"
android:textSize="15sp" />
<TextView
android:id="@+id/card_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:drawableLeft="@drawable/card_left2"
android:drawablePadding="2dp"
android:gravity="center_horizontal"
android:text="2"
android:textColor="#999"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>