如何在滚动文本时在文本视图下隐藏浮动按钮

如何在滚动文本时在文本视图下隐藏浮动按钮

本文介绍了如何在滚动文本时在文本视图下隐藏浮动按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有文本视图和浮动操作按钮的android应用程序.我希望在滚动Textview时显示/隐藏按钮.我试图使用addOnScrollListner方法,但我认为它仅适用于recyclerView而不适用于Text View.你能告诉我如何以编程方式做到这一点吗?我将textview放在Scrollview中.

I am developing an android application which has text view and floating action button. I want the button to show/hide when scrolling Textview. I have tried to use addOnScrollListner method but i think its only working on recyclerView not on Text View.Can you tell how can i do it programmatically?I put my textview inside Scrollview.

感谢您的帮助!谢谢

我的活动

package com.byc3.bman202.tafachlovetwo;

/**
 * Created by hp on 6/24/2017.
 */
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;


public class abat1 extends Activity {
    RelativeLayout container;
    AnimationDrawable anim;
    TextView textView;
    FloatingActionButton floatingActionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ab1);

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                //.addTestDevice("3ACD2930EB5A1E83583AA8EA86BCAB53")
                .build();
        mAdView.loadAd(adRequest);
        View.OnClickListener handler = new View.OnClickListener() {
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.fab2:
                        shareTextUrl();
                        break;
                    //case R.id.buttonShareImage:
                    //shareImage();
                    //break;

                }
            }

        };

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab2);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareTextUrl();
            }
        });
        container = (RelativeLayout) findViewById(R.id.containerab1);

        anim = (AnimationDrawable) container.getBackground();
        anim.setEnterFadeDuration(2000);
        anim.setExitFadeDuration(3000);
        findViewById(R.id.fab2).setOnClickListener(handler);
        TextView textView = (TextView) findViewById(R.id.textView1);
        textView.addOnScrollListener(new TextView.OnScrollListener() {
            @Override
            public void onScrolled(TextView textView, int dx, int dy) {
                super.onScrolled(teView, dx, dy);

                if (dy >0) {
                    // Scroll Down
                    if (floatingActionButton.isShown()) {
                        floatingActionButton.hide();
                    }
                }
                else if (dy <0) {
                    // Scroll Up
                    if (!floatingActionButton.isShown()) {
                        floatingActionButton.show();
                    }
                }
            }
        });

    }

    private void shareTextUrl() {
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        // Add data to the intent, the receiving app will decide
        // what to do with it.
        share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
        share.putExtra(Intent.EXTRA_TEXT, getString(R.string.ab1));
        startActivity(Intent.createChooser(share, getString(R.string.shareit)));
    }

    @Override
    public void onResume() {
        super.onResume();
        if (anim != null && !anim.isRunning())
            anim.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (anim != null && anim.isRunning())
            anim.stop();
    }

}

XML布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="0dp"
    android:paddingTop="15dp"
    android:id="@+id/containerab1"
    android:background="@drawable/layout_anim"
    android:orientation="vertical"
    android:weightSum="1"
    tools:context="com.byc3.bman202.tafachlovetwo.abat1">
    <ScrollView
        android:paddingTop="10dp"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        android:layout_above="@+id/adView">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:text="@string/ab1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#f2f2f2"
        android:lineSpacingExtra="12dp"
        android:gravity="start"/>
</ScrollView>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adView"
        android:layout_marginRight="5dp"
        android:layout_marginEnd="5dp"

        android:src="@android:drawable/ic_menu_share"
        app:backgroundTint="@color/color1"

        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
       />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        ads:adSize="SMART_BANNER"
        android:layout_alignParentBottom="true"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>



</RelativeLayout>

推荐答案

尝试一下,

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mScrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                    @Override
                    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                        if (scrollY > 0 && fab2.isShown()) {
                            fab2.setVisibility(View.GONE);
                        } else if (scrollY < 0) {
                            fab2.setVisibility(View.VISIBLE);

                        }
                    }
                });
            } else {
                mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                    @Override
                    public void onScrollChanged() {
                        int mScrollY = mScrollView.getScrollY();
                        if (mScrollY > 0 && fab2.isShown()) {
                            fab2.setVisibility(View.GONE);
                        } else if (mScrollY < 0) {
                            fab2.setVisibility(View.VISIBLE);
                        }
                    }
                });
            }

这篇关于如何在滚动文本时在文本视图下隐藏浮动按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:03