我有一个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