我有一个活动,其中有 2 个 sherlockfragment 前两个页面显示带有自定义列表视图的片段,这些视图是使用 AsyncTask 从服务器的 xml 构建的。但是,当应用程序运行时,只显示一个列表视图,其他页面只是空白
public class VpiAbsTestActivity extends SherlockFragmentActivity {
private static final String[] CONTENT = new String[] { "1","2"};
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
mIndicator.notifyDataSetChanged();
}
class TestFragmentAdapter extends FragmentPagerAdapter {
private int mCount = CONTENT.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new customlist();
case 1:
return new customlistnotuser();
default:
return null;
}
}
@Override
public int getCount() {
return mCount;
}
public CharSequence getPageTitle(int position) {
return VpiAbsTestActivity.CONTENT[position % VpiAbsTestActivity.CONTENT.length].toUpperCase();
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
}
}
更改页面时我可以更新 viewpager 什么?
customlistnotuser 页面喜欢 customlist 页面但不显示
public class customlistnotuser extends SherlockFragment {
// All static variables
static final String URL = "url";
// XML node keys
static final String KEY_TEST = "test"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_Description = "description";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_PRICE = "price";
static final String KEY_URL = "url";
private ProgressDialog pDialog;
ListView list;
LazyAdapterbeth adapter;
XMLParser parser = new XMLParser();
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new getFeed().execute();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View thisfragment = inflater.inflate(R.layout.dovomi, container, false);
return thisfragment;
}
private class getFeed extends AsyncTask<Void, Void, Document> {
}
protected Document doInBackground(Void... params) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
return doc;
}
protected void onPostExecute(Document doc) {
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
NodeList nl = doc.getElementsByTagName(KEY_TEST);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_Description, parser.getValue(e, KEY_Description));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
map.put(KEY_PRICE, parser.getValue(e, KEY_PRICE));
map.put(KEY_URL, parser.getValue(e, KEY_URL));
// adding HashList to ArrayList
songsList.add(map);
pDialog.dismiss();
}
list=(ListView)getActivity().findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapterbeth(getActivity(), songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
customlistnotuser 页面喜欢 customlist 页面但不显示 .. :(