2016-03-03 45 views
0

所以我有一點困難。我創建了一個類似底部表單的活動。我的許多用戶要求的一項功能是,一旦底部紙張完全展開後,就可以製作底部紙張,然後紙張將滾動,以便他們可以查看其餘的物品。我試圖把我的線性佈局放入nestedscrollview中,但這只是導致崩潰。請不要推薦我使用支持底部表格類,因爲它沒有我需要的所有工具。先謝謝你!如何讓視圖拖動到全高,然後使其滾動?

下面是底部片材的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/totalScreen" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignTop="@+id/fab" 
    android:layout_gravity="bottom|center" 
    android:gravity="bottom|center_vertical" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/sheetTitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#424242" 
     android:padding="12dp" 
     android:text="Dummy Title" 
     android:textSize="18sp" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignTop="@+id/sheetTitle" 
     android:layout_marginEnd="20dp" 
     android:layout_marginTop="-28dp" 
     android:visibility="invisible" 
     app:fabSize="normal" /> 

    <LinearLayout 
     android:id="@+id/sheetScreen" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/sheetTitle" 
     android:background="#424242" 
     android:gravity="bottom|center_vertical" 
     android:orientation="vertical" 
     android:paddingBottom="8dp"> 

    </LinearLayout> 

</RelativeLayout> 

這裏是ontouch聽者,其允許所述底部片材的行爲:

ID: 
protected LinearLayout main; 
protected RelativeLayout full; 

protected void setAdjustableTouchListener() { 
     full.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getActionMasked()) { 
        case MotionEvent.ACTION_DOWN: 
         mDragStartY = event.getY(); 
         mPointerOffset = event.getRawY() + main.getMeasuredHeight(); 
         return true; 
        case MotionEvent.ACTION_UP:      DisplayMetrics metrics = new DisplayMetrics(); 
         getWindowManager().getDefaultDisplay().getMetrics(metrics); 
         int sheetHeight = (int) (metrics.heightPixels - event.getY()); 
         if (mDragStartY < (event.getY() + TAP_DRIFT_TOLERANCE) && mDragStartY > (event.getY() - TAP_DRIFT_TOLERANCE) 
           && ((SystemClock.elapsedRealtime() - mDraggingStarted) < SINGLE_TAP_MAX_TIME) 
           && sheetHeight > (main.getMeasuredHeight() + findViewById(R.id.sheetTitle).getMeasuredHeight())) { 
          result = "Dismissed"; 
          finish(); 
         } else { 
          if (main.getMeasuredHeight() > (viewConversion + heightDifference)) { 
           ResizeAnimation a = new ResizeAnimation(main); 
           a.setDuration(500); 
           a.setParams(main.getMeasuredHeight(), extendedViewHeight); 
           main.startAnimation(a); 
           state = 2; 
          } else if (main.getMeasuredHeight() < (viewConversion + heightDifference) 
            && main.getMeasuredHeight() > viewConversion) { 
           ResizeAnimation a = new ResizeAnimation(main); 
           a.setDuration(500); 
           a.setParams(main.getMeasuredHeight(), viewConversion); 
           main.startAnimation(a); 
           state = 1; 
          } else if (main.getMeasuredHeight() < viewConversion && main.getMeasuredHeight() > (viewConversion/2)) { 
           ResizeAnimation a = new ResizeAnimation(main); 
           a.setDuration(500); 
           a.setParams(main.getMeasuredHeight(), viewConversion); 
           main.startAnimation(a); 
           state = 1; 
          } else if (main.getMeasuredHeight() < (viewConversion/2)) { 
           result = "Dismissed"; 
           finish(); 
          } 
         } 
         return true; 
        case MotionEvent.ACTION_MOVE: 
         if (mPointerOffset - event.getRawY() < extendedViewHeight) 
          setPrimaryContentHeight((int) (mPointerOffset - event.getRawY())); 
         return true; 
        default: 
         return true; 
       } 
      } 
     }); 
    } 

下面是實際設置的新的高度的方法該工作表:

