【发布时间】:2015-02-26 07:48:04
【问题描述】:
我用了PagerSlidingTabStrip,不得不分片。
在每个片段中,我使用AsyncTask 从服务器获取数据。
下面是我的片段类,它从服务器获取 html 并显示列表视图。
public class MainFragmentSubList extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_list, container, false);
ListView mainList = (ListView) view.findViewById(R.id.fragment_list);
ArrayList<String> infoList;
// this is AsyncTask class and i made an interface.
new GetHtml(new OnRequestFinish(){
@Override
void onFinish(){
// Below are about ui
ArrayList<String> titleList = (ArrayList<String>) User.subName.clone();
int size = infoList.size();
MainListAdapter adapter = new MainListAdapter(titleList, infoList, size + 1);
mainList.setAdapter(adapter);
}
}).execute();
return view;
}
由于 AsyncTask 在后台做事,return view; 没有像我预期的那样工作。
这是我的主要活动,其中包含片段。
public class MainActivity extends ActionBarActivity {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setShouldExpand(true);
tabs.setViewPager(pager);
}
}
如何使用 AsyncTask 从片段返回视图?
【问题讨论】:
-
为什么要从 AsyncTask 中返回 `View`?
-
我想在片段类中返回
view。我刚刚使用了 AsyncTask,因为我必须从服务器获取文件。 -
有什么错误吗?或者你想要达到的结果是什么?
-
先生为什么要在片段类中返回视图?
-
PagerSlidingTabStrip 需要片段才能返回视图。我错了吗?
标签: android android-fragments android-asynctask