Android5.0 development examples Book Reading Notes (3) and android5.0 examples
(2) User Interaction
2.14 forward touch events
1. TouchDelegate is suitable for simple touch forwarding. It specifies any rectangular area to forward touch events to a small view. Its disadvantage is that each forwarded event will be forwarded to the middle position of the proxy view.
public class TouchDelegateLayout extends FrameLayout { public TouchDelegateLayout(Context context) { this(context, null); } public TouchDelegateLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TouchDelegateLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private CheckBox mCheckBox; private void init(Context context) { mCheckBox = new CheckBox(context); mCheckBox.setText("tap anywhere"); addView(mCheckBox, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER)); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (w != oldw || h != oldh) { Rect rect = new Rect(0, 0, w, h); TouchDelegate touchDelegate = new TouchDelegate(rect, mCheckBox); setTouchDelegate(touchDelegate); } }}
2. Custom touch forwarding
Change event information in onTouch
@Override public boolean onTouch(View v, MotionEvent event) { event.setLocation(event.getX(),event.getY()/2); scrollView.dispatchTouchEvent(event); return true; }
2.15 stop touch thieves
1. Call the requestDisallowTouchIntercept () method and request the parent control not to intercept the current touch event.
2.16 create a drag-and-drop View
Drag1. Use DragShadowBuilder to construct the exterior of the dragged View
2. Call View. startDrag () to enable drag.
@Override public boolean onLongClick(View v) { View.DragShadowBuilder shadowBuilder=new View.DragShadowBuilder(v); v.startDrag(null, shadowBuilder, ((ImageView) v).getDrawable(),0); return true; }
Release
3. You can use OnDragListener. onDrag () to listen to drag events. You can customize a view to implement the listener interface. The following is the core code.
@Override public boolean onDrag(View v, DragEvent event) { PropertyValuesHolder pvhX, pvhY; switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: pvhX = PropertyValuesHolder.ofFloat("scaleX", 0.5f); pvhY = PropertyValuesHolder.ofFloat("scaleY", 0.5f); ObjectAnimator.ofPropertyValuesHolder(this, pvhX, pvhY).start(); setImageDrawable(null); mDropped = false; break; case DragEvent.ACTION_DRAG_ENDED: if (!mDropped) { pvhX = PropertyValuesHolder.ofFloat("scaleX", 1f); pvhY = PropertyValuesHolder.ofFloat("scaleY", 1f); ObjectAnimator.ofPropertyValuesHolder(this, pvhX, pvhY).start(); mDropped = false; } break; case DragEvent.ACTION_DRAG_ENTERED: pvhX = PropertyValuesHolder.ofFloat("scaleX", 0.75f); pvhY = PropertyValuesHolder.ofFloat("scaleY", 0.75f); ObjectAnimator.ofPropertyValuesHolder(this, pvhX, pvhY).start(); break; case DragEvent.ACTION_DRAG_EXITED: pvhX = PropertyValuesHolder.ofFloat("scaleX", 0.5f); pvhY = PropertyValuesHolder.ofFloat("scaleY", 0.5f); ObjectAnimator.ofPropertyValuesHolder(this, pvhX, pvhY).start(); break; case DragEvent.ACTION_DROP: Keyframe frame0=Keyframe.ofFloat(0f,0.75f); Keyframe frame1=Keyframe.ofFloat(0.5f,0f); Keyframe frame2=Keyframe.ofFloat(1f,0.75f); pvhX = PropertyValuesHolder.ofKeyframe("scaleX", frame0,frame1,frame2); pvhY = PropertyValuesHolder.ofKeyframe("scaleY", frame0, frame1, frame2); ObjectAnimator.ofPropertyValuesHolder(this, pvhX, pvhY).start(); setImageDrawable((Drawable) event.getLocalState()); mDropped=true; break; default: return false; } return true; }
2.17 build navigation Drawer
1. DrawerLayout is provided only in the Android support library. The key point is to set gravity attributes.
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/middle" android:layout_width="match_parent" android:layout_height="match_parent"/> <ListView android:id="@+id/left" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#555"/> <LinearLayout android:id="@+id/right" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="right" android:orientation="vertical" android:background="#ccc"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is right"/> </LinearLayout></android.support.v4.widget.DrawerLayout>
2. When DrawerLayout is combined with ActionBar, use ActionBarDrawerToggle
mToggle = new ActionBarDrawerToggle(this, mDrawerContainer, R.string.open, R.string.close) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); supportInvalidateOptionsMenu(); } @Override public void onDrawerStateChanged(int newState) { super.onDrawerStateChanged(newState); supportInvalidateOptionsMenu(); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); supportInvalidateOptionsMenu(); } };
3. When combined with ToolBar, there are two similar points: one is the style of the topic, and the other is to call the setActionBar (toolbar) method.
2.18 slide between views
1. Define the page layout of PagerAdapter. The main method is instantiateItem ()
public class ImagePagerAdapter extends PagerAdapter { private Context mContext; private static final int[] IMAGES = { android.R.drawable.ic_menu_camera, android.R.drawable.ic_menu_add, android.R.drawable.ic_menu_delete, android.R.drawable.ic_menu_share, android.R.drawable.ic_menu_edit }; private static final int[] COLORS = { Color.RED, Color.BLUE, Color.GREEN, Color.GRAY, Color.MAGENTA }; public ImagePagerAdapter(Context context) { super(); mContext = context; } @Override public int getCount() { return IMAGES.length; } @Override public float getPageWidth(int position) { return 0.333f; } @Override public boolean isViewFromObject(View view, Object object) { return (view == object); } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView iv = new ImageView(mContext); iv.setImageResource(IMAGES[position]); iv.setBackgroundColor(COLORS[position]); container.addView(iv); return iv; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); }}
2. Adapt adaper to viewpager
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ImagePagerAdapter adapter=new ImagePagerAdapter(this); ViewPager viewPager=new ViewPager(this); viewPager.setAdapter(adapter); setContentView(viewPager); }
2.18 use tab navigation
1. There is an SlidingTabBasic SDK in the sample. First, copy the SlidingTabLayout and SlidingTabStrip to your project.
2. Use the TabsPagerAdapter to adapt the content. The core method is still instantiateItem ()
private static class TabsPagerAdapter extends PagerAdapter { private Context mContext; public TabsPagerAdapter(Context mContext) { this.mContext = mContext; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "PRIMARY"; case 1: return "SECONDARY"; case 2: return "TERTIARY"; case 3: return "QUATERNARY"; case 4: return "QUINARY"; default: return ""; } } @Override public int getCount() { return 5; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(mContext); imageView.setScaleType(ImageView.ScaleType.CENTER); imageView.setImageResource(R.mipmap.ic_launcher); container.addView(imageView); return imageView; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
3. Finally, you can call it in activity.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_action_tabs); ViewPager pager = (ViewPager) findViewById(R.id.pager); SlidingTabLayout tabLayout = (SlidingTabLayout) findViewById(R.id.tabs); assert pager != null; pager.setAdapter(new TabsPagerAdapter(this)); assert tabLayout != null; tabLayout.setViewPager(pager); tabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { @Override public int getIndicatorColor(int position) { return Color.WHITE; } @Override public int getDividerColor(int position) { return 0; } }); }
4. There are a lot of examples of samples that Google comes with. After the soft test, the official documents and API examples will be taken one by one.