由于某种原因,setCurrentTab第一次没有切换标签。 这是我使用的代码。
private OnClickListener buttonListener = new OnClickListener(){
@Override
public void onClick(View v) {
tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));
}
};
它连接到按钮,每个按钮的标签等于要显示的标签的数量。 第一次单击该按钮并调用此方法时,将显示该选项卡。我还可以看到它执行创建选项卡内容的代码。
但是,一旦选项卡显示一次并且我已移至另一个选项卡,则单击返回该选项卡不起作用。 该方法肯定被调用,标签也是正确的。我输入命令打印到日志以确认。此外,它第一次工作,所以一定没事。
有什么想法吗?
完整代码:
public class TestTabs extends TabbedScreen implements TabHost.TabContentFactory{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initTabs();
}
@Override
protected void initTabs() {
addTab("0",this);
addTab("1",this);
addTab("2",this);
addTab("3",this);
}
/*
*
* @param extras The Bundle received in onCreate when this class is first created, and which contains the initial set of objects to be displayed.
* @return An array containing all of the objects in the list.
*/
protected Serializable[] getDataArray(Bundle extras) {
int size = extras.size();
Serializable[] data = new Serializable[size];
for (int i = 0; i < size; i++) {
data[i] = extras.getSerializable(Integer.toString(i));
}
return data;
}
@Override
public View createTabContent(String tag) {
// Get the data returned from the servelet and display it in the ListView
Log.d("TestTabs","createTabContent");
ListView lv = (ListView) findViewById(R.id.list_view);
List<String> list1Strings = new ArrayList<String>();
switch (Integer.parseInt(tag)){
case 0:
Log.d("TestTabs","Case 0");
lv.setAdapter(new MyAdapter(this,lv,null));
break;
case 1:
Log.d("TestTabs","Case 1");
list1Strings.add("Item 21");
list1Strings.add("Item 22");
list1Strings.add("Item 23");
list1Strings.add("Item 24");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
break;
case 2:
Log.d("TestTabs","Case 2");
list1Strings.add("Item 31");
list1Strings.add("Item 32");
list1Strings.add("Item 33");
list1Strings.add("Item 34");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
break;
case 3:
Log.d("TestTabs","Case 3");
list1Strings.add("Item 41");
list1Strings.add("Item 42");
list1Strings.add("Item 43");
list1Strings.add("Item 44");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
break;
}
return lv;
}
protected void addTab(String tabName, TabHost.TabContentFactory tabFactory){
TabSpec tabSpec = tabHost.newTabSpec(tabName);
tabSpec.setIndicator(tabName); // Don't set tab layout since we are going to make it invisible
tabSpec.setContent(tabFactory);
tabHost.addTab(tabSpec);
addButton(tabName);
}
protected void addButton(String tabName){
Button button = (Button) buttonHolder.getChildAt(nextChild);
button.setText(tabName);
button.setVisibility(View.VISIBLE);
button.setOnClickListener(buttonListener);
nextChild--;
}
private OnClickListener buttonListener = new OnClickListener(){
@Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.tab_selected);
tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));
Log.d("TabbedScreen","Set tab to " + v.getTag());
View view;
for (int i=0; i< childCount; i++)
if ((view = buttonHolder.getChildAt(i)) != v){
view.setBackgroundResource(R.drawable.button_tab);
view.invalidate();
}
}
};
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:roundedListView="http://schemas.android.com/apk/res/com.applicat.meuchedet"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/content_screen_user_details">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
<TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:visibility="gone"/>
<LinearLayout android:id="@+id/tabButtons" android:layout_width="wrap_content" android:layout_height="20dip">
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="@+id/button3"
android:background="@drawable/button_tab" android:visibility="invisible"
android:tag="3"/>
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="@+id/button2"
android:background="@drawable/button_tab" android:visibility="invisible"
android:tag="2"/>
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="@+id/button1"
android:background="@drawable/button_tab" android:visibility="invisible"
android:tag="1"/>
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="@+id/button0"
android:background="@drawable/tab_selected" android:visibility="invisible"
android:tag="0"/>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.applicat.meuchedet.views.RoundedListView
android:id="@+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:dividerHeight="1dip"
android:footerDividersEnabled="true"
android:listSelector="@drawable/listview_selected_item_background"
android:fadingEdge="none"
android:cacheColorHint = "#00000000"
roundedListView:radius="20"
roundedListView:border="2"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
编辑:
我想知道问题是否在行中:
ListView lv = (ListView) findViewById(R.id.list_view);
我每次都回到同一个对象吗? 如果是这样,我如何根据FrameLayout中定义的那个每次获得一个新的。
答案 0 :(得分:1)
要使用getTag
,您必须先设置它。所以我相信您忘记在addButton
方法中添加以下一行:
button.setTag(tabName);
答案 1 :(得分:1)
如何确定xml中分配给按钮的值与nextChild相同_。
不是那么好的解决方案,你可以测试一下。
void addButton(String tabName){
Button button = (Button) buttonHolder.getChildAt(nextChild);
button.getTag() != String.valueOf(nextChild); ERROR;
但最好将它设置为与您始终使用buttonHolder.getChildAt()
相同 Button button = (Button) buttonHolder.getChildAt(nextChild);
button.setTag(String.valueOf(nextChild));
<强>改性:强>
protected void addButton(String tabName){
Button button = (Button) buttonHolder.getChildAt(nextChild);
// ----------- modified -----------
button.setTag(String.valueOf(nextChild));
button.setText(tabName);
button.setVisibility(View.VISIBLE);
button.setOnClickListener(buttonListener);
nextChild--;
}
private OnClickListener buttonListener = new OnClickListener(){
@Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.tab_selected);
// ----------- modified -----------
Button btc = (Button)v;
int idx;
try {
idx = Integer.parseInt((String) btc.getTag());
} catch(NumberFormatException exx) {
System.out.println("Could not parse " + exx);
}
if ( idx < childCount) {
Log.d("TabbedScreen","Set tab to " + String.valueOf(idx);
Button butv;
for (int i=0; i< childCount; i++)
if ( i != idx){
butv = (Button) buttonHolder.getChildAt(i);
butv.setBackgroundResource(R.drawable.button_tab);
butv.invalidate();
}
tabHost.setCurrentTab(idx);
tabHost.focusCurrentTab(idx);
}
// ----------- modified -----------
}
};
}