6

Tôi đang cố gắng hiểu cách làm việc với danh sách và bộ điều hợp tùy chỉnh. Vì vậy, tôi xây dựng ví dụ nhỏ này và tôi đã tự hỏi nơi tôi có thể thiết lập chia của listview của tôi để null.Cách đặt Bộ chia (thành vô) của bố cục tùy chỉnh ListFragment

tôi tìm thấy nhiều cách khác nhau: - android: dividerHeight = "1dip" - android: chia = "@ android: màu/minh bạch" Nhưng tôi không có một layout XML với listview

Tôi cũng thấy một cái gì đó như listview.setDivider (null), nhưng tôi không biết nơi tôi có thể sử dụng phương thức đó trong mã của tôi vì việc sử dụng listfragments.

Mã của tôi:

listview_item_row.xml 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/ivCountry" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" 
     android:layout_weight="1"/> 

    <TextView 
     android:id="@+id/tvCountry" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical" 
     android:text="TextView" 
     android:layout_weight="4" /> 

</LinearLayout> 

lớp CountryList

public class CountryList extends ListFragment { 

    Country[] countries2 = new Country[] 
      { 
       new Country("Netherlands"), 
       new Country(R.drawable.bas,"Basque Country") 
      }; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     CountryAdapter adapter = new CountryAdapter(inflater.getContext(),R.layout.listview_item_row,countries2); 
     setListAdapter(adapter); 
     getListView().setDivider(null); 

     return super.onCreateView(inflater, container, savedInstanceState); 
    } 

CountryAdapter My

public class CountryAdapter extends ArrayAdapter<Country>{ 

Context context; 
int layoutResourceId; 
Country[] data = null; 

public CountryAdapter(Context context, int layoutResourceId, Country[] data) { 
    super(context,layoutResourceId,data); 
    this.context = context; 
    this.layoutResourceId = layoutResourceId; 
    this.data = data; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View row = convertView; 
    CountryHolder holder = null; 

    if (row == null) { 
     LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
     row = inflater.inflate(layoutResourceId, parent,false); 

     holder = new CountryHolder(); 
     holder.imgIcon = (ImageView) row.findViewById(R.id.ivCountry); 
     holder.txtTitle = (TextView)row.findViewById(R.id.tvCountry); 

     row.setTag(holder); 

    } else { 
     holder = (CountryHolder) row.getTag(); 
    } 

    Country country = data[position]; 
    holder.txtTitle.setText(country.getTitle()); 
    holder.imgIcon.setImageResource(country.getIcon()); 

    return row; 

} 

static class CountryHolder 
{ 
    ImageView imgIcon; 
    TextView txtTitle; 
} 

Trả lời

18

Bạn luôn có thể đặt chế độ xem tùy chỉnh cho ListFragment bằng cách trả lại một cái nhìn về onCreateView() nhưng bạn cần phải có một ListView trong nó với một "@ id/android: danh sách" như được giải thích trong documentation.

Trong xml của bạn, bạn có thể làm android:divider="@null" hoặc nếu bạn thực sự muốn làm điều đó trong mã ListFragment getListView().setDivider(null); (trong phương thức onActivityCreated()).

+0

tôi đã cố gắng thêm getListView() setDivider (null) (xem mã của tôi "xem nội dung chưa tạo") nhưng điều đó mang lại cho tôi một lỗi.. Tôi có đôi chút hoang mang. Tôi hiểu rằng tôi cần phải trỏ đến một bố cục xml với một listview, nhưng tôi phải làm điều đó ở đâu? Và nếu tôi tạo bố cục xml với listview, tôi sẽ đặt listview_item_row.xml ở đâu? – Lokkio

+3

Thay vì gọi 'getListView(). SetDivider (null);' in 'onCreateView', hãy gọi nó trong' onActivityCreated' và điều đó sẽ khắc phục được sự cố. Tôi làm cho nó một điểm để không bao giờ chạm vào chức năng 'onCreateView' bên trong một' ListFragment'. – MCeley

+0

CÓ người cuối cùng làm việc: D cảm ơn! – Lokkio

3

Bên onCreateView sử dụng:

getListView().setDivider(null);

+0

bạn quên dấu ngoặc đơn? – Lokkio

+0

Dấu ngoặc đơn nào? – Rawkode

+0

getListView()? – Lokkio

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