gpt4 book ai didi

android - 查看分页器仅显示 1 个 ListView

转载 作者:行者123 更新时间:2023-11-29 01:53:33 26 4
gpt4 key购买 nike

我正在构建一些自定义 ListView ,然后将它们添加到 View Pager。
但是当我快速屏幕时,View Pager 只显示 1 个 Custom ListView。

这是问题的形象:
enter image description here

SpeakingActivity.java

public class SpeakingActivity extends SherlockFragmentActivity {
static final int NUM_ITEMS = 4;
MyAdapter mAdapter;
ViewPager mPager;

@Override
protected void onCreate(Bundle savedInstanceState){
setTheme(R.style.Theme_Sherlock); // Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}

public class MyAdapter extends FragmentPagerAdapter {
SpeakingFragment myFrag[];
int numItem = 4;

public MyAdapter(FragmentManager fm) {
super(fm);
myFrag = new SpeakingFragment[numItem];
for(int i = 0; i < numItem; i++) {
myFrag[i] = new SpeakingFragment();
}
}

@Override
public int getCount(){
return numItem;
}

@Override
public Fragment getItem(int position){
return myFrag[position];
}
}

SpeakingFragment.java

public class SpeakingFragment extends SherlockFragment {
public final String KEY_SONG = "song";
public final String KEY_ID = "id";
public final static String KEY_TITLE = "title";
public final static String KEY_ARTIST = "artist";
public final static String KEY_DURATION = "duration";
public final static String KEY_THUMB_URL = "thumb_url";
ListView list;
LazyAdapter adapter;
ArrayList<HashMap<String, String>> songsList;

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

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.main, container, false);
return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
list = (ListView) getSherlockActivity().findViewById(R.id.list);
songsList = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 15; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ID, String.valueOf(i));
map.put(KEY_TITLE, "Title" + i);
map.put(KEY_ARTIST, "Dolphin");
map.put(KEY_DURATION, "11:11");
map.put(KEY_THUMB_URL, "Thumb");
songsList.add(map);
}
adapter = new LazyAdapter(getSherlockActivity(), songsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
}
});
}
}

LazyAdapter.java

public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater = null;
public ImageLoader imageLoader;

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}

public int getCount(){
return data.size();
}

public Object getItem(int position){
return position;
}

public long getItemId(int position){
return position;
}

public View getView(int position, View convertView, ViewGroup parent){
View vi = convertView;
if(convertView == null) vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView) vi.findViewById(R.id.title); // title
TextView artist = (TextView) vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView) vi.findViewById(R.id.duration); // duration
ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
title.setText(song.get(SpeakingFragment.KEY_TITLE));
artist.setText(song.get(SpeakingFragment.KEY_ARTIST));
duration.setText(song.get(SpeakingFragment.KEY_DURATION));
imageLoader.DisplayImage(song.get(SpeakingFragment.KEY_THUMB_URL), thumb_image);
return vi;
}
}

您可以尝试完整代码 here

最佳答案

我自己发现了错误。
快速 Action 发生在 onActivityCreated 之前。它使下一个 fragment 不显示但是当快速返回时,onActivityCreated 发生所以它出现。
要修复此错误,请将代码从 onActivityCreated 更改为 onCreateView(因为 onCreateView 在 swift 之前发生)。

关于android - 查看分页器仅显示 1 个 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16610407/

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