搜尋

首頁  >  問答  >  主體

<长期寻求解决办法>Android ViewPager+Fragment页面空白

Adapter

public class AdvViewPagerAdapter extends FragmentPagerAdapter {
    private Context context;
    private List<String> urls;
    private List<ImageView> imgs;
    private List<Fragment> fragments;

    public AdvViewPagerAdapter(FragmentManager fm, Context context, List<String> urls) {
        super(fm);
        this.context = context;
        this.urls = urls;
        // imgs = new ArrayList<ImageView>();
        fragments = new ArrayList<Fragment>();
        for (String url : urls) {
            MyImageFragment fragment = new MyImageFragment(context, url);
            fragments.add(fragment);
        }
    }

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

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

    @Override
    public Fragment getItem(int position) {
        MLog.i(fragments.get(position));
        return fragments.get(position);
    }
}

Fragment

class MyImageFragment extends BaseFragment {
        private View view;
        private ImageView mImageView;
        private String url;

        public MyImageFragment(Context context, String url) {
            super(context);
            this.url = url;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            view = getLayout(inflater, container, R.layout.image_layout, false);
            initView();
            Picasso.with(getContext()).load(url).into(mImageView);
            return view;
        }

        @Override
        public void initView() {
            mImageView = (ImageView) view.findViewById(R.id.image_layout_iv);
        }
    }

R.layout.image_layout

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/image_layout_iv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/dark_gray" />
    </LinearLayout>

Test代码

    public class WelcomeActivity extends BaseActivity {

    private ViewPager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        initView();
    }

    @Override
    public void initView() {
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        List<String> urls = new ArrayList<String>();
        urls.add("http://hiphotos.baidu.com/baidu/pic/item/4ec2d5628535e5dd3c10363671c6a7efce1b621f.png");
        urls.add("http://hiphotos.baidu.com/baidu/pic/item/a8ec8a13632762d07842064fa6ec08fa503dc6c6.png");
        urls.add("http://fanyi.baidu.com/static/translation/img/footer/app_e8ff780.png");
        viewpager.setAdapter(new AdvViewPagerAdapter(getSupportFragmentManager(), getContext(), urls));
    }
}

!BaseActivity 继承自
android.support.v4.app.FragmentActivity

!BaseFragment 继承自
android.support.v4.app.Fragment

大家讲道理大家讲道理2771 天前612

全部回覆(4)我來回復

  • 天蓬老师

    天蓬老师2017-04-17 17:26:49

    該問題已關閉_(:3”∠)_

    回覆
    0
  • PHP中文网

    PHP中文网2017-04-17 17:26:49

    Fragment不是不建議有參的構造嗎?另外畢卡索的用法是,盡量定長寬,如果不定的話用自動適配,例如:

                Picasso.with(context)
                        .load(url)
                        .placeholder(res_holder)
                        .error(res_holder)
                        .config(Bitmap.Config.RGB_565)
                        .fit()
                        .centerInside()
                        .into(view);

    建議Picasso盡量設定詳細點,有助於顯示和控制。如果你說的空白,就是說連imageView的背景都沒顯示的話,還需查看Acitivty裡面的程式碼,看是否是設定有問題。

    回覆
    0
  • 迷茫

    迷茫2017-04-17 17:26:49

    你的initView()方法在父類別BaseFragment中有呼叫嗎?還是單純的抽象?在fragment中,context最好使用getActivity()來獲取,會對Picasso友善一些,即Picasso.with(getActivity()),可以先試試看。

    回覆
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:26:49

    fragmentment沒複寫getItem()所以得不到子每個pager需要載入的fragment。

    回覆
    0
  • 取消回覆