我有一個列表視圖,其中包含16個項目。當我點擊項目1時,我改變了項目1的圖像。但是當我點擊項目2時,項目1的圖像不會改變。當項目2被點擊時,我想讓項目1的圖像回到默認狀態。並休息same.I搜索了很多在這個網上,但我沒有找到解決辦法。任何人都幫助我?在列表視圖上單擊此項時更改圖片
這裏我的代碼:
@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);
}
但它不工作
保持上次改變圖像和視圖(它們保存在一個臨時變量)的軌道,當你點擊其他下一個項目復位,第二項也是這樣做 – Pragnani
是的。我知道算法,但我無法準確編寫代碼。你可以編寫代碼來幫助我! – Tungken