android之GridView和Gallery

GridView:

/activity_main.xml

<RelativeLayout 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">


    <GridView 
        android:id="@+id/gvGenerals"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="60dp"
        android:horizontalSpacing="2dp"
        android:verticalSpacing="20dp"
        android:numColumns="auto_fit"
        />
    


</RelativeLayout>

java代碼:

package com.example.day05_01;


import java.util.ArrayList;
import java.util.List;


import com.litsoft.entity.General;




import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
private List<General> generals;//要顯示的數據集合
private GridView gvGenerals;//GridView對象
private BaseAdapter generalAdapt;//適配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();//初始化要顯示的數據集合、ListView對象、以及適配器
setListener();//設置按item事件
}



private void setListener() {
// TODO Auto-generated method stub
//短按事件監聽
gvGenerals.setOnItemClickListener(new OnItemClickListener() {


@Override

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, generals.get(position).getName()+":被短按 ", 50000).show();
}

});

//長按事件監聽
gvGenerals.setOnItemLongClickListener(new OnItemLongClickListener() {


@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, generals.get(position).getName()+":被長按 ", 50000).show();
return true;//1、如果返回false,長按後,他也會觸發短按事件2、如果返回true的話,長按後就不會觸發短按事件
}
});
}


private void init() {
// TODO Auto-generated method stub
//初始化要顯示的數據集合---start
generals = new ArrayList<General>();
//圖片資源集合
int[] resImags = {
R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
R.drawable.zhuyuanzhang
};
//將資源中的字符串組數轉換爲Java數組
String [] names = getResources().getStringArray(R.array.generals);
for (int i =0;i<resImags.length;i++){
General general = new General(resImags[i],names[i]);
generals.add(general);
}
//初始化要顯示的數據集合---end
//初始化listView
gvGenerals = (GridView) findViewById(R.id.gvGenerals);
//初始化適配器以及設置該listView的適配器
generalAdapt = new GeneralAdapter();
gvGenerals.setAdapter(generalAdapt);
}

class GeneralAdapter extends BaseAdapter {

//得到listView中item的總數
@Override
public int getCount() {
// TODO Auto-generated method stub
return generals.size();
}


@Override
public General getItem(int position) {
// TODO Auto-generated method stub
return generals.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


//簡單來說就是拿到單行的一個佈局,然後根據不同的數值,填充主要的listView的每一個item
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//拿到ListViewItem的佈局,轉換爲View類型的對象
View layout = View.inflate(MainActivity.this, R.layout.activity_item_generals_, null);
//找到顯示軍事家頭像的ImageView
ImageView ivThumb = (ImageView) layout.findViewById(R.id.ivThumb);
//找到顯示軍事家名字的TextView
TextView tvName = (TextView) layout.findViewById(R.id.tvName);
//獲取軍事中下標是position的軍事家對象
General general =  generals.get(position);
//顯示軍事家頭像
ivThumb.setImageResource(general.getImageSrc());
//顯示軍事家的姓名
tvName.setText(general.getName());

return layout;
}

}


}

適配佈局頁面 :layout\activity_item_generals_.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">


    <ImageView 
         android:id="@+id/ivThumb"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/baiqi"/>
    <TextView
        android:id="@+id/tvName"
          android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:text="白起"
        android:textSize="20sp"
        android:gravity="center_horizontal"
        />


</LinearLayout>

strings.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">Day05_01</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_item_generals_">ItemGenerals_Activity</string>
<string-array name="generals">
   <item>白起</item>
   <item>曹操</item>
   <item>成吉思汗</item>
   <item>韓信</item>
   <item>李世民</item>
   <item>努爾哈赤</item>
   <item>孫臏</item>
   <item>孫武</item>
   <item>朱元璋</item>
   <item>岳飛</item>
</string-array>
</resources>

將軍資源java對象:

package com.litsoft.entity;


public class General {
private int imageSrc;
private String name;
public int getImageSrc() {
return imageSrc;
}
public void setImageSrc(int imageSrc) {
this.imageSrc = imageSrc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public General(int imageSrc, String name) {
super();
this.imageSrc = imageSrc;
this.name = name;
}

}

效果:


Gallery:

 

/activity_main.xml

<RelativeLayout 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">


    <Gallery
        android:id="@+id/gvGenerals"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spacing="2dp"
        />
    


</RelativeLayout>

java代碼:

package com.example.day05_01;


import java.util.ArrayList;
import java.util.List;


import com.litsoft.entity.General;




import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
private List<General> generals;//要顯示的數據集合
private Gallery gvGenerals;//GridView對象
private BaseAdapter generalAdapt;//適配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();//初始化要顯示的數據集合、ListView對象、以及適配器
setListener();//設置按item事件
}



private void setListener() {
// TODO Auto-generated method stub
//短按事件監聽
gvGenerals.setOnItemClickListener(new OnItemClickListener() {


@Override

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, generals.get(position).getName()+":被短按 ", 50000).show();
}

});

//長按事件監聽
gvGenerals.setOnItemLongClickListener(new OnItemLongClickListener() {


@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, generals.get(position).getName()+":被長按 ", 50000).show();
return true;//1、如果返回false,長按後,他也會觸發短按事件2、如果返回true的話,長按後就不會觸發短按事件
}
});
}


private void init() {
// TODO Auto-generated method stub
//初始化要顯示的數據集合---start
generals = new ArrayList<General>();
//圖片資源集合
int[] resImags = {
R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
R.drawable.zhuyuanzhang
};
//將資源中的字符串組數轉換爲Java數組
String [] names = getResources().getStringArray(R.array.generals);
for (int i =0;i<resImags.length;i++){
General general = new General(resImags[i],names[i]);
generals.add(general);
}
//初始化要顯示的數據集合---end
//初始化listView
gvGenerals = (Gallery) findViewById(R.id.gvGenerals);
//初始化適配器以及設置該listView的適配器
generalAdapt = new GeneralAdapter();
gvGenerals.setAdapter(generalAdapt);
}

class GeneralAdapter extends BaseAdapter {

//得到listView中item的總數
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}


@Override
public General getItem(int position) {
// TODO Auto-generated method stub
return generals.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


//簡單來說就是拿到單行的一個佈局,然後根據不同的數值,填充主要的listView的每一個item
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//拿到ListViewItem的佈局,轉換爲View類型的對象
View layout = View.inflate(MainActivity.this, R.layout.activity_item_generals_, null);
//找到顯示軍事家頭像的ImageView
ImageView ivThumb = (ImageView) layout.findViewById(R.id.ivThumb);
//找到顯示軍事家名字的TextView
TextView tvName = (TextView) layout.findViewById(R.id.tvName);
//獲取軍事中下標是position的軍事家對象
General general =  generals.get(position%10);
//顯示軍事家頭像
ivThumb.setImageResource(general.getImageSrc());
//顯示軍事家的姓名
tvName.setText(general.getName());

return layout;
}

}


}

適配佈局頁面 :layout\activity_item_generals_.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">


    <ImageView 
         android:id="@+id/ivThumb"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/baiqi"/>
    <TextView
        android:id="@+id/tvName"
          android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:text="白起"
        android:textSize="20sp"
        android:gravity="center_horizontal"
        />


</LinearLayout>

strings.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">Day05_01</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_item_generals_">ItemGenerals_Activity</string>
<string-array name="generals">
    <item>白起</item>
    <item>曹操</item>
    <item>成吉思汗</item>
    <item>韓信</item>
    <item>李世民</item>
    <item>努爾哈赤</item>
    <item>孫臏</item>
    <item>孫武</item>
    <item>朱元璋</item>
    <item>岳飛</item>
</string-array>
</resources>

將軍資源java對象:

package com.litsoft.entity;


public class General {
private int imageSrc;
private String name;
public int getImageSrc() {
return imageSrc;
}
public void setImageSrc(int imageSrc) {
this.imageSrc = imageSrc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public General(int imageSrc, String name) {
super();
this.imageSrc = imageSrc;
this.name = name;
}

}

效果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章