2011-11-20 62 views
0
public Object instantiateItem(View collection, int position) { 
      View v = collection; 


      if (v == null) { 
       LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.gallery_layout, null); 

      } 


      final String url = listOfImages.get(position); 
       TextView tv = (TextView)v.findViewById(R.id.textView1); 
       tv.setText(url); 
       tv.setTextColor(Color.WHITE); 
       tv.setTextSize(30); 



       ((ViewPager) v).addView(tv,0); 

       return v; 
     } 

上面可能是ViewPager的一个非常糟糕的实现。我试图只为每个页面使用我的XML布局。我基于我的代码使用我的图库小部件,但我不认为它会一样。将ViewPager适配器中的内容添加到xml布局中

回答

0

你几乎就在那里,但是你正在混合容器,instantiateItem的第一个参数会传递给你正在膨胀的内容。 ViewPager适配器与ListView适配器的工作方式不同,不通过convertView

只需将您的布局膨胀,将参数视图投射到ViewGroup(或ViewPager,两者都将与当前支持库一起使用)并将addView作为膨胀布局。

相关问题