我有一個TabActivity有兩個選項卡。我在每個選項卡中加載單獨的活動(活動A & B)。在標籤1中加載的活動A具有某些控件和列表視圖。我使用AsyncTask從該服務加載數據,該數據爲ListView。當用戶點擊ListView項目時,我不得不加載另一個活動,但是應該在同一個選定的Tab中加載新的活動C。如何在android-support-v4.jar中使用asynctask片段?
我在互聯網上搜索,發現我可以使用ActivityGroup ...但同時,我發現它的棄用。因此,使用Android的支持-V4兼容性Frangment提出..
我下載了Android的支持-v4.jar,看着FragmentTabs樣品,我知道了如何使用碎片即使在Android 2.1的....所以我換成兩個活動,這點我是顯示的標籤,有兩個片段
但現在我不知道如何處理後續情況:
ListView &當我運行下面的代碼時,其他控件不顯示。
我要調用的AsyncTask ...這將需要時間來從服務器獲取&解析XML數據和片段的onCreateView早叫...所以我怎樣才能後設置的ListView在其片段onCreateView被調用?
ListView項被點擊後,我將不得不加載另一個片段相同標籤內ç ...我怎樣才能做到這一點?
代碼爲我的片段如下:
public class UpdatesFragment extends Fragment implements IFeedReceiver {
ArrayList<Feed> _feeds;
Integer _currentPage = 1;
ListView _lvUpdates;
UserMessage _userMessage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFeeds();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.updates, container, false);
this._lvUpdates = (ListView)v.findViewById(R.id.listUpdates);
return v;
}
private void getFeeds() {
FeedsCollectorAsyncTask task = new FeedsCollectorAsyncTask(
getActivity(), this._currentPage);
task.execute();
}
//This method is called from AsyncTask upon receiving & parsing data. So i m trying to populate my ListView here
@Override
public void onFeedReceived(ArrayList<Feed> feeds) {
FeedsAdapter adapter = new FeedsAdapter(getActivity(),
R.layout.feed_list_item, this._feeds);
this._lvUpdates.setAdapter(adapter);
// Load detail on item click
this._lvUpdates.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int index,
long id) {
}
});
}
}
主要選項卡活動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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-6dp"/>
</LinearLayout>
最後...
public class PlayMakerActivity extends FragmentActivity {
/** Called when the activity is first created. */
TabHost tabHost;
TabManager mTabManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
// TabHost tabHost = getTabHost(); // The activity TabHost
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
mTabManager = new TabManager(this, tabHost, R.id.realtabcontent);
mTabManager.addTab(tabHost.newTabSpec("Upudates").setIndicator("Updates"),
UpdatesFragment.class, null);
}
}
//also included the following AS It IS
public static class TabManager implements TabHost.OnTabChangeListener {
....
....
....
}
你可以點我的一些例子。 我們非常感謝您的幫助!
非常感謝您的詳細解答。我有其他問題,但他們的解決方案之一包括你的建議。所以我會接受它作爲答案..再次感謝 – Aamir 2012-03-31 08:08:28
沒問題,很高興我能以某種方式幫助小夥子。 – RonMon 2012-03-31 15:41:10