Android滑动卡片效果:AndroidSwipeableCardStack

本文介绍了如何在Android项目中实现滑动卡片效果,通过下载.aar文件并将其添加到项目库中,配置布局文件,创建自定义适配器,并监听卡片堆栈事件来实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类社交app tinder的滑动卡片效果,流畅,体验很好。可以用来实现滑到左边喜欢,右边不喜欢之类的功能,卡片内容的添加是用的Adapter。

Installation

  1. Download released .aar file Download current release

  2. put it into your project lib dir, "libs" for example.

  3. put following lines to your gradle.build file

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile(name:'android-card-stack-0.1.0', ext:'aar')
}

Configuration

Put CardStack in your layout file

 <com.wenchao.cardstack.CardStack
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding = "20dp"
        android:clipChildren="false"
        android:clipToPadding="false"
    />

 

Create your card view layout file.

Example: card_layout.xml, contain only a textview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

</LinearLayout>

 

Implement your own adapter for the card stack. The CardStack will accept ArrayAdapter. The Following example extends a simeple ArrayAdapter, overridinggetView()to supply your costomized card layout

public class CardsDataAdapter extends ArrayAdapter<String> {

    @Override
    public View getView(int position, final View contentView, ViewGroup parent){
        //supply the layout for your card
        TextView v = (TextView)(contentView.findViewById(R.id.content));
        v.setText(getItem(position));
        return contentView;
    }

}

 

Get the CardStack instance in your activity

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        mCardStack = (CardStack)findViewById(R.id.container);

        mCardStack.setContentResource(R.layout.card_content);
        mCardStack.setStackMargin(20);

  }

 

Finally, set the adapter

    mCardAdapter = new CardsDataAdapter(getApplicationContext(),0);
    mCardAdapter.add("test1");
    mCardAdapter.add("test2");
    mCardAdapter.add("test3");
    mCardAdapter.add("test4");
    mCardAdapter.add("test5");

    mCardStack.setAdapter(mCardAdapter);

Listening to card stack event

implement CardStack.CardEventListener, and set it as listenermCardStack.setListener(yourListener);

Class YourListener extends CardStack.CardEventListener{
    //implement card event interface
    @Override
    public boolean swipeEnd(int direction, float distance) {
        //if "return true" the dismiss animation will be triggered 
        //if false, the card will move back to stack
        //distance is finger swipe distance in dp

        //the direction indicate swipe direction
        //there are four directions
        //  0  |  1
        // ----------
        //  2  |  3

        return (distance>300)? true : false;
    }

    @Override
    public boolean swipeStart(int direction, float distance) {

        return true;
    }

    @Override
    public boolean swipeContinue(int direction, float distanceX, float distanceY) {

        return true;
    }

    @Override
    public void discarded(int direction,int direction) {
       //this callback invoked when dismiss animation is finished. 
    }

    @Override
    public void topCardTapped() {
         //this callback invoked when a top card is tapped by user. 
    }
}

 



项目主页:http://www.open-open.com/lib/view/home/1446624860263

类似社交app tinder的滑动卡片效果,流畅,体验很好。可以用来实现滑到左边喜欢,右边不喜欢之类的功能,卡片内容的添加是用的Adapter。项目地址:https://github.com/wenchaojiang/AndroidSwipeableCardStack 效果图:如何使用创建控件实例<com.wenchao.cardstack.CardStack         android:id="@ id/container"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:padding = "20dp"         android:clipChildren="false"         android:clipToPadding="false"     />mCardStack = (CardStack)findViewById(R.id.container);2. 设置单张卡片的布局文件mCardStack.setContentResource(R.layout.card_content);3.设置AdaptermCardStack.setAdapter(mCardAdapter);一个简单的Adapterpublic class CardsDataAdapter extends ArrayAdapter<String> {     public CardsDataAdapter(Context context, int resource) {         super(context, resource);     }     @Override     public View getView(int position, final View contentView, ViewGroup parent){         TextView v = (TextView)(contentView.findViewById(R.id.content));         v.setText(getItem(position));         return contentView;     } }由于在上面已经设置了单张卡片的布局文件R.layout.card_content,所以在getView()中你不需要在加载并实例化R.layout.card_content,CardStack已经帮你实例化,你只需要使用contentView。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值