コンテンツを動的にロードする scrollView があります。コンテンツが大量にある場合があるため、ユーザーが一番下までスクロールしたときに、より多くのコンテンツをロードしたいと考えています。
適切な方法を探したところ、次の 2 つが見つかりました。
onScrollChanged()
と
getScrollY()
しかし、私は自分の目的のためにそれを使用する方法がわかりません。アドバイスをお願いします。
コンテンツを動的にロードする scrollView があります。コンテンツが大量にある場合があるため、ユーザーが一番下までスクロールしたときに、より多くのコンテンツをロードしたいと考えています。
適切な方法を探したところ、次の 2 つが見つかりました。
onScrollChanged()
と
getScrollY()
しかし、私は自分の目的のためにそれを使用する方法がわかりません。アドバイスをお願いします。
より良い方法はを使用することだと思います.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);
}
}
}
これを試して、問題が解決するかどうかをお知らせください。
ありがとう