Tôi nghĩ cách tốt hơn là sử dụng ListView
. Nhưng nếu bạn chỉ yêu cầu ScrollView
.
Trước tiên, bạn khắc phục chiều cao ngưỡng của ScrollView
để bất cứ khi nào bạn vượt qua giới hạn tải dữ liệu mới & nối thêm ScrollView
& đặt lại chiều cao ngưỡng của bạn.
Đây là cách bạn có thể thử ..
Tạo tùy chỉnh ScrollView
như thế này.
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);
}
}
}
& sau đó tạo ra một giao diện
public interface ScrollViewListener
{
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
& layout XML của bạn nên được như thế này
<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>
& cuối cùng sử dụng tất cả những hoạt động của bạn trong
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);
}
}
}
Hãy thử điều này & cho tôi biết, cho dù nó giải quyết vấn đề của bạn hay không.
Cảm ơn
Tại sao không sử dụng một 'ListView'? Đây là những gì nó làm – codeMagic
Tôi cần một số cấu hình lượt xem mà listview không cho phép. (Hiển thị hình ảnh như trong sở thích) – TpoM6oH
Tôi không sử dụng pinterest vì vậy tôi không biết bố trí của họ nhưng tôi đang làm việc trên một cái gì đó ngay bây giờ cho phép bạn chụp ảnh và hiển thị nó trong listview. Giống như SpongeBob nói, Bất cứ điều gì có thể với trí tưởng tượng, có tôi có con :) – codeMagic