繁体   English   中英

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

[英]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.

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