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
PHP中文网2017-04-17 17:26:49
Isn’t it not recommended for Fragment to have a parameterized structure? In addition, Picasso’s usage is to determine the length and width as much as possible. If not, use automatic adaptation, such as:
Picasso.with(context)
.load(url)
.placeholder(res_holder)
.error(res_holder)
.config(Bitmap.Config.RGB_565)
.fit()
.centerInside()
.into(view);
It is recommended that Picasso set as many details as possible to help with display and control. If what you mean by blank means that even the background of the imageView is not displayed, you also need to check the code in Activity to see if there is a problem with the settings.
迷茫2017-04-17 17:26:49
Is your initView() method called in the parent class BaseFragment? Or is it simply abstract? In the fragment, it is best to use getActivity() to get the context, which will be more friendly to Picasso, that is, Picasso.with(getActivity()). You can try it first.
大家讲道理2017-04-17 17:26:49
fragmentment does not override getItem(), so it cannot get the fragment that each pager needs to load.