2014-03-12 107 views
15

我創建一個自定義視圖尋呼機適配器視圖尋呼機,但是當我運行該項目中,我看不到任何東西ViewPager不顯示任何

public class CustomAdapter extends PagerAdapter { 

     private List<NewsObject> newsObjects; 
     private Activity mActivity; 
     private LayoutInflater inflater; 
     private ImageLoaderConfiguration imageConfig; 
     private ImageLoader imageLoader = ImageLoader.getInstance(); 

     public CustomAdapter(List<NewsObject> newsObjects, Activity mActivity) { 
      super(); 
      Log.d("Con :", "structor"); 
      this.newsObjects = newsObjects; 
      this.mActivity = mActivity; 
      imageConfig = new ImageLoaderConfiguration.Builder(
        mActivity.getApplicationContext()).build(); 
      imageLoader.init(imageConfig); 
     } 

     @Override 
     public int getCount() { 
      return this.newsObjects.size(); 
     } 

     @Override 
     public void destroyItem(View collection, int position, Object view) { 
      ((ViewPager) collection).removeView((View) view); 
     } 

     @Override 
     public boolean isViewFromObject(View view, Object object) { 
      return view == object; 
     } 

     @Override 
     public Object instantiateItem(ViewGroup container, int position) { 

      ImageViewPlus img_news_image; 
      TextView txt_news_title; 
      TextView txt_news_description; 

      inflater = (LayoutInflater) mActivity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View viewLayout = inflater.inflate(R.layout.detail_news_page, null); 

      img_news_image = (ImageViewPlus) viewLayout 
        .findViewById(R.id.img_detail_news_image); 
      txt_news_title = (TextView) viewLayout 
        .findViewById(R.id.txt_detail_news_title); 
      txt_news_description = (TextView) viewLayout 
        .findViewById(R.id.txt_detail_news_description); 

      Log.d("TAG :", "instantiateItem"); 

      imageLoader.displayImage(newsObjects.get(position).getNews_image(), 
        img_news_image); 

      Toast.makeText(mActivity.getApplicationContext(), 
        Constantz.newsObjects.get(position).getNews_id() + "", 
        Toast.LENGTH_SHORT).show(); 

      txt_news_title.setText(newsObjects.get(position).getNews_title()); 
      txt_news_description.setText(newsObjects.get(position) 
        .getNews_description()); 

      return viewLayout; 
     } 

    } 

但土司被顯示,但我不能看到我的應用程序的任何文本

回答

50

container.addView(viewLayout); 

instantiateItem

返回視圖之前
+0

非常感謝你的工作現在:) –

+1

不期待這一點。這個API的設計者至多有一半左腦。 – NeoWang

3

您並未將視圖添加到容器。

container.addView(viewLayout, 0); 
+0

非常感謝你 –

+0

在我的情況下,第二個參數'0'有所作爲,例如它的工作。 – Leukipp

8

您錯過了將視圖添加到容器。在返回您的視圖之前需要添加。

((ViewPager) container).addView(viewLayout); 

也確定您已設置View Pager的當前項目。

+0

非常感謝你 –

0
<TabHost 
    ...... 
<TabWidget 
..... 
<FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="0dp" 
    android:layout_height="0dp" > 
</FrameLayout> 
<android.support.v4.view.ViewPager 
+10

有些解釋不會傷害。請參閱[我如何寫出一個好的答案?](http://stackoverflow.com/help/how-to-answer) – Jubobs