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. 最短路(Dijkstra) HDOJ 4318 Power transmission

    题目传送门 题意:起点s到终点t送电,中途会有损耗,问最小损耗是多少 分析:可以转换为单源最短路问题,用优先队列的Dijkstra版本,d[]表示从s出发到当前点的最小损耗,用res保存剩下的电量.当 ...

  2. 找规律 Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks

    题目传送门 /* 找规律,水 */ #include <cstdio> #include <iostream> #include <algorithm> #incl ...

  3. shell 调试 `<<' is not matched

    我的这段脚本,验证数据库连接是否正常: #! /bin/sh...while ..do....sqlplus $user/ $passwd@$sid  <<!quit;! ... 单独执行 ...

  4. Spirng MVC +Velocity 表单绑定命令对象

    通常,表单中的数据在提交之后可以通过Spring MVC的@RequestParam注解在控制器函数的参数列表中中提取出来,但是一旦表单数据过多的话,参数列表将会变得非常长,最好的解决方案是将表单中的 ...

  5. shell脚本中定义路径变量出现的BUG

    =========================================================================== if 语句中的定义路径变量 引发命令的PATH路 ...

  6. 来自AJPFX的二分法查找

    package com.heima.array; public class Demo2_Array { /**         * * A:案例演示                        * ...

  7. 一个小方法解决RGBA不兼容IE8

    原网页http://blog.csdn.net/leihope_/article/details/70158902 要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如 ...

  8. ES6特性的两点分析

    块级作用域声明let.constES6中const 和let的功能,转换为ES5之后,我们会发现实质就是在块级作用改变一下变量名,使之与外层不同.ES6转换前: let a1 = 1; let a2 ...

  9. 迅为IMX6核心板开发平台智能交通解决方案

    智能交通系统它是将先进的信息技术.数据通讯传输技术.电子传感技术.控制技术及计算机技术等有效地集成运用于整个地面交通管理系统而建立的一种在大范围内.全方位发挥作用的,实时.准确.高效的综合交通运输管理 ...

  10. sh NonUniqueObjectException

    话题引入: 使用hibernate进行更新操作时,首先调用了findById方法获取要修改的对象,此时session没有被关闭,接着重新创建一个对象,将要修改的属性值赋值给这个对象.调用修改方法抛出如 ...