我在嘗試瞭解如何使用listfragments和自定義適配器。所以我建立這個小例子,我想知道我可以在哪裏設置我的列表視圖的分隔符爲空。如何設置ListFragment自定義佈局的分隔符(爲空)
我發現了不同的方式: - 機器人:dividerHeight = 「1dip」 - 機器人:分隔= 「@安卓:彩色/透明」 但我沒有與列表視圖的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;
}
我嘗試添加了getListView()setDivider(空)(見我的代碼 「尚未創建內容視圖」),但給我一個錯誤。我有點困惑。我知道我需要指向一個帶有listview的xml佈局,但是我該在哪裏做呢?如果我用listview製作xml佈局,我應該在哪裏放置listview_item_row.xml? – Lokkio
而不是調用'getListView()。setDivider(null);'在'onCreateView'中,調用它'onActivityCreated',並應該解決這個問題。我從不觸及ListFragment中的'onCreateView'函數。 – MCeley
是的最後一個工作:D謝謝! – Lokkio