2017-03-05 85 views
1

我有列表。列表中的每個項目都有3個TextView和一個ViewPager。我從服務器下載了關於不同項目的信息並將它們上傳到列表中。每個項目都有不同的文字和圖像。我從ListAdapter調用ViewPager的適配器。我的應用程序返回正確的項目信息,但ViewPager中的圖像在所有項目中都是相同的:它們是最後一個項目的圖像。那麼,如何解決這個問題呢?從列表適配器調用viewpager適配器

我ListAdapter:

public class loadersfragment extends ArrayAdapter<Projects> { 

public Context context; 
private ArrayList<Projects> values; 
CustomerAdapter adapter; 
private LayoutInflater inflater; 
String[] imagesfortest={"http://img1.sendscraps.com/se/042/004.jpg","http://i.telegraph.co.uk/multimedia/archive/03589/Wellcome_Image_Awa_3589699k.jpg","http://www.freedigitalphotos.net/images/category-images/131.jpg"}; 

private String[] img; 

public loadersfragment(Context context, ArrayList<Projects> values) { 
    super(context,R.layout.listitem,values); 

    this.context = context; 
    this.values = values; 

    inflater = LayoutInflater.from(context); 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 


    if (null == convertView) { 
     convertView = inflater.inflate(R.layout.listitem, parent, false); 

    } 

    TextView textView = (TextView) convertView.findViewById(R.id.name); 
    TextView textView2 = (TextView) convertView.findViewById(R.id.description); 
    TextView textView3 = (TextView) convertView.findViewById(R.id.status); 
    TextView textView4 = (TextView) convertView.findViewById(R.id.version); 
    String [] imageses; 


    Projects mProduct = values.get(position); 

    textView.setText(mProduct.getyourText()); 
    textView2.setText(mProduct.getyourdescription()); 
    textView3.setText(mProduct.getyourstatu()); 
    textView4.setText(mProduct.getYourversion()); 
    imageses=mProduct.getYourimages(); 
    ViewPager viewPager = (ViewPager)convertView.findViewById(R.id.view_pager); 
    adapter = new CustomerAdapter(getContext(),imageses); 
    viewPager.setAdapter(adapter); 
    viewPager.setCurrentItem(0); 
    // adapter.setTimer(viewPager,5000,4,imagesfortest.length); 
    //viewPager.setPageTransformer(true, new ZoomOutPageTransformer()); 

    adapter.autoSlider(viewPager); 


    return convertView; 

} 
    } 

我ViewPager適配器:

公共類CustomerAdapter擴展PagerAdapter {

private Context ctx; 
private String[] urls; 
private LayoutInflater inflater; 
final Handler handler = new Handler(); 
public Timer swipeTimer ; 

public CustomerAdapter(Context ctx,String []urls){ 
    this.ctx = ctx; 
    this.urls = urls; 
} 

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

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

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflater.inflate(R.layout.swip,container,false); 
    ImageView img =(ImageView)v.findViewById(R.id.itemview); 

    Picasso.with(ctx).load(urls[position]).into(img); 

    container.addView(v); 
    return v; 
} 

@Override 
public void destroyItem(View container, int position, Object object) { 
    container.refreshDrawableState(); 
} 
public void autoSlider(final ViewPager viewPager) { 

    final Runnable rr = new Runnable() { 
     public void run() { 
      int pos = viewPager.getCurrentItem(); 
      int i=0; 
      if(pos > i && pos != urls.length - 1){ 
       i = pos; 
       i++; 
      } 

      else if(pos < (i-1)){ 
       i = pos; 
       i++; 
      } 
      else if(pos == i){ 
       i = pos; 
       i++; 
      } 
      viewPager.setCurrentItem(i, true); 
      viewPager.setPageTransformer(true, new ZoomOutPageTransformer()); 
      i++; 
      if (i >= urls.length) 
       i = 0; 
      autoSlider(viewPager); 
     }}; 
    handler.postDelayed(rr, 3000); 
} 

注:我將圖像發送每一個項目的URL到適配器字符串的形式。

+0

我真的不知道如何做到這一點,你可以給我一個例子 –

回答

0

試試這個

public class loadersfragment extends ArrayAdapter<Projects> { 

List<CustomerAdapter> adapters = new ArrayList<>(); 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 


    if (null == convertView) { 
     convertView = inflater.inflate(R.layout.listitem, parent, false); 

    } 

    TextView textView = (TextView) convertView.findViewById(R.id.name); 
    TextView textView2 = (TextView) convertView.findViewById(R.id.description); 
    TextView textView3 = (TextView) convertView.findViewById(R.id.status); 
    TextView textView4 = (TextView) convertView.findViewById(R.id.version); 
    String [] imageses; 


    Projects mProduct = values.get(position); 

    textView.setText(mProduct.getyourText()); 
    textView2.setText(mProduct.getyourdescription()); 
    textView3.setText(mProduct.getyourstatu()); 
    textView4.setText(mProduct.getYourversion()); 
    imageses=mProduct.getYourimages(); 
    ViewPager viewPager = (ViewPager)convertView.findViewById(R.id.view_pager); 

CustomerAdapter adapter = new CustomerAdapter(getContext(),imageses); 
adapters.add(adapter); 
    viewPager.setAdapter(adapters.get(position)); 
    viewPager.setCurrentItem(0); 
    // adapter.setTimer(viewPager,5000,4,imagesfortest.length); 
    //viewPager.setPageTransformer(true, new ZoomOutPageTransformer()); 

    adapters.get(position).autoSlider(viewPager); 


    return convertView; 

} 



    } 
+0

相同的結果。他們返回最後一個項目的圖像 –

+0

viewPager.setAdapter(adapters.get(position)); –