2013-06-14 27 views
5

Tôi đã tạo một Ứng dụng Android trong đó tôi sử dụng một ListView. ListView bao gồm các mục danh sách CheckedTextView. Hoạt động của tôi chứa EditText ở trên cùng, một TextView bên dưới và một Nút ở dưới cùng. Khoảng cách giữa TextView và nút là khu vực bị chiếm bởi ListView. Bây giờ, vì ListView của tôi chứa khá ít mục, toàn bộ không gian của ListView không bị chiếm bởi chúng. Ngoài ra không gian còn lại rõ ràng là khác nhau giữa các thiết bị. Những gì tôi muốn biết là nếu có một cách để thay đổi chiều cao hàng của ListView để toàn bộ không gian được phân bổ cho ListView được bảo hiểm. Và cũng có thể xảy ra nếu cần phải thêm một số mặt hàng, chiều cao hàng có thể tự điều chỉnh cho phù hợp. Tôi đã cung cấp một số đoạn mã bên dưới để dễ hiểu Giao diện của tôi giống như thế nào.Tự động thay đổi chiều cao hàng trong một listview dựa trên số lượng các mục danh sách

activity_main.xml

<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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:background="#D0D0D0" 
tools:context=".MainActivity" > 

<Button 
    android:id="@+id/addcourse" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="Add Course" /> 

<EditText 
    android:id="@+id/coursenameet" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:ems="10" 
    android:layout_marginBottom="2dp" 
    android:layout_marginTop="2dp" 
    android:hint="Course Name" 
    android:inputType="textCapWords" > 

    <requestFocus /> 
</EditText> 

<View 
    android:id="@+id/divider" 
    android:layout_width="match_parent" 
    android:layout_height="3dp" 
    android:layout_below="@+id/coursenameet" 
    android:layout_marginBottom="5dp" 
    android:layout_marginTop="5dp" 
    android:background="@android:color/darker_gray" > 
</View> 

<TextView 
    android:id="@+id/weekdaytv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/divider" 
    android:layout_marginBottom="3dp" 
    android:layout_marginTop="3dp" 
    android:layout_centerHorizontal="true" 
    android:text="Select weekdays for this Course" > 
</TextView> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/addcourse" 
    android:layout_below="@+id/weekdaytv" 
    android:divider="#FFFFFF" 
    android:dividerHeight="2px"> 

</ListView> 

</RelativeLayout> 

Các mặt hàng được CheckedTextViews được triển khai bằng BaseAdapter. The View phương pháp của lớp BaseAdapter là

public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    int colorpos = position % colors.length; 
    if(v == null){ 
     LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.listviewitem, null); 
    } 
    CheckedTextView newrow = (CheckedTextView) v.findViewById(R.id.checkedtextview); 
    v.setBackgroundColor(colors[colorpos]); 
    newrow.setTextColor(Color.BLACK); 
    newrow.setText(items[position]); 
    return v; 
} 

EDIT: Snippet từ Hoạt động chính

lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    adapter = new ItemsAdapter(MainActivity.this, weekdays, 6, lv.getBottom() - lv.getTop()); 
    lv.setAdapter(adapter); 

    lv.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      CheckedTextView ctv = (CheckedTextView) arg1; 
      adapter.toggle(ctv); 
     } 
    }); 
+0

Có giải pháp phù hợp nào không? –

Trả lời

0

Để thiết ListView của bạn để lấp đầy toàn bộ không gian được phân bổ, bạn có thể thiết lập các thuộc tính ListView trong XML:

android:fillViewport=”true” 

Hoặc trong mã:

myListView.setFillViewport(true); 

Nếu bạn muốn thay đổi kích thước ô, bạn có thể tính ViewParams để đặt. Ví dụ, đối với một tế bào duy nhất, bạn có thể sử dụng:

AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.FILL_PARENT); 
v.setLayoutParams(params); 

cho n tế bào, bạn có thể tính toán kích thước thẳng đứng dựa vào kích thước của ListView của bạn:

int n = /* number of cells */ 3; 
int height = (myListView.getBottom() - myListView.getTop()); 
AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, height/n); 
v.setLayoutParams(params); 
+0

Nó đưa ra một lỗi: - Trích dẫn mở được mong đợi cho thuộc tính "android: fillViewport" được kết hợp với loại phần tử "ListView". Vui lòng trợ giúp. – tintin

+0

Vui lòng cho tôi biết myCell là gì trong dòng cuối cùng của câu trả lời của bạn. Tôi không có giá trị tương ứng trong mã của mình. Cũng cho tôi biết nếu điều này được thực hiện trong hoạt động chính hoặc lớp BaseAdapter mà tôi có. – tintin

+0

@tintin, tôi cập nhật mã để sử dụng biến số – Phil

11

Hi Tạo một lớp như thế này

public class Utility { 

    public static void setListViewHeightBasedOnChildren(ListView listView) { 
     ListAdapter listAdapter = listView.getAdapter(); 
     if (listAdapter == null) { 
      // pre-condition 
      return; 
     } 

     int totalHeight = 0; 
     int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); 
     for (int i = 0; i < listAdapter.getCount(); i++) { 
      View listItem = listAdapter.getView(i, null, listView); 
      listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 

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


} 

Gọi phương thức này ở nơi bạn muốn tự động thay đổi chiều cao hàng trong danh sách xem.

Utility.setListViewHeightBasedOnChildren(YourListView); 

Để biết thêm Reference .. Hy vọng điều này sẽ giúp bạn.

+0

Tôi đã khai báo sự xuất hiện của các hàng trong Lớp BaseAdapter. Xin vui lòng cho tôi biết chính xác điều này sẽ làm gì ?? Ngoài ra tôi đang thiết lập bộ điều hợp của tôi bằng cách sử dụng: - lv.setAdapter (adapter); Tôi có nên gọi phương thức này ngay sau đó không? Giả sử tôi có số hàng cố định ngay bây giờ và hiện tại tôi không thêm bất kỳ hàng nào. – tintin

+1

chỉ cần khai báo listview của bạn trong phương pháp đó. nó sẽ cung cấp cho bạn chính xác những gì bạn muốn. – Nirmal

+0

Tôi thực sự xin lỗi nhưng tôi không hiểu bạn. Tôi đã chỉnh sửa câu hỏi của mình và đưa ra một đoạn mã hoạt động chính để cho bạn biết chính xác tôi đang làm gì. Đề xuất nơi tôi nên gọi phương thức. – tintin

0

Đối với những người vẫn còn lỗi ở

listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 

viết những dòng mã, như thế này.

if (listItem instanceof ViewGroup)

listItem.setLayoutParams (LayoutParams mới (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

 listItem.measure(0, 0); 
Các vấn đề liên quan