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



我的应用程序中的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
};

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

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最终静态字符串LOG_TAG="MineFragment";

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来存储GridView中加载的每个ImageButton,这样以后当我必须更改ImageButton的图像资源时,我只需要从SparseArray中获取该ImageButton,然后将其图像资源设置为mineIconsSelected int数组中的相应图像资源。在执行此操作之前,我将确保我之前点击的ImageButton的图像资源将设置为mineIconsint数组中的相应图像资源。

现在,奇怪的问题来了。

我能够在点击时成功更改存储在SparseArray中的任何ImageButton的图像资源,除了第一个。无论我检查了多少次代码,做了多少次日志,我都找不到问题出在哪里。

请帮帮我。非常感谢!

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

<?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将自动为您切换可提取资产。

相关内容

  • 没有找到相关文章

最新更新