在android中将多个图像添加到水平滚动视图

时间:2014-01-13 10:48:56

标签: android image horizontalscrollview

enter image description here我正在构建一个应用程序,我想在水平滚动视图中添加多个图像,并希望在点击两个(左和右)srcolling按钮时滚动这些图像,但我无法做到这一点因为我是Android开发新手。所以任何人都可以提供我的解决方案如何做到这一点?提前谢谢....

2 个答案:

答案 0 :(得分:3)

将此代码放入xml

<HorizontalScrollView
        android:id="@+id/xml_full_img_hor_below_view"
        android:layout_width="match_parent"
        android:layout_height="@dimen/full_img_below_relative_height"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/xml_full_img_linear_below_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

///////////////////////////////////// java代码

这个 model_ProductMaster.listProductImages.size()是图片列表:

private void setProductImageInView() {

        LayoutInflater layoutInflater = (LayoutInflater) viewFullImage
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        linear_below_img_view.removeAllViews();
        // Set the image height,widt,scaling and margin of the imageview
        for (int i = 0; i < model_ProductMaster.listProductImages.size(); i++) {
            convertView = layoutInflater.inflate(
                    R.layout.xml_row_full_image_view, null, false);
            final ImageView img = (ImageView) convertView
                    .findViewById(R.id.xml_full_img_below_middleimg);
            img.setMinimumWidth((int) viewFullImage.getResources()
                    .getDimension(R.dimen.full_img_below_img_width_height));
            img.setMaxWidth((int) viewFullImage.getResources().getDimension(
                    R.dimen.full_img_below_img_width_height));
            img.setMinimumHeight((int) viewFullImage.getResources()
                    .getDimension(R.dimen.full_img_below_img_width_height));
            img.setMaxHeight((int) viewFullImage.getResources().getDimension(
                    R.dimen.full_img_below_img_width_height));
            img.setScaleType(ScaleType.CENTER_INSIDE);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(10, 0, 0, 0);
            layoutParams.gravity = Gravity.CENTER;
            convertView.setLayoutParams(layoutParams);
            img.setScaleType(ScaleType.FIT_XY);
            imageLoader.displayImage(
                    model_ProductMaster.listProductImages.get(i).ImageUrl, img,
                    options, animateFirstListener);
            linear_below_img_view.addView(convertView);
            convertView.setId(i);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < linear_below_img_view.getChildCount(); i++) {
                        View view = linear_below_img_view.getChildAt(i);
                        if (view == v) {
                            view.setBackgroundResource(R.drawable.xml_popup_border_with_bg);
                        } else {
                            view.setBackgroundResource(R.drawable.common_bg_with_dark_border);
                        }
                    }

                    loadImageonCenter(model_ProductMaster.listProductImages
                            .get(v.getId()).ImageUrl);
                    System.gc();
                }
            });

        }
        linear_below_img_view.getChildAt(0).setBackgroundResource(
                R.drawable.xml_popup_border_with_bg);
        // Image Starting load

        System.gc();
    }

答案 1 :(得分:2)

main.xml中

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="100dp" >

    <ImageView
        android:id="@+id/xml_home_screen_img_leftarrow"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:padding="5dp"
        android:src="@drawable/leftarrow" />

    <HorizontalScrollView
        android:id="@+id/xml_full_img_hor_below_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/xml_full_img_linear_below_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal" >

            <include
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                layout="@layout/xml_row_full_image_view" />

            <include
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                layout="@layout/xml_row_full_image_view" />

            <include
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                layout="@layout/xml_row_full_image_view" />

            <include
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                layout="@layout/xml_row_full_image_view" />

            <include
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                layout="@layout/xml_row_full_image_view" />
        </LinearLayout>
    </HorizontalScrollView>

    <ImageView
        android:id="@+id/xml_home_screen_img_rightarrow"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:padding="5dp"
        android:src="@drawable/rightarrow" />
</RelativeLayout>

xml_row_full_image_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/xml_full_img_below_mainlinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:orientation="horizontal"
    android:padding="4dp" >

    <ImageView
        android:id="@+id/xml_full_img_below_middleimg"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" />

</LinearLayout>