GridViewのページングスライド表示によるジャンプActivityの実現

20052 ワード

まずGridView表示を実現する画像ページを定義し、ジャンプするすべての画像を3ページに分けて表示します.
 
MainManagerActivity
package com.hwtt.android.sellmanager.ui;

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

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;

import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.hwtt.android.sellmanager.adapter.MainGridViewAdapter;
import com.hwtt.android.sellmanager.adapter.MainPagerAdapter;
import com.hwtt.android.sellmanager.bean.ItemInfo;
import com.hwtt.android.sellmanager.utils.HttpUtil;

public class MainManagerActivity extends BaseReceiverActivity {
	private SharedPreferences sp1;
	GridView gridView;
	ViewPager pager;
	Context context = MainManagerActivity.this;
	MainPagerAdapter adapter;
	Class<?> mActivities1[] = { AttendanceManagerActivity.class,
			GoodsSalesManagerActivity.class, GoodsManagerActivity.class,
			TaskManagerActivity.class, NoticeActivity.class,
			PepoleManagerActivity.class };
	Class<?> mActivities2[] = { CustomerActivity.class,
			SystemSetActivity.class, InquireActivity.class,
			NetWorkActivity.class, MessageActivity.class,
			AddressBookActivity.class };
	Class<?> mActivities3[] = { ContactActivity.class,SMSMessageActivity.class };

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);

		
		setContentView(R.layout.sellmainpage);
		sp1 = this
				.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
		init();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		pager.setCurrentItem(requestCode);

	}

	void init() {
		pager = (ViewPager) findViewById(R.id.vPager);
		ArrayList<View> list = new ArrayList<View>();
		LayoutInflater layout = getLayoutInflater();
		View view1 = layout.inflate(R.layout.lay1, null);
		View view2 = layout.inflate(R.layout.lay2, null);
		View view3 = layout.inflate(R.layout.lay3, null);
		GridView gridview1 = (GridView) view1.findViewById(R.id.lay1_grid_view);
		GridView gridview2 = (GridView) view2.findViewById(R.id.lay2_grid_view);
		GridView gridview3 = (GridView) view3.findViewById(R.id.lay3_grid_view);
		MainGridViewAdapter gridAdapter1 = new MainGridViewAdapter(getList(1),
				context);
		MainGridViewAdapter gridAdapter2 = new MainGridViewAdapter(getList(2),
				context);
		MainGridViewAdapter gridAdapter3 = new MainGridViewAdapter(getList(3),
				context);
		gridview1.setAdapter(gridAdapter1);
		gridview2.setAdapter(gridAdapter2);
		gridview3.setAdapter(gridAdapter3);
		list.add(view1);
		list.add(view2);
		list.add(view3);
		MainPagerAdapter adapter = new MainPagerAdapter(list);
		pager.setAdapter(adapter);
	 gridview1.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				onListItemClick(arg2, 1);
			}
		});
		gridview2.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				onListItemClick(arg2, 2);
			}
		});
		gridview3.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				onListItemClick(arg2, 3);
			}
		});
	}

	void onListItemClick(int index, int page) {
		if (page == 1) {
			if (index < 0 || index >= mActivities1.length)
				return;
			Intent intent = new Intent(context, mActivities1[index]);
			startActivityForResult(intent, page - 1);

			// this.startActivity(intent);
		} else if (page == 2) {
			if (index < 0 || index >= mActivities2.length)
				return;
			Intent intent = new Intent(context, mActivities2[index]);
			startActivityForResult(intent, page - 1);
		} else {
			if (index < 0 || index >= mActivities3.length) {
				return;
			} else if (index == 0) {
				Intent intent = new Intent(context, mActivities3[index]);
				startActivityForResult(intent, page - 1);
			} else if (index == 1) {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.putExtra("address", " ");
				intent.putExtra("sms_body", " ");
				intent.setType("vnd.android-dir/mms-sms");
				startActivity(intent);
			}
		}
		int version = Integer.valueOf(android.os.Build.VERSION.SDK);
		if (version >= 5) {
			overridePendingTransition(R.anim.fade, R.anim.hold);
			// 
			// overridePendingTransition(R.anim.alpha_rotate,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.alpha_scale_rotate,
			// R.anim.my_alpha_action);
			// overridePendingTransition(
			// R.anim.alpha_scale_translate_rotate,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.alpha_scale_translate,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.alpha_scale,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.alpha_translate_rotate,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.alpha_translate,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.my_rotate_action,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.my_scale_action,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.my_translate_action,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.myanimation_simple,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.myown_design,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.scale_rotate,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.scale_translate_rotate,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.scale_translate,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.translate_rotate,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.hyperspace_in,
			// R.anim.hyperspace_out);
			// overridePendingTransition(R.anim.shake,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.push_left_in,
			// R.anim.push_left_out);
			// overridePendingTransition(R.anim.push_up_in,
			// R.anim.push_up_out);
			// overridePendingTransition(R.anim.slide_left,
			// R.anim.slide_right);
			// overridePendingTransition(R.anim.slide_top_to_bottom,
			// R.anim.my_alpha_action);
			// overridePendingTransition(R.anim.wave_scale,
			// R.anim.my_alpha_action);
		}
	}

	protected void onStart() {
		super.onStart();

	}

	protected void dialog() {
		AlertDialog.Builder builder = new Builder(context);
		builder.setMessage(" ?");
		builder.setTitle(" ");
		builder.setPositiveButton(" ",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						// update zongshiwei close all activity by
						// broadcastReceiver
						myExit();
						// finish();
						String path =getResources().getString(R.string.exitPPTUrl);
						List<NameValuePair> list = new ArrayList<NameValuePair>();

						NameValuePair value1 = new BasicNameValuePair(
								"username", sp1.getString("USER_NAME", ""));

						list.add(value1);
						HttpUtil httpUtil = new HttpUtil();

						String result = httpUtil.getMsgFromServer(path, list);
					}
				});
		builder.setNegativeButton(" ",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	protected void myExit() {
		Intent intent = new Intent();
		intent.setAction("ExitApp");
		this.sendBroadcast(intent);
		super.finish();
	}

	public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode && event.getRepeatCount() == 0)
			dialog();
		return true;
	};

	ArrayList<ItemInfo> getList(int i) {
		ArrayList<ItemInfo> list1 = new ArrayList<ItemInfo>();
		ArrayList<ItemInfo> list2 = new ArrayList<ItemInfo>();
		ArrayList<ItemInfo> list3 = new ArrayList<ItemInfo>();
		ItemInfo info1 = new ItemInfo();
		info1.setItem_img(R.drawable.advancedance_icon);
		info1.setItem_name(" ");
		ItemInfo info2 = new ItemInfo();
		info2.setItem_img(R.drawable.sale_manager);
		info2.setItem_name(" ");
		ItemInfo info3 = new ItemInfo();
		info3.setItem_img(R.drawable.goods_mana);
		info3.setItem_name(" ");
		ItemInfo info4 = new ItemInfo();
		info4.setItem_img(R.drawable.task_mana);
		info4.setItem_name(" ");
		ItemInfo info5 = new ItemInfo();
		info5.setItem_img(R.drawable.ico_msg_notice);
		info5.setItem_name(" ");
		ItemInfo info6 = new ItemInfo();
		info6.setItem_img(R.drawable.pepole_locat);
		info6.setItem_name(" ");
		ItemInfo info7 = new ItemInfo();
		info7.setItem_img(R.drawable.kehuguanli);
		info7.setItem_name(" ");
		ItemInfo info8 = new ItemInfo();
		info8.setItem_img(R.drawable.systemset);
		info8.setItem_name(" ");
		ItemInfo info9 = new ItemInfo();
		info9.setItem_img(R.drawable.cru);
		info9.setItem_name(" ");
		ItemInfo info10 = new ItemInfo();
		info10.setItem_img(R.drawable.network_manage);
		info10.setItem_name(" ");
		ItemInfo info11 = new ItemInfo();
		info11.setItem_img(R.drawable.upload_message);
		info11.setItem_name(" ");
		ItemInfo info12 = new ItemInfo();
		info12.setItem_img(R.drawable.address_book);
		info12.setItem_name(" ");
		ItemInfo info13 = new ItemInfo();
		info13.setItem_img(R.drawable.call);
		info13.setItem_name(" ");
		ItemInfo info14 = new ItemInfo();
		info14.setItem_img(R.drawable.sms);
		info14.setItem_name(" ");
		list1.add(info1);
		list1.add(info2);
		list1.add(info3);
		list1.add(info4);
		list1.add(info5);
		list1.add(info6);
		list2.add(info7);
		list2.add(info8);
		list2.add(info9);
		list2.add(info10);
		list2.add(info11);
		list2.add(info12);
		list3.add(info13);
		list3.add(info14);
		if (i == 1)
			return list1;
		else if (i == 2)
			return list2;
		else
			return list3;
	}

}

 
ページを塗りつぶした画像と画像の下にある文字を定義するMainGridViewAdapter
 
 
package com.hwtt.android.sellmanager.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hwtt.android.sellmanager.bean.ItemInfo;
import com.hwtt.android.sellmanager.ui.R;

