gpt4 book ai didi

android - 选项卡主机列表适配器和列表

转载 作者:行者123 更新时间:2023-11-30 04:40:27 25 4
gpt4 key购买 nike

嗨我的 tabhost 有问题,我真的不明白为什么。我的选项卡主机包含 4 个 ListView 并在每个选项卡中显示一个列表。有时,当我更改选项卡(单击新选项卡)时,内容不会更改或卡住。已经在多台设备上试过了,问题依旧

这是我的代码:

public class TabHostActivity extends TabActivity implements OnTabChangeListener {

private TabHost mTabHost;
private Resources mResources;
private SharedPreferences prefs;
private ListView lv1;
private ListView lv2;
private ListView lv3;
private ListView lv4;
private String servicio = "teatre"; // por ahora luego varia segun los
// eventos
private String servicio2 = "expos";
private static final String Tab_title1 = "Agenda";
private static final String Tab_title3 = "Teatre";
private static final String Tab_title4 = "Exposicions";
private static final String Tab_title2 = "Cinema";
private EventsList eventsL;
private ArrayList<Event> events;
private EventAdapterAprop mAdapter;
private EventAdapterAprop mAdapter2;
private EventAdapterAprop mAdapter3;
private EventAdapterAprop mAdapter4;
private EventsList eventsL3;
private EventsList eventsL4;
private ArrayList<Event> events3;
private ArrayList<Event> events4;
private EventsList eventsL2;
private ArrayList<Event> events2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);

mTabHost = getTabHost();
lv1 = (ListView) findViewById(R.id.list1);

lv2 = (ListView) findViewById(R.id.list2);
lv3 = (ListView) findViewById(R.id.list3);
lv4 = (ListView) findViewById(R.id.list4);

Bundle b = getIntent().getExtras();

//agenda
try{
eventsL = b.getParcelable("events");
events = new ArrayList<Event>();

for (int i = 0; i < eventsL.size(); i++) {
events.add(eventsL.get(i));
}


mAdapter = new EventAdapterAprop(this, events);
} catch (Exception ex) {
ex.printStackTrace();
mAdapter = new EventAdapterAprop(this, crearVacio());
}
// teatro

try{
eventsL3 = b.getParcelable("events3");
events3 = new ArrayList<Event>();

for (int i = 0; i < eventsL3.size(); i++) {
events3.add(eventsL3.get(i));
}


mAdapter3 = new EventAdapterAprop(this, events3);
} catch(Exception ex) {
mAdapter3 = new EventAdapterAprop(this, crearVacio());
ex.printStackTrace();
}


// expos
try{
eventsL4 = b.getParcelable("events4");
events4 = new ArrayList<Event>();

for (int i = 0; i < eventsL4.size(); i++) {
events4.add(eventsL4.get(i));
}

mAdapter4 = new EventAdapterAprop(this, events4);
} catch(Exception ex){
mAdapter4 = new EventAdapterAprop(this, crearVacio());
ex.printStackTrace();
}

// cine
try{
eventsL2 = b.getParcelable("events2");
events2 = new ArrayList<Event>();

for (int i = 0; i < eventsL2.size(); i++) {
events2.add(eventsL2.get(i));
}


mAdapter2 = new EventAdapterAprop(this, events2);
} catch(Exception ex) {
mAdapter2 = new EventAdapterAprop(this, crearVacio());
ex.printStackTrace();
}

//mAdapter2=new EventAdapterAprop(this, crearVacio());

lv1.setAdapter(mAdapter);
lv1.setTextFilterEnabled(true);
lv1.computeScroll();

lv2.setAdapter(mAdapter2);
lv2.setTextFilterEnabled(true);
lv2.computeScroll();

lv3.setAdapter(mAdapter3);
lv3.setTextFilterEnabled(true);
lv3.computeScroll();

lv4.setAdapter(mAdapter4);
lv4.setTextFilterEnabled(true);
lv4.computeScroll();

mTabHost.addTab(mTabHost.newTabSpec(Tab_title1)
.setIndicator(Tab_title1).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return lv1;
}
}));
mTabHost.addTab(mTabHost.newTabSpec(Tab_title2)
.setIndicator(Tab_title2).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return lv2;
}
}));

mTabHost.addTab(mTabHost.newTabSpec(Tab_title3)
.setIndicator(Tab_title3).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return lv3;
}
}));
mTabHost.addTab(mTabHost.newTabSpec(Tab_title4)
.setIndicator(Tab_title4).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return lv4;
}
}));
}

public ArrayList<Event> crearVacio() {
ArrayList<Event> arrayList = new ArrayList<Event>();
Event e = new Event();
e.setTitle("Cap esdeveniment correspon a la seva recerca");
e.setDates("");
e.setTimes("");
e.setVenue("");
arrayList.add(e);
return arrayList;
}

@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub

}

}

和 xml:

<?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"
xmlns:android1="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/fonsbutxacadroid2">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android1:background="@color/amarillo1"/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="@+id/list1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
>
</ListView>
<ListView android:id="@+id/list2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
>
</ListView>
<ListView android:id="@+id/list3" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
>
</ListView>
<ListView android:id="@+id/list4" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
>
</ListView>
</FrameLayout>
</LinearLayout>
</TabHost>

怎么了?

提前致谢。

最佳答案

让每个选项卡成为一个单独的 Activity 解决了这个问题

布局示例:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
xmlns:android1="http://schemas.android.com/apk/res/android"
android1:id="@+id/@android:id/tabhost" android1:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</FrameLayout>
</LinearLayout>

tabhost类:

public class AllActivitiesTabs extends TabActivity {

private static final String Tab_title = "Nueva";
private TabHost mTabHost;
private TextView txtTabInfo;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


mTabHost = getTabHost();

TabSpec spec;
//to add one tab activity
txtTabInfo = new TextView(getApplicationContext());
txtTabInfo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
txtTabInfo.setBackgroundDrawable(getResources().getDrawable(
R.drawable.tab_incidencia));
Intent intent = new Intent();
intent.setClass(this, ListActivityClass.class);
spec = mTabHost.newTabSpec(Tab_title).setIndicator(txtTabInfo)
.setContent(intent);
mTabHost.addTab(spec);

//do the same for other activities
//your activities can be lists or something else:


txtTabInfo2 = new TextView(getApplicationContext());
txtTabInfo2.setBackgroundDrawable(getResources().getDrawable(
R.drawable.tab_solucions));
txtTabInfo2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
intent = new Intent();
intent.setClass(this, FirstGroup.class);

spec = mTabHost.newTabSpec(Tab_title2).setIndicator(txtTabInfo2)
.setContent(intent);
mTabHost.addTab(spec);
}
}

关于android - 选项卡主机列表适配器和列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6058708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com