我们在运行时添加选项卡时如何删除重复选项卡

时间:2013-10-31 08:29:11

标签: android android-tabhost android-tabs

我在listview的onClick中动态创建了标签。每当我单击相同的列表项时,重复的选项卡将打开相同的列表项。如何防止在列表项的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);
}
}

1 个答案:

答案 0 :(得分:0)

在创建选项卡后将单击的位置存储在ArrayList中,并在单击检查中是否所选位置已存在于ArrayList中。如果存在,则不添加选项卡,否则添加新选项卡。

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