Android SwipeSelector

Android SwipeSelector是github上一个第三方开源的项目,其项目主页:https://github.com/roughike/SwipeSelector


SwipeSelector实现一种选择方式,类似于ViewPager,如动图所示:

SwipeSelector本身的例子比较清晰,容易看懂,使用SwipeSelector,先写一个布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="披萨饼"
android:textAppearance="@style/TextAppearance.AppCompat.Title" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="选项"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="#DDDDDD" /> <com.roughike.swipeselector.SwipeSelector
android:id="@+id/sizeSelector"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="#DDDDDD" /> <com.roughike.swipeselector.SwipeSelector
android:id="@+id/toppingSelector"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="#DDDDDD" /> <com.roughike.swipeselector.SwipeSelector
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/deliverySelector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:swipe_descriptionGravity="center"
app:swipe_descriptionTextAppearance="@style/TextAppearance.AppCompat.Body1"
app:swipe_indicatorActiveColor="@android:color/holo_red_light"
app:swipe_indicatorInActiveColor="@android:color/holo_blue_light"
app:swipe_indicatorMargin="20dp"
app:swipe_indicatorSize="10dp"
app:swipe_titleTextAppearance="@style/TextAppearance.AppCompat.Title" /> <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="#DDDDDD" /> <Button
android:id="@+id/sendButton"
style="?attr/buttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="@android:color/holo_orange_dark"
android:text="提交"
android:textColor="#FFFFFF" />
</LinearLayout>
</ScrollView>

注意最后一个SwipeSelector的变化,重新定制了一些样式。

上层Java代码:

package zhangphil.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast; import com.roughike.swipeselector.SwipeItem;
import com.roughike.swipeselector.SwipeSelector; public class MainActivity extends AppCompatActivity {
/**
* Size options
*/
private static final int SIZE_KIDS = 0;
private static final int SIZE_NORMAL = 1;
private static final int SIZE_LARGE = 2;
private static final int SIZE_HUGE = 3; /**
* Topping options
*/
private static final int TOPPINGS_EMILY = 0;
private static final int TOPPINGS_BOB = 1;
private static final int TOPPINGS_HANS = 2;
private static final int TOPPINGS_ANDREI = 3; /**
* 邮递选项
*/
private static final int DELIVERY_NONE = 0;
private static final int DELIVERY_YES = 1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final SwipeSelector sizeSelector = (SwipeSelector) findViewById(R.id.sizeSelector);
sizeSelector.setItems(
new SwipeItem(-1, "请选择尺寸", "Start by swiping left."),
new SwipeItem(SIZE_KIDS, "Kids' size", "For the small appetite. Can be shared by four toddlers."),
new SwipeItem(SIZE_NORMAL, "Normal", "Our most popular size. Ideal for kids before their growth spurt."),
new SwipeItem(SIZE_LARGE, "Large", "This is two times the normal size. Suits well for the hangover " +
"after a bachelor party."),
new SwipeItem(SIZE_HUGE, "Huge", "Suitable for families. Also perfect for a bigger appetite if your " +
"name happens to be Furious Pete.")
); final SwipeSelector toppingSelector = (SwipeSelector) findViewById(R.id.toppingSelector);
toppingSelector.setItems(
new SwipeItem(-1, "Select toppings", "Start by swiping left."),
new SwipeItem(TOPPINGS_EMILY, "Aunt Emily's", "Strawberries, potatoes and cucumber. Just what Aunt " +
"Emily found in her backyard."),
new SwipeItem(TOPPINGS_BOB, "Uncle Bob's Special", "Ranch dressing, bacon, kebab and double pepperoni. " +
"And also some lettuce, because lettuce is healthy."),
new SwipeItem(TOPPINGS_HANS, "Hans' Meat Monster", "Ham, sauerbraten, salami and bratwurst. Hans likes " +
"his meat."),
new SwipeItem(TOPPINGS_ANDREI, "Andreis' Russian Style", "Whole pickles and sour cream. Prijat" +
"novo appetita!")
); final SwipeSelector deliverySelector = (SwipeSelector) findViewById(R.id.deliverySelector);
deliverySelector.setItems(
new SwipeItem(-1, "选择邮递选项", "Start by swiping left."),
new SwipeItem(DELIVERY_NONE, "No delivery", "Come to our lovely restaurant and pick up the pizza yourself."),
new SwipeItem(DELIVERY_YES, "Delivery", "Our minimum-wage delivery boy will bring you the pizza by his own " +
"scooter using his own gas money.")
); findViewById(R.id.sendButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SwipeItem selectedSize = sizeSelector.getSelectedItem();
SwipeItem selectedToppings = toppingSelector.getSelectedItem();
SwipeItem selectedDelivery = deliverySelector.getSelectedItem(); String toastMessage = ""; if ((Integer) selectedSize.value != -1) {
toastMessage += "Size: " + selectedSize.title;
} else {
toastMessage += "No size selected.";
} if ((Integer) selectedToppings.value != -1) {
toastMessage += "\nToppings: " + selectedToppings.title;
} else {
toastMessage += "\nNo toppings selected.";
} if ((Integer) selectedDelivery.value != -1) {
toastMessage += "\nDelivery: " + selectedDelivery.title;
} else {
toastMessage += "\nNo delivery method selected.";
} Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_LONG).show();
}
});
}
}

