大家好,我在使用不同的值调用标签栏事件时出现问题。因为标签栏视图在单个调用时获取负载。不能为类传递不同的值。例如,我必须加载fri17,sat18,sun19标签在标签栏上。它的内容为第17天的值,第18天的值,第19天的值。在将日期17传递给列表类之后必须在列表视图中加载。我已经尝试更改listener的标签。无法找到解决方案这个问题。任何人都可以帮助我
package com.androidexample.tabbar;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class TabBar extends TabActivity implements OnTabChangeListener{
public static String name;
static final String URL = "http://182.160.161.2/~mani/homeshow/web_serv/timeTableGetByRequest.php?event_id=14&sortBy=time";
static final String KEY_SONG = "date"; // parent node
static final String KEY_IDs = "timetable_date";
public HashMap<String, String> map;
public ArrayList<HashMap<String, String>> songsList;
String kid,ktit,kart,kdur,kurl,EID;
TabHost tabHost;
TabHost.TabSpec spec;
Intent intent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.t2);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
songsList = new ArrayList<HashMap<String, String>>();
final NewProdCompleteXMLParser parser = new NewProdCompleteXMLParser();
String xml = parser.getXmlFromUrl(URL);
final Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_SONG);
for (int i = 0; i < nl.getLength(); i++) {
map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_IDs, parser.getValue(e, KEY_IDs));
songsList.add(map);
kid=parser.getValue(e, KEY_IDs);
System.out.println("kid"+kid);
intent = new Intent().setClass(this, Tab1.class);
spec= tabHost.newTabSpec("hhj").setIndicator(kid)
.setContent(intent);
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.WHITE);
}
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_hover);
}
public void onTabChanged(String tabId) {
name=songsList.get(tabHost.getCurrentTab()).get("timetable_date");
System.out.println("on tab changed"+name);
/************ Called when tab changed *************/
//********* Check current selected tab and change according images *******/
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
}
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab_hover);
}
Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
while(tabHost.getCurrentTab()==1)
{
Intent intent1 = new Intent().setClass(this, Tab1.class);
spec.setContent(intent1);
//Add intent to tab
System.out.println("on tab changed"+name);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab_bg);
}
}
}
必须发送System.out.println("on tab changed"+name);
。这个名称val(ie.fri17..sat 18.on每个标签点击下一个Tab1类)
public class Tab1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
TabBar tb=new TabBar();
String name = tb.name;
System.out.println("on tab changed TT"+name);
}
}
答案 0 :(得分:1)
你有两个更好的Android特定选项
1&gt;使用JakeWharton的Android-ViewPagerIndicator
2&gt;非常有名的astuetz PagerSlidingTabStrip
使用此库和示例执行一些黑客操作,以在每个父标签中加载那些amples活动或片段。
除此之外,您可以two way communication between a TabActivity(即承载TabHost的活动)及其各自的子标签(每个标签内的活动)。
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:tag="tab0"
android:text="Tab 1"
android:background="@android:drawable/btn_star_big_on"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<TextView
android:tag="tab1"
android:text="Tab 2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<TextView
android:tag="tab2"
android:text="Tab 3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Hallo1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:text="Hallo2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:text="Hallo3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
活动:
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
public class SensorActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabsample);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
final TabWidget tabWidget = tabHost.getTabWidget();
final FrameLayout tabContent = tabHost.getTabContentView();
// Get the original tab textviews and remove them from the viewgroup.
TextView[] originalTextViews = new TextView[tabWidget.getTabCount()];
for (int index = 0; index < tabWidget.getTabCount(); index++) {
originalTextViews[index] = (TextView) tabWidget.getChildTabViewAt(index);
}
tabWidget.removeAllViews();
// Ensure that all tab content childs are not visible at startup.
for (int index = 0; index < tabContent.getChildCount(); index++) {
tabContent.getChildAt(index).setVisibility(View.GONE);
}
// Create the tabspec based on the textview childs in the xml file.
// Or create simple tabspec instances in any other way...
for (int index = 0; index < originalTextViews.length; index++) {
final TextView tabWidgetTextView = originalTextViews[index];
final View tabContentView = tabContent.getChildAt(index);
TabSpec tabSpec = tabHost.newTabSpec((String) tabWidgetTextView.getTag());
tabSpec.setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
return tabContentView;
}
});
if (tabWidgetTextView.getBackground() == null) {
tabSpec.setIndicator(tabWidgetTextView.getText());
} else {
tabSpec.setIndicator(tabWidgetTextView.getText(), tabWidgetTextView.getBackground());
}
tabHost.addTab(tabSpec);
}
// tabHost.setCurrentTab(0);
}
}
来源:gist
答案 1 :(得分:0)
您必须为每个标签内容编写不同的类
然后使用此
TabSpec tab0 = tabHost.newTabSpec("tab0");
tab0.setIndicator("Tab0");
Intent tab0Intent = new Intent(this, Tab0.class);
tab0.setContent(tab0Intent);
TabSpec tab1 = tabHost.newTabSpec("tab1");
tab1.setIndicator("Tab1");
Intent tab1Intent = new Intent(this, Tab1.class);
tab1.setContent(tab1Intent);
TabSpec tab2 = tabHost.newTabSpec("tab2");
tab2.setIndicator("Tab2");
Intent tab2Intent = new Intent(this, Tab2.class);
tab2.setContent(tab2Intent);
TabSpec tab3 = tabHost.newTabSpec("tab3");
tab3.setIndicator("Tab3");
Intent tab3Intent = new Intent(this, Tab3.class);
tab3.setContent(tab3Intent);
TabSpec tab4 = tabHost.newTabSpec("tab4");
tab4.setIndicator("Tab4");
Intent tab4Intent = new Intent(this, Tab4.class);
tab4.setContent(tab4Intent);
tabHost.addTab(tab0);
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
tabHost.addTab(tab4);
另见
http://www.mkyong.com/android/android-tablayout-example/
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/