2013-10-31 112 views
0

我在listview的onClick中动态创建标签。每当我点击相同的列表项重复的选项卡打开了相同的列表item.How我能阻止列表项当我们在运行时添加标签时如何删除重复标签

这里的的onClick重复攀开口是我的代码

protected void onListItemClick(ListView l, View v, final int position, long id) 
{ 

    super.onListItemClick(l, v, position, id); 
    TabHost tabHost = Tabviewactivity.self.getTabHost(); 
    FriendInfo friend = friendAdapter.getItem(position); 
    Intent i = new Intent(); 
    i.setClass(this, Messaging.class); 
    i.putExtra(FriendInfo.USERNAME, friend.userName); 

    String friend_name = friend.userName; 
    tabHost.addTab(tabHost.newTabSpec(friend_name + Integer.toString(z)). 
          setIndicator(friend_name).setContent(i)); 
    tabHost.setCurrentTab(z); 
    z++; 

} 

感谢

TabHost tabHost = AllFriendList.self.getTabHost(); 
    int position = tabHost.getCurrentTab(); 

Log.d("Position",Integer.toString(position)); 
Log.d("Z val in delete()",Integer.toString(z)); 
if(position >0) 
{ 
tabHost.getCurrentTabView().setVisibility(View.GONE); 
tabHost.setCurrentTab(position+1); 
z-=1; 
if(z<0) 
z=0; 
} 
else if(position == 0) 
{ 
tabHost.getCurrentTabView().setVisibility(View.GONE); 
tabHost.setCurrentTab(position+1); 
z=0; 
} 
else if(position == z) 
{ 
tabHost.getCurrentTabView().setVisibility(View.GONE); 
tabHost.setCurrentTab(z-1); 
Log.d("Z value in final","lol"); 
Log.d("Pos",Integer.toString(position)); 
Log.d("z pos",Integer.toString(z)); 
} 
TabActivity parent = (TabActivity) getParent(); 
TabHost tabhost = parent.getTabHost(); 
tabhost.setCurrentTab(z+1); 
for(int i=0;i<tabList1.size();i++) 
{ 
if(tabList1.contains(frnd_position1)) 
{ 
tabList1.remove(i); 
} 
} 

回答

0

商店在ArrayList点击的位置创建选项卡后在点击检查,如果所选择的位置是在ArrayList中或not..if目前已经存在不添加其他选项卡添加新的标签..

ArrayList<Integer> tabList = new ArrayList<Integer>(); 
protected void onListItemClick(ListView l, View v, final int position, long id) { 

super.onListItemClick(l, v, position, id); 

if(!tabList.contains(position)) { 
TabHost tabHost = Tabviewactivity.self.getTabHost(); 

FriendInfo friend = friendAdapter.getItem(position); 
Intent i = new Intent(); 
i.setClass(this, Messaging.class); 
i.putExtra(FriendInfo.USERNAME, friend.userName); 

String friend_name = friend.userName; 
tabHost.addTab(tabHost.newTabSpec(friend_name + Integer.toString(z)).setIndicator(friend_name).setContent(i)); 
tabHost.setCurrentTab(z); 
z++; 

tabList.add(position); 
} 
} 
+0

非常感谢@ bakriOnFire。这很好用 – user2873624

+0

多一个帮助@bakriOnFire可以设置当我第二次单击列表项目时已经打开的标签 – user2873624

+0

您可以在其他条件下使用tabHost.setCurrentTabByTag(标签)使用您设置的标签同时创建newTabSpec .. – bakriOnFire