6

Tôi đang cố gắng tạo bố cục có tiêu đề, biểu ngữ bên dưới nó và sau đó là một vài ListView bên dưới biểu ngữ. Tôi muốn màn hình hoàn chỉnh có thể cuộn được ngoại trừ tiêu đề. Bây giờ tôi biết rằng ListView không di chuyển trong một ScrollView vì vậy tôi đã thiết lập chiều cao của ListView đủ lớn để hiển thị tất cả các mục. Vấn đề là, ngay cả sau những thay đổi này, tôi thấy rằng ListView đang di chuyển độc lập và màn hình hoàn chỉnh không thể cuộn được.Android: ScrollView không cuộn

'

<include 
    android:id="@+id/logo_header" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    layout="@layout/screen_header" /> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:layout_weight="0.8" 
      android:background="#f2f2f2"> 

      <include 
       android:id="@+id/mcUser" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       layout="@layout/complaint_reporters_details" 
       android:layout_weight="2" /> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" 
       android:weightSum="9" 
       android:layout_weight="2"> 

       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" 
        android:weightSum="1" 
        android:layout_weight="3" 
        android:focusableInTouchMode="false" 
        android:paddingLeft="30dp" 
        android:paddingRight="30dp"> 

        <ImageView android:id="@+id/mcShowList" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:gravity="center" 
         android:layout_marginTop="5dp" 
         android:layout_marginBottom="5dp" 
         android:scaleType="fitXY" 
         android:src="@drawable/list_icon" 
         android:layout_weight="0.2" /> 

        <TextView android:id="@+id/mcShowList_label" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:gravity="center" 
         android:textStyle="bold" 
         android:textSize="12sp" 
         android:textColor="#929292" 
         android:maxLines="1" 
         android:text="List" 
         android:layout_weight="0.8" 
         android:layout_marginTop="-8dp" /> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" 
        android:weightSum="1" 
        android:layout_weight="3" 
        android:gravity="center_vertical|center_horizontal" 
        android:paddingLeft="30dp" 
        android:paddingRight="30dp"> 

        <ImageView android:id="@+id/mcShowMap" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:gravity="center" 
         android:layout_marginTop="5dp" 
         android:layout_marginBottom="5dp" 
         android:scaleType="fitXY" 
         android:src="@drawable/map_view" 
         android:layout_weight="0.2"/> 

        <TextView android:id="@+id/mcShowMap_label" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:gravity="center" 
         android:textStyle="bold" 
         android:textSize="12sp" 
         android:textColor="#929292" 
         android:maxLines="1" 
         android:text="Map" 
         android:layout_weight="0.8" 
         android:layout_marginTop="-8dp" /> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" 
        android:weightSum="1" 
        android:layout_weight="3" 
        android:focusableInTouchMode="false" 
        android:paddingRight="30dp" 
        android:paddingLeft="30dp"> 

        <ImageView android:id="@+id/mcShowAnalytics" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:gravity="center" 
         android:layout_marginTop="5dp" 
         android:layout_marginBottom="5dp" 
         android:scaleType="fitXY" 
         android:src="@drawable/analytics_icon" 
         android:layout_weight="0.2" /> 

        <TextView android:id="@+id/mcShowAnalytics_label" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:gravity="center" 
         android:textStyle="bold" 
         android:textSize="12sp" 
         android:textColor="#929292" 
         android:maxLines="1" 
         android:text="Analytics" 
         android:layout_weight="0.8" 
         android:layout_marginTop="-10dp" /> 

       </LinearLayout> 

      </LinearLayout> 

     </LinearLayout> 

     <RelativeLayout 
      android:layout_weight="0.2" 
      android:layout_width="fill_parent" 
      android:layout_height="400dp"> 

      <FrameLayout 
       android:id="@+id/mcMapContainer" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent"/> 

      <LinearLayout 
       android:id="@+id/mcAnalyticsContainer" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent"> 

       <FrameLayout 
        android:id="@+id/mcChartContainer" 
        android:layout_gravity="center" 
        android:layout_width="200dp" 
        android:layout_height="200dp"/> 

       <GridView 
        android:id="@+id/mcAmenityList" 
        android:layout_height="0dp" 
        android:layout_weight="7" 
        android:layout_width="match_parent" 
        android:numColumns="3"/> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/mcListContainer" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:weightSum="1"> 

       <TextView 
        android:text="Open Complaints" 
        android:textColor="@color/red_btn_bg_color" 
        android:textSize="18dp" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_vertical|center_horizontal" 
        android:padding="5dp" /> 

       <ListView 
        android:id="@+id/mcListOpen" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/> 

       <TextView 
        android:text="Closed Complaints" 
        android:textColor="@color/red_btn_bg_color" 
        android:textSize="18dp" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_vertical|center_horizontal" 
        android:padding="5dp" /> 

       <ListView 
        android:id="@+id/mcListClosed" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/> 

      </LinearLayout> 

     </RelativeLayout> 

    </LinearLayout> 

