編輯:關於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中的事件分發機制的懵懂期應該是Listview中對於item的點擊和長按事件,那個時候知道item的長按事件是返回boolean值的方法,我們知道要把
思路分析:1、自定義View實現字母導航欄2、ListView實現聯系人列表3、字母導航欄滑動事件處理4、字母導航欄與中間字母的聯動5、字母導航欄與ListView的聯動
1、如圖片1所示,在一個Android應用中,除了標題欄和底層的ActionBar的區域,是我們可以操縱的UI區域,那是不是說我們就不能改變除了這兩個區域的UI呢?答案是
時間過得真快,又到了寫博客的時候了(/▽╲)。這次按照計劃記錄一個簡單的自定義ViewGroup:流布局FlowLayout的實現過程,將View的繪制流程和Layout