-1

我有一個正規ViewPager與適配器:ViewPager適配器中的O​​nPageChangeListener?

public class MyDialogFragment extends DialogFragment { 

    private ViewPager viewPager; 

    private MyViewPagerAdapter myViewPagerAdapter; 

    private City city; 

    public static MyDialogFragment newInstance() { 
     MyDialogFragment fragment = new MyDialogFragment(); 

     return fragment; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_layout, container, false); 

     viewPager = (ViewPager) v.findViewById(R.id.viewPager); 

     city = Parcels.unwrap(getArguments().getParcelable("city")); 

     myViewPagerAdapter = new MyViewPagerAdapter(getContext()); 
     viewPager.setAdapter(myViewPagerAdapter); 

     viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       // Page changed 
      } 
     }); 

     return v; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    public class MyViewPagerAdapter extends PagerAdapter { 

     private Context context; 

     private LayoutInflater layoutInflater; 

     public MyViewPagerAdapter(Context context) { 
      this.context = context; 
     } 

     @Override 
     public Object instantiateItem(ViewGroup container, int position) { 
      layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View view = layoutInflater.inflate(R.layout.city_layout, container, false); 

      ImageView imageView = (ImageView) view.findViewById(R.id.cityImage); 

      Glide.with(context) 
        .load(city.getImage()) 
        .into(imageView); 
      } 

      container.addView(view); 

      return view; 
     } 

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

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

     @Override 
     public void destroyItem(ViewGroup container, int position, Object object) { 
      container.removeView((View) object); 
     } 
    } 
} 

正如你所看到的,我有一個頁面變化監聽我的ViewPager設置。但是,當頁面發生變化時,我需要能夠通知適配器,以便我可以修改適配器的ImageView。我怎樣才能做到這一點?

+0

你應該看看android sdk教程。你不理解適配器背後的概念。 – Mussa

回答

0

的方法添加到您的適配器類

public class MyViewPagerAdapter extends PagerAdapter { 

    ... 
    public void pageChanged(int position) { 
     // Do something 
    } 
    ... 
} 

後,在您OnPageChangeListener使用該方法

@Override 
public void onPageSelected(int position) { 
    myViewPagerAdapter.pageChanged(position); 
} 
+0

如何在適配器中獲得'ImageView',以便我可以修改它? – user7758932

0

一種方法是在你的適配器映射尋呼機視圖創建一個數據結構它的位置。一個SparseArray是最有效的:

private SparseArray<View> pageMap = new SparseArray<>(); 

然後在適配器的instantiateItem()方法,將視圖添加到SparseArray

public Object instantiateItem(ViewGroup container, int position) { 
    ... 
    pageMap.put(position, view); 
    ... 
} 

現在您的OnPageChangeListener你可以做到以下幾點:

@Override 
public void onPageSelected(int position) { 
    View view = adapter.getView(pos); 
    if (view != null) { 
     ImageView iv = (ImageView) view.findViewById(R.id.your_image_view); 
     // You now have the image view 
    } 
} 

確保您從適配器的destroyItem()中的SparseArray中刪除視圖ethod:

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    pageMap.remove(position); 
    ... 
} 
0

您可以使用構造函數與圖像列表傳遞和自動調用旁邊page..In你的代碼將像::: ArrayList的圖像列表=新的ArrayList <>()進行修改;

imageList.add("ImagePath here"); 
myViewPagerAdapter = new MyViewPagerAdapter(getContext(),imageList); 
viewPager.setAdapter(myViewPagerAdapter); 

然後適配器會像::

public class MyViewPagerAdapter extends PagerAdapter { 

      private Context context; 
      private ArrayList<String> imageList 
      private LayoutInflater layoutInflater; 

      public MyViewPagerAdapter(Context context,ArrayList<String> imageList) { 
       this.context = context; 
       this.imageList = imageList; 
      } 

      @Override 
      public Object instantiateItem(ViewGroup container, int position) { 
       layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

       View view = layoutInflater.inflate(R.layout.city_layout, container, false); 

       ImageView imageView = (ImageView) view.findViewById(R.id.cityImage); 

       Glide.with(context) 
         .load(imageList.get(position))   // This will be changes like 
         .into(imageView); 
       } 

       container.addView(view); 

       return view; 
      } 

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

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

      @Override 
      public void destroyItem(ViewGroup container, int position, Object object) { 
       container.removeView((View) object); 
      } 
     } 

,做完了.. 在此之後,也有一些建議,即 - 請在使用viewpager使用ArrayList .. 現在你只從捆綁中取出裝訂物件。 您可以對數組列表執行相同的操作, 只需從同一個捆綁包中接收數組列表,即可立即傳遞整個數組列表。 所以你不需要找到該網頁變化event..viewpager會自動處理的.. 這裏是如何發送&接收parceable的ArrayList

// while sending the list 
     Bundle bundle = new Bundle(); 
     bundle.putParcelableArrayList("citylist", new ArrayList<City>()); 

     // while receiving the list 
     ArrayList<City> citylist = getIntent().getParcelableArrayListExtra("citylist"); 

在此之後只得到了所有的圖像,並添加圖像列表陣列中

 for (int i = 0; i < citylist.size(); i++) { 
      imageList.add(citylist.get(i).getImage()); 
     } 

而且通過這個imageList

myViewPagerAdapter = new MyViewPagerAdapter(getContext(),imageList); 
viewPager.setAdapter(myViewPagerAdapter); 

如果需要更多的幫助,告訴我。

相關問題