Android選取gridview並且標出選擇的順序

我們都見過QQ發送照片的時候有一個選擇照片的時候,會標識出選擇的順序。具體用語言來描述一下就是:

gridview中,有很多item,當我們點擊item的時候,會在右上角標識出你已經選擇該item,並且,標識出你選擇該item的順序。如果取消選擇某一個,那麼其他的選擇順序全部-1。

先上個圖吧,讓大家更加直觀的理解,如下圖,第一個選擇的是我們標記1,第二個選擇標記2,第三個則是3,如果我們再點擊2,則是取消選擇,取消選擇2的話,那麼3就-1,變成2。而原先的2則是取消掉了。這就是我們要實現的效果。


這個界面的實現效果,我們給大家簡單的說一下吧。重點是說一下右上角的計順序的實現。

首先,gridview。然後定義item。定義adapter。

item的佈局中,說明一下右上角這個控件,我用的是checkbox,因爲有個選中和沒選中的效果展示。

checkbox的背景選擇的drawable如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@mipmap/num_check" android:state_checked="true" />
    <item android:drawable="@mipmap/num_normal" android:state_checked="false" />
    <item android:drawable="@mipmap/num_normal" />

</selector>  
然後,重點實現就是在我們的adapter中。

實現邏輯先說一下:

定義一個list,來存我們選中的checkbox,也就是你選擇點一下checkbox,那麼我們在這個list中add一個,然後使用notifyDataSetChanged()。而如果取消選擇,則從我們的list中remove掉,然後notify。然後add到list中就有順序了,這個數字標號就利用添加的順序,然後index的基礎上加1,就是我們的角標值。

廢話也就這了,Adapter方面的代碼吧:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import com.kegoal.model.Plan;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/26 0026.
 */
public class GridPlanAdapter extends BaseAdapter {

    Context context;
    List<Plan> list;
    /**
     * 存已經選擇checkboxlist
     */
    List<Plan> selectedPostions;

    public GridPlanAdapter(Context _context, List<Plan> _list) {
        this.list = _list;
        this.context = _context;
//        selectedPostions = new ArrayList<Plan>();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Plan getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Plan plan = getItem(position);
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.make_plan_item, null);
            holder.num_plan = (CheckBox) convertView.findViewById(R.id.num_make_plan);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
//        final ViewHolder finalHolder = holder;
        int selectNumber = getSelectedIndex(plan);
        if (selectNumber != 0) {
            holder.num_plan.setText(String.valueOf(selectNumber));
        } else {
            holder.num_plan.setText("");
        }
        holder.num_plan.setTag(plan);
        holder.num_plan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Plan item = (Plan) buttonView.getTag();
                if (isChecked) {
                    selectPlan(item);
                    notifyDataSetChanged();
                } else {
                    unSelectPlan(item);
                    notifyDataSetChanged();
                }

            }
        });

        return convertView;
    }


    class ViewHolder {
        public CheckBox num_plan;
    }

    //選擇checkbox的時候,將plan添加到list    void selectPlan(Plan p) {
        if (selectedPostions == null) {
            selectedPostions = new ArrayList<>();
        }
        if (!selectedPostions.contains(p))
            selectedPostions.add(p);
    }

    //取消選擇,從listremove    void unSelectPlan(Plan p) {
        if (selectedPostions == null)
            return;
        if (selectedPostions.contains(p))
            selectedPostions.remove(p);
    }

    //獲取右上角的角標,角標就是listindex+1
    int getSelectedIndex(Plan p) {
        if (selectedPostions == null || !selectedPostions.contains(p))
            return 0;
        else
            return (selectedPostions.indexOf(p) + 1);
    }
}
這就是實現的代碼,關鍵地方我也都有註釋啦,還有一處比較需要說明一下的地方,個人覺得比較重要,就是對於checkbox這個view的setTag。然後在CheckChangeListener中getTag,實際上,是確認我們選擇取消的是哪一個checkbox,然後將其在list中移除或者add即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章