Resimleri GridView ile göstermek

 

Resimlerin kutular şekilden(thumbnails) göstermek için Android GridView nesnesi kullanılabilir. Gridview resimleri ekrana istediğiniz boyutta etikleri ile birlikte göstermeye yardımcı olur.

Örneğimize bakalım

package com.gridviewornek;

import java.util.ArrayList;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.GridView;

import android.widget.Toast;

import android.widget.AdapterView.OnItemClickListener;

public class GridViewExampleActivity extends Activity {

/** Called when the activity is first created. */

private GridviewAdapter mAdapter;

private ArrayList<String> listCountry;

private ArrayList<Integer> listFlag;

private GridView gridView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

prepareList();

// prepared arraylist and passed it to the Adapter class

mAdapter = new GridviewAdapter(this,listCountry, listFlag);

// Set custom adapter to gridview

gridView = (GridView) findViewById(R.id.gridView1);

gridView.setAdapter(mAdapter);

// Implement On Item click listener

gridView.setOnItemClickListener(new OnItemClickListener()

{

@Override

public void onItemClick(AdapterView<?> arg0, View arg1, intposition,

long arg3) {

Toast.makeText(GridViewExampleActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();

}

});

}

public void prepareList()

{

listCountry = new ArrayList<String>();

listCountry.add(“india”);

listCountry.add(“Brazil”);

listCountry.add(“Canada”);

listCountry.add(“China”);

listCountry.add(“France”);

listCountry.add(“Germany”);

listCountry.add(“Iran”);

listCountry.add(“Italy”);

listCountry.add(“Japan”);

listCountry.add(“Korea”);

listCountry.add(“Mexico”);

listCountry.add(“Netherlands”);

listCountry.add(“Portugal”);

listCountry.add(“Russia”);

listCountry.add(“Saudi Arabia”);

listCountry.add(“Spain”);

listCountry.add(“Turkey”);

listCountry.add(“United Kingdom”);

listCountry.add(“United States”);

listFlag = new ArrayList<Integer>();

listFlag.add(R.drawable.india);

listFlag.add(R.drawable.brazil);

listFlag.add(R.drawable.canada);

listFlag.add(R.drawable.china);

listFlag.add(R.drawable.france);

listFlag.add(R.drawable.germany);

listFlag.add(R.drawable.iran);

listFlag.add(R.drawable.italy);

listFlag.add(R.drawable.japan);

listFlag.add(R.drawable.korea);

listFlag.add(R.drawable.mexico);

listFlag.add(R.drawable.netherlands);

listFlag.add(R.drawable.portugal);

listFlag.add(R.drawable.russia);

listFlag.add(R.drawable.saudi_arabia);

listFlag.add(R.drawable.spain);

listFlag.add(R.drawable.turkey);

listFlag.add(R.drawable.united_kingdom);

listFlag.add(R.drawable.united_states);

}

}

GridviewAdapter.java
GridviewAdapter.java
package com.technotalkative.gridviewexample;import java.util.ArrayList;

import android.app.Activity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;

public class GridviewAdapter extends BaseAdapter

{

private ArrayList<String> listCountry;

private ArrayList<Integer> listFlag;

private Activity activity;

public GridviewAdapter(Activity activity,ArrayList<String> listCountry, ArrayList<Integer> listFlag) {

super();

this.listCountry = listCountry;

this.listFlag = listFlag;

this.activity = activity;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return listCountry.size();

}

@Override

public String getItem(int position) {

// TODO Auto-generated method stub

return listCountry.get(position);

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}

public static class ViewHolder

{

public ImageView imgViewFlag;

public TextView txtViewTitle;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

ViewHolder view;

LayoutInflater inflator = activity.getLayoutInflater();

if(convertView==null)

{

view = new ViewHolder();

convertView = inflator.inflate(R.layout.gridview_row, null);

view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);

view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);

convertView.setTag(view);

}

else

{

view = (ViewHolder) convertView.getTag();

}

view.txtViewTitle.setText(listCountry.get(position));

view.imgViewFlag.setImageResource(listFlag.get(position));

return convertView;

}

}

main.xml

?

<?xml version=”1.0″ encoding=”utf-8″?><LinearLayoutxmlns:android=”http://schemas.android.com/apk/res/android

android:orientation=”vertical”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”>

 

<GridView

android:layout_height=”wrap_content”

android:id=”@+id/gridView1″

android:layout_width=”match_parent”

android:numColumns=”auto_fit”

android:horizontalSpacing=”10dp”

android:verticalSpacing=”10dp”>

</GridView>

</LinearLayout>

gridview_row.xml
?xml version=”1.0″ encoding=”utf-8″?><RelativeLayout

android:id=”@+id/relativeLayout1″

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

xmlns:android=”http://schemas.android.com/apk/res/android

android:background=”@drawable/border”

android:padding=”5dp”>

 

<ImageView

android:layout_height=”64dp”

android:id=”@+id/imageView1″

android:layout_width=”64dp”

android:src=”@drawable/icon”

android:layout_alignParentTop=”true”

android:layout_centerHorizontal=”true”>

</ImageView>

 

<TextView

android:text=”TextView”

android:layout_height=”wrap_content”

android:id=”@+id/textView1″

android:layout_width=”wrap_content”

android:layout_below=”@+id/imageView1″

android:layout_marginTop=”2dp”

android:layout_centerHorizontal=”true”

android:textSize=”18sp”

android:ellipsize=”marquee”></TextView>

 

 

</RelativeLayout>

Örneği buradan indirebilirsiniz.
https://github.com/PareshMayani/Android-GridView-Example

Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir