Android SwipeSelector



Android SwipeSelector

Android SwipeSelector是github上一个第三方开源的项目,其项目主页:https://github.com/roughike/SwipeSelector 
SwipeSelector实现一种选择方式,类似于ViewPager,如动图所示:



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

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent">  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="vertical">  
  11.   
  12.         <TextView  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_marginLeft="16dp"  
  16.             android:layout_marginRight="16dp"  
  17.             android:layout_marginTop="16dp"  
  18.             android:text="披萨饼"  
  19.             android:textAppearance="@style/TextAppearance.AppCompat.Title" />  
  20.   
  21.         <TextView  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:layout_marginBottom="16dp"  
  25.             android:layout_marginLeft="16dp"  
  26.             android:layout_marginRight="16dp"  
  27.             android:text="选项"  
  28.             android:textAppearance="@style/TextAppearance.AppCompat.Body1" />  
  29.   
  30.         <View  
  31.             android:layout_width="match_parent"  
  32.             android:layout_height="1dp"  
  33.             android:layout_marginLeft="16dp"  
  34.             android:layout_marginRight="16dp"  
  35.             android:background="#DDDDDD" />  
  36.   
  37.         <com.roughike.swipeselector.SwipeSelector  
  38.             android:id="@+id/sizeSelector"  
  39.             android:layout_width="match_parent"  
  40.             android:layout_height="wrap_content" />  
  41.   
  42.         <View  
  43.             android:layout_width="match_parent"  
  44.             android:layout_height="1dp"  
  45.             android:layout_marginLeft="16dp"  
  46.             android:layout_marginRight="16dp"  
  47.             android:background="#DDDDDD" />  
  48.   
  49.         <com.roughike.swipeselector.SwipeSelector  
  50.             android:id="@+id/toppingSelector"  
  51.             android:layout_width="match_parent"  
  52.             android:layout_height="wrap_content" />  
  53.   
  54.         <View  
  55.             android:layout_width="match_parent"  
  56.             android:layout_height="1dp"  
  57.             android:layout_marginLeft="16dp"  
  58.             android:layout_marginRight="16dp"  
  59.             android:background="#DDDDDD" />  
  60.   
  61.         <com.roughike.swipeselector.SwipeSelector  
  62.             xmlns:app="http://schemas.android.com/apk/res-auto"  
  63.             android:id="@+id/deliverySelector"  
  64.             android:layout_width="match_parent"  
  65.             android:layout_height="wrap_content"  
  66.             app:swipe_descriptionGravity="center"  
  67.             app:swipe_descriptionTextAppearance="@style/TextAppearance.AppCompat.Body1"  
  68.             app:swipe_indicatorActiveColor="@android:color/holo_red_light"  
  69.             app:swipe_indicatorInActiveColor="@android:color/holo_blue_light"  
  70.             app:swipe_indicatorMargin="20dp"  
  71.             app:swipe_indicatorSize="10dp"  
  72.             app:swipe_titleTextAppearance="@style/TextAppearance.AppCompat.Title" />  
  73.   
  74.         <View  
  75.             android:layout_width="match_parent"  
  76.             android:layout_height="1dp"  
  77.             android:layout_marginLeft="16dp"  
  78.             android:layout_marginRight="16dp"  
  79.             android:background="#DDDDDD" />  
  80.   
  81.         <Button  
  82.             android:id="@+id/sendButton"  
  83.             style="?attr/buttonStyle"  
  84.             android:layout_width="match_parent"  
  85.             android:layout_height="wrap_content"  
  86.             android:layout_margin="16dp"  
  87.             android:background="@android:color/holo_orange_dark"  
  88.             android:text="提交"  
  89.             android:textColor="#FFFFFF" />  
  90.     </LinearLayout>  
  91. </ScrollView>  

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



