美文网首页Android
一个item有选中效果的Gridview

一个item有选中效果的Gridview

作者: lipeiyan | 来源:发表于2016-06-01 15:57 被阅读1402次

    不难,所以不废话,直接上代码

    /**
     * 点击item背景的gridView
     * 至于为什么前缀为radio,因为这是单选的,你也可以改为多选的,原理一样,有空我再写
     * Created by payne.
     */
    public class RadioGridView extends GridView implements AdapterView.OnItemClickListener {
      private int currentPosition = 0;
      private OnRadioItemClickListener mListener;
      private int mBgImageSelected;
      private int mBgImageUnselected;
    
      public RadioGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
      }
    //可以在xml文件直接设置item背景,是不是很方便
      private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RadioGridView);
        mBgImageSelected = typedArray.getResourceId(R.styleable.RadioGridView_item_selected,
            R.drawable.item_selected);
        mBgImageUnselected = typedArray.getResourceId(R.styleable.RadioGridView_item_unselected,
            R.drawable.item_unselected);
        typedArray.recycle();
        setOnItemClickListener(this);
      }
    
      @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //一个很小的逻辑,刚开始用的for循环,感觉太low了,改为这种
        int lastPosition = currentPosition;
        currentPosition = position;
        parent.getChildAt(lastPosition).setBackgroundResource(mBgImageUnselected);//未被选择时的背景
        view.setBackgroundResource(mBgImageSelected);//被选择是的背景
        if (mListener != null) {  
            mListener.onItemClick(getId(), position);
        }
      }
    
    //设置高度,因为我发现高度变小了,不是正常高度
      @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(
            Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
      }
    
      /**
       * 设置item点击监听器
       *
       * @param listener item点击监听器
       */
      public void setOnRadioItemClickListener(OnRadioItemClickListener listener) {
        mListener = listener;
      }
    
      /**
       * item点击监听器
       */
      public interface OnRadioItemClickListener {
        /**
         * @param gridViewId gridView id
         * @param position item 位置
         */
        void onItemClick(int gridViewId, int position);
      }
    }
    

    attrs文件

     <declare-styleable name="RadioGridView">
        <attr format="reference" name="item_selected"/>
        <attr format="reference" name="item_unselected"/>
      </declare-styleable>
    

    背景文件是单纯的白色和紫红色,就不上传了


    xml文件中的使用

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:paddingLeft="16dp"
      android:paddingRight="16dp"
      android:paddingTop="20dp">
    
      <com.payne.demo.RadioGridView
        android:id="@+id/rgv"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        app:item_selected="@drawable/item_selected"
        app:item_unselected="@drawable/item_unselected"
        tools:listitem="@layout/view_device_version_item"/>
    </LinearLayout>
    

    效果

    Paste_Image.png

    相关文章

      网友评论

      本文标题:一个item有选中效果的Gridview

      本文链接:https://www.haomeiwen.com/subject/brphdttx.html