ViewPager中有两个列表视图,只显示一个

时间:2012-08-24 00:46:59

标签: android android-listview android-viewpager

我目前有一个ViewPager,其中有三个片段作为页面。前两页显示具有自定义列表视图的片段,这些视图是使用AsyncTask从RSS源构建的。但是,当应用程序运行时,只显示一个列表视图,另一个页面只是空白。 (注意:我在这个项目中使用ViewPagerIndicator。)

主要活动:

public class ViewPagerExample extends FragmentActivity {

private static final String[] TITLETAB = new String[] { "Latest", "New", "Misc" };
private MyAdapter mAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_tabs);

    mAdapter = new MyAdapter(getSupportFragmentManager());

    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
    mIndicator.setCurrentItem(1);


}
public static class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {

        return ViewPagerExample.TITLETAB.length;
    }
    @Override
    public Fragment getItem(int position) {


        switch(position) {

        case 0:
            return new CustomizedListView();


        case 1:
            return new CustomizedListViewn();


        default:
            return null;

        }


    }

    @Override
    public CharSequence getPageTitle(int position) {
        return ViewPagerExample.TITLETAB[position % ViewPagerExample.TITLETAB.length].toUpperCase();
    }


     @Override
        public void destroyItem(View collection, int position, Object view) {
             ((ViewPager) collection).removeView((View) view);
        }
}}

ListView片段:

public class CustomizedListView extends Fragment {
// All static variables
static final String URL = "http://url.net/booking/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";

ListView list;
LazyAdapter adapter;
XMLParser parser = new XMLParser();

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new getFeed().execute();


}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 

{
    View thisfragment1 = inflater.inflate(R.layout.main, container, false);

    return thisfragment1;
}


private class getFeed extends AsyncTask<Void, Void, Document> {
    @Override
    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;
    }

    @Override
    protected void onPostExecute(Document doc) {

        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // 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_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }

        list=(ListView)getActivity().findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(getActivity(), songsList);        
        list.setAdapter(adapter);


        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {


            }
        }); 
    }
}}

第二个片段与上面完全相同,只有RSS源的URL不同。任何帮助赞赏。感谢

1 个答案:

答案 0 :(得分:0)

请在list.notifyDataSetChanged();

片段中设置适配器后添加行CustomizedListView & CustomizedListViewn