问题
I am building an application in which I want to add multiple images in a horizontal scrollview and want to scroll these imageson click of two (left and right) srcolling buttons, but I am not able to do that as I am new to android development.So can anybody provide my with the solution how to do it ? Thanks in advance....
回答1:
put this code in your 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 code
this model_ProductMaster.listProductImages.size() is images list:
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();
}
回答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>
来源:https://stackoverflow.com/questions/21089384/adding-multiple-images-to-horizontal-scroll-view-in-android