android:Gridview按钮点击显示更多项目

时间:2016-10-20 11:38:49

标签: android gridview paging

我想创建一个这样的网格视图。 (对不起,它有点凌乱,绘图不好:D) sorry it's a bit messy 所以基本上gridview将显示第1至第4项。如果我点击下一步,第5至第8项将显示..依此类推。

我目前的想法是设置一个包含4个项目的gridview,并在每次单击按钮时更改适配器。但我觉得这不太方便。另一个想法是使用'paging'..

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以使用包含线性布局的水平滚动视图,并动态地在linearlayout中添加griditem(视图)。 如果您仍然需要这个想法的帮助,请告诉我们!

答案 1 :(得分:0)

To achieve this, use RecyclerView with horizontal LinearLayoutManager.. However, user will be able to scroll through the items as well. To disable scrolling on a RecyclerView, you can subclass it and override onTouchEvent() method, which will disable any reaction to MOVE event:

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch(event.getAction()){

        case MotionEvent.ACTION_DOWN:
            return super.onTouchEvent(event);

        case MotionEvent.ACTION_MOVE:
            break; //Don't call super.

        case MotionEvent.ACTION_UP:
            return super.onTouchEvent(event);
    }
    return false;
}

When the user clicks the button to move to next page, you can use one of the following methods on the LinearLayoutManager object to find the current position within the adapter:

int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();

When you have determined the new position, you can scroll to new data with either scrollToPosition() or smoothScrollToPosition() methods..