protected boolean setPrimaryContentHeight(int newHeight) { 
    // the new primary content height should not be less than 0 to make the 
    // handler always visible 
    newHeight = Math.max(0, newHeight); 
    // the new primary content height should not be more than the SplitView 
    // height minus handler height to make the handler always visible 
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) main.getLayoutParams(); 
    if (newHeight >= 0) { 
     params.height = newHeight; 
     // set the primary content parameter to do not stretch anymore and 
     // use the height specified in the layout params 
    } 
    main.setLayoutParams(params); 
    return true; 
} 

這是使用NestedScrollView時發生的崩潰:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nick.mowen.sceneplugin/com.nick.mowen.sceneplugin.ui.BottomSheetActivity}: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class android.support.v4.widget.NestedScrollView 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5466) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class android.support.v4.widget.NestedScrollView 
at android.view.LayoutInflater.inflate(LayoutInflater.java:539) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129) 
at com.nick.mowen.sceneplugin.ui.BottomSheetActivity.onCreate(BottomSheetActivity.java:37) 
at android.app.Activity.performCreate(Activity.java:6251) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)  
at android.app.ActivityThread.-wrap11(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5466)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class android.support.v4.widget.NestedScrollView 
at android.view.LayoutInflater.createView(LayoutInflater.java:645) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)  
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)  
at com.nick.mowen.sceneplugin.ui.BottomSheetActivity.onCreate(BottomSheetActivity.java:37)  
at android.app.Activity.performCreate(Activity.java:6251)  
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)  
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)  
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)  
at android.app.ActivityThread.-wrap11(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5466)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
Caused by: java.lang.reflect.InvocationTargetException 
at java.lang.reflect.Constructor.newInstance(Native Method) 
at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)  
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)  
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)  
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)  
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)  
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)  
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)  
at com.nick.mowen.sceneplugin.ui.BottomSheetActivity.onCreate(BottomSheetActivity.java:37)  
at android.app.Activity.performCreate(Activity.java:6251)  
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)  
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)  
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)  
at android.app.ActivityThread.-wrap11(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5466)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.NestedScrollingChildHelper.setNestedScrollingEnabled(boolean)' on a null object reference 
at android.support.v4.widget.NestedScrollView.setNestedScrollingEnabled(NestedScrollView.java:212) 
at android.view.View.<init>(View.java:4256) 
at android.view.ViewGroup.<init>(ViewGroup.java:573) 
at android.widget.FrameLayout.<init>(FrameLayout.java:97) 
at android.widget.FrameLayout.<init>(FrameLayout.java:92) 
at android.support.v4.widget.NestedScrollView.<init>(NestedScrollView.java:189) 
at android.support.v4.widget.NestedScrollView.<init>(NestedScrollView.java:185) 
at java.lang.reflect.Constructor.newInstance(Native Method)  
at android.view.LayoutInflater.createView(LayoutInflater.java:619)  
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)  
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)  
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)  
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)  
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)  
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)  
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)  
at com.nick.mowen.sceneplugin.ui.BottomSheetActivity.onCreate(BottomSheetActivity.java:37)  
at android.app.Activity.performCreate(Activity.java:6251)  
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)  
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)  
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)  
at android.app.ActivityThread.-wrap11(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5466)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
+0

我想知道:什麼是來自支持庫的新底部表單沒有,所以您可以而不是用它呢? 此外,另一個解決方案,而不是使用自定義的touchlistener,將使用帶有不可見標題項的「ListView」,即與屏幕高度相同。然後,當headerview消失(滾動出視圖)時,您可以刪除標題視圖或將其設置爲消失。我用這種方法將兩個片段放在一起 - 它工作得很好。 – Darwind

+0

我擁有它,因此我的用戶可以將諸如FAB之類的東西添加到視圖頂部。這會被切斷,也可以從任何地方拖動,然後點擊以解除關閉背景,等等。我最終只是將一個recyclerview放在LinearLayout的位置 –

回答

0

好的,解決了這個問題,我最終做的只是添加一個recyclerview代替linearlayout。然後,我只是使LayoutManager被覆蓋,所以它只能在設置爲可滾動時使用:

main.setLayoutManager(new LinearLayoutManager(this) { 
      @Override 
      public boolean canScrollVertically() { 
       return canScroll && super.canScrollVertically(); 
      } 
     }); 
相關問題