2016-05-30 29 views
6

Tôi đã có một bố trí với ScrollView có một ListView bên trong. Tất cả mọi thứ hoạt động tốt, nhưng nếu tôi mở DrawerLayout, ScrollView sẽ được cuộn một chút. Chỉ cần nhìn vào các ảnh chụp màn hình này.ListView bên trong ScrollView với DrawerLayout

enter image description here

Ở đây tôi mở DrawerLayout

enter image description here

Sau đó, tôi đóng DrawerLayout. enter image description here

Như bạn có thể thấy nó được cuộn một chút. Làm thế nào để sửa lỗi này? Đây là xml của tôi:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/menuLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <RelativeLayout 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <ProgressBar 
      android:id="@+id/progressWheelBar" 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" /> 

     <TextView 
      android:id="@+id/noDataTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="@string/noData" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:visibility="gone" /> 

     <LinearLayout 
      android:id="@+id/actionBarLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/transparent" 
      android:orientation="vertical" > 

      <RelativeLayout 
       android:id="@+id/innerActionBarLayout" 
       android:layout_width="match_parent" 
       android:layout_height="55dp" 
       android:background="@color/material_blue_500" > 

       <ImageButton 
        android:id="@+id/menuButton" 
        android:layout_width="36dp" 
        android:layout_height="36dp" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        android:layout_marginLeft="5dp" 
        android:background="@null" 
        android:src="@drawable/ic_menu_white_24dp" /> 

       <TextView 
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:layout_marginLeft="16dp" 
        android:layout_toRightOf="@+id/menuButton" 
        android:textColor="@color/white" 

        android:textSize="18sp" /> 
      </RelativeLayout> 
     </LinearLayout> 

     <ScrollView 
      android:id="@+id/mainScrollView" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_below="@+id/actionBarLayout" 
      android:fillViewport="true" 
      android:scrollbars="none" 
      > 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" > 
       <!-- Here goes the frame layout with a listview inside --> 
       <FrameLayout 
        android:id="@+id/container" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:visibility="gone" 
        tools:context="ru.tenet.ttk.MainActivity" /> 
      </LinearLayout> 
     </ScrollView> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="2dp" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/actionBarLayout" 
      android:background="@drawable/shadow_down" /> 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="250dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="left|start" 
     android:background="@color/white" 
     android:orientation="vertical" 
     android:clickable="true" > 

     <ListView 
      android:id="@+id/menuListView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:listSelector="@drawable/list_selector" 
      android:layout_marginTop="15dp" 
      android:divider="@null" /> 
    </LinearLayout> 

</android.support.v4.widget.DrawerLayout> 
+4

Bạn có thực sự cần 'ScrollView' không? Một 'ListView' cuộn một mình. –

+0

Bạn có thể sử dụng 'NestedScrollView' –

+0

Bạn có thể đặt ListView bên trong chế độ xem cuộn nhưng u phải xử lý cả cuộn, chế độ xem danh sách Bec có tính năng cuộn riêng và chế độ xem cuộn có tính năng cuộn của nó, khi bạn cố gắng cuộn cả cuộn sẽ được mâu thuẫn nên bạn cần phải xử lý các sự kiện cuộn cha và con. –

Trả lời

0

Thật khó để đưa ra câu trả lời rõ ràng vì bạn chưa đăng mã của mình. Một lý do có thể là bạn có cài đặt kiểu "windowActionBarOverlay = true" trong hoạt động của mình. Do cài đặt đó, nội dung của hoạt động bắt đầu ngay từ đầu trang và nằm sau ActionBar.

Bên cạnh đó, như những người nhận xét ở trên đã nêu, bạn có thực sự cần một ListView bên trong một ScrollView không? ListView chính nó là một ScrollView.

Tốt nhất nếu bạn đăng mã hoạt động ngăn kéo và hoạt động nội dung.

0

Giải pháp đơn giản đang sử dụng headerView - ListView.addHeaderView(); Tôi nghĩ rằng bạn không cần phải sử dụng scrollview & chỉ cần thêm container như headerView

0

Tốt nhất là nên sử dụng các RecyclerView và xác định một headerRow để hiển thị tiêu đề của bạn.

0

Gán DrawerListener cho DrawerLayout, và trong onDrawerĐiều kiện sự kiện đã cuộn cuộn scrollView lên trên cùng.

mDrawer.setDrawerListener(new DrawerListener() { 
     @Override 
     public void onDrawerSlide(View view, float v) { 

     } 

     @Override 
     public void onDrawerOpened(View view) { 

     } 

     @Override 
     public void onDrawerClosed(View view) { 
      mScrollView.fullScroll(ScrollView.FOCUS_UP); 
     } 

     @Override 
     public void onDrawerStateChanged(int i) { 

     } 
    }); 
Các vấn đề liên quan