2013-02-08 7 views
5

ディメンションで読み込まれたコンテンツを含むscrollViewがあります。時には多くのコンテンツが存在することがあるので、ユーザーが下にスクロールするときにもっと読み込みたいと思います。ScrollViewが下にスクロールされたときにさらにデータを読み込みます。

私は、適切なmetodsためsearcedと2が見つかりました:

onScrollChanged() 

getScrollY() 

しかし、私は私の目的のためにそれを使用する方法を知りません。 お願いします。

+3

をやっていますか?これはそうです – codeMagic

+0

私はlistviewが許可していないビュー設定が必要です。 (pinterestのような写真を表示) – TpoM6oH

+0

私はpinterestを使用しないので、レイアウトはわかりませんが、写真を撮ってlistviewに表示できるようになっています。スポンジボブのように、想像力で何でもできるよ、子供がいるよ: – 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 
} 

をそのインターフェイスを実装し、 `ListView`を使用しないのはなぜあなたが

+0

diffを取得できません。 –

関連する問題