我有一个列表视图,其中包含16个项目。点击项目1的时候,变更了项目1的图像。但是点击项目2的话,项目1的图像不变化。单击项目2时,我希望项目1的图像恢复为默认值。我在网上搜索了很多人,但我找不到解决方案。有人帮我吗?
这里是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
menuitemfix = getResources().getStringArray(R.array.ns_menu_items_fix);
viewslide = new View(getActivity());
textslide = new TextView(getActivity());
imageslide = new ImageView(getActivity());
items.add(new HeaderSection("ĐĂNG NHẬP"));
items.add(new EntryItem("Thông báo", R.drawable.ic_slidemenu_notice));
items.add(new EntryItem("Nhắc lịch", R.drawable.ic_slidemenu_prompted));
items.add(new SectionItem("TRUYỀN HÌNH"));
items.add(new EntryItem("Kênh ", R.drawable.ic_slidemenu_live));
items.add(new EntryItem("Lịch phát sóng", R.drawable.ic_slidemenu_epg));
items.add(new SectionItem("THƯ VIỆN"));
items.add(new EntryItem("Rạp phim", R.drawable.ic_slidemenu_movie));
items.add(new EntryItem("Video Clip", R.drawable.ic_slidemenu_video));
items.add(new EntryItem("TV Show", R.drawable.ic_slidemenu_tvshow));
items.add(new EntryItem("Đã phát sóng", R.drawable.ic_slidemenu_catchup));
items.add(new SectionItem("BẠN BÈ"));
items.add(new EntryItem("Danh bạ", R.drawable.ic_slidemenu_contacts));
items.add(new EntryItem("Tin nhắn", R.drawable.ic_slidemenu_message));
items.add(new SectionItem("NO NAME"));
items.add(new EntryItem("Cổng tiện ích", R.drawable.ic_slidemenu_gate));
items.add(new EntryItem("Đăng xuất", R.drawable.ic_slidemenu_logout));
adapter = new EntryAdapter(getActivity(), items);
setListAdapter(adapter);
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//set background cho slidemenu
viewslide.setBackgroundResource(0);
v.setBackgroundColor(Color.parseColor("#191a1e"));
viewslide = v;
//thay đổi icon slidemenu
ImageView iv = (ImageView) v.findViewById(R.id.list_item_entry_drawable);
iv = imageslide;
switch (position) {
case 0:
((MainActivity) getActivity()).toggleSideMenu();
fm = getFragmentManager();
Fragment mfragment = fm.findFragmentByTag(FacebookLoginFragment.TAGfacebook);
if (mfragment == null) {
addFragment(new FacebookLoginFragment(), false,
FacebookLoginFragment.TAGfacebook);
}
break;
case 1:iv.setImageResource(R.drawable.ic_slidemenu_notice_active);
break;
case 2:
((MainActivity) getActivity()).toggleSideMenu();
((MainActivity) getActivity()).SetTitle(menuitemfix[position - 1]);
((MainActivity) getActivity()).setTouchmodeAboveFull();
fm = getFragmentManager();
Fragment fragmentepglist = fm.findFragmentByTag(EPGEventUserFragment.TAGEPGEvent);
if (fragmentepglist == null) {
addFragment(new EPGEventUserFragment(), false,
EPGEventUserFragment.TAGEPGEvent);
}
iv.setImageResource(R.drawable.ic_slidemenu_prompted_active);
break;
case 4:
((MainActivity) getActivity()).toggleSideMenu();
((MainActivity) getActivity()).SetTitle(menuitemfix[position - 1]);
((MainActivity) getActivity()).setTouchmodeAboveFull();
fm = getFragmentManager();
Fragment mChannelListFragment = fm.findFragmentByTag(ChannelListFragment.TAGchannel);
if (mChannelListFragment == null) {
addFragment(new ChannelListFragment(), false,
ChannelListFragment.TAGchannel);
}
iv.setImageResource(R.drawable.ic_slidemenu_live_active);
break;
case 5:
((MainActivity) getActivity()).toggleSideMenu();
((MainActivity) getActivity()).SetTitle(menuitemfix[position - 1]);
((MainActivity) getActivity()).setTouchModeAbove();
fm = getFragmentManager();
Fragment mEPGFragment = fm.findFragmentByTag(EPGFragment.TAGEPGfragment);
if (mEPGFragment == null) {
addFragment(new EPGFragment(), false,
EPGFragment.TAGEPGfragment);
}
iv.setImageResource(R.drawable.ic_slidemenu_epg_active);
break;
case 7:
((MainActivity) getActivity()).toggleSideMenu();
((MainActivity) getActivity()).SetTitle(menuitemfix[position - 1]);
((MainActivity) getActivity()).setTouchmodeAboveFull();
fm = getFragmentManager();
Fragment fragmentmovie = fm.findFragmentByTag(MovieNewFragment.TAGMovieNewFrag);
if (fragmentmovie == null) {
addFragment(new MovieNewFragment(), false,
MovieNewFragment.TAGMovieNewFrag);
}
iv.setImageResource(R.drawable.ic_slidemenu_movie_active);
break;
case 8:
iv.setImageResource(R.drawable.ic_slidemenu_video_active);
break;
case 9:
iv.setImageResource(R.drawable.ic_slidemenu_tvshow_active);
break;
case 10:
((MainActivity) getActivity()).toggleSideMenu();
((MainActivity) getActivity()).SetTitle(menuitemfix[position - 1]);
((MainActivity) getActivity()).setTouchmodeAboveFull();
fm = getFragmentManager();
Fragment fragmentcatchup = fm.findFragmentByTag(CatchupNewFragment.TAGCatchupNewFrag);
if (fragmentcatchup == null) {
addFragment(new CatchupNewFragment(), false,
CatchupNewFragment.TAGCatchupNewFrag);
}
iv.setImageResource(R.drawable.ic_slidemenu_catchup_active);
break;
case 12:
iv.setImageResource(R.drawable.ic_slidemenu_contacts_active);
break;
case 13:
iv.setImageResource(R.drawable.ic_slidemenu_message_active);
break;
case 15:
iv.setImageResource(R.drawable.ic_slidemenu_gate_active);
break;
case 16:
new AlertDialog.Builder(getActivity())
.setMessage("Bạn muốn thoát khỏi chương trình?")
.setCancelable(false)
.setPositiveButton("Đồng ý",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
((MainActivity) getActivity()).logout();
}
}).setNegativeButton("Từ chối", null).show();
break;
default:
Toast.makeText(getActivity(), "Chuc nang nay dang duoc phat trien",
Toast.LENGTH_SHORT).show();
break;
}
**temp_position = position;**
}
void addFragment(Fragment fragment,boolean addToBackStack,String tag)
{
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment, tag);
if(addToBackStack) ft.addToBackStack(null);
ft.commit();
}
我在switch语句之前在onlistitemclick中编辑我的代码:
if(temp_position != 0){
// Get icon from array in array.xml which contains name of image in drawable
int id_icon = getResources().getIdentifier(menuItemsIcon[temp_position],
"drawable", getActivity().getPackageName());
iv.setImageResource(id_icon);
}
但它不起作用
答案 0 :(得分:0)
这是因为,您不会将之前的项目位置保存在任何位置。您应该首先将单击的项目位置存储在变量中,然后当您单击其他项目时,获取变量并将其设置为默认状态。
//your temp variable for the previous position
int tmpImagePosition;
然后在你的switch语句
的每一种情况下... access to your image with the position of the tmpImagePosition and set it to it's default
tmpImagePosition = postion; //position you get from your switch