Tpo*_*6oH 6 android android-scrollview
我有一个带有dinamically加载内容的scrollView.有时可能会有很多内容,所以我想在用户滚动到底部时加载更多内容.
我找到了合适的方法,发现了两个:
onScrollChanged()
Run Code Online (Sandbox Code Playgroud)
和
getScrollY()
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将它用于我的目的.请给我一些建议.
Jan*_*Jan 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);
}
Run Code Online (Sandbox Code Playgroud)
}
第2步:创建滚动视图侦听器接口
public interface ScrollViewListener {
void onScrollEnded(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
Run Code Online (Sandbox Code Playgroud)
第3步:在布局中添加视图
<com.platinumapps.facedroid.ObservableScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</com.platinumapps.facedroid.ObservableScrollView>
Run Code Online (Sandbox Code Playgroud)
第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
}
Run Code Online (Sandbox Code Playgroud)
你完成了
我认为更好的方法是使用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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后创建一个界面
public interface ScrollViewListener
{
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
Run Code Online (Sandbox Code Playgroud)
你的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>
Run Code Online (Sandbox Code Playgroud)
最后在您的活动中使用所有这些
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
试试这个并告诉我,它是否能解决您的问题.
谢谢
| 归档时间: |
|
| 查看次数: |
11448 次 |
| 最近记录: |