2

我建立一些自定義列表視圖,然後將它們添加到查看傳呼機。
但是當我swift屏幕時,View Pager只顯示1個Custom ListView。查看傳呼機只顯示1列表查看

這是問題的圖像:
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

回答

1

我發現了錯誤我的自我。
onActivityCreated之前的swift動作。它使得下一個片段不顯示,但是當快速返回時,onActivityCreated發生,因此它出現。
要解決此錯誤,請將onActivityCreated中的代碼更改爲onCreateView(因爲onCreateView在swift之前發生)。