• <tfoot id='Y5YBp'></tfoot>

    <small id='Y5YBp'></small><noframes id='Y5YBp'>

    <legend id='Y5YBp'><style id='Y5YBp'><dir id='Y5YBp'><q id='Y5YBp'></q></dir></style></legend>
  • <i id='Y5YBp'><tr id='Y5YBp'><dt id='Y5YBp'><q id='Y5YBp'><span id='Y5YBp'><b id='Y5YBp'><form id='Y5YBp'><ins id='Y5YBp'></ins><ul id='Y5YBp'></ul><sub id='Y5YBp'></sub></form><legend id='Y5YBp'></legend><bdo id='Y5YBp'><pre id='Y5YBp'><center id='Y5YBp'></center></pre></bdo></b><th id='Y5YBp'></th></span></q></dt></tr></i><div id='Y5YBp'><tfoot id='Y5YBp'></tfoot><dl id='Y5YBp'><fieldset id='Y5YBp'></fieldset></dl></div>
      <bdo id='Y5YBp'></bdo><ul id='Y5YBp'></ul>

      1. PagerAdapter 总是返回空白

        时间:2023-10-04
        • <legend id='35F9u'><style id='35F9u'><dir id='35F9u'><q id='35F9u'></q></dir></style></legend>

            • <bdo id='35F9u'></bdo><ul id='35F9u'></ul>

              <tfoot id='35F9u'></tfoot>
              • <small id='35F9u'></small><noframes id='35F9u'>

                1. <i id='35F9u'><tr id='35F9u'><dt id='35F9u'><q id='35F9u'><span id='35F9u'><b id='35F9u'><form id='35F9u'><ins id='35F9u'></ins><ul id='35F9u'></ul><sub id='35F9u'></sub></form><legend id='35F9u'></legend><bdo id='35F9u'><pre id='35F9u'><center id='35F9u'></center></pre></bdo></b><th id='35F9u'></th></span></q></dt></tr></i><div id='35F9u'><tfoot id='35F9u'></tfoot><dl id='35F9u'><fieldset id='35F9u'></fieldset></dl></div>
                    <tbody id='35F9u'></tbody>

                  本文介绍了PagerAdapter 总是返回空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试制作一个图像寻呼机,看起来我已经按照我查找的所有教程正确地完成了它,但我得到的只是一个空白屏幕.适配器的 instantiateItem 方法上的断点告诉我它正在被调用,并且所有正确的信息都被设置到视图中,并且滑动甚至可以工作,但我仍然什么也没看到.这是我的代码

                  I'm trying to make an image pager and it looks like I've done it correct according to all the tutorials that I've looked up, but all I get is a blank screen. A breakpoint on the adapter's instantiateItem method tells me that it is being called and all the right information is getting set into the views, and swiping even works, but I still don't see anything. Here is my code

                  activity_photos.xml

                  <RelativeLayout> // I'm not including that code, irrelevant.
                  <android.support.v4.view.ViewPager
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:id="@+id/imagePager"
                      android:background="@android:color/black"/>
                  </RelativeLayout>
                  

                  viewpager_itemx.xml

                  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                android:orientation="vertical"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent">
                  
                      <ImageView
                          android:layout_width="match_parent"
                          android:layout_height="0dp"
                          android:layout_weight="1"
                          android:id="@+id/imageView"/>
                  
                      <TextView
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:id="@+id/imageLabel"
                          android:textColor="@android:color/white"/>
                  </LinearLayout>
                  

                  PhotosActivity.java

                      final String[] images = getResources().getStringArray(R.array.tips_images);
                      final String[] labels = getResources().getStringArray(R.array.tips_text);
                  
                      final ViewPager viewPager = (ViewPager) findViewById(R.id.imagePager);
                      final ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(PhotoTipsActivity.this, images, labels);
                  
                      viewPager.setAdapter(viewPagerAdapter);
                  

                  最后是 ViewPagerAdapter.java

                  and finally, ViewPagerAdapter.java

                  公共类 ViewPagerAdapter 扩展 PagerAdapter {

                  public class ViewPagerAdapter extends PagerAdapter {

                  private Context context;
                  private String[] images;
                  private String[] labels;
                  
                  public ViewPagerAdapter(Context context, String[] images, String[] labels) {
                      this.context = context;
                      this.images = images;
                      this.labels = labels;
                  }
                  
                  @Override
                  public int getCount() {
                      return images.length;
                  }
                  
                  @Override
                  public boolean isViewFromObject(View view, Object object) {
                      return view == object;
                  }
                  
                  @Override
                  public Object instantiateItem(ViewGroup container, int position) {
                      final View itemView = LayoutInflater.from(context).inflate(R.layout.viewpager_item, container, false);
                      final ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
                      final TextView imageLabel = (TextView) itemView.findViewById(R.id.imageLabel);
                  
                      // Get drawable image.
                      final int imageId = context.getResources().getIdentifier(images[position], "drawable", context.getPackageName());
                  
                      imageView.setImageResource(imageId);
                      imageLabel.setText(labels[position]);
                  
                      return itemView;
                  }
                  
                  @Override
                  public void destroyItem(ViewGroup container, int position, Object object) {
                      container.removeView(((LinearLayout) object));
                  }
                  

                  }

                  我没有看到我的图像有什么明显的原因吗?

                  Any blatantly obvious reason why I'm not seeing my images?

                  推荐答案

                  同理destroyItem()需要从容器中移除视图,instantiateItem()需要将视图添加到容器中.

                  The same way destroyItem() needs to remove the view from the container, instantiateItem() needs to add the view to the container.

                  只需添加

                      container.addView(itemView);
                  

                  在从 instantiateItem() 返回之前,您将开始做生意.

                  before returning from instantiateItem() and you'll be in business.

                  这篇关于PagerAdapter 总是返回空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Navigation Drawer 和 SlidingTabLayout 如何一起实现? 下一篇:如何在 Android 中向 ViewPager 添加进度条

                  相关文章

                    <i id='pbsaa'><tr id='pbsaa'><dt id='pbsaa'><q id='pbsaa'><span id='pbsaa'><b id='pbsaa'><form id='pbsaa'><ins id='pbsaa'></ins><ul id='pbsaa'></ul><sub id='pbsaa'></sub></form><legend id='pbsaa'></legend><bdo id='pbsaa'><pre id='pbsaa'><center id='pbsaa'></center></pre></bdo></b><th id='pbsaa'></th></span></q></dt></tr></i><div id='pbsaa'><tfoot id='pbsaa'></tfoot><dl id='pbsaa'><fieldset id='pbsaa'></fieldset></dl></div>
                    • <bdo id='pbsaa'></bdo><ul id='pbsaa'></ul>
                  1. <tfoot id='pbsaa'></tfoot>
                  2. <small id='pbsaa'></small><noframes id='pbsaa'>

                  3. <legend id='pbsaa'><style id='pbsaa'><dir id='pbsaa'><q id='pbsaa'></q></dir></style></legend>