2013-02-08 25 views
5

Tôi đã có một scrollView với tải nội dung một cách ngoạn mục. Đôi khi có thể có nhiều nội dung, vì vậy tôi muốn tải thêm khi người dùng cuộn xuống dưới.Tải thêm dữ liệu khi ScrollView được cuộn xuống dưới cùng

tôi searced cho metods phù hợp và tìm thấy hai:

onScrollChanged() 

getScrollY() 

Nhưng tôi không biết làm thế nào để sử dụng nó cho mục đích của tôi. Xin vui lòng cho tôi một số lời khuyên.

+3

Tại sao không sử dụng một 'ListView'? Đây là những gì nó làm – codeMagic

+0

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

+0

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

Trả lời

6

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

11

xem Scroll không cung cấp bất kỳ phương pháp để kiểm tra xem bạn đã đạt đến đáy của cái nhìn rất kỹ thuật tốt nhất là để mở rộng giao diện tùy chỉnh của riêng bạn với xem di chuyển. Đây là cách tôi triển khai trong ứng dụng của mình.

Bước 1: Tạo một lớp tùy chỉnh để xem cuộn

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); 
} 

}

Bước 2: Tạo một cái nhìn cuộn Listener Interface

public interface ScrollViewListener { 

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



} 

Bước 3: Thêm chế độ xem trong bố cục của bạn

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

Bước 4: Thực hiện giao diện trong lớp học của bạn

public class MyFragment extends Fragment implements ScrollViewListener 

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

      //Perform your action 
} 

Và bạn đã làm xong

+0

không thể nhận được diff 0 nhận được diff trực tiếp ít hơn 0 –

Các vấn đề liên quan