</ScrollView> 

'

+0

Vui lòng tham khảo câu trả lời của tôi trong bài sau đây [Thêm một quan điểm] (https://stackoverflow.com/questions/30282580/scrollview-not-scrolling -android) –

+0

Vui lòng xem câu trả lời của tôi trong câu hỏi khác .. [Thêm một chế độ xem trống] (https://stackoverflow.com/questions/30282580/scrollview-not-scrolling-android) –

Trả lời

7

Thay đổi chiều cao của xem di chuyển từ match_parent để wrap_content. Vì chế độ xem cuộn chỉ cho phép cuộn nếu tổng chiều cao của nó cao hơn chiều cao của chế độ xem gốc. tức là thay thế

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

với mã này:

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true"> 
+0

Tôi đã thực hiện cả chiều cao và chiều rộng bố cục cho "wrap_content". Di chuyển vẫn không hoạt động. – vaibhav

+1

Thay đổi chiều cao của bố cục tuyến tính cũng thành wrap_content rồi thử. –

+0

Tôi đã làm điều đó. Nhưng chỉ nhận ra rằng chiều cao của RelativeLayout có ListView, đã được sửa. Thay đổi nó thành "fill_parent" đã giải quyết được vấn đề. – vaibhav

0

Bạn có thể thử này,

listView.setOnTouchListener(new OnTouchListener() { 

public boolean onTouch(View v, MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_MOVE) { 
     return true; 
    } 
    return false; 
    } 
    }); 
2

Tôi đã sử dụng phương pháp sau nhiều lần trên một số ứng dụng. tôi sử dụng một lớp con đặc biệt của ListView:

package com.yourcompany.appname.views; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.ViewGroup; 
import android.widget.ListView; 

public class NonScrollableListView extends ListView { 
    public NonScrollableListView(Context context) { 
     super(context); 
    } 

    public NonScrollableListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public NonScrollableListView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
       Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom); 
     ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = getMeasuredHeight(); 
    } 
} 

Sau đó, trong cách bố trí của bạn, thay vì sử dụng 's, sử dụng một cái gì đó như thế này:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <com.yourcompany.appname.views.NonScrollableListView 
     android:id="@+id/listId" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 

Hope this helps!

+0

Cảm ơn Jonathan. Điều này làm cho ListView được mở rộng hoàn toàn, đó là điều tôi đã làm trước đây, mặc dù đây là một cách làm tốt hơn và rõ ràng hơn. Nhưng ngay cả bây giờ, ScrollView không cuộn cho tôi và tôi chỉ có thể xem các mục phù hợp với màn hình. – vaibhav

+0

@vaibhav, bạn cũng đã đặt chiều cao 400dp trong chế độ xem tương đối của mình. Đó có nên là wrap_content để có thứ gì đó cuộn trong chế độ xem cuộn không? Nói chung, các chế độ xem trong chế độ xem cuộn phải bao quanh nội dung theo chiều dọc để chúng sử dụng không gian dọc bổ sung mà scrollview tạo ra. – Jon

+0

Đó là chính xác. Tôi nhận ra rằng hôm qua và sửa nó. Cảm ơn bạn đã giúp đỡ. Thật không may, tôi không có đủ điểm danh tiếng để upvote câu trả lời của bạn. – vaibhav

0

danh sách đặt nhằm xml khác và bao gồm nó như thế này dưới đây

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<include layout="@layout/show_message_header" /> 
    <ScrollView 
     android:id="@+id/myscrollview_Show_message" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/header" 
     android:fadingEdge="none" 
     android:fillViewport="false" 
     android:paddingRight="0dp" 
     android:scrollbarAlwaysDrawVerticalTrack="false" 
     android:scrollbarSize="0dp" 
     android:scrollbarStyle="insideOverlay" 
     android:scrollbars="none" > 
     <TableLayout 
      android:id="@+id/Show_message_dynamic_table" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" > 
      <include layout="@layout/desinglayoutforlistviewincludehere" /> 
     </TableLayout> 
    </ScrollView> 

Các vấn đề liên quan