2016-02-25 22 views
5

Tôi đã tạo một ứng dụng có trang cần tải nội dung động từ dịch vụ web. Tôi muốn có listview có thể di chuyển cùng với một layout tuyến tính bên trong NestedScrollView. Nhưng khi nội dung được tải vào listview, nó không trầy xước đến chiều cao đầy đủ của nó.Làm cách nào để có một ListView bên trong NestedScrollView

Đây là mã của tôi.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.myquestionth.myquestionth10.Profile2Activity" 
    tools:showIn="@layout/activity_profile2"> 

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

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

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="400dp" 
       android:background="#BBBBBB" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:text="Media heading" 
       android:id="@+id/textView2" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:text="Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis." 
       android:id="@+id/textView8" /> 

     </LinearLayout> 

     <ListView 
      android:id="@+id/listView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#70bcf5" /> 

    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 

Tôi có một số tìm kiếm về chế độ xem cuộn không thể lồng nhau. Đây là ví dụ tôi muốn theo thiết kế bố cục của tôi từ trang đánh giá của Google Play. Phương pháp họ sử dụng là gì? Đề nghị tôi nếu tôi làm điều gì sai. Cảm ơn nhiều.

enter image description here

Đây là những gì tôi muốn.

enter image description here

+1

sửa chiều cao của chế độ xem danh sách.hoặc cách tốt nhất là sử dụng coordinatorlayout và recyclerview.Refer [this] (http://inthecheesefactory.com/blog/android-design-support-library-codelab/en) –

Trả lời

4

Vâng, tôi sẽ đề nghị bạn 2 cách để giải quyết vấn đề đó:

1) Cố gắng làm cho LinearLayout một tiêu đề của ListView của bạn. Lưu ý rằng tiêu đề phải được tăng cao vì nó được viết here.

2) Bạn nói rằng bạn sử dụng NestedScrollView, vì vậy có lẽ bạn cũng nên cố gắng thay thế ListView bên Nested scrollview với LinearLayout, như người khôn ngoan đề nghị here, thêm quan điểm liên tiếp trong vòng lặp tương tự như cách thức hoạt động chuyển đổi của bạn.

Chúc may mắn!

1

Thay vì thêm một ListView dưới một Linear Layout và bên trong một scrollview, tôi sẽ đề nghị để đưa mọi thứ bên trong ListView.

Có thể.

Thực hiện (override) phương pháp sau đây trên bộ chuyển đổi của bạn:

public class MyAdapter extends BaseAdapter { 
    // One view to Header 
    // One view to filter options ("most helpful first" and "Options") 
    // One view to comments 
    private final static int VIEW_HEADER = 0; 
    private final static int VIEW_OPTIONS = 1; 
    private final static int VIEW_COMMENTS = 2; 
    private final static int VIEW_TYPE_MAX = 3; 

    @Override 
    public int getViewTypeCount() { 
     // It will return 3 since I have 3 different types of VIEW 
     return VIEW_TYPE_MAX; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     if (position == 0) 
      return VIEW_HEADER; 
     else if (position == 1) 
      return VIEW_OPTIONS; 
     else 
      return VIEW_COMMENTS; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      if(getItemViewType(position) == VIEW_HEADER) 
       // Inflate HEADER Layout 
      else if (getItemViewType(position) == VIEW_OPTIONS) 
       // Inflate Options Layout 
      else 
       // Inflate comments Layout 
     } 

     // Fill the view contents according to its type 
     .... 
     return convertView; 
    } 
} 

Android sẽ tái sử dụng các quan điểm. Tuy nhiên, Android sẽ luôn sử dụng lại các chế độ xem cùng loại.

+0

Cảm ơn Guiherme tôi sẽ thử cái này – Marcus

+0

Bạn được chào đón .. Hãy thử Alex Pepper trả lời là tốt ... có vẻ như đơn giản hơn – W0rmH0le

3

trên Lollipop trở đi bạn có thể sử dụng

yourtListView.setNestedScrollingEnabled(true); 

này bật hoặc tắt di chuyển lồng nhau cho quan điểm này nếu bạn cần tương thích ngược với phiên bản cũ của hệ điều hành bạn sẽ phải sử dụng các RecyclerView.

+0

[Tín dụng] (https://stackoverflow.com/a/35198484/4015799) cho người trả lời ban đầu. –

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