2017-06-29 1 views
-4

Ich entwickle eine Android-Anwendung, die Textansicht und Floating-Aktion-Schaltfläche hat. Ich möchte, dass der Button beim Scrollen von Textview ein-/ausgeblendet wird. Ich habe versucht, addOnScrollListner-Methode zu verwenden, aber ich denke, dass es nur auf RecyclerView nicht in der Textansicht funktioniert. Können Sie sagen, wie kann ich es programmgesteuert machen? Ich habe meine Textansicht in Scrollview eingefügt.Wie man eine schwebende Schaltfläche unter Textansicht beim Scrollen der Texte ausblenden

Ich schätze Ihre Hilfe! Danke

Meine Aktivität

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-Layout

<?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> 

Antwort

0

die Sie interessieren,

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

Thank you! Das ist nah an meiner Frage Antwort. Wenn ich nach oben scrolle, ist die Schaltfläche versteckt (das ist, was ich brauche), das Problem ist, dass, wenn ich nach unten scrolle, die Schaltfläche verschwunden ist. Wie kann man es sichtbar machen? –

+0

Es funktioniert gut in Marshmallow, welches Gerät ur testen –

+0

Ich teste auf Lollipop –

Verwandte Themen