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. C# System.IO 文件流输入输出

    一.读写文本文件 可以用fileStream来读写文本文件,但是FileStream是通过字节形式来读写数据的,要把字节数据转换为文本,要自己处理编码转换. 对于文本文件的读写,通常用 StreamR ...

  2. Tian Ji -- The Horse Racing HDU - 1052

    Tian Ji -- The Horse Racing HDU - 1052 (有平局的田忌赛马,田忌赢一次得200块,输一次输掉200块,平局不得钱不输钱,要使得田忌得到最多(如果只能输就输的最少) ...

  3. C# 操作Access的Ole对象[转]

    原文链接 OLE对象数据类型 (1)OLE 对象用于使用 OLE 协议在其他程序中创建的 OLE 对象,如 Microsoft Word 文档. Microsoft Excel 电子表格.图片.声音或 ...

  4. 嵌套查询--------关联一对多关系----------collection

    参考来源:   http://www.cnblogs.com/LvLoveYuForever/p/6689577.html <resultMap id="BaseResultMap&q ...

  5. js学习笔记-事件委托

    通过事件委托,你可以把事件处理器绑定到父元素上,避免了把事件处理器添加到多个子级元素上.从而优化性能. 事件代理用到了事件冒泡和目标元素.而任何一个元素的目标元素都是一开始的那个元素. 这里首先要注意 ...

  6. [Luogu1848][USACO12OPEN]书架Bookshelf DP+set+决策单调性

    题目链接:https://www.luogu.org/problem/show?pid=1848 题目要求书必须按顺序放,其实就是要求是连续的一段.于是就有DP方程$$f[i]=min\{f[j]+m ...

  7. Farseer.net轻量级ORM开源框架 V1.x 入门篇:数据库配置文件

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:新版本说明 下一篇:Farseer.net轻量级ORM开源框架 ...

  8. jquery日期控件+时分秒

    因为项目需要,一些时间上的查询要精确的时分.先看下效果图吧. 所需要的js 跟css 文件 jsp://特别注意引入的先后顺序 <link rel="stylesheet" ...

  9. QQ感叹号是什么鬼?原来是服务器波动,腾讯官方来辟谣了

    今天晚上很多网友在用QQ发送消息的时候发现,自己发送的消息一直是感叹号❗到底是怎么回事呢?是消息都发不出去了吗?马浩周通过手机测试后发现,其实消息是可以发出去的,而官方手机QQ出来已经通知了,是服务器 ...

  10. 自己编辑Nuget拓展包,并发布Nuget服务器,提供下载使用

    1. 在NuGet官网上注册并获取API Key 到NuGet上注册一个新的账号,然后在My Account页面,获取一个API Key,如果没有则在API keys 页面创建一个就可以. 2. 下载 ...