2014-09-28 7 views
5

मैं छविदृश्य सरणी, टेक्स्टव्यू सरणी प्रदर्शित करने के लिए पेजरैडाप्टर और व्यूपार्जर का उपयोग कर रहा हूं। लेकिन मैं बटन क्लिक पर पेजर को कैसे हटा सकता हूं। नीचे दिए गए कोड में कोड कोड नहीं है, लेकिन मैंने एक्सएमएल ऑनक्लिक में बटन जोड़ा है पेज को हटाना चाहिए।व्यूपर और पेजरैडप्टर से आइटम को कैसे हटाएं

http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/

package com.androidbegin.viewpagertutorial; 

    import android.content.Context; 
    import android.support.v4.view.PagerAdapter; 
    import android.support.v4.view.ViewPager; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.view.ViewGroup; 
    import android.widget.Button; 
    import android.widget.ImageView; 
    import android.widget.RelativeLayout; 
    import android.widget.TextView; 

    public class ViewPagerAdapter extends PagerAdapter { 
     // Declare Variables 
     Context context; 
     String[] rank; 
     String[] country; 
     String[] population; 
     int[] flag; 
     LayoutInflater inflater; 

     public ViewPagerAdapter(Context context, String[] rank, String[] country, 
       String[] population, int[] flag) { 
      this.context = context; 
      this.rank = rank; 
      this.country = country; 
      this.population = population; 
      this.flag = flag; 
     } 

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

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

     @Override 
     public Object instantiateItem(final ViewGroup container, final int position) { 

      // Declare Variables 
      TextView txtrank; 
      TextView txtcountry; 
      TextView txtpopulation; 
      ImageView imgflag; 

      Button b; 

      inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View itemView = inflater.inflate(R.layout.viewpager_item, container, 
        false); 

      // Locate the TextViews in viewpager_item.xml 
      txtrank = (TextView) itemView.findViewById(R.id.rank); 
      txtcountry = (TextView) itemView.findViewById(R.id.country); 
      txtpopulation = (TextView) itemView.findViewById(R.id.population); 
      b=(Button)itemView.findViewById(R.id.button1); 


      b.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 



       } 
      }); 

      // Capture position and set to the TextViews 
      txtrank.setText(rank[position]); 
      txtcountry.setText(country[position]); 
      txtpopulation.setText(population[position]); 

      // Locate the ImageView in viewpager_item.xml 
      imgflag = (ImageView) itemView.findViewById(R.id.flag); 
      // Capture position and set to the ImageView 
      imgflag.setImageResource(flag[position]); 

      // Add viewpager_item.xml to ViewPager 
      ((ViewPager) container).addView(itemView); 

      return itemView; 
     } 

     @Override 
     public void destroyItem(ViewGroup container, int position, Object object) { 
      // Remove viewpager_item.xml from ViewPager 
      ((ViewPager) container).removeView((RelativeLayout) object); 

     } 
    } 

////////////////////

// Locate the ViewPager in viewpager_main.xml 
     viewPager = (ViewPager) findViewById(R.id.pager); 
     // Pass results to ViewPagerAdapter Class 
     adapter = new ViewPagerAdapter(MainActivity.this, rank, country, population, flag); 
     // Binds the Adapter to the ViewPager 
     viewPager.setAdapter(adapter); 

उत्तर

8

पहले आप क्योंकि गतिशील को हटाने वस्तुओं की List उपयोग करने की आवश्यकता । दूसरा आप

@Override 
    public int getItemPosition(Object object){ 
     return PagerAdapter.POSITION_NONE; 
    } 

नया पृष्ठ हर बार बनाने के लिए एडाप्टर के लिए मजबूर करने और स्मृति पृष्ठों में उपयोग करने की जरूरत नहीं है।

public class ViewPagerAdapter extends PagerAdapter { 
    // Declare Variables 
    Context context; 
    List<String> rank; 
    List<String> country; 
    List<String> population; 
    List<Integer> flag = new ArrayList<Integer>(); 
    LayoutInflater inflater; 

    public ViewPagerAdapter(Context context, String[] rank, String[] country, 
      String[] population, int[] flag) { 
     this.context = context; 
     this.rank = new ArrayList<String>(Arrays.asList(rank)); 
     this.country = new ArrayList<String>(Arrays.asList(country)); 
     this.population = new ArrayList<String>(Arrays.asList(population)); 
     for (int i : flag){ 
      this.flag.add(i); 
     } 

    } 

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

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

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 

     // Declare Variables 
     TextView txtrank; 
     TextView txtcountry; 
     TextView txtpopulation; 
     ImageView imgflag; 

     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View itemView = inflater.inflate(R.layout.viewpager_item, container, 
       false); 

     // Locate the TextViews in viewpager_item.xml 
     txtrank = (TextView) itemView.findViewById(R.id.rank); 
     txtcountry = (TextView) itemView.findViewById(R.id.country); 
     txtpopulation = (TextView) itemView.findViewById(R.id.population); 
     Button b=(Button)itemView.findViewById(R.id.button1); 

     final int delPosition = position; 

     b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       rank.remove(delPosition); 
       country.remove(delPosition); 
       population.remove(delPosition); 
       flag.remove(delPosition); 
       notifyDataSetChanged(); 
      } 
     }); 
     // Capture position and set to the TextViews 
     txtrank.setText(rank.get(position)); 
     txtcountry.setText(country.get(position)); 
     txtpopulation.setText(population.get(position)); 

     // Locate the ImageView in viewpager_item.xml 
     imgflag = (ImageView) itemView.findViewById(R.id.flag); 
     // Capture position and set to the ImageView 
     imgflag.setImageResource(flag.get(position)); 

     // Add viewpager_item.xml to ViewPager 
     ((ViewPager) container).addView(itemView); 

     return itemView; 
    } 

    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     // Remove viewpager_item.xml from ViewPager 
     ((ViewPager) container).removeView((RelativeLayout) object); 

    } 
    @Override 
    public int getItemPosition(Object object){ 
     return PagerAdapter.POSITION_NONE; 
    } 
} 
+0

मेरे पास एक प्रश्न है, जब मैं अपनी सूची के अंतिम आइटम को हटाने का प्रयास करता हूं तो मेरा ऐप क्रैश हो जाता है। यह कहता है, "java.lang.IndexOutOfBoundsException: अमान्य अनुक्रमणिका 6, आकार 6 है"। यह इस पंक्ति को इंगित करता है: imageView.setImageResource (regionImages.get (स्थिति)); –

+0

@KalaJ एसओ पर एक प्रश्न के रूप में अपना कोड पोस्ट करें। तो मुझे लिंक बताओ! – mmlooloo

+0

मैंने इसे समझ लिया, धन्यवाद: डी –

संबंधित मुद्दे