2

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

这是问题的图像:
在此处输入图像描述

SpeechActivity.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;
    }
}

你可以在这里尝试完整的代码

4

1 回答 1

1

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

于 2013-05-17T18:01:30.093 回答