使gridview项可一一点击

时间:2018-12-31 12:36:46

标签: android gridview adapter onitemclicklistener

级别选择GridView图像

enter image description here

我需要使级别1可单击,使其余部分不可单击,当用户单击级别1时,它应使级别2可单击,而使其余部分不可单击,依此类推。另外,如果用户处于5级,则应该从1级到4级单击gridview

myAdapter = new MyCustomAdapter(getActivity());
    gridView.setAdapter(myAdapter);


    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {

               //do something

                       }

private class MyCustomAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    public MyCustomAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridObject object = my.get(position);
        GridObject revers=reverseobj.get(position);
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_icon_set, null);
            holder = new ViewHolder();
            holder.text = (ImageView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        if (check.get(position).equals("true"))
        {
            holder.text.setImageResource(revers.getName());
        }
        else {
            holder.text.setImageResource(object.getName());

        }
        return convertView;
    }

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

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

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


public class GridObject extends ArrayList<GridObject> {

private int image;
private int state;

public GridObject(int name, int state) {
    super();
    this.image = name;
    this.state = state;
}

public int getName() {
    return image;
}

public void setName(int name) {
    this.image = name;
}

public int getState() {
    return state;
}

public void setState(int state) {
    this.state = state;
}   
}

2 个答案:

答案 0 :(得分:0)

如果我正确阅读,则需要一个“当前级别”,该级别允许您选中/取消选中该级别之前的任何网格项目,还可以单击该项目进入下一个级别以使新的当前级别。

如果是这样,那么很容易通过添加cat aa |awk -v temp ="$NF" '{print $NF}' 变量并调整currentLevel来允许您单击最高级别(包括该级别)的项目来修改适配器。

这是您的适配器的更新版本,显示以下内容:

getView()

然后可以像以前一样在GridView上启用它:

private class MyCustomAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

    private LayoutInflater mInflater;
    // implement check as boolean array rather than what appears to be string array
    private final SparseBooleanArray check  = new SparseBooleanArray();
    // keep track of the current level
    private int currentLevel = 0;

    public MyCustomAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridObject object = my.get(position);
        GridObject revers = reverseobj.get(position);
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_icon_set, null);
            holder = new ViewHolder();
            holder.text = (ImageView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        if (check.get(position))  // used to be ...equals("true")
        {
            holder.text.setImageResource(revers.getName());
        }
        else {
            holder.text.setImageResource(object.getName());
        }
        // make items up to current level clickable
        convertView.setClickable(currentLevel < position);
        // show unclickable items as disabled
        convertView.setEnabled(currentLevel >= position);
        return convertView;
    }

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

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

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

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // increase the current level if the item for the current level is clicked
        currentLevel = Math.max(currentLevel, position + 1);
        // toggle the checked state for the clicked item
        check.put(position, !check.get(position, false));
        // refresh the views
        notifyDataSetChanged();
    }

}

请注意,我已将丢失的myAdapter = new MyCustomAdapter(getActivity()); gridView.setAdapter(myAdapter); gridView.setOnItemClickListener(myAdapter); 列表实现为check,而不是在您的版本中看起来像带有SparseBooleanArray"true"值的字符串数组。您可以根据需要将其更改回去。

答案 1 :(得分:0)

我使用了一个简单的检查列表boolean = gridview项目,并且可以正常工作。...默认情况下,我将第一个值设置为true,其余值设置为false。......

package-lock.json

`