2012-10-10 33 views
6

का उपयोग करते समय दो बार बुलाया जा रहा है। मैं अपने टुकड़े वर्ग को तुरंत चालू करने के लिए एक टुकड़ा पेजर एडाप्टर का उपयोग कर रहा हूं। मैं ऐसा करने में सक्षम हूं लेकिन मेरी समस्या यह है कि मेरी getItem() विधि को दो बार बुलाया जा रहा है समस्या आगे बढ़ रही है। क्या आप मुझे समझाते हैं कि यह क्यों हो रहा है।getItem() विधि को फ्रैगमेंट पेजर एडाप्टर

package com.creatiosoft.rssfeed.adaptor; 

    import android.content.Context; 
    import android.support.v4.app.Fragment; 
    import android.support.v4.app.FragmentManager; 
    import android.support.v4.app.FragmentPagerAdapter; 
    import android.util.Log; 

    import com.creatiosoft.rssfeed.utils.RssItem; 
    import com.viewpagerindicator.IconPagerAdapter; 

    public class NewsFeedsAdapter extends FragmentPagerAdapter implements 
      IconPagerAdapter { 

     int[] icon = null; 
     String[] content = null; 
     String[] URLs = null; 
     Context cont; 

     public NewsFeedsAdapter(FragmentManager fm, Context context) { 
      super(fm); 
      Log.i("jk", "constructor"); 
      this.cont = context; 
      RssItem newsFeedAppliaction = (RssItem) cont; 
      /* 
      * Retrieving the values of the Icons and contents from the application 
      * class in utils package 
      */ 
      icon = newsFeedAppliaction.getICONS(); 
      content = newsFeedAppliaction.getCONTENT(); 
      URLs = newsFeedAppliaction.getURL(); 

     } 

     /** instantiate a new fragment class */ 
     @Override 
     public Fragment getItem(int position) { 
      Log.i("yt", "hello" + position); 
      return TestFragment.newInstance(position % content.length, cont); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return content[position % content.length].toUpperCase(); 
     } 

     public int getIconResId(int index) { 
      return icon[index]; 
     } 

     /** return the no of views on the basis of array items */ 
     @Override 
     public int getCount() { 

      Log.i("hi", "length" + content.length); 
      return content.length; 
     } 
    }  

मैं इस कोड के साथ एडाप्टर बोल रहा हूँ:

NewsFeedsAdapter adapter = new NewsFeedsAdapter(
       getSupportFragmentManager(), getApplicationContext()); 
     /** 
     * get the id of the view pager declared in the xml resource and set the 
     * adaptor on the view pager 
     */ 
     ViewPager pager = (ViewPager) findViewById(R.id.pager); 
     pager.setAdapter(adapter); 
     //pager.setCurrentItem(0); 

     /** 
     * Tab page indicator class is used to indicate the tabs and is accessed 
     * from the library class 
     */ 
     TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator); 
     indicator.setViewPager(pager); 

उत्तर

2

अपने GetItem विधि में आप एक टुकड़ा का एक नया उदाहरण बना सकते हैं। यह भयानक है। यह आपको उस स्थिति में रखता है जिसमें आप अभी हैं। आप जो कर रहे हैं वह एक नई विलुप्त वस्तु बना रहा है जो आपके द्वारा निर्दिष्ट दिनों के दौरान जी रहेगा। लेकिन जब बुराई एंड्रॉइड सिस्टम निर्णय लेता है कि उसे अपने फ्रैगमेंट से फिर से बात करने की ज़रूरत है और इसे अपने एडाप्टर से अनुरोध करें, तो आप इसे बता रहे हैं "नहीं। मैं नहीं चाहता!" और इसके बजाय इसे एक नया टुकड़ा देकर अपनी पुरानी परेशानी के समान ही।

इसे ठीक करने के लिए, आप समय से पहले अपने सभी टुकड़े को फुलाएं और फिर उचित टुकड़े वापस कर दें। या यदि आप बल्कि, नहीं करते हैं। लेकिन आपके द्वारा बनाए गए उदाहरणों का रिकॉर्ड रखें ताकि आप पहले ही बनाए गए टुकड़े वापस कर सकें।

मुझे नहीं पता कि इसे दो बार क्यों कहा जा रहा है - मैंने v4 के स्रोत को खराब नहीं किया है, लेकिन यह संभवतः यह सुनिश्चित करने के लिए किया जाता है कि आइटम वास्तव में पुनर्प्राप्त हो या सिस्टम उस स्थिति में प्रवेश करता है जहां पिछला आइटम होना आवश्यक था refereshed।

निष्कर्ष निकालें, बनाए गए उदाहरण बनाए रखें - विशेष रूप से ऑब्जेक्ट के लिए टुकड़े, विचार और गतिविधियों के रूप में भारी है।

class Adapter extends MyWhateverAdapter { 
    Fragment[] myLovelyFragments; 

    public Adapter() { 
     // Instantiate your fragments and place them into myLovelyFragments 
    } 

    public int getFragmentCount() { 
     return myLovelyFragments.length; 
    } 

    public Fragment getItem(int position) { 
     return myLovelyFragments[position]; 
    } 
} 
+0

कृपया मुझे नमूना कोड दें यदि संभव हो तो। – yokees

+0

@ ऋषभ। क्रिएटियो सॉफ्ट आप वहां जाते हैं। :) – AedonEtLIRA

+0

लेकिन मुझे बताएं कि एक चीज की स्थिति मुझे 0 और 1 प्राप्त करें, जब पेगर एडाप्टर कॉल करें। तो हम 1 स्थिति पर खंड वस्तु प्राप्त कर रहे हैं। तो आप कैसे अपना कोड सही कह सकते हैं। – yokees

11

पता है कि पेजर आगे कम से कम एक पेज रखता हो। मतलब यह है कि जब पेजर बनाया जाता है, तो वह "पेजिंग" की अनुमति देने के लिए कम से कम दो पेज बनाता है - वह दिखाता है और अगला वाला।

0

आपके पास शायद समस्या है जो getItem() विधि की एकाधिक कॉल नहीं है। थोड़ी खोजबीन के बाद, मैंने पाया कि विधि isViewFromObject() एक बड़ी भूमिका है और इसके दस्तावेज में यह कहा जाता है "This method is required for a PagerAdapter to function properly."

मैंने यह भी पाया है कि विधि लागू करने के लिए सही तरीके से यह है कि:

@Override 
public boolean isViewFromObject(View view, Object object) { 
    if(object != null){ 
     return ((Fragment)object).getView() == view; 
    }else{ 
     return false; 
    } 
} 

आप here देख सकते हैं।

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