无法在网格视图中更改图像按钮的图像资源

时间:2015-07-24 02:52:35

标签: android gridview android-imagebutton sparse-array

我的应用中的GridView包含多个ImageButtons。点击ImageButton时,该ImageButton的图像资源将发生变化。

当未点按ImageButton时,这是图像资源ID的int数组。

private final int mineIcons[] = 
{
    R.drawable.habit,
    R.drawable.story,
    R.drawable.submenu_voice_message,
    R.drawable.study,
    R.drawable.guess,
    R.drawable.poem,
    R.drawable.add
};

这是点击ImageButton时图像资源ID的int数组。

private final int mineIconsSelected[] = 
{
    R.drawable.habit_selected,
    R.drawable.story_selected,
    R.drawable.submenu_voice_message_selected,
    R.drawable.study_selected,
    R.drawable.guess_selected,
    R.drawable.poem_selected,
    R.drawable.add
};

加载此活动时,我希望ImageButton中的第一个GridView处于"状态"状态。所以我在加载ImageButtons时为ImageButtons的默认图像资源设置了这个int数组资源ID。

private final int mineIconsDefault[] = 
{
    R.drawable.habit_selected,
    R.drawable.story,
    R.drawable.submenu_voice_message,
    R.drawable.study,
    R.drawable.guess,
    R.drawable.poem,
    R.drawable.add
};

这是Fragment

的完整代码

公共类MineFragment扩展了Fragment {     private final static String LOG_TAG =" MineFragment&#34 ;;

private final int mineIcons[] = 
{
    R.drawable.habit,
    R.drawable.story,
    R.drawable.submenu_voice_message,
    R.drawable.study,
    R.drawable.guess,
    R.drawable.poem,
    R.drawable.add
};

private final int mineIconsSelected[] = 
{
    R.drawable.habit_selected,
    R.drawable.story_selected,
    R.drawable.submenu_voice_message_selected,
    R.drawable.study_selected,
    R.drawable.guess_selected,
    R.drawable.poem_selected,
    R.drawable.add
};

private SparseArray<ImageButton> submenuIconArray = new SparseArray<ImageButton>();
private SubMenuListAdapter submenuListAdapter = new SubMenuListAdapter();
private MineListAdapter mineListAdapter = new MineListAdapter();

private GridView gvSubmenu;
private ListView lvMine;

private int selectedItemNumber = 0;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_mine, container, false);
    init(view);

    return view;
}

private void init(View view)
{
    lvMine = (ListView)view.findViewById(R.id.lv_mine);
    lvMine.setAdapter(mineListAdapter);

    gvSubmenu = (GridView)getActivity().findViewById(android.R.id.content).findViewById(R.id.gv_submenu);
    gvSubmenu.setAdapter(submenuListAdapter);
}

public void showSubmenu()
{
    if(gvSubmenu != null)
    {
        gvSubmenu.setVisibility(View.VISIBLE);
    }
}

public void hideSubmenu()
{
    if(gvSubmenu != null)
    {
        gvSubmenu.setVisibility(View.GONE);
    }
}

private class SubMenuListAdapter extends BaseAdapter
{       
    private final int mineIconsDefault[] = 
    {
        R.drawable.habit_selected,
        R.drawable.story,
        R.drawable.submenu_voice_message,
        R.drawable.study,
        R.drawable.guess,
        R.drawable.poem,
        R.drawable.add
    };

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup root) 
    {
        if(convertView == null)
        {
            convertView = LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.list_item_submenu, root, false);
        }

        ImageButton ibSubmenuIcon = (ImageButton)convertView.findViewById(R.id.ib_item_submenu_icon);
        ibSubmenuIcon.setImageResource(mineIconsDefault[position]);
        ibSubmenuIcon.setOnClickListener(new OnClickListener() 
        {   
            @Override
            public void onClick(View arg0) 
            {
                submenuIconArray.get(selectedItemNumber).setImageResource(mineIcons[selectedItemNumber]);
                submenuIconArray.get(position).setImageResource(mineIconsSelected[position]);

                selectedItemNumber = position;
            }
        });

        submenuIconArray.append(position, ibSubmenuIcon);

        return convertView;
    }
}

private class MineListAdapter extends BaseAdapter
{
    //temp 
    private String[] tempListItems = {"item 1", "item 2", "item 3", "item 4", "item 5"};

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

    @Override
    public Object getItem(int arg0) 
    {
        return tempListItems[arg0];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup root) 
    {
        if(convertView == null)
        {
            convertView = LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.list_item_layout_1, root, false);
        }

        ImageView ivItemIcon = (ImageView)convertView.findViewById(R.id.iv_item_icon);
        TextView tvItemLabel = (TextView)convertView.findViewById(R.id.tv_item_label);

        tvItemLabel.setText(tempListItems[position]);

        return convertView;
    }
}

}

我使用SparseArray存储ImageButton中加载的每个GridView,以便稍后当我必须更改ImageButton的图像资源时,我只需要从ImageButton获取SparseArray,然后将其图像资源设置为mineIconsSelected int数组中的相应资源。在此之前,我将确保先前点击的ImageButton的图像资源将设置为mineIcons int数组中的相应图像资源。

现在,奇怪的问题出现了。

我可以在点击时成功更改ImageButton中存储的任何SparseArray的图像资源, 第一之一。无论我多少次检查我的代码,制作日志,我都无法找到问题所在。

请帮帮我。非常感谢!

1 个答案:

答案 0 :(得分:2)

我强烈建议您先简化代码。在这样做之后,您的问题将会消失或更容易调试。由于你有这么多按钮,因此前期会有点乏味,但它会使你的代码更容易管理。 为每个按钮创建一个简单的选择器可绘制文件。即习惯按钮: habit_selector.xml(放在drawable文件夹中)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/habit" android:android:state_selected="true"/>
    <item android:drawable="@drawable/habit_selected"/>
</selector>

然后在按钮上设置:

private final int mineIconSelectors[] = 
{
    R.drawable.habit_selector,
    R.drawable.story_selector,
    R.drawable.submenu_voice_message_selector,
    ...
}

    ...
    ImageButton ibSubmenuIcon = (ImageButton)convertView.findViewById(R.id.ib_item_submenu_icon);
    ibSubmenuIcon.setBackground(mineIconSelectors[position]);

因此,当选择该按钮时,Android将自动为您切换可绘制资产。