代码运行结果:

Android SwipeSelector的更多相关文章

  1. 59.Android开源项目及库 (转)

    转载 : https://github.com/Tim9Liu9/TimLiu-Android?hmsr=toutiao.io&utm_medium=toutiao.io&utm_so ...

  2. Android开源项目及库搜集

    TimLiu-Android 自己总结的Android开源项目及库. github排名 https://github.com/trending,github搜索:https://github.com/ ...

  3. 各种Android UI开源框架 开源库

    各种Android UI开源框架 开源库 转 https://blog.csdn.net/zhangdi_gdk2016/article/details/84643668 自己总结的Android开源 ...

  4. 2019年最新android常用开源库汇总上篇(转)

    1.基本控件 1.1.TextView ScrollNumber ReadMoreTextView HtmlImage android-autofittextview html-textview Ba ...

  5. 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新

    本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...

  6. 配置android sdk 环境

    1:下载adnroid sdk安装包 官方下载地址无法打开,没有vpn,使用下面这个地址下载,地址:http://www.android-studio.org/

  7. Android SwipeRefreshLayout 下拉刷新——Hi_博客 Android App 开发笔记

    以前写下拉刷新 感觉好费劲,要判断ListView是否滚到顶部,还要加载头布局,还要控制 头布局的状态,等等一大堆.感觉麻烦死了.今天学习了SwipeRefreshLayout 的用法,来分享一下,有 ...

  8. Android Studio配置 AndroidAnnotations——Hi_博客 Android App 开发笔记

    以前用Eclicps 用习惯了现在 想学学 用Android Studio 两天的钻研终于 在我电脑上装了一个Android Studio 并完成了AndroidAnnotations 的配置. An ...

  9. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

随机推荐

  1. Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)

    题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...

  2. 洛谷 P1600 天天爱跑步

    https://www.luogu.org/problemnew/show/P1600 (仅做记录) 自己的假方法: 每一次跑从a到b:设l=lca(a,b)对于以下产生贡献: a到l的链上所有的点( ...

  3. 暴力(判凸四边形) FZOJ 2148 Moon Game

    题目传送门 题意:给了n个点的坐标,问能有几个凸四边形 分析:数据规模小,直接暴力枚举,每次四个点判断是否会是凹四边形,条件是有一个点在另外三个点的内部,那么问题转换成判断一个点d是否在三角形abc内 ...

  4. 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

    题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...

  5. 命名管道实现进程间通信--石头、剪刀、布游戏 分类: linux 2014-06-01 22:50 467人阅读 评论(0) 收藏

    下面这个程序利用命名管道实现进程间通信,模拟石头剪刀布游戏. 主进程为裁判进程,两个子进程为选手进程.裁判与选手间各建立一个命名管道. 进行100次出招,最后给出游戏胜负. #include < ...

  6. iOS生成PDF的关键代码-备忘

    //此方法只是把当前页面的内容生成PDF并保存在沙盒中. //还需要做:把当前面没有显示的内容以分页的形式生成PDF,并把PDF读取并显示出来 //关于显示可以参考:念茜的博客 iOS开发笔记——PD ...

  7. Xcode7 使用AFNetWorking 报错 添加Security.framework

    Undefined symbols for architecture x86_64: "_SecCertificateCopyData", referenced from: _AF ...

  8. Mybatis事务处理

    知识点有事务处理的配置,还有事务处理的方法 事务处理的配置: mybatis的事务处理由两种方式控制,JDBC和MANAGED:  MANAGED就是说事务处理由第三方的插件来完成,比如说spring ...

  9. android开发学习——facebook第三方登录,看了你不会后悔

    给APP用原生android进行facebook第三方登录. 我们做一件事情,首先得了解其原理,这样才不会迷茫,才知道自己做到什么程度了,心里才会有底. 所以,第一步,了解第三方登录的原理:下面贴一些 ...

  10. AJPFX总结面向对象特征之一的继承知识

    继 承(面向对象特征之一) 好处: 1:提高了代码的复用性. 2:让类与类之间产生了关系,提供了另一个特征多态的前提.   父类的由来:其实是由多个类不断向上抽取共性内容而来的. java中对于继承, ...