gpt4 book ai didi

android - ViewPager 中的两个 ListView ,仅显示一个

转载 作者:太空狗 更新时间:2023-10-29 15:16:44 30 4
gpt4 key购买 nike

我目前有一个包含三个 fragment 作为页面的 ViewPager。前两个页面显示带有自定义 ListView 的 fragment ,这些 fragment 是使用 AsyncTask 从 RSS 提要构建的。但是,当应用程序运行时,只显示一个 ListView ,另一个页面只是空白。 (注意:我在这个项目中使用了 ViewPagerIndicator)。

主要 Activity :

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 fragment :

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) {


}
});
}
}}

第二个 fragment 与上面完全相同,只是 RSS 提要的 URL 不同。任何帮助表示赞赏。谢谢

最佳答案

请添加行 list.notifyDataSetChanged(); 在两个 fragment 中设置适配器后 CustomizedListView & CustomizedListViewn

关于android - ViewPager 中的两个 ListView ,仅显示一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12101898/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com