0

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

ImageButton这是未点击an 时图像资源 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
};

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

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“已点击”状态。因此,我将这个图像资源 id 的 int 数组设置为加载ImageButtons时的默认图像资源。ImageButtons

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 扩展片段 { 私有最终静态字符串 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将每个ImageButton加载的内容存储在 中,GridView以便稍后当我必须更改 an 的图像资源时ImageButton,我只需ImageButton要从中获取它SparseArray,然后将其图像资源设置为mineIconsSelectedint 数组中的相应资源。在此之前,我将确保ImageButton我之前点击的图像资源将设置为mineIconsint 数组中的相应资源。

现在,奇怪的问题来了。

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

请帮我。非常感谢!

4

1 回答 1

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]);

Thus when the button is selected, Android will automatically switch the drawable asset for you.

于 2015-07-24T03:26:27.453 回答