简体   繁体   English

设置不带适配器的TextView的列表视图高度

[英]Set list view height without TextView with adapter

I have a ListView which is supposed to become a menu with two drawables and two text views per row. 我有一个ListView ,应该成为一个菜单,每行有两个可绘制对象和两个文本视图。

Activity Code: 活动代码:

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;

                }
            }
        });

Adapter code: 适配器代码:

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

The problem I have right now is that I can't set the listview height based on the children. 我现在listview height的问题是我无法基于子级设置listview height I'm trying to do that here: Utils.setListViewHeightBasedOnChildren(listView); 我在这里尝试这样做: Utils.setListViewHeightBasedOnChildren(listView); but getting the error: arrayadapter requires the resource id to be a textview at this row. 但出现错误: arrayadapter requires the resource id to be a textview在这一行arrayadapter requires the resource id to be a textview Does anyone know a solution for this? 有谁知道解决方案吗?

Can I use some other method for setting the ListView height? 我可以使用其他方法设置ListView高度吗?

Thanks 谢谢

In fact it does not really make sense to set the height of ListView depending on its content. 实际上,根据其内容设置ListView的高度实际上没有任何意义。

Because the whole point of a ListView is to make its content scrollable (however big it is)...so it is supposed to have a fixed height. 因为ListView的全部目的是使其内容可滚动(无论它有多大)...所以它应该具有固定的高度。

but getting the error: arrayadapter requires the resource id to be a textview at this row. 但出现错误:arrayadapter要求资源ID在这一行是textview。

R.layout.row is a layout file which it doesn't have just a TextView . R.layout.row是一个布局文件,它不仅具有TextView If you call the super constructor you have used and you also call the super.getView (in the getView method) method in the adapter, then ArrayAdapter will complain as it expects a single widget in the layout file passed to it(a single TextView ). 如果调用已使用的超级构造函数,并且还调用适配器中的super.getView (在getView方法中)方法,则ArrayAdapter会抱怨,因为它期望将布局文件中的单个小部件传递给它(单个TextView ) 。

I don't understand why you have that piece of code in the getView method(with the super call) when you know precisely that the row can't be an instance of Textview . 当您确切地知道该行不能是Textview的实例时,我不明白为什么在getView方法中有一段代码(通过超级调用)。

I'm not sure about setting the height of the ListView either, if you're trying to show all the rows of the ListView , don't do it(as you make the ListView basically useless). 我也不确定要设置ListView的高度,如果您要显示ListView所有行,请不要这样做(因为您使ListView基本无用)。 If you still want to do this, then it's better to lose the ListView and build the row layouts manually. 如果仍然要执行此操作,则最好丢失ListView并手动构建行布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM