Android GridView子元素item按击交互设计:背景颜色改变

《Android GridView子元素按击交互设计:背景颜色改变》

转载https://blog.csdn.net/zhangphil/article/details/46048583

效果图:


大致的需求和ListView相仿,就是要求用户点击GridView中的子元素时候,要有一定的交互响应(背景颜色改变表明用户的操作)。重点是在GridView的适配器中,子元素的布局文件中,把android:background属性配置成一个响应的selector,在selector中分别处理android:state_pressed事件在true和false两种状态下的情况。

首先写一个MainActivity:

[java]  view plain  copy
  1. package zhangphil.bg_change;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.support.v7.app.ActionBarActivity;  
  7. import android.content.Context;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.AdapterView;  
  11. import android.widget.AdapterView.OnItemClickListener;  
  12. import android.widget.GridView;  
  13. import android.widget.SimpleAdapter;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends ActionBarActivity {  
  17.   
  18.     private final String IMAGE_TAG = "image", TEXT_TAG = "text";  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.   
  25.         GridView gridview = (GridView) findViewById(R.id.gridview);  
  26.   
  27.         // 数据集  
  28.         ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();  
  29.         for (int i = 0; i < 19; i++) {  
  30.             HashMap<String, Object> map = new HashMap<String, Object>();  
  31.             map.put(IMAGE_TAG, R.drawable.ic_launcher);  
  32.             map.put(TEXT_TAG, String.valueOf(i));  
  33.             data.add(map);  
  34.         }  
  35.   
  36.         SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,  
  37.                 new String[] { IMAGE_TAG, TEXT_TAG }, new int[] { R.id.image,  
  38.                         R.id.text });  
  39.   
  40.         gridview.setAdapter(adapter);  
  41.   
  42.         // 添加点击事件  
  43.         final Context context = this;  
  44.         gridview.setOnItemClickListener(new OnItemClickListener() {  
  45.             public void onItemClick(AdapterView<?> parent, View v,  
  46.                     int position, long id) {  
  47.                 Toast.makeText(context, String.valueOf(position),  
  48.                         Toast.LENGTH_SHORT).show();  
  49.             }  
  50.         });  
  51.     }  
  52. }  

MainActivity需要的布局文件activity_main.xml:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:id="@+id/gridview"  
  4.     android:layout_width="match_parent"   
  5.     android:layout_height="match_parent"  
  6.     android:padding="10dip"  
  7.     android:numColumns="auto_fit"  
  8.     android:verticalSpacing="10dip"  
  9.     android:horizontalSpacing="10dip"  
  10.     android:stretchMode="columnWidth" />  

在为GridView添加子元素时候,数据集中需要的item.xml,该XML文件中的item_selector将对交互时间做出响应(用户长按、点击的背景颜色改变):

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:gravity="center"  
  10.         android:orientation="vertical" >  
  11.   
  12.         <ImageView  
  13.             android:id="@+id/image"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:src="@drawable/ic_launcher" />  
  17.   
  18.         <TextView  
  19.             android:id="@+id/text"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content" />  
  22.     </LinearLayout>  
  23.   
  24.     <View  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="match_parent"  
  27.         android:background="@drawable/item_selector" />  
  28.   
  29. </FrameLayout>  

item_selector.xml:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:drawable="@drawable/item_pressed" android:state_pressed="true"/>  
  4.     <item android:drawable="@drawable/item_normal" android:state_pressed="false"/>  
  5. </selector>  

item_selector需要的两个文件item_normal.xml 和 item_pressed.xml:

item_normal

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <solid android:color="#00000000" />  
  5.   
  6.     <corners  
  7.         android:radius="5dp" />  
  8.       
  9.     <stroke android:width="1px" android:color="#CCCCCC" />  
  10.   
  11. </shape>  

item_pressed

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <solid android:color="#5501D1FF" />  
  5.   
  6.     <corners android:radius="5dp" />  
  7.   
  8.     <stroke  
  9.         android:width="1px"  
  10.         android:color="#CCCCCC" />  
  11.   
  12. </shape>  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值