2

我有一个 ScrollView 和 GridView,我想在 GridView 上放置一个 TextView,但在用户滚动到它之前不能看到它。我考虑过负边距,但我找不到解决方案如何在负边距上使用滚动来获取此视图。

所以基本上:唯一看到的东西应该是gridView,但是当用户在gridView顶部并拉起时,他应该看到一个TextView。

编辑:这一切的重点是我想对此进行拉动刷新。我不想在互联网库上使用流行的,因为它不能按我想要的方式工作。

编辑2:

我得到了答案,但这并不完全是我试图实现的目标。我想平滑地显示隐藏的 TextView(就像在拉动刷新解决方案中一样)并且 setVisibility 使它快速且没有任何平滑。这是我的代码:

XML:

<com.tas.android.quick.ui.controls.LockableScrollView
        android:id="@+id/scrollView"
        android:fillViewport="true"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/pullText"
                android:text="some text" />

            <com.tas.android.quick.ui.controls.ExpandableGridView
                android:id="@+id/gridview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:clipToPadding="false"
                android:drawSelectorOnTop="true"
                android:fadingEdge="none"
                android:gravity="top"
                android:horizontalSpacing="@dimen/image_grid_hspacing"
                android:listSelector="@drawable/selector_shadow"
                android:numColumns="@integer/grid_num_cols"
                android:paddingBottom="50dp"
                android:scrollbarAlwaysDrawVerticalTrack="false"
                android:scrollbars="none"
                android:scrollingCache="true"
                android:smoothScrollbar="false"
                android:stretchMode="columnWidth"
                android:verticalSpacing="@dimen/image_grid_vspacing"
                android:visibility="visible" />
        </LinearLayout>
    </com.tas.android.quick.ui.controls.LockableScrollView>

和代码:

textview = (TextView) findViewById(R.id.pullText);
        textview.setVisibility(View.VISIBLE);

        scrollView = (LockableScrollView) findViewById(R.id.scrollView);

        scrollView.setScrollingEnabled(false);

...

gridView.setOnScrollListener(new OnScrollListener() {
                        @Override
                        public void onScrollStateChanged(AbsListView view, int scrollState) {
                            switch(scrollState) {
                            case 2: // SCROLL_STATE_FLING 
                                //hide button here
                                textview.setVisibility(View.VISIBLE);
                                break;

                            case 1: // SCROLL_STATE_TOUCH_SCROLL 
                                //hide button here
                                textview.setVisibility(View.GONE);
                                break;

                            case 0: // SCROLL_STATE_IDLE 
                                //show button here
                                textview.setVisibility(View.GONE);
                                break;
                            }
                        }

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

3 回答 3

11

您可以通过使用 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
    yourTextView.setVisibility(View.GONE);
    break;

    case 1: // SCROLL_STATE_TOUCH_SCROLL 
    //hide button here
    yourTextView.setVisibility(View.GONE);
    break;

    case 0: // SCROLL_STATE_IDLE 
    //show button here
    yourTextView.setVisibility(View.VISIBLE);
    break;

        default:
     //show button here
    btn.setVisibility(View.VISIBLE);
    break;
       }
    }
 });

编辑 1

为了光滑,试试这个(未经测试)

 switch(scrollState) {
case 2: // SCROLL_STATE_FLING 

Animation animation = new TranslateAnimation(0,0,0,1000);
animation.setDuration(1000);
mytextview.startAnimation(animation);
mytextview.setVisibility(View.GONE);

break;

case 1: // SCROLL_STATE_TOUCH_SCROLL 
//hide button here
Animation animation = new TranslateAnimation(0,0,0,1000);
animation.setDuration(1000);
mytextview.startAnimation(animation);
yourTextView.setVisibility(View.GONE);
break;

case 0: // SCROLL_STATE_IDLE 
//show button here
Animation animation = new TranslateAnimation(0,0,0,1000);
animation.setDuration(1000);
mytextview.startAnimation(animation);
yourTextView.setVisibility(View.VISIBLE);
break;

    default:
 //show button here
Animation animation = new TranslateAnimation(0,0,0,1000);
animation.setDuration(1000);
mytextview.startAnimation(animation);
yourTextView.setVisibility(View.VISIBLE);
break;
于 2013-09-06T12:17:25.513 回答
9

将 GridView 和 TextView 都放在 LinearLayout(或 RelativeLayout)中

然后让 ScrollView 只包含您的线性/相对布局。

要隐藏 textview,请使用findViewById(R.id.textviewId).setVisibility(View.GONE);(使用 View.VISIBLE 将其再次设置为可见)。

于 2013-09-06T12:06:12.097 回答
0

xml中有一个可见性选项。单击 TextView,然后将其更改为 Invisible 或 Gone。

于 2014-05-14T20:35:59.770 回答