Java ViewPager 没有显示任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22345409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ViewPager not showing anything
提问by Kamal Upasena
I am create view pager with custom view pager adapter but when i run the project i can not see anything
我正在使用自定义视图寻呼机适配器创建视图寻呼机,但是当我运行该项目时,我什么也看不到
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;
}
}
but the toast is displaying but i can not see any text on my application
但吐司正在显示,但我在我的应用程序上看不到任何文字
采纳答案by vipul mittal
write
写
container.addView(viewLayout);
before returning the view in instantiateItem
在返回视图之前 instantiateItem
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());
container.addView(viewLayout);
return viewLayout;
}
回答by Jonathon Fry
You aren't adding your view to the container.
您没有将视图添加到容器中。
container.addView(viewLayout, 0);
回答by Piyush
You have missed to add your view to the container. Need to add before return your view.
您没有将视图添加到容器中。在返回您的视图之前需要添加。
((ViewPager) container).addView(viewLayout);
also sure that you have set current item for View Pager.
还要确保您已经为 View Pager 设置了当前项目。
回答by Vu Duy Hao
<TabHost
......
<TabWidget
.....
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp" >
</FrameLayout>
<android.support.v4.view.ViewPager