İçeriğe geç

Android Gallery İçin ImageAdapter Oluşturma

Galeri için kendi ImageAdapter nesnemizi BaseAdapter üzerinden üreterek oluşturabiliriz.

Keys;

  • Gallery
  • Gallery setAdapter
  • ImageView
  • ImageView setImageResource
  • Toast makeText
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gallery.MainActivity"
android:orientation="vertical">
	<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Images of Turkey" />
	<Gallery
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gallery"/>
	<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<declare-styleable name="galleryTurkey">
		<attr name="android:galleryItemBackground"/>
	</declare-styleable>
</resources>
public class MainActivity extends AppCompatActivity {

  // Images to display
  Integer[] imageIDs = {
    R.drawable.res1,
    R.drawable.res2,
    R.drawable.res3,
    R.drawable.res4,
    R.drawable.res5
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Gallery gallery = (Gallery) findViewById(R.id.gallery);

    gallery.setAdapter(new ImageAdapter(this));
    gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Override
      public void onItemClick(AdapterView < ?>adapterView, View view, int i, long l) {
        Toast.makeText(getBaseContext(), "pic" + (i + 1) + " selected", Toast.LENGTH_SHORT).show();

        // Display selected image
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageResource(imageIDs[i]);
      }
    });
  }

  public class ImageAdapter extends BaseAdapter {

    Context context;
    int itemBacground;

    public ImageAdapter(Context c) {
      context = c;
      // Set style
      TypedArray typedArray = obtainStyledAttributes(R.styleable.galleryTurkey);

      itemBacground = typedArray.getResourceId(R.styleable.galleryTurkey_android_galleryItemBackground, 0);

      typedArray.recycle();
    }

    public int getCount() {
      return imageIDs.length;

    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;
      if (convertView == null) {
        imageView = new ImageView(context);
        imageView.setImageResource(imageIDs[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
      } else {
        imageView = (ImageView) convertView;
      }
      imageView.setBackgroundResource(itemBacground);
      return imageView;
    }
  }

}
Tarih:Android

İlk Yorumu Siz Yapın

Bir Cevap Yazın