2016-01-27 35 views
3

中保存並拖動(重新定位)佈局及其關聯的佈局我正在製作一個Android應用程序。我有一個場景。首先看下面的屏幕截圖,所以任何人來這裏回答有一些清晰的圖片,我想要的。如何在Andr oid

enter image description here

場景:活動具有低於導航欄地圖,RelativeLayout的(紅色背景)的中心,下面紅色的RelativeLayout一個ListView

我想要什麼:我想拖或再位置(無論他人可能使用什麼術語)通過握住我的手指並沿着下面的列表視圖在屏幕上上下移動時,紅色RelativeLayout也應隨此佈局移動

代碼我想:

XML佈局

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/fifty_five_dp" 
      android:id="@+id/topBar" 
      android:background="@color/bg_color" > 

      <Button 
       android:id="@+id/button_menu" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="4dp" 
       android:background="@drawable/list_btn" 
       android:onClick="toggleMenu" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:text="Watchover" 
       android:textColor="@android:color/white" 
       android:textSize="@dimen/eighteen_sp" 
       android:textStyle="bold" /> 
     </RelativeLayout> 

     <!-- This is where fragment will show up --> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/topBar" 
      android:id="@+id/root" 
      android:background="@android:color/white" > 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@android:color/white" > 

       <fragment 
        android:id="@+id/map" 
        android:name="com.google.android.gms.maps.SupportMapFragment" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_above="@+id/greenBarImage" /> 

       <RelativeLayout 
        android:id="@+id/redLayout" 
        android:layout_width="match_parent" 
        android:layout_height="@dimen/forty_dp" 
        android:layout_centerVertical="true" 
        android:background="@color/btn_color" > 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true" 
         android:text="Fences Info" 
         android:textColor="@android:color/white" 
         android:textSize="@dimen/sixteen_sp" /> 

        <Button 
         android:id="@+id/btn_drag" 
         android:layout_width="40dp" 
         android:layout_height="40dp" 
         android:layout_alignParentRight="true" 
         android:layout_centerVertical="true" 
         android:layout_marginRight="4dp" 
         android:background="@drawable/list_btn" 
         android:onClick="toggleMenu" 
         android:visibility="gone" /> 
       </RelativeLayout> 

       <ListView 
        android:id="@+id/list_devices" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/greenBarImage" 
        android:padding="@dimen/five_dp" 
        android:scrollbars="none" /> 
      </RelativeLayout> 
     </FrameLayout> 

    </RelativeLayout> 

Java代碼:

public class MyActivity extends FragmentActivity implements OnTouchListener 
{ 
    private int _xDelta; 
    private int _yDelta; 
    RelativeLayout redLayout; 

    FrameLayout rootLayout; 

    ListView devicesList; 

    @Override 
    protected void onCreate(Bundle arg0) 
    { 
     super.onCreate(arg0); 

     setContentView(R.layout.main_screen); 

     devicesList = (ListView) findViewById(R.id.list_devices); 

     rootLayout = (FrameLayout) findViewById(R.id.root); 

     redLayout = (RelativeLayout) findViewById(R.id.redLayout); 
     redLayout.setOnTouchListener(this); 
    } 

    @Override 
    public boolean onTouch(View view, MotionEvent event) 
    { 
     final int X = (int) event.getRawX(); 
     final int Y = (int) event.getRawY(); 
     switch (event.getAction() & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: 
      RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
      _xDelta = X - lParams.leftMargin; 
      _yDelta = Y - lParams.topMargin; 
      break; 
     case MotionEvent.ACTION_UP: 
      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      break; 
     case MotionEvent.ACTION_POINTER_UP: 
      break; 
     case MotionEvent.ACTION_MOVE: 
      RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
      layoutParams.leftMargin = X - _xDelta; 
      layoutParams.topMargin = Y - _yDelta; 
      layoutParams.rightMargin = -250; 
      layoutParams.bottomMargin = -250; 
      view.setLayoutParams(layoutParams); 
      break; 
     } 
     rootLayout.invalidate(); 
     return true; 
    } 
} 

來源爲獲得上面的代碼幫助here

