How to make a Button Visible/Gone on scroll listener of gridView in Android

天涯浪子 提交于 2019-12-22 11:47:09

问题


I have a Custom gridView and a Button in Frame Layout. Code is following

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
     <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/Rel_Spinner"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth" >
    </GridView>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >


        <Button
            android:id="@+id/btnLoadMore"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Load More" />

    </FrameLayout>

</RelativeLayout>

It loads images and Text from adapter. as shown in figure

Now I want is that the Button should appear when the final position of the GridView Scroll is reached other wise it should again disappear. as shown in figure.


回答1:


you can do this by using setOnScrollListener for GridView as:

gridview.setOnScrollListener(new OnScrollListener() {

   @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
              int visibleItemCount, int totalItemCount) {

      }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
      switch(scrollState) {
        case 2: // SCROLL_STATE_FLING 
        //hide button here
        break;

        case 1: // SCROLL_STATE_TOUCH_SCROLL 
        //hide button here
        break;

        case 0: // SCROLL_STATE_IDLE 
        //show button here
        break;

            default:
             //show button here
        break;
           }
        }
     });


来源:https://stackoverflow.com/questions/13965720/how-to-make-a-button-visible-gone-on-scroll-listener-of-gridview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!