2013-02-08 12 views
5

मुझे डायनामिक रूप से लोडिंग सामग्री के साथ स्क्रॉल दृश्य मिला है। कभी-कभी बहुत सारी सामग्री हो सकती है, इसलिए जब उपयोगकर्ता नीचे स्क्रॉल करता है तो मैं अधिक लोड करना चाहता हूं।स्क्रॉलव्यू को नीचे स्क्रॉल किया गया है जब अधिक डेटा लोड करें

मैं उपयुक्त metods के लिए searced और दो पाया:

onScrollChanged() 

और

getScrollY() 

लेकिन मुझे पता है कि मेरी प्रयोजनों के लिए उपयोग करने के लिए नहीं है। कृपया मुझे कुछ सलाह दें।

+3

एक 'ListView' का उपयोग क्यों नहीं किया जाता? यह – codeMagic

+0

है जो मुझे कुछ दृश्य कॉन्फ़िगरेशन की आवश्यकता है जो सूचीदृश्य अनुमति नहीं देता है। (Pinterest में फोटो दिखाएं) – TpoM6oH

+0

मैं पिन रुचि का उपयोग नहीं करता इसलिए मुझे उनके लेआउट को नहीं पता लेकिन मैं अभी कुछ पर काम कर रहा हूं जो आपको एक तस्वीर लेने और सूचीदृश्य में प्रदर्शित करने की अनुमति देता है। स्पंज की तरह कहते हैं, कल्पना के साथ कुछ भी संभव है, हाँ मेरे बच्चे हैं :) – codeMagic

उत्तर

6

मुझे लगता है कि बेहतर तरीका ListView का उपयोग करना होगा। लेकिन यदि आपको केवल ScrollView की आवश्यकता है।

सबसे पहले आप अपने ScrollView की एक सीमा से ऊंचाई को ठीक तो जब भी आप को पार करती है कि सीमा लोड नए डेटा ScrollView & को संलग्न & अपनी सीमा ऊंचाई रीसेट।

यह आप कैसे की कोशिश कर सकते है ..

एक स्वनिर्धारित ScrollView इस तरह बनाएँ।

public class ObservableScrollView extends ScrollView 
{ 

    private ScrollViewListener scrollViewListener = null; 

    public ObservableScrollView(Context context) 
    { 
     super(context); 
    } 

    public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
    } 

    public ObservableScrollView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
    } 

    public void setScrollViewListener(ScrollViewListener scrollViewListener) 
    { 
     this.scrollViewListener = scrollViewListener; 
    } 

    @Override 
    protected void onScrollChanged(int x, int y, int oldx, int oldy) 
    { 
     super.onScrollChanged(x, y, oldx, oldy); 
     if (scrollViewListener != null) 
     { 
      scrollViewListener.onScrollChanged(this, x, y, oldx, oldy); 
     } 
    } 

} 

& तो एक अंतरफलक

public interface ScrollViewListener 
{ 
    void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); 
} 

& अपने XML लेआउट इस

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <com.solve.stackoverflow.ObservableScrollView 
     android:id="@+id/endless_scrollview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <LinearLayout 
      android:id="@+id/endless_scrollview_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" /> 
    </com.solve.stackoverflow.ObservableScrollView> 

</LinearLayout> 

& की तरह होना चाहिए बनाने के अंत में अपनी गतिविधि में इन सभी का उपयोग

public class EndLessActivity extends Activity implements ScrollViewListener 
{ 
    ObservableScrollView scrollView; 
    LinearLayout layout; 

    int threshold = 20; //Setting threshold 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.endless_scrollview); 
     scrollView = (ObservableScrollView) findViewById(R.id.endless_scrollview); 
     layout = (LinearLayout) findViewById(R.id.endless_scrollview_layout); 
     scrollView.setScrollViewListener(this); 
     appendData(); 
    } 

    @Override 
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) 
    { 
     // TODO Auto-generated method stub 
     if (y > threshold) 
      appendData(); 
    } 

    void appendData() 
    { 
     for (int i = 0; i < 15; i++) 
     { 
      threshold += 10;//Reseting threshold. 
      TextView tv = new TextView(this); 
      tv.setBackgroundResource(R.drawable.ic_launcher); 
      layout.addView(tv); 
     } 
    } 
} 

इस & आज़माएं मुझे बताएं, भले ही यह आपकी समस्या का समाधान करे या नहीं।

धन्यवाद

11

स्क्रॉल दृश्य इतना बेहतरीन तकनीक पुस्तक को देखने के साथ अपने स्वयं के कस्टम दृश्य का विस्तार करने के लिए है अगर आप दृश्य के निचले पहुँच गए हैं जाँच करने के लिए कोई भी तरीका प्रदान नहीं करता है। इस तरह मैंने अपने ऐप में कार्यान्वित किया।

चरण 1: पुस्तक देखने के लिए एक कस्टम कक्षा बनाएं

public class ObservableScrollView extends ScrollView { 

private ScrollViewListener scrollViewListener = null; 

public ObservableScrollView(Context context) { 
    super(context); 
} 

public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

public ObservableScrollView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public void setScrollViewListener(ScrollViewListener scrollViewListener) { 
    this.scrollViewListener = scrollViewListener; 
} 

@Override 
protected void onScrollChanged(int x, int y, int oldx, int oldy) { 

    View view = (View) getChildAt(getChildCount() - 1); 
    int diff = (view.getBottom() - (getHeight() + getScrollY())); 
    if (diff == 0) { // if diff is zero, then the bottom has been reached 
     if (scrollViewListener != null) { 
      scrollViewListener.onScrollEnded(this, x, y, oldx, oldy); 
     } 
    } 
    super.onScrollChanged(x, y, oldx, oldy); 
} 

}

चरण 2: एक स्क्रॉल दृश्य श्रोता इंटरफ़ेस

बनाएं
public interface ScrollViewListener { 

void onScrollEnded(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); 



} 

चरण 3: अपने लेआउट में दृश्य जोड़ें

<com.platinumapps.facedroid.ObservableScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </com.platinumapps.facedroid.ObservableScrollView> 

चरण 4: अपने वर्ग

public class MyFragment extends Fragment implements ScrollViewListener 

@Override 
public void onScrollEnded(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) { 

      //Perform your action 
} 

में है कि इंटरफ़ेस को लागू करने और आप

+0

diff 0 प्राप्त करने में असमर्थ 0 0 से कम सीधे भिन्न हो रहा है –

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

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