【问题标题】:Endless horizontal scroll view in androidandroid中无尽的水平滚动视图
【发布时间】:2015-11-30 03:19:03
【问题描述】:

我是一名新的 Android 应用程序开发人员。我想知道如何创建无尽的水平滚动视图。例如,有三个按钮(Button1、Button2 和 Button3)。当用户滚动视图时,我仍然想在 Button3 之后再次显示 Button1。您能否提供任何示例代码或任何想法?

谢谢。

【问题讨论】:

  • 捷径 - 复制内容并在特定点倒回滚动偏移。真正的方式 - 覆盖 ScrollView 的一些 dispatch* 方法以进行正确的事件处理和内容呈现。并倒带滚动偏移。 xD

标签: android android-layout


【解决方案1】:

您可以检查您的按钮视图是否仍然可见。首先检查按钮一是否可见:

private boolean isViewVisible(View view) {
    Rect scrollBounds = new Rect();
    mScrollView.getDrawingRect(scrollBounds);

    float top = view.getY();
    float bottom = top + view.getHeight();

    if (scrollBounds.top < top && scrollBounds.bottom > bottom) {
        return true;
    } else {
        return false;
    }
}

如果它不可见,则再次添加按钮一。每次用户滚动时在滚动侦听器中调用此方法,以检查按钮是否不可见。如果按钮不可见,请重新添加。

如果你想让它自己无穷无尽,试试这个:

public class Test extends ListActivity implements OnScrollListener {

    Aleph0 adapter = new Aleph0();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(adapter); 
        getListView().setOnScrollListener(this);
    }

    public void onScroll(AbsListView view,
        int firstVisible, int visibleCount, int totalCount) {

        boolean loadMore = /* maybe add a padding */
            firstVisible + visibleCount >= totalCount;

        if(loadMore) {
            adapter.count += visibleCount; // or any other amount
            adapter.notifyDataSetChanged();
        }
    }

    public void onScrollStateChanged(AbsListView v, int s) { }    

    class Aleph0 extends BaseAdapter {
        int count = 40; /* starting amount */

        public int getCount() { return count; }
        public Object getItem(int pos) { return pos; }
        public long getItemId(int pos) { return pos; }

        public View getView(int pos, View v, ViewGroup p) {
                TextView view = new TextView(Test.this);
                view.setText("entry " + pos);
                return view;
        }
    }
}

看看这个:

Android Endless List

android:how to make infinite scrollview with endless scrolling

就是这样。只需查看用户滚动时视图何时超出范围即可。

当它不在视线范围内时,只需重新添加它。

【讨论】:

  • 您好 Ruchir,谢谢您的回答。我的问题是关于无尽的水平滚动视图。您对无尽列表视图的回答。有没有办法在滚动时动态设置按钮位置?
  • 我喜欢 Aleph0 的名字 :-)
猜你喜欢
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多