转载请注明出处:王亟亟的大牛之路
刚上班2,3天还没从放假的节奏中缓过来,总觉得懒洋洋的哈哈哈。
继续之前的介绍一些好用/有意思/有学习意义的干活给大家,今天是Swipecards
地址:https://github.com/Diolor/Swipecards
效果:
在国内的一些社交类的App中也见过类似的实现。
How to use?
Gradle:
dependencies {
compile 'com.lorentzos.swipecards:library:X.X.X@aar'
}
Eclipse的话就要把源码都Copy进去了(还好东西不多)
简单的介绍下怎么使用
首先,在你的布局里面拽入这个控件
<com.lorentzos.flingswipe.SwipeFlingAdapterView
android:id="@+id/frame"
android:background="#ffeee9e2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rotation_degrees="15.5"
tools:context=".MyActivity" />
他有一些自定义的标签,像这样
<declare-styleable name="SwipeFlingAdapterView">
<attr name="SwipeFlingStyle" format="reference"/>
<attr name="rotation_degrees" format="float"/>
<attr name="min_adapter_stack" format="integer"/>
<attr name="max_visible" format="integer"/>
</declare-styleable>
都是一些对控件进行配置的,具体的属性大家可以看下源码,这里就不做解释了
接下来那我们就在事物的Activity里获取这个控件(这边是用依赖注入做的)
@InjectView(R.id.frame) SwipeFlingAdapterView flingContainer;
再给我们的控件添加数据源
al = new ArrayList<>();
al.add("php");
al.add("c");
al.add("python");
al.add("java");
al.add("html");
al.add("c++");
al.add("css");
al.add("javascript");
arrayAdapter = new ArrayAdapter<>(this, R.layout.item, R.id.helloText, al );
flingContainer.setAdapter(arrayAdapter);
添加控件的事件监听
当卡片被移除,无论左右都会触发removeFirstObjectInAdapter()
左边触发onLeftCardExit()
右边出发onRightCardExit()
没数据了调用onAdapterAboutToEmpty()
滑动的过程中调用onScroll()
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
@Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
al.remove(0);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object dataObject) {
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
makeToast(MyActivity.this, "Left!");
}
@Override
public void onRightCardExit(Object dataObject) {
makeToast(MyActivity.this, "Right!");
}
@Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
// Ask for more data here
al.add("XML ".concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
@Override
public void onScroll(float scrollProgressPercent) {
View view = flingContainer.getSelectedView();
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
});
还支持点击时间(就是没有滑,单纯的点)
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
@Override
public void onItemClicked(int itemPosition, Object dataObject) {
makeToast(MyActivity.this, "Clicked!");
}
});
}
源码在最上面的连接里有!!!
谢谢!!!