我从响应中获取了三张图片,并且我在arraylist中存储,然后我尝试在PagerAdapter中设置,但是我的视图寻呼机中没有显示任何内容,以下是我的pageradapter类,任何人都可以告诉我犯了什么错误?
我正在使用Fragment
在我的Postexecute中,我将它设置在这样的视图寻呼机中
adapter = new ImageAdapter(All_Product_Details.this.getActivity());
viewPager.setAdapter(adapter);
}
下面是我在asynctask之后设置的适配器
public class ImageAdapter extends FragmentPagerAdapter {
Context context;
ImageAdapter(Context context)
{
this.context=context;
}
@Override
public int getCount() {
return multimglist.size();
}
@Override
public Fragment getItem(int i) {
return null;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.activity_horizontal_margin);
imageView.setPadding(padding, padding, padding, padding);
//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Picasso.with(context).load(multimglist.get(position)).into(imageView);
// imageView.setImageURI(Uri.parse(imgStr[position]));
((ViewPager) container).addView(imageView, 0);
return imageView;
}
}
答案 0 :(得分:2)
this is my pager adapter change according that
public class LoginViewPagerCA extends PagerAdapter {
SparseArray<View> views = new SparseArray<>();
List<Viewpager_POJO> listofPersons;
LayoutInflater inflater;
Context context;
private int count = 4;
public LoginViewPagerCA(Context context, List<Viewpager_POJO> listofPersons) {
this.context = context;
this.listofPersons = listofPersons;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listofPersons.size();
}
public void setCount(int count) {
this.count = count;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
// TODO Auto-generated method stub
return view == ((LinearLayout) obj);
}
public void setData(ArrayList<Viewpager_POJO> data) {
listofPersons = data;
}
public Object instantiateItem(ViewGroup container, final int position)
{
final TextView txtFstName, txtLstName, txtRollno, txtArrTime, txtTimeLeft;
ImageView imgView;
final Viewpager_POJO rowperson = listofPersons.get(position);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View itemView = inflater.inflate(R.layout.landing_pager_item, container, false);
txtFstName = (TextView) itemView.findViewById(R.id.pager_big_tv);
txtLstName = (TextView) itemView.findViewById(R.id.pager_small_tv);
imgView = (ImageView) itemView.findViewById(R.id.pager_image);
try {
// txtCompn.setText((position + 1) + "/" + StuFirstNM.length);
txtFstName.setText(rowperson.getDesc());
txtLstName.setText(rowperson.getRating());
//new LoadImage(imgView).execute(rowperson.getUrls());
if (!TextUtils.isEmpty(rowperson.getUrls())) {
// PicassoTrustAll.getInstance(context).load(rowperson.getUrls()).placeholder(context.getResources().getDr`enter code here`awable(R.drawable.ic_launcher)).error(context.getResources().getDrawable(R.drawable.ic_launcher)).into(imgView);
//
byte[] imgbytes = Base64.decode(rowperson.getStrImm(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(imgbytes, 0, imgbytes.length);
imgView.setImageBitmap(bitmap);
// PicassoTrustAll.getInstance(context)
// .load(rowperson.getUrls())
// .error(R.mipmap.landinglogo)
// .into(imgView);
Log.w("Testing", "Image Download URL=" + rowperson.getUrls());
} else {
imgView.setBackgroundResource(R.mipmap.landinglogo);
}
((ViewPager) container).addView(itemView);
views.put(position, itemView);
} catch (Exception e)
{
e.printStackTrace();
}
return itemView;
}
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
((ViewPager) container).removeView(view);
views.remove(position);
view = null;
}
答案 1 :(得分:1)
如果您使用Fragments
,则应使用FragmentPagerAdapter
,而不是PagerAdapter
。参见官方文档here。您应该覆盖getItem(int position)
以在正确的位置返回所需的Fragment
。
如果您查看文档,则会显示名为FragmentPagerAdapter
的{{1}}类。这看起来像:
MyAdapter
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
}
方法应该返回特定getItem(int position)
的新实例。 您目前正在返回Fragment
,这意味着不会显示任何内容。例如,在我自己的代码中,我会覆盖null
,如下所示:
getItem()