上层Java代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package zhangphil.demo;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.view.View;  
  6. import android.widget.Toast;  
  7.   
  8. import com.roughike.swipeselector.SwipeItem;  
  9. import com.roughike.swipeselector.SwipeSelector;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.     /** 
  13.      * Size options 
  14.      */  
  15.     private static final int SIZE_KIDS = 0;  
  16.     private static final int SIZE_NORMAL = 1;  
  17.     private static final int SIZE_LARGE = 2;  
  18.     private static final int SIZE_HUGE = 3;  
  19.   
  20.     /** 
  21.      * Topping options 
  22.      */  
  23.     private static final int TOPPINGS_EMILY = 0;  
  24.     private static final int TOPPINGS_BOB = 1;  
  25.     private static final int TOPPINGS_HANS = 2;  
  26.     private static final int TOPPINGS_ANDREI = 3;  
  27.   
  28.     /** 
  29.      * 邮递选项 
  30.      */  
  31.     private static final int DELIVERY_NONE = 0;  
  32.     private static final int DELIVERY_YES = 1;  
  33.   
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_main);  
  38.   
  39.         final SwipeSelector sizeSelector = (SwipeSelector) findViewById(R.id.sizeSelector);  
  40.         sizeSelector.setItems(  
  41.                 new SwipeItem(-1"请选择尺寸""Start by swiping left."),  
  42.                 new SwipeItem(SIZE_KIDS, "Kids' size""For the small appetite. Can be shared by four toddlers."),  
  43.                 new SwipeItem(SIZE_NORMAL, "Normal""Our most popular size. Ideal for kids before their growth spurt."),  
  44.                 new SwipeItem(SIZE_LARGE, "Large""This is two times the normal size. Suits well for the hangover " +  
  45.                         "after a bachelor party."),  
  46.                 new SwipeItem(SIZE_HUGE, "Huge""Suitable for families. Also perfect for a bigger appetite if your " +  
  47.                         "name happens to be Furious Pete.")  
  48.         );  
  49.   
  50.         final SwipeSelector toppingSelector = (SwipeSelector) findViewById(R.id.toppingSelector);  
  51.         toppingSelector.setItems(  
  52.                 new SwipeItem(-1"Select toppings""Start by swiping left."),  
  53.                 new SwipeItem(TOPPINGS_EMILY, "Aunt Emily's""Strawberries, potatoes and cucumber. Just what Aunt " +  
  54.                         "Emily found in her backyard."),  
  55.                 new SwipeItem(TOPPINGS_BOB, "Uncle Bob's Special""Ranch dressing, bacon, kebab and double pepperoni. " +  
  56.                         "And also some lettuce, because lettuce is healthy."),  
  57.                 new SwipeItem(TOPPINGS_HANS, "Hans' Meat Monster""Ham, sauerbraten, salami and bratwurst. Hans likes " +  
  58.                         "his meat."),  
  59.                 new SwipeItem(TOPPINGS_ANDREI, "Andreis' Russian Style""Whole pickles and sour cream. Prijat" +  
  60.                         "novo appetita!")  
  61.         );  
  62.   
  63.         final SwipeSelector deliverySelector = (SwipeSelector) findViewById(R.id.deliverySelector);  
  64.         deliverySelector.setItems(  
  65.                 new SwipeItem(-1"选择邮递选项""Start by swiping left."),  
  66.                 new SwipeItem(DELIVERY_NONE, "No delivery""Come to our lovely restaurant and pick up the pizza yourself."),  
  67.                 new SwipeItem(DELIVERY_YES, "Delivery""Our minimum-wage delivery boy will bring you the pizza by his own " +  
  68.                         "scooter using his own gas money.")  
  69.         );  
  70.   
  71.         findViewById(R.id.sendButton).setOnClickListener(new View.OnClickListener() {  
  72.             @Override  
  73.             public void onClick(View v) {  
  74.                 SwipeItem selectedSize = sizeSelector.getSelectedItem();  
  75.                 SwipeItem selectedToppings = toppingSelector.getSelectedItem();  
  76.                 SwipeItem selectedDelivery = deliverySelector.getSelectedItem();  
  77.   
  78.                 String toastMessage = "";  
  79.   
  80.                 if ((Integer) selectedSize.value != -1) {  
  81.                     toastMessage += "Size: " + selectedSize.title;  
  82.                 } else {  
  83.                     toastMessage += "No size selected.";  
  84.                 }  
  85.   
  86.                 if ((Integer) selectedToppings.value != -1) {  
  87.                     toastMessage += "\nToppings: " + selectedToppings.title;  
  88.                 } else {  
  89.                     toastMessage += "\nNo toppings selected.";  
  90.                 }  
  91.   
  92.                 if ((Integer) selectedDelivery.value != -1) {  
  93.                     toastMessage += "\nDelivery: " + selectedDelivery.title;  
  94.                 } else {  
  95.                     toastMessage += "\nNo delivery method selected.";  
  96.                 }  
  97.   
  98.                 Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_LONG).show();  
  99.             }  
  100.         });  
  101.     }  
  102. }  



代码运行结果:

0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值