在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
,然后将其图像资源设置为mineIconsSelected
int 数组中的相应资源。在此之前,我将确保ImageButton
我之前点击的图像资源将设置为mineIcons
int 数组中的相应资源。
现在,奇怪的问题来了。
我能够在点击时成功更改ImageButton
存储在其中的任何图像资源SparseArray
,除了第一个。无论我检查我的代码多少次,制作日志,我都找不到问题所在。
请帮我。非常感谢!