使用ViewDragHelper实现上拉或侧拉效果

转载声明:本文出自:http://gundumw100.iteye.com/blog/2114716

该例子位于https://github.com/umano/AndroidSlidingUpPanel 


ViewDragHelper的用法: 
http://flavienlaurent.com/blog/2013/08/28/each-navigation-drawer-hides-a-viewdraghelper/  

上拉前: 
使用ViewDragHelper实现上拉或侧拉效果_第1张图片

上拉中: 

使用ViewDragHelper实现上拉或侧拉效果_第2张图片

上拉后: 

使用ViewDragHelper实现上拉或侧拉效果_第3张图片

使用: 
Java代码   收藏代码
  1. package com.sothree.slidinguppanel.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.net.Uri;  
  6. import android.os.Build;  
  7. import android.os.Bundle;  
  8. import android.text.Html;  
  9. import android.text.method.LinkMovementMethod;  
  10. import android.util.Log;  
  11. import android.util.TypedValue;  
  12. import android.view.Menu;  
  13. import android.view.MenuItem;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.view.ViewGroup;  
  17. import android.view.Window;  
  18. import android.widget.Button;  
  19. import android.widget.TextView;  
  20.   
  21. import com.nineoldandroids.view.animation.AnimatorProxy;  
  22. import com.sothree.slidinguppanel.SlidingUpPanelLayout;  
  23. import com.sothree.slidinguppanel.SlidingUpPanelLayout.PanelSlideListener;  
  24.   
  25. public class DemoActivity extends Activity {  
  26.     private static final String TAG = "DemoActivity";  
  27.   
  28.     public static final String SAVED_STATE_ACTION_BAR_HIDDEN = "saved_state_action_bar_hidden";  
  29.   
  30.     private SlidingUpPanelLayout mLayout;  
  31.   
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);  
  36.         setContentView(R.layout.activity_demo);  
  37.   
  38.         mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);  
  39.         mLayout.setPanelSlideListener(new PanelSlideListener() {  
  40.             @Override  
  41.             public void onPanelSlide(View panel, float slideOffset) {  
  42.                 Log.i(TAG, "onPanelSlide, offset " + slideOffset);  
  43.                 setActionBarTranslation(mLayout.getCurrentParalaxOffset());  
  44.             }  
  45.   
  46.             @Override  
  47.             public void onPanelExpanded(View panel) {  
  48.                 Log.i(TAG, "onPanelExpanded");  
  49.   
  50.             }  
  51.   
  52.             @Override  
  53.             public void onPanelCollapsed(View panel) {  
  54.                 Log.i(TAG, "onPanelCollapsed");  
  55.   
  56.             }  
  57.   
  58.             @Override  
  59.             public void onPanelAnchored(View panel) {  
  60.                 Log.i(TAG, "onPanelAnchored");  
  61.             }  
  62.   
  63.             @Override  
  64.             public void onPanelHidden(View panel) {  
  65.                 Log.i(TAG, "onPanelHidden");  
  66.             }  
  67.         });  
  68.   
  69.         TextView t = (TextView) findViewById(R.id.main);  
  70.         t = (TextView) findViewById(R.id.name);  
  71.         t.setText(Html.fromHtml(getString(R.string.hello)));  
  72.           
  73.   
  74.         boolean actionBarHidden = savedInstanceState != null && savedInstanceState.getBoolean(SAVED_STATE_ACTION_BAR_HIDDEN, false);  
  75.         if (actionBarHidden) {  
  76.             int actionBarHeight = getActionBarHeight();  
  77.             setActionBarTranslation(-actionBarHeight);//will "hide" an ActionBar  
  78.         }  
  79.     }  
  80.   
  81.     @Override  
  82.     protected void onSaveInstanceState(Bundle outState) {  
  83.         super.onSaveInstanceState(outState);  
  84.         outState.putBoolean(SAVED_STATE_ACTION_BAR_HIDDEN, mLayout.isPanelExpanded());  
  85.     }  
  86.   
  87.     @Override  
  88.     public boolean onCreateOptionsMenu(Menu menu) {  
  89.         // Inflate the menu; this adds items to the action bar if it is present.  
  90.         getMenuInflater().inflate(R.menu.demo, menu);  
  91.         MenuItem item = menu.findItem(R.id.action_toggle);  
  92.         if (mLayout != null) {  
  93.             if (mLayout.isPanelHidden()) {  
  94.                 item.setTitle(R.string.action_show);  
  95.             } else {  
  96.                 item.setTitle(R.string.action_hide);  
  97.             }  
  98.         }  
  99.   
  100.         return true;  
  101.     }  
  102.   
  103.     @Override  
  104.     public boolean onPrepareOptionsMenu(Menu menu) {  
  105.         return super.onPrepareOptionsMenu(menu);  
  106.     }  
  107.   
  108.     @Override  
  109.     public boolean onOptionsItemSelected(MenuItem item) {  
  110.         switch (item.getItemId()){  
  111.             case R.id.action_toggle: {  
  112.                 if (mLayout != null) {  
  113.                     if (!mLayout.isPanelHidden()) {  
  114.                         mLayout.hidePanel();  
  115.                         item.setTitle(R.string.action_show);  
  116.                     } else {  
  117.                         mLayout.showPanel();  
  118.                         item.setTitle(R.string.action_hide);  
  119.                     }  
  120.                 }  
  121.                 return true;  
  122.             }  
  123.             case R.id.action_anchor: {  
  124.                 if (mLayout != null) {  
  125.                     if (mLayout.getAnchorPoint() == 1.0f) {  
  126.                         mLayout.setAnchorPoint(0.7f);  
  127.                         mLayout.expandPanel(0.7f);  
  128.                         item.setTitle(R.string.action_anchor_disable);  
  129.                     } else {  
  130.                         mLayout.setAnchorPoint(1.0f);  
  131.                         mLayout.collapsePanel();  
  132.                         item.setTitle(R.string.action_anchor_enable);  
  133.                     }  
  134.                 }  
  135.                 return true;  
  136.             }  
  137.         }  
  138.         return super.onOptionsItemSelected(item);  
  139.     }  
  140.   
  141.     private int getActionBarHeight(){  
  142.         int actionBarHeight = 0;  
  143.         TypedValue tv = new TypedValue();  
  144.         if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {  
  145.             actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());  
  146.         }  
  147.         return actionBarHeight;  
  148.     }  
  149.   
  150.     public void setActionBarTranslation(float y) {  
  151.         // Figure out the actionbar height  
  152.         int actionBarHeight = getActionBarHeight();  
  153.         // A hack to add the translation to the action bar  
  154.         ViewGroup content = ((ViewGroup) findViewById(android.R.id.content).getParent());  
  155.         int children = content.getChildCount();  
  156.         for (int i = 0; i < children; i++) {  
  157.             View child = content.getChildAt(i);  
  158.             if (child.getId() != android.R.id.content) {  
  159.                 if (y <= -actionBarHeight) {  
  160.                     child.setVisibility(View.GONE);  
  161.                 } else {  
  162.                     child.setVisibility(View.VISIBLE);  
  163.                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {  
  164.                         child.setTranslationY(y);  
  165.                     } else {  
  166.                         AnimatorProxy.wrap(child).setTranslationY(y);  
  167.                     }  
  168.                 }  
  169.             }  
  170.         }  
  171.     }  
  172.   
  173.     @Override  
  174.     public void onBackPressed() {  
  175.         if (mLayout != null && mLayout.isPanelExpanded() || mLayout.isPanelAnchored()) {  
  176.             mLayout.collapsePanel();  
  177.         } else {  
  178.             super.onBackPressed();  
  179.         }  
  180.     }  
  181. }  


