【问题标题】:How to hide TextView and only show when scrolled如何隐藏TextView并仅在滚动时显示
【发布时间】:2014-05-15 04:35:59
【问题描述】:

我有一个 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) {
                                }
                            }
                        }
                    });

【问题讨论】:

  • 您的默认情况在哪里? of switch default: //在此处显示按钮 btn.setVisibility(View.VISIBLE);休息;
  • 查看我的开关盒。你错过了 switch 中的默认值
  • 我忘了,现在添加了

标签: android scrollview


【解决方案1】:

您可以通过使用 setOnScrollListener for GridView 来做到这一点:

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;

【讨论】:

  • 经过一番思考......这是一个很好的解决方案,但我想平滑地显示这个 TextView,而 setVisibility 使它变得粗糙并立即显示 TextView。
  • 检查此链接以确保平滑不可见和可见stackoverflow.com/questions/12953284/…
  • 你知道如何将这个 smoot 动画与滚动位置结合起来吗?以便用户控制平滑度
  • 这将强制动画我试图让它们对用户滚动敏感。还是谢谢!
【解决方案2】:

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

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

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

【讨论】:

  • 是的,这是解决方案的一部分,但如何隐藏 textView ? ScrollView 将显示 LinearLayout 的两个元素
  • 谢谢,这是个好主意。但不能接受,因为 Qadir 提供了扩展版本。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多