public class MainGridViewAdapter extends BaseAdapter {
	ArrayList<ItemInfo> list;
	Context context;
 
	public MainGridViewAdapter(ArrayList<ItemInfo> list,Context context)
	{
		this.list=list;
		this.context=context;
	}
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null)
		{
			LayoutInflater layout=LayoutInflater.from(context);
			convertView=layout.inflate(R.layout.main_page_item, null);
		}
		ImageView itemImg=(ImageView)convertView.findViewById(R.id.item_img);
		TextView itemName=(TextView)convertView.findViewById(R.id.item_tv);
		itemImg.setImageResource(list.get(position).getItem_img());
		itemName.setText(list.get(position).getItem_name());
		return convertView;
	}
}

 
次に、ページングのMainPagerAdapterを定義します.
 
package com.hwtt.android.sellmanager.adapter;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;

import com.hwtt.android.sellmanager.ui.R;

public class MainPagerAdapter extends PagerAdapter{

	ArrayList<View> list;
	public MainPagerAdapter (ArrayList<View> list) 
	{
		this.list=list;
	}
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView(list.get(arg1));
	}

	@Override
	public void finishUpdate(View arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object instantiateItem(View arg0, int arg1) {
		((ViewPager) arg0).addView(list.get(arg1),0);
		return list.get(arg1);
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return  arg0==(arg1);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
		
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
		
	}

	
}

 
次に、画像と文字を埋め込むXML main_を定義します.page_item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
<ImageView android:id="@+id/item_img"
    android:layout_width="80dp"
    android:layout_height="70dp"
    android:layout_marginTop="15dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:scaleType="fitXY"/>
