├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── gaoneng │ │ └── autoscrollbacklayout │ │ ├── GridViewDemoActivity.java │ │ ├── ListViewDemoActivity.java │ │ ├── MainActivity.java │ │ └── RecyclerViewDemoActivity.java │ └── res │ ├── drawable │ └── bg_btn_shape.xml │ ├── layout │ ├── activity_grid_view_demo.xml │ ├── activity_listview_layout.xml │ ├── activity_recycler_view_demo.xml │ └── main_layout.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── autoscrollbacklayout ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── gaoneng │ │ └── library │ │ ├── AutoScrollBackLayout.java │ │ ├── DensityUtils.java │ │ └── ScrollUtil.java │ └── res │ ├── anim │ ├── fab_scale_down.xml │ └── fab_scale_up.xml │ ├── drawable-hdpi │ └── go_top.png │ ├── drawable-xhdpi │ └── go_top.png │ ├── drawable-xxhdpi │ └── go_top.png │ ├── drawable-xxxhdpi │ └── go_top.png │ └── values │ ├── attrs.xml │ └── strings.xml ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── preview.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | gradlew 8 | gradlew.bat 9 | gradle.properties 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AutoScrollBackLayout 2 | 在ListView,GridView,RecyclerView列表滚动向底部一段距离,就自动显示一个返回顶部的按钮 3 | 4 | ## 效果 5 | ![](https://github.com/gaoneng102/AutoScrollBackLayout/blob/master/preview.gif) 6 | 7 | ## 使用 8 | 1、添加依赖: 9 | ``` 10 | compile ('com.gaoneng.library:autoscrollbacklayout:1.1.1'){ 11 | exclude group: 'com.android.support', module: 'recyclerview-v7' 12 | } 13 | ``` 14 | 2、通过xml文件添加如下: 15 | ``` 16 | 17 | 26 | 27 | 33 | 34 | ``` 35 | 3、调用bindScrollBack(): 36 | ``` 37 | AutoScrollBackLayout autoScrollBackLayout = (AutoScrollBackLayout) findViewById(R.id.scroll_layout); 38 | ListView listView = (ListView) findViewById(android.R.id.list); 39 | List list = new ArrayList<>(); 40 | for (int i = 0; i < 20; i++) { 41 | list.add("this is a test! in " + i); 42 | } 43 | listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list)); 44 | autoScrollBackLayout.bindScrollBack(); 45 | ``` 46 | ## 原理 47 | - 针对ListView和GridView,通过反射和动态代理的方式监听OnScrollListener,这样就不会影响已有的OnScrollListener的正常运行。 48 | **但是这里需要注意的是,如果已经使用`AbsListView.setOnScrollListener()`设置过监听, 49 | 一定要在其后面调用`autoScrollBackLayout.bindScrollBack()`** 50 | ``` 51 | private void hookScrollListenerForListview() { 52 | try { 53 | //通过反射获取mOnScrollListener对象 54 | Field scrollListenerField = AbsListView.class.getDeclaredField("mOnScrollListener"); 55 | scrollListenerField.setAccessible(true); 56 | Object object = scrollListenerField.get(wrapView); 57 | //需要被代理的目前对象 58 | AbsListView.OnScrollListener target; 59 | if (object == null) { 60 | //如果mOnScrollListener没有设置过,就设置一个空的用来hook 61 | target = new FakeScrollLitener(); 62 | } else { 63 | target = (AbsListView.OnScrollListener) object; 64 | } 65 | //InvocationHandler对象,用于添加额外的控制处理 66 | ScrollListenerInvocationHandler listenerInvocationHandler = new ScrollListenerInvocationHandler(target); 67 | //Proxy.newProxyInstance生成动态代理对象 68 | AbsListView.OnScrollListener proxy = listenerInvocationHandler.getProxy(); 69 | if (DEBUG) { 70 | Log.i(TAG, "target=" + target.getClass().getName() + " ,proxy=" + proxy.getClass().getName() + ", proxied interfaces=" + Arrays.toString(proxy.getClass().getInterfaces())); 71 | } 72 | //将代理对象proxy设置到被反射的mOnScrollListener的字段中 73 | scrollListenerField.set(wrapView, proxy); 74 | } catch (Exception e) { 75 | Log.e(TAG, e.toString()); 76 | } 77 | } 78 | ``` 79 | - 针对RecyclerView,因为其内部的监听已经是`List`形式,所以直接`addOnScrollListener()`方式添加即可; 80 | ``` 81 | private void addScrollListenerForRecyclerView() { 82 | ((RecyclerView) wrapView).addOnScrollListener(new RecyclerView.OnScrollListener() { 83 | @Override 84 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 85 | if (myScrollLitener != null) 86 | myScrollLitener.onScrollStateChanged(recyclerView, newState); 87 | } 88 | 89 | @Override 90 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 91 | if (myScrollLitener != null) myScrollLitener.onScroll(recyclerView, dy, 0, 0); 92 | } 93 | }); 94 | } 95 | ``` 96 | 97 | ## 其他属性 98 | attrs | value 99 | ------------ | ------------- 100 | app:show_scroll | true/false 是否自动显示返回按钮,默认true 101 | app:scroll_distance | dp 触发显示返回按钮的滚动距离,默认100dp 102 | app:show_animation | anim 按钮出现动画,默认 R.anim.fab_scale_up 103 | app:hide_animation | anim 按钮出现动画,默认 R.anim.fab_scale_down 104 | app:auto_arrow_icon | drawable 按钮图标,默认 R.drawable.go_top 105 | app:scroll_gravity | Gravity 按钮的位置,默认 Gravity.BOTTOM and Gravity.CENTER_HORIZONTAL 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.0" 6 | defaultConfig { 7 | applicationId "com.gaoneng.autoscrollbacklayout" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(dir: 'libs', include: ['*.jar']) 23 | compile project(':autoscrollbacklayout') 24 | compile 'com.android.support:appcompat-v7:25.3.1' 25 | } 26 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/gaoneng/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 25 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/gaoneng/autoscrollbacklayout/GridViewDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.gaoneng.autoscrollbacklayout; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.widget.ArrayAdapter; 8 | import android.widget.GridView; 9 | 10 | import com.gaoneng.library.AutoScrollBackLayout; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | public class GridViewDemoActivity extends AppCompatActivity { 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_grid_view_demo); 21 | AutoScrollBackLayout autoScrollBackLayout = (AutoScrollBackLayout) findViewById(R.id.scroll_layout); 22 | GridView gridView = (GridView) findViewById(R.id.gridview); 23 | List list = new ArrayList<>(); 24 | for (int i = 0; i < 100; i++) { 25 | list.add("this is a test! in " + i); 26 | } 27 | gridView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list)); 28 | autoScrollBackLayout.bindScrollBack(); 29 | } 30 | 31 | public static void start(Context context) { 32 | Intent starter = new Intent(context, GridViewDemoActivity.class); 33 | context.startActivity(starter); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/gaoneng/autoscrollbacklayout/ListViewDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.gaoneng.autoscrollbacklayout; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.widget.ArrayAdapter; 8 | import android.widget.ListView; 9 | 10 | import com.gaoneng.library.AutoScrollBackLayout; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | public class ListViewDemoActivity extends AppCompatActivity { 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_listview_layout); 21 | AutoScrollBackLayout autoScrollBackLayout = (AutoScrollBackLayout) findViewById(R.id.scroll_layout); 22 | ListView listView = (ListView) findViewById(android.R.id.list); 23 | List list = new ArrayList<>(); 24 | for (int i = 0; i < 20; i++) { 25 | list.add("this is a test! in " + i); 26 | } 27 | listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list)); 28 | autoScrollBackLayout.bindScrollBack(); 29 | } 30 | 31 | public static void start(Context context) { 32 | Intent starter = new Intent(context, ListViewDemoActivity.class); 33 | context.startActivity(starter); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/gaoneng/autoscrollbacklayout/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.gaoneng.autoscrollbacklayout; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.View; 6 | 7 | public class MainActivity extends AppCompatActivity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | setContentView(R.layout.main_layout); 13 | findViewById(R.id.btn_1).setOnClickListener(new View.OnClickListener() { 14 | @Override 15 | public void onClick(View v) { 16 | ListViewDemoActivity.start(MainActivity.this); 17 | } 18 | }); 19 | findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() { 20 | @Override 21 | public void onClick(View v) { 22 | GridViewDemoActivity.start(MainActivity.this); 23 | } 24 | }); 25 | findViewById(R.id.btn_3).setOnClickListener(new View.OnClickListener() { 26 | @Override 27 | public void onClick(View v) { 28 | RecyclerViewDemoActivity.start(MainActivity.this); 29 | } 30 | }); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/gaoneng/autoscrollbacklayout/RecyclerViewDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.gaoneng.autoscrollbacklayout; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.support.v7.widget.DividerItemDecoration; 8 | import android.support.v7.widget.LinearLayoutManager; 9 | import android.support.v7.widget.RecyclerView; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | import android.widget.TextView; 14 | 15 | import com.gaoneng.library.AutoScrollBackLayout; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | public class RecyclerViewDemoActivity extends AppCompatActivity { 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_recycler_view_demo); 26 | AutoScrollBackLayout autoScrollBackLayout = (AutoScrollBackLayout) findViewById(R.id.scroll_layout); 27 | RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recylcerview); 28 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 29 | recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); 30 | List list = new ArrayList<>(); 31 | for (int i = 0; i < 20; i++) { 32 | list.add("this is a test! in " + i); 33 | } 34 | autoScrollBackLayout.bindScrollBack(); 35 | recyclerView.setAdapter(new MyAdapter(this, list)); 36 | } 37 | 38 | public static void start(Context context) { 39 | Intent starter = new Intent(context, RecyclerViewDemoActivity.class); 40 | context.startActivity(starter); 41 | } 42 | 43 | private class MyAdapter extends RecyclerView.Adapter { 44 | private Context context; 45 | private List list; 46 | 47 | public MyAdapter(Context context, List list) { 48 | this.context = context; 49 | this.list = list; 50 | } 51 | 52 | @Override 53 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 54 | return new ViewHolder(LayoutInflater.from(RecyclerViewDemoActivity.this).inflate(android.R.layout.simple_list_item_1, null)); 55 | } 56 | 57 | @Override 58 | public void onBindViewHolder(ViewHolder holder, int position) { 59 | holder.bind(list.get(position)); 60 | } 61 | 62 | @Override 63 | public int getItemCount() { 64 | return list.size(); 65 | } 66 | } 67 | 68 | private class ViewHolder extends RecyclerView.ViewHolder { 69 | TextView textView; 70 | 71 | public ViewHolder(View itemView) { 72 | super(itemView); 73 | textView = (TextView) itemView.findViewById(android.R.id.text1); 74 | } 75 | 76 | public void bind(String s) { 77 | textView.setText(s); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_btn_shape.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_grid_view_demo.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_listview_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_recycler_view_demo.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/main_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 |