[英]Set list view height without TextView with adapter
我有一個ListView
,應該成為一個菜單,每行有兩個可繪制對象和兩個文本視圖。
活動代碼:
ArrayList<MenuItem> itemArray = new ArrayList<MenuItem>();
itemArray.add(new MenuItem("Headertexxt", "subbtexdt"));
itemArray.add(new MenuItem("asf", "asf"));
ListView listView = (ListView) findViewById(R.id.listViewCM);
String[] array = getResources().getStringArray(R.array.buttonsCM);
int[] images = new int[] { R.drawable.btn_car, R.drawable.btn_star, R.drawable.btn_bag};
listView.setAdapter(new HomeScreenButtonsAdapterSubtext(this, R.layout.row,
itemArray, images, R.drawable.list_arrow));
Utils.setListViewHeightBasedOnChildren(listView);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
findViewById(R.id.buttonCreditCompilation).performClick();
break;
case 1:
findViewById(R.id.buttonYourCredits).performClick();
break;
}
}
});
適配器代碼:
public class HomeScreenButtonsAdapterSubtext extends ArrayAdapter<MenuItem> {
private Drawable[] drawables;
private Drawable arrowDrawable;
private ArrayList<MenuItem> items;
public HomeScreenButtonsAdapterSubtext(Context context, int resourceId,
ArrayList<MenuItem> items, int[] images, int arrowImage) {
super(context, resourceId, items);
this.items = items;
Resources resources = context.getResources();
if (images != null) {
drawables = new Drawable[images.length];
int i = 0;
for (int id : images) {
Drawable drawable = resources.getDrawable(id);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawables[i++] = drawable;
}
}
arrowDrawable = resources.getDrawable(arrowImage);
arrowDrawable.setBounds(0, 0, arrowDrawable.getIntrinsicWidth(),
arrowDrawable.getIntrinsicHeight());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (v instanceof TextView) {
Drawable dr = drawables != null ? drawables[position %
drawables.length] : null;
((TextView) v).setCompoundDrawables(dr, null, arrowDrawable, null);
Utils.setFont((TextView) v);
}
// View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
MenuItem station = items.get(position);
if (station != null) {
TextView tt = (TextView) v.findViewById(R.id.headerText);
TextView bt = (TextView) v.findViewById(R.id.subText);
if (tt != null) {
tt.setText(station.getHeaderText());
}
if (bt != null) {
bt.setText(station.getSubText());
}
}
return v;
}
我現在listview height
的問題是我無法基於子級設置listview height
。 我在這里嘗試這樣做: Utils.setListViewHeightBasedOnChildren(listView);
但出現錯誤: arrayadapter requires the resource id to be a textview
在這一行arrayadapter requires the resource id to be a textview
。 有誰知道解決方案嗎?
我可以使用其他方法設置ListView
高度嗎?
謝謝
實際上,根據其內容設置ListView
的高度實際上沒有任何意義。
因為ListView
的全部目的是使其內容可滾動(無論它有多大)...所以它應該具有固定的高度。
但出現錯誤:arrayadapter要求資源ID在這一行是textview。
R.layout.row
是一個布局文件,它不僅具有TextView
。 如果調用已使用的超級構造函數,並且還調用適配器中的super.getView
(在getView
方法中)方法,則ArrayAdapter
會抱怨,因為它期望將布局文件中的單個小部件傳遞給它(單個TextView
) 。
當您確切地知道該行不能是Textview
的實例時,我不明白為什么在getView
方法中有一段代碼(通過超級調用)。
我也不確定要設置ListView
的高度,如果您要顯示ListView
所有行,請不要這樣做(因為您使ListView
基本無用)。 如果仍然要執行此操作,則最好丟失ListView
並手動構建行布局。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.