布局: 
Xml代码   收藏代码
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".DemoActivity" >  
  6.   
  7.     <com.sothree.slidinguppanel.SlidingUpPanelLayout  
  8.         xmlns:sothree="http://schemas.android.com/apk/res-auto"  
  9.         android:id="@+id/sliding_layout"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:gravity="bottom"  
  13.         sothree:panelHeight="68dp"  
  14.         sothree:shadowHeight="4dp"  
  15.         sothree:paralaxOffset="100dp"  
  16.         sothree:dragView="@+id/dragView">  
  17.   
  18.         <!-- MAIN CONTENT -->  
  19.         <LinearLayout  
  20.             android:layout_width="match_parent"  
  21.             android:layout_height="match_parent"  
  22.             android:paddingTop="?android:attr/actionBarSize">  
  23.             <TextView  
  24.                 android:id="@+id/main"  
  25.                 android:layout_width="match_parent"  
  26.                 android:layout_height="match_parent"  
  27.                 android:gravity="center"  
  28.                 android:text="Main Content"  
  29.                 android:clickable="true"  
  30.                 android:focusable="false"  
  31.                 android:focusableInTouchMode="true"  
  32.                 android:textSize="16sp" />  
  33.         </LinearLayout>  
  34.   
  35.         <!-- SLIDING LAYOUT -->  
  36.         <LinearLayout  
  37.             android:layout_width="match_parent"  
  38.             android:layout_height="match_parent"  
  39.             android:background="#eeeeee"  
  40.             android:orientation="vertical"  
  41.             android:clickable="true"  
  42.             android:focusable="false"  
  43.             android:id="@+id/dragView">  
  44.   
  45.             <LinearLayout  
  46.                 android:layout_width="match_parent"  
  47.                 android:layout_height="68dp"  
  48.                 android:orientation="horizontal">  
  49.   
  50.                 <TextView  
  51.                     android:id="@+id/name"  
  52.                     android:layout_width="0dp"  
  53.                     android:layout_height="match_parent"  
  54.                     android:layout_weight="1"  
  55.                     android:textSize="14sp"  
  56.                     android:gravity="center_vertical"  
  57.                     android:paddingLeft="10dp"/>  
  58.   
  59.             </LinearLayout>  
  60.   
  61.             <ImageView  
  62.                 android:layout_width="match_parent"  
  63.                 android:layout_height="0dp"  
  64.                 android:layout_weight="1"  
  65.                 android:scaleType="fitStart"  
  66.                 android:src="@drawable/graphic" >  
  67.             </ImageView>  
  68.         </LinearLayout>  
  69.     </com.sothree.slidinguppanel.SlidingUpPanelLayout>  
  70.   
  71. </RelativeLayout>  



一个侧滑效果: 
https://github.com/BlueMor/DragLayout

你可能感兴趣的:(使用ViewDragHelper实现上拉或侧拉效果)