Android SwipeSelector
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(); } }); } }
代码运行结果: