2012-06-05 38 views
8

Tôi tự động thêm các mục vào một ListView và muốn làm cho tất cả các mục luôn hiển thị, mà không cần cuộn.Có thể tắt cuộn trên listView không?

Layout Code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/recipe_inside" 
android:orientation="vertical" > 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="match_parent" 
      android:layout_height="16dp" 
      android:src="@drawable/head_cella" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="41dp" 
      android:background="@drawable/body_cella" 
      android:gravity="center_horizontal" 
      android:paddingTop="5dp" 
      android:text="Remember to save before leaving this page or inserted/modified data will be lost." 
      android:textColor="#000000" 
      android:textSize="13dp" /> 

     <ImageView 
      android:id="@+id/ImageView01" 
      android:layout_width="match_parent" 
      android:layout_height="16dp" 
      android:layout_marginTop="30dp" 
      android:src="@drawable/head_cella" /> 

     <TextView 
      android:id="@+id/TextView01" 
      android:layout_width="wrap_content" 
      android:layout_height="35dp" 
      android:background="@drawable/body_cella" 
      android:gravity="center_horizontal" 
      android:paddingTop="10dp" 
      android:text="Category" 
      android:textColor="#000000" 
      android:textSize="15dp" /> 

     <Spinner 
      android:id="@+id/category_spinner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:id="@+id/ImageView02" 
      android:layout_width="match_parent" 
      android:layout_height="16dp" 
      android:layout_marginTop="30dp" 
      android:src="@drawable/head_cella" /> 

     <TextView 
      android:id="@+id/TextView02" 
      android:layout_width="wrap_content" 
      android:layout_height="35dp" 
      android:background="@drawable/body_cella" 
      android:gravity="center_horizontal" 
      android:paddingTop="10dp" 
      android:text="Name" 
      android:textColor="#000000" 
      android:textSize="15dp" /> 

     <EditText 
      android:id="@+id/recipe_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:hint="Recipe title" android:lines="1"/> 

     <ImageView 
      android:id="@+id/ImageView03" 
      android:layout_width="match_parent" 
      android:layout_height="16dp" 
      android:layout_marginTop="30dp" 
      android:src="@drawable/head_cella" /> 

     <TextView 
      android:id="@+id/TextView03" 
      android:layout_width="wrap_content" 
      android:layout_height="35dp" 
      android:background="@drawable/body_cella" 
      android:gravity="center_horizontal" 
      android:paddingTop="10dp" 
      android:text="Ingredients" 
      android:textColor="#000000" 
      android:textSize="15dp" /> 
     <ListView 
      android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" android:cacheColorHint="#00000000" android:divider="#00000000" 
      android:isScrollContainer="false" 
      > 
     </ListView> 

     <Button 
      android:id="@+id/button_ing" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="" android:background="@drawable/add_recipe_button" android:onClick="addItems"/> 

    </LinearLayout> 

</ScrollView></LinearLayout> 

OnCreate() code chức năng cũ:

protected void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.add_recipe_form); 

    spinner = (Spinner)myhead.findViewById(R.id.category_spinner); 

    Cursor cursor = mydb.getCategoriesList(); 
    ArrayList<Category> list = new ArrayList<Category>(); 
    list.add(new Category(0,"Choose a category")); 

    while (cursor.moveToNext()) { 
     list.add(new Category(cursor.getInt(0),cursor.getString(1))); 
    } 
    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects 
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list); 

    // Step 3: Tell the spinner about our adapter 
    spinner.setAdapter(spinnerArrayAdapter); 
    spinner.setOnItemSelectedListener(this); 


    setListAdapter(adapter); 
    ingredients.add((View)findViewById(R.layout.row_add_recipe)); 
    adapter.notifyDataSetChanged(); 
} 

Nếu tôi di chuyển mã liên quan đến điền spinner của tôi với dữ liệu cơ sở dữ liệu, nó luôn luôn trả tôi một NullPointerException như nó không phải được tìm thấy trong bố cục của tôi. Vì vậy, tôi cần phải giữ nó ở đây và các giải pháp bổ sung addHeaderView() để ListView của tôi có vẻ là ok, nhưng tôi có NullPointerException khác liên quan đến bố trí đầu của tôi:

protected void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.add_recipe_form); 
    ListView mylistview = (ListView) findViewById(android.R.id.list); 
    LinearLayout myhead = (LinearLayout)findViewById(R.id.linear_form); 
    mylistview.addHeaderView(myhead); 
    spinner = (Spinner)myhead.findViewById(R.id.category_spinner); 

    Cursor cursor = mydb.getCategoriesList(); 
    ArrayList<Category> list = new ArrayList<Category>(); 
    list.add(new Category(0,"Choose a category")); 

    while (cursor.moveToNext()) { 
     list.add(new Category(cursor.getInt(0),cursor.getString(1))); 
    } 
    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects 
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list); 

    // Step 3: Tell the spinner about our adapter 
    spinner.setAdapter(spinnerArrayAdapter); 
    spinner.setOnItemSelectedListener(this); 


    setListAdapter(adapter); 
    ingredients.add((View)findViewById(R.layout.row_add_recipe)); 
    adapter.notifyDataSetChanged(); 
} 
+0

