图像高度控制

时间:2013-11-11 07:04:10

标签: android android-view android-listfragment

我有一个List Fragment,它附有一个适配器,如下所示。在getView中,imageLoader会将图像加载到该视图,时间和加载工作正常,没有问题,但我不知道为它设置布局高度的最佳方法是什么,因为我知道如果我从中设置它一个imageLoader,它与调用的getview不一致。所以我不确定,如何确保视图刷新和更新,以及我如何知道收到的图像的高度?

代码:TestCustomArrayAdapter

package com.adapters;

public class TestCustomArrayAdapter extends ArrayAdapter<TestItemModel> {

    /** Instance Variables **/

    // private Instances
    private final LayoutInflater _inflater;
    public OnItemClickListener _ol;
    public ImageLoader _imageLoader;

    public TestCustomArrayAdapter(Context context) {

        super(context, R.layout.test_list_fragment);

        // Set-up inflater
        _inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // Set-up up Image Loader
        _imageLoader = new ImageLoader(context);

    }

    // Set List Data
    public void setData(List<TestItemModel> data) {
        clear();
        if (data != null) {
            for (TestItemModel appEntry : data) {
                add(appEntry);
            }
        }
    }

    /**
     * Populate new items in the list.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // create viewHolder
        View view;

        // Check Views
        if (convertView == null) {
            view = _inflater.inflate(R.layout.test_single_item, parent, false);
        } else {
            view = convertView;
        }

        // Create Items
        TestItemModel item = getItem(position);

        TextView itemLabel = (TextView) view.findViewById(R.id.text_view_label);
        TextView itemId = (TextView) view
                .findViewById(R.id.text_view_description);

        // Create ImageViews
        ImageView image = (ImageView) view.findViewById(R.id.image_id);

        // Create Buttons
        Button btn1 = (Button) view.findViewById(R.id.button_id_1);
        Button btn2 = (Button) view.findViewById(R.id.button_id_2);
        Button btn3 = (Button) view.findViewById(R.id.button_id_3);

        // Create Resources
        Resources resources = this.getContext().getResources();

        // Create Listeners
        _ol = new OnItemClickListener(position, item);

        // Setup TextViews
        itemLabel.setText(item.getName().toString());
        itemId.setText(item.getName().toString());

        // Setup Images
        _imageLoader.DisplayImage(item.getBm(), image);
        // TODO set image size based on the value of the image here
        // image.getLayoutParams().height = 100;

        // Setup Buttons
        btn1.setOnClickListener(_ol);
        btn1.setTag(1);
        btn2.setOnClickListener(_ol);
        btn2.setTag(2);
        btn3.setOnClickListener(_ol);
        btn3.setTag(3);

        return view;
    }

    private class OnItemClickListener implements OnClickListener {
        private int _position;
        private TestItemModel _testItem;

        public OnItemClickListener(int position, TestItemModel ti) {
            _position = position;
            _testItem = ti;
        }

        // TODO
        // provide functionality for which button was clicked then pass the item
        // to which it was clicked in.
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button_id_1:
                // btn1 clicked
                Toast.makeText(getContext(),
                        " Button1 clicked at positon" + v.getTag(),
                        Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_id_2:
                // btn2 clicked
                Toast.makeText(getContext(),
                        " Button2 clicked at positon" + v.getTag(),
                        Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_id_3:
                Toast.makeText(getContext(),
                        " Button3 clicked at positon" + v.getTag(),
                        Toast.LENGTH_SHORT).show();
                // btn 3 clciked
                break;
            }

            // the view is the button, so you get get the tag it has set with
            // v.getTag() to know what button is pressed.
            Log.v("YOOO",
                    "Button Click at position " + _position + " " + v.getTag()
                            + " Item ID = " + _testItem.getId());

        }
    }

}

2 个答案:

答案 0 :(得分:1)

您可以尝试从ImageLoader获取Bitmap,然后使用getWith和getHeight获取大小

您还可以使用以下代码设置imageView的最大尺寸:

imageView.setMaxHeight(myMaxHeight);
imageView.setMaxWidth(myMaxWidth);
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ScaleType.FIT_CENTER);

答案 1 :(得分:0)

为了回答我的问题,我将ImageView布局中的所有内容更改为高度和宽度以填充父级,并且图像大小适当加载到该级别,这意味着我可以通过编程方式设置自己的最小高度和宽度,对于任何人遇到了同样的问题。

IMAGEVIEW的布局:布局:

<ImageView
        android:id="@+id/image_id"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="Image Is Here"
        android:src="@drawable/ic_save"
        android:text="Sample Image"
        android:textSize="20sp" />