Android GridView 实现9宫格菜单,并实现item点击按压效果更改图片

前言,大家可能开发的时候都会遇到9空格菜单的需求,并且ui还有item点击效果更改不同的图片,我也是在项目中发现的问题,并解决的,在这里记录下!!
1,刚刚开始的时,本来想用linerLayout布局用权重去实现的,但是这样实现,布局文件就好烦,要写好多布局….
所以想到GridView去实现,下面就是布局文件

<GridView
    android:id="@+id/id_gv_jj"
    android:numColumns="3"
    android:horizontalSpacing="1dp"
    android:verticalSpacing="1dp"
    android:layout_below="@+id/id_rl_head"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
 android:horizontalSpacing="1dp"
    android:verticalSpacing="1dp"

这两个属性就是航向 和纵向的宽度,
2 gridView 的布局文件了

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/gv_click"
                android:gravity="center"
                android:minHeight="100dp">



    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_centerHorizontal="true"
            android:id="@+id/id_iv_bg"
            android:layout_width="wrap_content"
            android:background="@mipmap/ks_m"
            android:layout_height="wrap_content"/>
        <TextView
            android:layout_marginTop="5dp"
            android:layout_below="@+id/id_iv_bg"
            android:id="@+id/id_tv_ks"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="快速推荐"
            android:textColor="@color/text_clours_selector"
            android:textSize="13dp"/>
    </RelativeLayout>




</RelativeLayout>

这里没有什么好说的 ,就一个imageView 和TextView
3接下来就是我们所熟悉的适配器Adapter

package com.dhfjj.program.adapters;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.dhfjj.program.R;
import com.dhfjj.program.utils.GvUtils;

/**
 * Created by Administrator on 2016/2/17.
 * GridView 的adapter
 */
public class GvJJAdapter extends BaseAdapter {
    private Context mContext;
    private int[] mresId;
    private GridView mgvJJ;
    public GvJJAdapter(Context mContext,GridView mgvJJ) {
        this.mContext = mContext;
        this.mgvJJ = mgvJJ;
        mresId = GvUtils.gvResComId;
    }

    @Override
    public int getCount() {
        return mresId.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHOlder viewHOlder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_gv_jj, parent, false);
            viewHOlder = new ViewHOlder();
            viewHOlder.tvKs = (TextView) convertView.findViewById(R.id.id_tv_ks);
            viewHOlder.iv_flag = (ImageView) convertView.findViewById(R.id.id_iv_bg);
            convertView.setTag(viewHOlder);

        } else {
            viewHOlder= (ViewHOlder) convertView.getTag();
        }
        viewHOlder.tvKs.setText(GvUtils.gvTextDataStr[position]);

        Drawable drawCilckTop = mContext.getResources().getDrawable(GvUtils.gvResClickId[position]);
        Drawable drawableCom = mContext.getResources().getDrawable(GvUtils.gvResComId[position]);
        StateListDrawable listDrawable = new StateListDrawable();
        listDrawable.addState(new int[]{android.R.attr.state_pressed}, drawCilckTop);
        listDrawable.addState(new int[]{}, drawableCom);
        viewHOlder.iv_flag.setBackgroundDrawable(listDrawable);
        // gridView 的点击事件

        return convertView;
    }

    public  class  ViewHOlder{
        TextView tvKs;
        ImageView iv_flag;
    }
}

下面的两句代码是 获得点击时候 的Drawable和默认的时候 Drawable的值

        Drawable drawCilckTop = mContext.getResources().getDrawable(GvUtils.gvResClickId[position]);
        Drawable drawableCom = mContext.getResources().getDrawable(GvUtils.gvResComId[position]);

下面的几句代码的作用就跟在drawable 文件夹下 新建seteror 的xml文件一样

      StateListDrawable listDrawable = new StateListDrawable();
        listDrawable.addState(new int[]{android.R.attr.state_pressed}, drawCilckTop);
        listDrawable.addState(new int[]{}, drawableCom);

下面就直接设置背景到ImageView 就可以实现了

   viewHOlder.iv_flag.setBackgroundDrawable(listDrawable);
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值