问题描述
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;}@Overridepublic 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;}@Overridepublic void initView() { mImageView = (ImageView) view.findViewById(R.id.image_layout_iv);} }
R.layout.image_layout
<LinearLayoutandroid:layout_width='match_parent'android:layout_height='match_parent' ><ImageView android: 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
问题解答
回答1:该问题已关闭_(:3」∠)_
回答2:Fragment不是不建议有参的构造吗?另外毕加索的用法是,尽量定出长宽,如果不定的话用自动适配,比如:
Picasso.with(context) .load(url) .placeholder(res_holder) .error(res_holder) .config(Bitmap.Config.RGB_565) .fit() .centerInside() .into(view);
建议Picasso尽量设置详细点,有助于显示和控制。如果你说的空白,就是说连imageView的背景都没显示的话,还需查看Acitivty里面的代码,看是否是设置有问题。
回答3:你的initView()方法在父类BaseFragment中有调用吗?还是单纯的抽象?在fragment中,context最好使用getActivity()来获取,会对Picasso友好一些,即Picasso.with(getActivity()),可以先试一下。
回答4:fragmentment没复写getItem()所以得不到子每个pager需要加载的fragment。