android - 如何设置 ListFragment 自定义布局的分隔符(为空)

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

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

我发现了不同的方法: - 机器人: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>

国家列表类

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

我的国家适配器

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

最佳答案

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

在你的 xml 中你可以做 android:divider="@null" 或者如果你真的想在你的 ListFragment 代码中做 getListView().setDivider(null);(在 onActivityCreated() 方法中)。

关于android - 如何设置 ListFragment 自定义布局的分隔符(为空),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13992168/

相关文章:

java - 在 ListFragment 的适配器显示的列表之前添加自定义布局

Android:动画 View 位置

android - 错误 :Connection refused: no further information android studio

android - 如何连接多个 recyclerview 或 fragment 以显示动画

java - Android Studio : How to get EditText values from Fragment 1 to Fragment 2 respectively on button clicked

java - 由于 "Method does not override method of its superclass"错误,单击事件无法正常工作

android - ListView 中的第一项在 Android 中无法正常工作

android - 在 Canvas 中旋转两个单独的对象(路径)

android - 如何在 Android 上使用 ndk 独立工具链的 libcurl

android - android.support.v4.Fragment 上的 3D 翻转动画