我需要知道如何在后台解析来自在线 xml 的数据并将其显示在列表视图中。
目前我有一个扩展 FragmentActivity 的类。下面是班级布局
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
我已经在类中声明了操作栏选项卡和查看寻呼机,如下所示
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayUseLogoEnabled(true);
viewPager=(ViewPager)findViewById(R.id.pager);
viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
viewPager.setOffscreenPageLimit(6);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
getActionBar().setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
ActionBar.Tab TabOne = actionbar.newTab().setText("TabOne ");
ActionBar.Tab TabTwo = actionbar.newTab().setText("TabTwo ");
ActionBar.Tab TabThree = actionbar.newTab().setText("TabThree ");
ActionBar.Tab TabFour = actionbar.newTab().setText("TabFour ");
ActionBar.Tab TabFive = actionbar.newTab().setText("TabFive ");
ActionBar.Tab TabSix = actionbar.newTab().setText("TabSix ");
TabOneActivity one = new TabOneActivity ();
TabTwoActivity two= new TabTwoActivity ();
TabThreeActivity three= new TabThreeActivity ();
TabFourActivity four= new TabFourActivity ();
TabOFiveActivity five= new TabOFiveActivity ();
TabSixActivity six=new TabSixActivity ();
TabOne .setTabListener(new MyTabsListener(one));
TabTwo .setTabListener(new MyTabsListener(two));
TabThree .setTabListener(new MyTabsListener(three));
TabFour .setTabListener(new MyTabsListener(four));
TabFive .setTabListener(new MyTabsListener(five));
TabSix .setTabListener(new MyTabsListener(six));
actionbar.addTab(TabOne);
actionbar.addTab(TabTwo);
actionbar.addTab(TabThree);
actionbar.addTab(TabFour);
actionbar.addTab(TabFive);
actionbar.addTab(TabSix);
有两个内部类 MyTabListener 和 ViewPageAdapter 。下面是 MyTabListener 类
public class MyTabsListener implements ActionBar.TabListener {
protected TabOneActivity oneContext;
protected TabTwoActivity twoContext;
protected TabThreeActivity threeContext;
protected TabFourActivity fourContext;
protected TabFiveActivity fiveContext;
protected TabSixActivity sixcontext;
public MyTabsListener(TabOneActivity onetab) {
// TODO Auto-generated constructor stub
this.oneContext=onetab;
}
public MyTabsListener(TabTwoActivity twoTab) {
// TODO Auto-generated constructor stub
this.twoContext=twoTab;
}
public MyTabsListener(TabThreeActivity threeTab) {
// TODO Auto-generated constructor stub
this.threeContext=threeTab;
}
public MyTabsListener(TabFourActivity fourTab) {
this.fourContext= fourTab;
}
public MyTabsListener(TabFiveActivity fiveTab) {
// TODO Auto-generated constructor stub
this.fiveContext=fiveTab;
}
public MyTabsListener(TabSixActivity sixTab) {
// TODO Auto-generated constructor stub
this.sixContext=sixTab;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
下面是 ViewPagerAdapter 类
public class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 6;
public ViewPagerAdapter(FragmentManager supportFragmentManager) {
// TODO Auto-generated constructor stub
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
switch(position){
case 0:
return TabOneActivity.create(position);
case 1:
return TabTwoActivity.create(position);
case 2:
return TabThreeActivity.create(position);
case 3:
return TabFourActivity.create(position);
case 4:
return TabFiveActivity.create(position);
case 5:
return TabSixActivity.create(position);
default :
return TabOneActivity.create(position);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
每个 Activity 都在扩展片段并使用 AsyncTask 访问和解析在线 xml,并在列表视图中显示结果。
在数据被填充到列表视图之前,我正在显示一个进度条。
我需要做的是在启动画面上显示一个进度条,在后台应该点击并解析在线数据,然后执行一次在列表视图中显示数据。
这将使 UI 看起来不错,因为用户不需要等待数据在主屏幕中加载,而是在 spalsh 屏幕中下载。
请建议如何实现这一目标。因为我使用的是一个 FragmentActivity,它有六个不同的片段访问六个不同的 URL