'hiển thị tất cả các mục mà không cần cuộn' Điều gì sẽ xảy ra nếu bạn có nhiều mục hơn kích thước màn hình của mình? –

+0

bạn có thể giải thích chi tiết hơn không? Các mục của bạn sẽ được hiển thị như thế nào khi bạn thêm quá nhiều mục để màn hình hiển thị (ví dụ: bạn đã thêm 100 mục)? – Sprigg

+0

ListView của tôi nằm bên trong một Linearlayout có các phần tử khác bên trong, đó là trong một ScrollView. – dany84

Trả lời

4

Nếu tôi hiểu bạn tốt, bạn chỉ cần một số chi tiết lượt xem có thể cuộn cùng với chế độ xem danh sách của bạn. Cách tốt nhất để thực hiện nó là gọi addHeaderView()addFooterView() trên số ListView() của bạn.

EDIT:

Nó nên đi như thế này, tôi nghĩ rằng:

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.your_main_layout); 
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View header = inflater.inflate(R.layout.your_header_layout); //R.layout, not R.id! 

    (ListView) list = findViewById(android.R.list); 
    list.addHeaderView(header); 

    list.setAdapter(adapter); //according to docs you should call setAdapter() after addHeaderView() 

    //to get the spinner 
    (Spinner) spinner = (Spinner) header.findViewById(R.id.spinner); 

} 

Tôi đang viết từ bộ nhớ và không kiểm tra nó, nhưng tôi nghĩ rằng nó sẽ làm việc. Hãy thử nó khi bạn nhận được một chút thời gian.

+0

Thật thú vị. Tôi đã tạo một tệp sơ đồ bố trí XML mới cho đầu nhưng khi tôi sử dụng mã này: LinearLayout myhead = (LinearLayout) findViewById (R.id.linear_form); nó trả về cho tôi "vô giá trị". Bất kỳ ý tưởng? – dany84

+0

Bạn gọi nó ở đâu? Trong 'onCreate()' của hoạt động của bạn? –

+0

có, tôi gọi nó trên OnCreate(). Nếu nó có thể hữu ích, thì đây là một phần của hàm OnCreate() của tôi: setContentView (R.layout.add_recipe_form); \t \t ListView mylistview = (ListView) findViewById (android.R.id.list); \t \t LinearLayout myhead = (LinearLayout) findViewById (R.id.linear_form); \t \t mylistview.addHeaderView (myhead); – dany84

2

Một giải pháp là làm chiều cao ListView lớn như Nội dung. Bạn có thể đạt được hiệu ứng này bằng cách sử dụng lớp sau thay vì ListView:

public class ExpandedListView extends ListView { 

    private int old_count = 0; 

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

    @Override 
    protected void onDraw(Canvas canvas) { 
    if (getCount() != old_count) { 
     old_count = getCount(); 
     int totalHeight = 0; 
     int desiredWidth = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST); 
     for (int i = 0; i < getCount(); i++) { 
      View listItem = getAdapter().getView(i, null, this); 
      listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 

     ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = totalHeight + (getDividerHeight() * (getCount() - 1)); 
     setLayoutParams(params); 
     requestLayout(); 
    } 

    super.onDraw(canvas); 
    } 

} 

Dù sao, đó là một ý tưởng tồi để có một ListView trong một scrollview, vì vậy tôi sẽ thêm các yếu tố khác trong Layout của bạn như là một tiêu đề/xem chân với các phương thức addHeaderView() và addFooterView() như MichałK đã chỉ ra.

Ở đây bạn có một ví dụ về cách sử dụng các phương pháp:

https://github.com/commonsguy/cw-advandroid/tree/master/ListView/HeaderFooter

+0

Không cần phải nhận các vật phẩm cụ thể trong danh sách xem. 'addHeaderView()' và 'addFooterView()' thực hiện thủ thuật dễ dàng hơn. –

+0

Các đầu trang/chân trang này có được cố định và luôn hiển thị không? Tôi nghĩ anh ấy muốn mọi thứ có thể cuộn được. –

+0

Đây là những trang có thể cuộn được, chỉ một số chế độ xem tùy chỉnh được thêm dưới dạng hàng vào danh sách. Thêm vào đó, bạn có thể thêm một vài trong số chúng hoặc chỉ cần đặt ở đó một 'LinearLayout' với các chế độ xem khác, vì vậy đó là một giải pháp có thể tùy chỉnh tốt đẹp –

1

Làm bạn ListViewchiều cao lớn hơn của bạn kích thước nội dung.

nói nếu bạn có 5 listitems mỗi có chiều cao 20sp bộ danh sách xem chiều cao đến 100 hoặc cao hơn cách này bạn có thể đạt được nó

+0

Bằng cách này, anh ta có thể đạt được một 'ListView' không thể cuộn được nhưng có nhiều cách tốt hơn để đạt được mục đích cuối cùng là có một số khung nhìn sorround' ListView' và được cuộn dọc theo. Như nó đã được đề cập, đặt 'ListView' bên trong' ScrollView' là một ý tưởng tồi –

+1

Điều này có thể được tự động đạt được với ExpandedListView mà tôi đã sao chép trong câu trả lời, nhưng nó không được tối ưu. –

+0

Tôi nhận được một ClassCastException cố gắng sửa đổi các thông số bố trí của listview của tôi khi tôi thêm một mục mới. – dany84

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