2012-07-24 44 views
7

我想在我的應用程序中使用SlidingDrawer。但是我必須隱藏handle並且必須顯示SlidingDrawer已關閉時,內容的20%可見。 另外我想分配所有滑動(觸摸或拖動)動作的handlecontent。如果任何人有這個解決方案,請幫助我。SlidingDrawer沒有句柄和滑動動作到內容

請參考我試過的下面的代碼片段。

<View 
    android:id="@id/handle" 
    android:layout_width="0dip" 
    android:layout_height="fill_parent" /> 

<LinearLayout 
    android:id="@id/content" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/sliding_drawer" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/sliding_drawer" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/sliding_drawer" /> 
</LinearLayout> 

,這裏是我的活動:

 import android.app.Activity; 
    import android.graphics.Color; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.SlidingDrawer; 
    import android.widget.SlidingDrawer.OnDrawerCloseListener; 
    import android.widget.SlidingDrawer.OnDrawerOpenListener; 

    public class SlidingDrawerActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final SlidingDrawer drawer = (SlidingDrawer) findViewById(R.id.drawer); 
     drawer.setOnDrawerCloseListener(new OnDrawerCloseListener() { 

      public void onDrawerClosed() { 
       // TODO Auto-generated method stub 
       drawer.setBackgroundColor(Color.BLACK); 
      } 
     }); 

     drawer.setOnDrawerOpenListener(new OnDrawerOpenListener() { 

      public void onDrawerOpened() { 
       // TODO Auto-generated method stub 
       drawer.setBackgroundColor(Color.BLUE); 
      } 
     }); 

     View content = drawer.getContent(); 
     content.setClickable(true); 
     content.setTouchDelegate(drawer.getHandle().getTouchDelegate()); 
    } 
} 

在這裏,我能夠通過設置width=0dip隱藏handle,但無法知道如何表達20當SlidingDrawer關閉時,內容的%,並將操作設置爲SlidingDrawerContent

我試着拿到手柄的touchDelegate並將其設置爲Content,但它不起作用。 請幫我解決這個問題。

+0

嗨拉傑,酷概念! :)。你可以發表一些你寫的代碼,或者你未能達到的代碼。每個人都很樂意幫助你。 – Abhilasha 2012-07-24 10:40:48

+1

嗨Abhilasha,我已更新我的帖子。如果您有任何解決方案,請檢查並幫助我。 – Raj 2012-07-24 10:53:01

+0

+1爲你的努力:) – Abhilasha 2012-07-24 11:05:02

回答

0

我不認爲這是可能的目前SlidingDrawer,除非你正在處理的東西,可以削減和分成20%。如果您查看SlidingDrawer的源代碼,特別是open(),您會看到它調用私有方法openDrawer()。下面是做什麼:

private void openDrawer() { 
    moveHandle(EXPANDED_FULL_OPEN); 
    mContent.setVisibility(View.VISIBLE); 

    if (mExpanded) { 
     return; 
    } 

    mExpanded = true; 

    if (mOnDrawerOpenListener != null) { 
     mOnDrawerOpenListener.onDrawerOpened(); 
    } 
} 

正如你所看到的內容(以下簡稱「抽屜」),直到手柄開始移動是不可見的。當抽屜移動時,只有當它開始或結束時纔會出現鉤子,因此您無法動態更新手柄或抽屜中的內容。

如果你正在處理圖像,你可以將它分成兩部分,分別是20%和80%。但是因爲你正在使用3個TextViews,所以它看起來不是那種選擇。

鑑於我認爲一個選項將採取SlidingDrawer的來源和創建自己的類。這樣你就可以完全控制圖紙的處理方式,但仍然可以重新使用所有的監聽器。這裏有一個鏈接來源: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/SlidingDrawer.java

0

我要去做你喜歡的事情。

由於現在Slidingrawer已被棄用,我使用了名爲MultiDirectionSlidingDrawer的自定義slidedraw。 以下是我在網上找到的代碼:MultiDirectionSlidingDrawer Source Code

要將內容視圖顯示爲句柄,我不會隱藏句柄,而是隱藏內容。 我設置內容視圖的高度爲0浸

<it.sephiroth.demo.slider.widget.MultiDirectionSlidingDrawer 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/drawer" 
    custom:handle="@+id/handle" 
    custom:content="@+id/content" 
    custom:direction="topToBottom" 
    custom:animateOnClick="false" 
    custom:allowSingleTap="false" 

    > 
    <LinearLayout 
     android:id="@id/handle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center" 
     > 
     <!-- 
     paste your content here 
     --> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@id/content" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:orientation="vertical" 
     android:gravity="center" 
     > 

    </LinearLayout> 
</it.sephiroth.demo.slider.widget.MultiDirectionSlidingDrawer> 

從頂部我的抽屜裏開始,我計算頂部onMeasure

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{ 
    int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); 
    int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); 

    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); 
    int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); 

     if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { throw new RuntimeException(
      "SlidingDrawer cannot have UNSPECIFIED dimensions"); } 

     final View handle = mHandle; 
     measureChild(handle, widthMeasureSpec, heightMeasureSpec); 

     //Custom top offset 
     int intHandleHeight = handle.getHeight(); 
     mTopOffset = intHandleHeight*80*-1/100; 

     if (mVertical) { 
      int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset; 
      mContent.measure(MeasureSpec.makeMeasureSpec(widthSpecSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); 
     } else { 
      int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset; 
      mContent.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSpecSize, MeasureSpec.EXACTLY)); 
     } 

     setMeasuredDimension(widthSpecSize, heightSpecSize); 
    } 
0

抵消我試圖做一些非常相似。你有沒有運氣?

爲了在SlidingDrawer關閉時顯示20%的內容,我推薦使用SemiClosedSlidingDrawer。我相信這是我最初發現它的地方:http://pastebin.com/FtVyrcEb

而且,我在attr.xml文件在res /值加本/

<!-- SemiClosedSlidingDrawer --> 
<declare-styleable name="SemiClosedSlidingDrawer"> 
    <attr name="handle" format="integer" /> 
    <attr name="content" format="integer" /> 
    <attr name="topOffset" format="dimension" /> 
    <attr name="bottomOffset" format="dimension" /> 
    <attr name="allowSingleTap" format="boolean" /> 
    <attr name="animateOnClick" format="boolean" /> 
    <attr name="semiClosedOrientation" format="string" /> 
    <attr name="semiClosedContentSize" format="dimension" /> 
</declare-styleable> 
1

爲了有一個slidingDrawer無柄,訣竅是剛剛設置的手柄的寬度和高度0dp。歡呼聲