当GridView滚动时,它将更改其视图的激活状态 [英] When the GridView scrolls, it changes its view's activated status

查看:43
本文介绍了当GridView滚动时,它将更改其视图的激活状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一些String创建一个gridview.当我单击每个按钮时,将其状态更改为激活",然后使用该属性更改所选项目的颜色.

I create a gridview with some String. WHen I click on each of it I change its status to activated and I use that property to change the color of the selected item.

如果不滚动gridview,所有组件都可以正常工作,但是如果我选择了一个项目,并且在滚动gridview之后,每次滚动gridview时,该项目就会开始更改其颜色.

All works correctly if I don't scroll the gridview, but If I selected one item and after I scroll the gridview , the item start to change its color every time that I scroll the gridview.

您能帮我找到错误吗? :(

Can you help me to find the error ? :(

这是我的代码:

@Override
    public View getView(int i, View view, ViewGroup viewGroup) 
    {
        ViewHolder viewHolder;
        // General ListView optimization code.

        if (view == null) {
            view = mInflator.inflate(R.layout.gridview_command_element, null);
            viewHolder = new ViewHolder();
            viewHolder.boh = (TextView) view.findViewById(R.id.cmd_name);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }


        final String Name = lista.get(i);

        if (Name != null)
        {  
            viewHolder.boh.setText(Name);
        }

        return view;
    }

    static class ViewHolder 
    {
       TextView boh;
    }

这是onitemclicklistener:

this is the onitemclicklistener:

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


        if(adapter4selectedCmd.getCount() < 10)
        {   

            view.setActivated(true);
            String cmd = (String)adapter.getItem(position);
        }
        else
        {
            view.setActivated(false);
        }

    }

这是xml布局和选择器:

This is the xml layout and selector :

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:color="@color/tableTextSelectedColor" />
    <item android:color="@color/tableTextColor" />
</selector>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_vertical|center_horizontal"
              android:id="@+id/ecu_list_element"
              >

    <TextView android:id="@+id/cmd_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24dp"
            android:textColor="@color/text_selector"
            android:gravity="center_horizontal|center_vertical"/>


</LinearLayout>

推荐答案

滚动网格视图时,视图将被回收.如果您更改一个子视图的状态,然后向下滚动并重新使用该子视图,但是新数据的状态不相同,则您的子视图将具有与您第一次设置时相同的状态.创建一个 SparseBooleanArray 以保存每个对象的状态.除了将视图设置为仅在按钮上单击以激活之外,还需要更新稀疏布尔数组并在getView()方法调用中检查布尔数组以设置激活位置.

When you scroll your grid view the views are recycled. If you change the state of one child view and then scroll down and that child view is reused but the new data does not have the same state your child view will have the same state that it had the first time you set it. Create a SparseBooleanArray to save the state of each object. Instead of setting the view to activated only in the button click you also need to update the sparse boolean array and check the boolean array in the getView() method call to set the activated position.

SparseBooleanArray booleanArray = new SparseBooleanArray();
@Override
public View getView(int i, View view, ViewGroup viewGroup) 
{
    ViewHolder viewHolder;
    // General ListView optimization code.

    if (view == null) {
        view = mInflator.inflate(R.layout.gridview_command_element, null);
        viewHolder = new ViewHolder();
        viewHolder.boh = (TextView) view.findViewById(R.id.cmd_name);
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }


    final String Name = lista.get(i);

    if (Name != null)
    {  
        viewHolder.boh.setText(Name);
    }
    if(booleanArray.get(i)){
        view.setActivated(true);
    } else {
        view.setActivated(false);
    }
    return view;
}

static class ViewHolder 
{
   TextView boh;
}

这是onitemclicklistener:

here is the onitemclicklistener:

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


    if(adapter4selectedCmd.getCount() < 10)
    {   

        view.setActivated(true);
        booleanArray.put(position, true);
        String cmd = (String)adapter.getItem(position);
    }
    else
    {
        view.setActivated(false);
        view.put(position, false);
    }

}

这篇关于当GridView滚动时,它将更改其视图的激活状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