編輯:Android開發實例
本文實例講述了Android實現GridView中ImageView動態變換的方法。分享給大家供大家參考。具體如下:
使用YY影音的時候,發現點擊GridView的某一個Item,Item裡面的圖標會在按下的時候發生變換,變成另外一個圖片。
自己寫了一個類似的demo,具體步驟如下:
1、創建一個包含Grid的Acitity
2、創建item.xml 裡面包含一個imageview和一個textview
3、自定義一個adapter,從baseadapter繼承
4、在getView中為每個imageView添加setOnTouchListener
代碼入下:
MainActivity.java package com.mygriddemo; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ private GridView gv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gv = (GridView) findViewById(R.id.mygrid); AddAdapter addadapter = new AddAdapter(this); gv.setAdapter(addadapter); } public class AddAdapter extends BaseAdapter { private final LayoutInflater mInflater; private final ArrayList<ListItem> mItems = new ArrayList<ListItem>(); // public static final int ITEM_SHORTCUT = 0; // public static final int ITEM_APPWIDGET = 1; // public static final int ITEM_LIVE_FOLDER = 2; // public static final int ITEM_WALLPAPER = 3; /** * Specific item in our list. */ public class ListItem { public final CharSequence text; public final Drawable image; //public final int actionTag; public final Drawable touchimage; //, int actionTag public ListItem(Resources res, int textResourceId, int imageResourceId,int touchImageResourceId) { text = res.getString(textResourceId); if (imageResourceId != -1) { image = res.getDrawable(imageResourceId); } else { image = null; } if (touchImageResourceId != -1) { touchimage = res.getDrawable(touchImageResourceId); } else { touchimage = null; } } } public AddAdapter(MainActivity launcher) { super(); mInflater = (LayoutInflater) launcher.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // Create default actions Resources res = launcher.getResources(); mItems.add(new ListItem(res, R.string.item1, R.drawable.item1, R.drawable.item1back)); mItems.add(new ListItem(res, R.string.item2, R.drawable.item2, R.drawable.item2back)); mItems.add(new ListItem(res, R.string.item3, R.drawable.item3, R.drawable.item3back)); mItems.add(new ListItem(res, R.string.item4, R.drawable.item4, R.drawable.item4back)); } public View getView(int position, View convertView, ViewGroup parent) { final ListItem item = (ListItem) getItem(position); if (convertView == null) { convertView = mInflater.inflate(R.layout.grid_item, parent, false); } final TextView textView = (TextView) convertView;//.findViewById(R.id.mygrid); textView.setTag(item); textView.setText(item.text); textView.setCompoundDrawablesWithIntrinsicBounds(null, item.image, null, null); textView.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View arg0, MotionEvent arg1) { // TODO Auto-generated method stub if (arg1.getAction() == MotionEvent.ACTION_DOWN){ Log.d("WeatherWidget", MotionEvent.ACTION_DOWN+""); textView.setCompoundDrawablesWithIntrinsicBounds(null, item.touchimage, null, null); } else if(arg1.getAction() == MotionEvent.ACTION_UP) { Log.d("WeatherWidget", arg1.getAction()+""); textView.setCompoundDrawablesWithIntrinsicBounds(null, item.image, null, null); Intent i = new Intent(MainActivity.this,Page1Activity.class); startActivity(i); } return true; } }); return convertView; } public int getCount() { return mItems.size(); } public Object getItem(int position) { return mItems.get(position); } public long getItemId(int position) { return position; } } }
main.xml代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <GridView android:id="@+id/mygrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="2" android:verticalSpacing="10dip" android:horizontalSpacing="10dip" android:stretchMode="columnWidth" android:gravity="center" > </GridView> </LinearLayout>
griditem.xml代碼:
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minHeight="?android:attr/listPreferredItemHeight" android:layout_centerHorizontal="true" android:drawablePadding="14dip" android:paddingLeft="15dip" android:paddingRight="15dip" android:gravity="center_horizontal" />
希望本文所述對大家的Android程序設計有所幫助。
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
上一節中講了App->Activity->Custom Title