<TextView android:id="@+id/item_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/item_img"
    android:maxLines="2"
    android:layout_alignLeft="@id/item_img"
    android:layout_alignRight="@id/item_img"
    android:gravity="center_horizontal"
    android:textSize="13sp"
    android:textColor="#000000"
   />
</RelativeLayout>

あとGridViewのレイアウトを表示するsellmainpage
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    
     >
       <TextView 
           android:id="@+id/main_tv_hint"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:textColor="#000000"
        android:gravity="center"
        android:layout_marginTop="30dp"
        android:textSize="25sp"
        android:text=" "/>
       <TextView android:id="@+id/weather_tv"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"/>
         <android.support.v4.view.ViewPager
        android:id="@+id/vPager"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_below="@id/main_tv_hint"
        android:layout_gravity="center"
        android:layout_weight="1.0"
         
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />
 <!--  <GridView
        android:id="@+id/main_manager_grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dip"
        android:layout_margin="15dip"
        android:layout_marginTop="60dp"
        android:numColumns="3"
        android:verticalSpacing="20dip" /> -->
</RelativeLayout>

 
最後にGridViewの画像と文字を埋め込むbean ItemInfoを定義する.java
package com.hwtt.android.sellmanager.bean;

public class ItemInfo {

	int item_img;
	String item_name;
	public int getItem_img() {
		return item_img;
	}
	public void setItem_img(int item_img) {
		this.item_img = item_img;
	}
	public String getItem_name() {
		return item_name;
	}
	public void setItem_name(String item_name) {
		this.item_name = item_name;
	}
	
}