简体   繁体   English

如何在Android中的gridView的滚动侦听器上使按钮可见/消失

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

I have a Custom gridView and a Button in Frame Layout. 我在框架布局中有一个Custom gridView和一个按钮。 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. 现在我想要的是,当到达GridView Scroll的最终位置时,按钮应该出现,否则应该再次消失。 as shown in figure. 如图所示。

在此处输入图片说明

you can do this by using setOnScrollListener for GridView as: 您可以通过使用GridView的setOnScrollListener来做到这一点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM