ورود

View Full Version : مشکل در استفاده از gallery



saeedhushmand
جمعه 05 اردیبهشت 1393, 12:15 عصر
با عرض سلام

من می خوام یک list<drawable> رو توی یک گالری نشان بدم

اینم کدم

List<Drawable> drawables = new ArrayList<Drawable>();
drawables.add(LoadImageFromWebOperations(lstEstate[1]));

Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setSpacing(1);




چه جوری این لیست رو تو gallery ست کنم؟

#root#
جمعه 05 اردیبهشت 1393, 13:34 عصر
اول این مثال رو پیاده کنید ، بعد با توجه به نیازتون توش تغییرات رو بدید

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/imageView1"
android:layout_marginTop="100dp"
android:layout_width="250dp"
android:layout_gravity="center_horizontal"
android:layout_height="250dp"
android:src="@drawable/image1" />

</LinearLayout>

MainActivity

public class MainActivity extends Activity
{

ImageView selectedImage;
private Integer[] mImageIds = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7,
R.drawable.image8
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Gallery gallery = (Gallery) findViewById(R.id.gallery1);
selectedImage=(ImageView)findViewById(R.id.imageVi ew1);
gallery.setSpacing(1);
gallery.setAdapter(new GalleryImageAdapter(this));

// clicklistener for Gallery
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "Your selected position = " + position, Toast.LENGTH_SHORT).show();
// show the selected Image
selectedImage.setImageResource(mImageIds[position]);
}
});
}

}

GalleryImageAdapter



public class GalleryImageAdapter extends BaseAdapter {
private Context mContext;

private Integer[] mImageIds = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7,
R.drawable.image8
};

public GalleryImageAdapter(Context context) {
mContext = context;
}

public int getCount() {
return mImageIds.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}


// Override this method according to your need
public View getView(int index, View view, ViewGroup viewGroup) {
ImageView i = new ImageView(mContext);

i.setImageResource(mImageIds[index]);
i.setLayoutParams(new Gallery.LayoutParams(200, 200));

i.setScaleType(ImageView.ScaleType.FIT_XY);

return i;
}
}



منبع (http://www.learn-android-easily.com/2013/07/android-gallery-view-example.html)