如何设置ListFragment自定义布局的Divider(为null)

时间:2012-12-21 14:45:17

标签: android android-fragments android-listfragment android-adapter

我正在尝试了解如何使用listfragments和自定义适配器。所以我构建了这个小例子,我想知道在哪里可以将listview的分隔符设置为null。

我找到了不同的方法: - android:dividerHeight =“1dip” - android:divider =“@ android:color / transparent” 但我没有listview的XML布局

我也看到了类似listview.setDivider(null)的内容,但由于使用了listfragments,我不知道在代码中我可以使用该方法。

我的代码:

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>

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

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;
}

2 个答案:

答案 0 :(得分:18)

您总是可以通过在onCreateView()上返回一个视图来设置ListFragment的自定义视图,但是您需要在其中包含一个带有“{id / android:list”的ListView,如documentation中所述。

在您的xml中,您可以执行android:divider="@null"或者如果您真的想在ListFragment代码getListView().setDivider(null);中执行此操作(在onActivityCreated()方法中)。

答案 1 :(得分:3)

onCreateView内使用:

getListView().setDivider(null);