我試過的代碼沒有得到期望的結果。當我上下拖動紅色的RelativeLayout時,該佈局上的列表視圖疊加層並未按我的需要進行調整。任何類型的幫助將不勝感激。

回答

3
  1. 材料的方式

    您可以存檔(或至少是非常接近你想要的)與CoordinatorLayout並保持你的地圖佈局內CollapsingToolbarLayout

    您的佈局可以是這樣的:

    <android.support.design.widget.AppBarLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 
    
        <android.support.design.widget.CollapsingToolbarLayout 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         app:layout_scrollFlags="scroll|exitUntilCollapsed"> 
    
         <!-- MAP --> 
         <RelativeLayout 
          ... 
          app:layout_collapseMode="parallax"> 
          ..... 
         </RelativeLayout> 
         <!-- Toolbar --> 
         <android.support.v7.widget.Toolbar 
          android:layout_width="match_parent" 
          android:layout_height="?attr/actionBarSize" 
          app:layout_collapseMode="pin" /> 
    
        </android.support.design.widget.CollapsingToolbarLayout> 
    
        <!-- This red thingy, if you need it --> 
        <View 
         android:layout_width="match_parent" 
         android:layout_height=".."/> 
    </android.support.design.widget.AppBarLayout> 
    
    <!--List Of your content items --> 
    <android.support.v7.widget.RecyclerView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
    

    你可以在這裏閱讀更多Android Design Support Library這裏Design Support Library (III): Coordinator Layout

  2. View.OnTouchListener方式

    (這是您使用的)

    的想法很簡單:

    • 集地圖android:layout_above紅色分離&的ListView - android:layout_below紅色分離
    • 一旦移動的動作完成的 - 翻譯分離器分別向上或向下並設置合適的頂部&底部芒果

    下面是它的外觀:

    enter image description here

    public class MainActivity extends AppCompatActivity implements View.OnTouchListener { 
         private int _yDelta; 
         @Override 
         protected void onCreate(Bundle arg0) { 
          super.onCreate(arg0); 
          setContentView(R.layout.activity_main); 
          findViewById(R.id.separator).setOnTouchListener(this); 
         } 
    
         @Override 
         public boolean onTouch(View view, MotionEvent event) { 
          final int Y = (int) event.getRawY(); 
          switch (event.getAction() & MotionEvent.ACTION_MASK) { 
           case MotionEvent.ACTION_DOWN: 
            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
            _yDelta = Y - lParams.bottomMargin; 
            break; 
           case MotionEvent.ACTION_UP: 
            break; 
           case MotionEvent.ACTION_POINTER_DOWN: 
            break; 
           case MotionEvent.ACTION_POINTER_UP: 
            break; 
           case MotionEvent.ACTION_MOVE: 
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
            layoutParams.bottomMargin = (Y - _yDelta); 
            layoutParams.topMargin = -layoutParams.bottomMargin; 
            view.setLayoutParams(layoutParams); 
            view.animate().translationY(Y - _yDelta).setDuration(0); 
            break; 
          } 
          findViewById(R.id.root).invalidate(); 
          return true; 
         } 
        } 
    

    而且activity_main.xml

    <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id="@+id/root"> 
        <View 
         android:background="#AA0F00FF" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:layout_above="@+id/separator"/> 
        <View 
         android:layout_below="@+id/separator" 
         android:background="#AA0FF000" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" /> 
        <View 
         android:background="#FF0000" 
         android:layout_centerVertical="true" 
         android:id="@+id/separator" 
         android:layout_width="match_parent" 
         android:layout_height="20dp" /> 
    </RelativeLayout> 
    

我希望,它幫助!

+0

如何在eclipse中使用這些庫?因爲我在eclipse中開發這個應用程序 –

+0

@ZubairAhmadKhan檢查這個鏈接:http://stackoverflow.com/questions/26878803/how-to-add-android-support-v7-libraries-in-eclipse和這個:https:/ /developer.android.com/tools/support-library/setup.html#libs-with-res(PS Android Studio 2.0比*更好的體驗,比Eclipse更好,試試吧) –

+0

我知道Android Studio 。目前我沒有太多時間從eclipse轉移到Studio。我想知道是否有可能在eclipse中使用這些庫?如果是好心地建議我一種方式謝謝 –