├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── felipecsl │ │ └── quickreturn │ │ └── app │ │ └── MainActivity.java │ └── res │ ├── color │ └── sticky_item_text.xml │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ ├── drawable │ └── sticky_item_background.xml │ ├── layout │ ├── action_bar_spinner_text.xml │ ├── activity_main.xml │ ├── activity_main_grid.xml │ ├── activity_main_scrollview.xml │ └── list_item.xml │ ├── menu │ └── main.xml │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── gradle-mvn-push.gradle ├── gradle.properties ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── felipecsl │ └── quickreturn │ └── library │ ├── AbsListViewQuickReturnAttacher.java │ ├── CompositeAbsListViewOnScrollListener.java │ ├── CompositeOnScrollListener.java │ ├── QuickReturnAttacher.java │ ├── QuickReturnStateTransition.java │ ├── ScrollViewQuickReturnAttacher.java │ ├── SimpleAnimationListener.java │ └── widget │ ├── AbsListViewScrollTarget.java │ ├── ObservableScrollView.java │ ├── QuickReturnAdapter.java │ ├── QuickReturnTargetView.java │ └── ScrollViewScrollTarget.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | local.properties 3 | /.idea/workspace.xml 4 | .DS_Store 5 | .idea 6 | *.iml 7 | *.keystore 8 | build -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Version 1.5.0 2 | 3 | * Fixed onItemLongClick position offset issue 4 | 5 | # Version 1.4.0 6 | 7 | * Changed QuickReturnAttacher interface. ``QuickReturnAttacher.forView()`` should be used now. 8 | * Implemented preliminar support for ``ScrollView``. ``ObservableScrollView`` should be used. 9 | * Fixed jumping quick return view when listView had a positive divider height. 10 | * Fixed incorrect position for ``AdapterView.OnItemClickListener`` ``onItemClick()`` callback. 11 | * Fixed jump on scroll issue. 12 | 13 | # Version 1.3.2 14 | 15 | * Fixes bug with incorrectly calculated ``QuickReturnAdapter.getViewTypeCount()`` 16 | 17 | # Version 1.3.1 18 | 19 | * Prevents overlap of listView elements with targetView: Automatically adjusts the ListView/GridView to prevent it from being hidden behind the target view, when it is placed at the top of the list.\ 20 | 21 | # Version 1.3.0 22 | 23 | * ``QuickReturnAttacher`` API has changed. You can now assign multiple QuickReturn targets using ``QuickReturnAttacher.addTargetView()``. 24 | * Fixes weird transition when using a GridView. 25 | * Minor performance optimizations. 26 | 27 | # Version 1.2.4 28 | 29 | * Fixes quickreturn view jumping sometimes when scrolling the list 30 | 31 | # Version 1.2.3 32 | 33 | * Fixes messed up package names 34 | 35 | # Version 1.2.2 36 | 37 | * Fixes incorrect quick return view position when listview adapter has not enough items to fill the screen. 38 | * QuickReturnAttacher constructor now takes AbsListView instead of ListView, so you can use any concrete implementation with it. 39 | 40 | # Version 1.2.1 41 | 42 | * Allows the listView adapter to be an instance of WrapperListAdapter. 43 | 44 | # Version 1.2.0 45 | 46 | * Fixes problem with using ``ListView.setOnScrollListener()`` outside of ``QuickReturnAttacher`` would cause QuickReturn to stop working. Scroll listeners should be added via ``QuickReturnAttacher.addOnScrollListener()``. 47 | * Prevents crash in ``QuickReturnAttacher.setAnimatedTransition(true)`` on Gingerbread. Ignores call and logs warning instead. 48 | * Prevents crash with empty adapter in ``QuickReturnAdapter.getMaxVerticalOffset()`` 49 | 50 | # Version 1.1 51 | 52 | * Added new ``QuickReturnAttacher`` and removed ``QuickReturnListView``. This way, users are not required to subclass a custom listView in order to user the lib. 53 | * Added position argument to ``QuickReturnAttacher``, so now you can choose whether to use top or bottom quick return. **Note: Don't forget to set the view's gravity correctly according to the position chosen!** 54 | 55 | # Version 1.0 56 | 57 | Initial release. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Felipe Lima 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QuickReturn 2 | 3 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-felipecsl%2FQuickReturn-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/697) 4 | 5 | Android ListView that implements the QuickReturn UI pattern. Written from scratch with focus on performance. 6 | 7 | ### Demo 8 | 9 | [![video thumbnail](http://img.youtube.com/vi/BwLjMMIWNQU/hqdefault.jpg)](https://www.youtube.com/watch?v=BwLjMMIWNQU) 10 | 11 | ### Usage 12 | 13 | In your ``build.gradle`` file: 14 | 15 | ```groovy 16 | dependencies { 17 | // ... 18 | compile 'com.felipecsl.quickreturn:library:1.5.1' 19 | } 20 | ``` 21 | 22 | In your activity class: 23 | 24 | ```java 25 | private ListView listView; 26 | private ArrayAdapter adapter; 27 | private QuickReturnAttacher quickReturnAttacher; 28 | private TextView quickReturnTarget; 29 | 30 | @Override 31 | protected void onCreate(Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | 34 | setContentView(R.layout.activity_main); 35 | 36 | // your listView :) 37 | listView = (ListView) findViewById(R.id.listView); 38 | 39 | // the quick return target view to be hidden/displayed 40 | quickReturnTarget = (TextView) findViewById(R.id.quickReturnTarget); 41 | 42 | // your inner adapter 43 | adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1); 44 | 45 | // Wrap your adapter with QuickReturnAdapter 46 | listView.setAdapter(new QuickReturnAdapter(adapter)); 47 | 48 | // Attach a QuickReturnAttacher, which takes care of all of the hide/show functionality. 49 | quickReturnAttacher = QuickReturnAttacher.forView(listView); 50 | 51 | // Add a quick return targetView to the attacher. 52 | // You can pass a position argument (POSITION_TOP or POSITION_BOTTOM). 53 | // You can also optionally pass the size of the target view, which will be used to 54 | // offset the list height, preventing it from hiding content behind the target view. 55 | quickReturnAttacher.addTargetView(quickReturnTarget, QuickReturnTargetView.POSITION_TOP, 50); 56 | 57 | // If you need to add an OnScrollListener to the listView, this is the correct 58 | // way to do so. 59 | // You have to add it on the QuickReturnAttacher instead 60 | // of adding on the listView directly. 61 | quickReturnAttacher.addOnScrollListener(this); 62 | } 63 | ``` 64 | 65 | Check the [sample app](https://github.com/felipecsl/QuickReturn/blob/master/app/src/main/java/com/felipecsl/quickreturn/app/MainActivity.java) for an example of usage. 66 | 67 | ### Features 68 | 69 | * Supports dynamic adapters. That means you can add and remove items from your adapter and it will still work nicely. 70 | * You don't have to subclass ``QuickReturnAdapter`` in order to use it. Just pass your own adapter to the constructor and you're done. 71 | * Animated transitions via ``QuickReturnAttacher.setAnimatedTransition(true)`` 72 | * Supports bottom (footer) quick return position via ``QuickReturnAttacher.setPosition(QuickReturnListView.POSITION_BOTTOM).`` 73 | * You can use it with any subclass of ``AbsListView``, including ``ListView`` and ``GridView``. 74 | * If you're using a ``GridView``, you have to tell ``QuickReturnAdapter`` how many columns it has, via its constructor: 75 | * Automatically adjusts the ListView/GridView to prevent it from being hidden behind the target view, when it is placed at the top of the list. 76 | * Supports ScrollView as well! 77 | 78 | ```java 79 | public QuickReturnAdapter(final ListAdapter wrappedAdapter, final int numColumns) 80 | ``` 81 | 82 | 83 | Works with API Level 10 and above. 84 | 85 | ### Known issues/Caveats 86 | 87 | * The Animated Transition will sometimes hide/show randomly when scrolling down very slowly. 88 | 89 | ### Changelog 90 | 91 | Please see the [Changelog](https://github.com/felipecsl/QuickReturn/blob/master/CHANGELOG.md) to check what's recently changed. 92 | 93 | ### Credits 94 | 95 | Heavily inspired/influenced by the [nice work of Roman Nurik's and Nick Butcher's](https://plus.google.com/+RomanNurik/posts/1Sb549FvpJt) and Lars Werkman's [QuickReturnListView](https://github.com/LarsWerkman/QuickReturnListView) 96 | 97 | ### Contributing 98 | 99 | * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet 100 | * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it 101 | * Fork the project 102 | * Start a feature/bugfix branch 103 | * Commit and push until you are happy with your contribution 104 | * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. 105 | 106 | ### Copyright and license 107 | 108 | Code and documentation copyright 2011-2014 Felipe Lima. 109 | Code released under the [MIT license](https://github.com/felipecsl/QuickReturn/blob/master/LICENSE.txt). 110 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "22.0.0" 6 | 7 | defaultConfig { 8 | minSdkVersion 10 9 | targetSdkVersion 22 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | compileOptions { 14 | sourceCompatibility JavaVersion.VERSION_1_7 15 | targetCompatibility JavaVersion.VERSION_1_7 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(dir: 'libs', include: ['*.jar']) 27 | compile 'com.android.support:appcompat-v7:22.0.0' 28 | compile 'com.android.support:support-v4:22.0.0' 29 | compile project(path: ':library') 30 | } 31 | -------------------------------------------------------------------------------- /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 /Applications/Android Studio.app/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 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/felipecsl/quickreturn/app/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.app; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.support.annotation.NonNull; 6 | import android.support.v7.app.ActionBar; 7 | import android.support.v7.app.ActionBarActivity; 8 | import android.view.Menu; 9 | import android.view.MenuItem; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.AbsListView; 13 | import android.widget.AdapterView; 14 | import android.widget.ArrayAdapter; 15 | import android.widget.GridView; 16 | import android.widget.SpinnerAdapter; 17 | import android.widget.TextView; 18 | import android.widget.Toast; 19 | 20 | import com.felipecsl.quickreturn.library.AbsListViewQuickReturnAttacher; 21 | import com.felipecsl.quickreturn.library.QuickReturnAttacher; 22 | import com.felipecsl.quickreturn.library.widget.AbsListViewScrollTarget; 23 | import com.felipecsl.quickreturn.library.widget.QuickReturnAdapter; 24 | import com.felipecsl.quickreturn.library.widget.QuickReturnTargetView; 25 | 26 | public class MainActivity extends ActionBarActivity implements AbsListView.OnScrollListener, 27 | ActionBar.OnNavigationListener, View.OnClickListener, AdapterView.OnItemClickListener, 28 | AdapterView.OnItemLongClickListener { 29 | 30 | private ArrayAdapter adapter; 31 | private int offset; 32 | private QuickReturnTargetView topTargetView; 33 | private TextView topTextView; 34 | private TextView bottomTextView; 35 | private int currentPos; 36 | 37 | @Override protected void onCreate(Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | 40 | ActionBar actionBar = getSupportActionBar(); 41 | actionBar.setTitle(""); 42 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); 43 | String[] actionBarItems = { "ListView", "GridView", "ScrollView" }; 44 | SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(this, R.layout.action_bar_spinner_text, 45 | actionBarItems); 46 | 47 | actionBar.setListNavigationCallbacks(spinnerAdapter, this); 48 | 49 | initialize(R.layout.activity_main); 50 | } 51 | 52 | private void initialize(int layoutId) { 53 | setContentView(layoutId); 54 | offset = 0; 55 | ViewGroup viewGroup = (ViewGroup) findViewById(R.id.listView); 56 | topTextView = (TextView) findViewById(R.id.quickReturnTopTarget); 57 | bottomTextView = (TextView) findViewById(R.id.quickReturnBottomTarget); 58 | 59 | adapter = new ArrayAdapter<>(this, R.layout.list_item); 60 | addMoreItems(100); 61 | 62 | if (viewGroup instanceof AbsListView) { 63 | int numColumns = (viewGroup instanceof GridView) ? 3 : 1; 64 | AbsListView absListView = (AbsListView) viewGroup; 65 | absListView.setAdapter(new QuickReturnAdapter(adapter, numColumns)); 66 | } 67 | 68 | QuickReturnAttacher quickReturnAttacher = QuickReturnAttacher.forView(viewGroup); 69 | quickReturnAttacher.addTargetView(bottomTextView, AbsListViewScrollTarget.POSITION_BOTTOM); 70 | topTargetView = quickReturnAttacher.addTargetView(topTextView, 71 | AbsListViewScrollTarget.POSITION_TOP, 72 | dpToPx(this, 50)); 73 | 74 | if (quickReturnAttacher instanceof AbsListViewQuickReturnAttacher) { 75 | // This is the correct way to register an OnScrollListener. 76 | // You have to add it on the QuickReturnAttacher, instead 77 | // of on the viewGroup directly. 78 | AbsListViewQuickReturnAttacher 79 | attacher = 80 | (AbsListViewQuickReturnAttacher) quickReturnAttacher; 81 | attacher.addOnScrollListener(this); 82 | attacher.setOnItemClickListener(this); 83 | attacher.setOnItemLongClickListener(this); 84 | } 85 | } 86 | 87 | public static int dpToPx(Context context, float dp) { 88 | // Took from http://stackoverflow.com/questions/8309354/formula-px-to-dp-dp-to-px-android 89 | float scale = context.getResources().getDisplayMetrics().density; 90 | return (int) ((dp * scale) + 0.5f); 91 | } 92 | 93 | private void addMoreItems(int amount) { 94 | for (int i = 0; i < amount; i++) { 95 | adapter.add("Item " + String.valueOf(offset + i)); 96 | } 97 | 98 | offset += amount; 99 | } 100 | 101 | @Override public boolean onCreateOptionsMenu(Menu menu) { 102 | getMenuInflater().inflate(R.menu.main, menu); 103 | return true; 104 | } 105 | 106 | @Override public boolean onOptionsItemSelected(MenuItem item) { 107 | int id = item.getItemId(); 108 | if (id == R.id.add_more) { 109 | addMoreItems(10); 110 | } else if (id == R.id.add_a_lot_more) { 111 | addMoreItems(100); 112 | } else if (id == R.id.reset) { 113 | reset(); 114 | } else if (id == R.id.animated) { 115 | topTargetView.setAnimatedTransition(!topTargetView.isAnimatedTransition()); 116 | item.setTitle(topTargetView.isAnimatedTransition() ? "Disable animated transition" 117 | : "Enable animated transition"); 118 | reset(); 119 | } else if (id == R.id.bottom_view) { 120 | bottomTextView.setVisibility(bottomTextView.getVisibility() == View.GONE 121 | ? View.VISIBLE : View.GONE); 122 | } else if (id == R.id.top_view) { 123 | topTextView.setVisibility(topTextView.getVisibility() == View.GONE 124 | ? View.VISIBLE : View.GONE); 125 | } 126 | return true; 127 | } 128 | 129 | private void reset() { 130 | adapter.clear(); 131 | offset = 0; 132 | addMoreItems(10); 133 | } 134 | 135 | @Override public void onScrollStateChanged(AbsListView view, int scrollState) { 136 | 137 | } 138 | 139 | @Override 140 | public void onScroll(@NonNull AbsListView view, int firstVisibleItem, int visibleItemCount, 141 | int totalItemCount) { 142 | } 143 | 144 | @Override public boolean onNavigationItemSelected(int itemPos, long itemId) { 145 | if (itemPos == currentPos) { 146 | return false; 147 | } 148 | 149 | currentPos = itemPos; 150 | switch (itemPos) { 151 | case 0: 152 | currentPos = R.layout.activity_main; 153 | break; 154 | case 1: 155 | currentPos = R.layout.activity_main_grid; 156 | break; 157 | case 2: 158 | currentPos = R.layout.activity_main_scrollview; 159 | break; 160 | } 161 | initialize(currentPos); 162 | 163 | return true; 164 | } 165 | 166 | @Override public void onClick(@NonNull View v) { 167 | 168 | } 169 | 170 | @Override public void onItemClick(@NonNull AdapterView parent, @NonNull View view, 171 | int position, long id) { 172 | Toast.makeText(this, "Item " + position + " clicked", Toast.LENGTH_SHORT).show(); 173 | } 174 | 175 | @Override public boolean onItemLongClick(AdapterView parent, View view, int position, 176 | long id) { 177 | Toast.makeText(this, "Item " + position + " long clicked", Toast.LENGTH_SHORT).show(); 178 | return true; 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /app/src/main/res/color/sticky_item_text.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/felipecsl/QuickReturn/88e52e5df83846f870cfc2039ebf702603b67798/app/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/felipecsl/QuickReturn/88e52e5df83846f870cfc2039ebf702603b67798/app/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/felipecsl/QuickReturn/88e52e5df83846f870cfc2039ebf702603b67798/app/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/felipecsl/QuickReturn/88e52e5df83846f870cfc2039ebf702603b67798/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/sticky_item_background.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/res/layout/action_bar_spinner_text.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 12 | 13 | 22 | 23 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main_grid.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 13 | 14 | 23 | 24 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main_scrollview.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 17 | 18 | 22 | 23 | 24 | 25 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 47 | 48 | 49 | 50 | 51 | 52 | 61 | 62 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/list_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 11 | 12 | 17 | 18 | 23 | 24 | 29 | 30 | 35 | 36 | 41 | 42 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | #a6c 19 | #FF151E54 20 | #FF45ADCC 21 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 100dp 6 | 16dp 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | QuickReturn 5 | Hello world! 6 | Settings 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 12 | 13 | 16 | 17 | 28 | 29 | 32 | 33 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.5.0' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Settings specified in this file will override any Gradle settings 5 | # configured through the IDE. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true 19 | 20 | VERSION_CODE=1 21 | VERSION_NAME=1.5.1 22 | GROUP=com.felipecsl.quickreturn 23 | 24 | POM_DESCRIPTION=Android ListView that implements the QuickReturn UI pattern. Written from scratch with focus on performance. 25 | POM_URL=https://github.com/felipecsl/QuickReturn/tree/new-scv 26 | POM_SCM_URL=https://github.com/felipecsl/QuickReturn/tree/new-scv 27 | POM_SCM_CONNECTION=scm:git@github.com:felipecsl/QuickReturn.git 28 | POM_SCM_DEV_CONNECTION=scm:git@github.com:felipecsl/QuickReturn.git 29 | POM_LICENCE_NAME=The MIT License (MIT) 30 | POM_LICENCE_URL=https://github.com/felipecsl/QuickReturn/blob/master/LICENSE 31 | POM_LICENCE_DIST=repo 32 | POM_DEVELOPER_ID=felipecsl 33 | POM_DEVELOPER_NAME=Felipe Lima -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/felipecsl/QuickReturn/88e52e5df83846f870cfc2039ebf702603b67798/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 01 23:58:52 BRST 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | versionCode 4 9 | versionName "1.5.1" 10 | targetSdkVersion 23 11 | } 12 | compileOptions { 13 | sourceCompatibility JavaVersion.VERSION_1_7 14 | targetCompatibility JavaVersion.VERSION_1_7 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile 'com.android.support:support-annotations:23.1.1' 26 | } 27 | 28 | apply from: 'gradle-mvn-push.gradle' -------------------------------------------------------------------------------- /library/gradle-mvn-push.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Alex Curran 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'maven' 18 | apply plugin: 'signing' 19 | 20 | def isReleaseBuild() { 21 | return VERSION_NAME.contains("SNAPSHOT") == false 22 | } 23 | 24 | def getReleaseRepositoryUrl() { 25 | println 'Release build' 26 | return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL 27 | : "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 28 | } 29 | 30 | def getSnapshotRepositoryUrl() { 31 | println 'Snapshot build' 32 | return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL 33 | : "https://oss.sonatype.org/content/repositories/snapshots/" 34 | } 35 | 36 | def getRepositoryUsername() { 37 | return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : "" 38 | } 39 | 40 | def getRepositoryPassword() { 41 | return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "" 42 | } 43 | 44 | afterEvaluate { project -> 45 | uploadArchives { 46 | repositories { 47 | mavenDeployer { 48 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 49 | 50 | pom.groupId = GROUP 51 | pom.artifactId = POM_ARTIFACT_ID 52 | pom.version = VERSION_NAME 53 | 54 | repository(url: getReleaseRepositoryUrl()) { 55 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 56 | } 57 | snapshotRepository(url: getSnapshotRepositoryUrl()) { 58 | authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) 59 | } 60 | 61 | pom.project { 62 | name POM_NAME 63 | packaging POM_PACKAGING 64 | description POM_DESCRIPTION 65 | url POM_URL 66 | 67 | scm { 68 | url POM_SCM_URL 69 | connection POM_SCM_CONNECTION 70 | developerConnection POM_SCM_DEV_CONNECTION 71 | } 72 | 73 | licenses { 74 | license { 75 | name POM_LICENCE_NAME 76 | url POM_LICENCE_URL 77 | distribution POM_LICENCE_DIST 78 | } 79 | } 80 | 81 | developers { 82 | developer { 83 | id POM_DEVELOPER_ID 84 | name POM_DEVELOPER_NAME 85 | } 86 | } 87 | } 88 | } 89 | } 90 | } 91 | 92 | signing { 93 | required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } 94 | sign configurations.archives 95 | } 96 | 97 | task apklib(type: Zip) { 98 | appendix = extension = 'apklib' 99 | 100 | from 'src/main/AndroidManifest.xml' 101 | into('res') { 102 | from 'src/main/res' 103 | } 104 | into('src') { 105 | from 'src/main/java' 106 | } 107 | } 108 | 109 | task androidJavadocs(type: Javadoc) { 110 | title = "ShowcaseView ${VERSION_NAME}" 111 | ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar" 112 | source = android.sourceSets.main.java.getSrcDirs() 113 | classpath = files(ext.androidJar) 114 | options.links("http://docs.oracle.com/javase/7/docs/api/") 115 | options.linksOffline("http://d.android.com/reference/", "/Applications/Android Studio.app/sdk/docs/reference/") 116 | exclude '**/BuildConfig.java' 117 | exclude '**/R.java' 118 | } 119 | 120 | task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { 121 | classifier = 'javadoc' 122 | from androidJavadocs.destinationDir 123 | } 124 | 125 | task androidSourcesJar(type: Jar) { 126 | classifier = 'sources' 127 | from android.sourceSets.main.java.getSrcDirs() 128 | } 129 | 130 | artifacts { 131 | archives androidSourcesJar 132 | archives androidJavadocsJar 133 | archives apklib 134 | } 135 | } -------------------------------------------------------------------------------- /library/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_NAME=QuickReturn library 2 | POM_ARTIFACT_ID=library 3 | POM_PACKAGING=aar -------------------------------------------------------------------------------- /library/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 /Applications/Android Studio.app/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 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/AbsListViewQuickReturnAttacher.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.view.View; 5 | import android.widget.AbsListView; 6 | import android.widget.AdapterView; 7 | import android.widget.GridView; 8 | import android.widget.ListView; 9 | 10 | import com.felipecsl.quickreturn.library.widget.AbsListViewScrollTarget; 11 | import com.felipecsl.quickreturn.library.widget.QuickReturnTargetView; 12 | 13 | public class AbsListViewQuickReturnAttacher extends QuickReturnAttacher 14 | implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener { 15 | private final CompositeAbsListViewOnScrollListener onScrollListener = 16 | new CompositeAbsListViewOnScrollListener(); 17 | private AbsListView.OnItemClickListener onItemClickListener; 18 | private final AbsListView absListView; 19 | private AdapterView.OnItemLongClickListener onItemLongClickListener; 20 | 21 | public AbsListViewQuickReturnAttacher(AbsListView listView) { 22 | this.absListView = listView; 23 | listView.setOnScrollListener(onScrollListener); 24 | listView.setOnItemClickListener(this); 25 | listView.setOnItemLongClickListener(this); 26 | } 27 | 28 | public QuickReturnTargetView addTargetView(View view, int position) { 29 | return addTargetView(view, position, 0); 30 | } 31 | 32 | public QuickReturnTargetView addTargetView(View view, int position, int viewHeight) { 33 | AbsListViewScrollTarget targetView = 34 | new AbsListViewScrollTarget(absListView, view, position, viewHeight); 35 | onScrollListener.registerOnScrollListener(targetView); 36 | 37 | return targetView; 38 | } 39 | 40 | public void removeTargetView(AbsListViewScrollTarget targetView) { 41 | onScrollListener.unregisterOnScrollListener(targetView); 42 | } 43 | 44 | public void addOnScrollListener(AbsListView.OnScrollListener listener) { 45 | onScrollListener.registerOnScrollListener(listener); 46 | } 47 | 48 | public void setOnItemClickListener(AbsListView.OnItemClickListener listener) { 49 | onItemClickListener = listener; 50 | } 51 | 52 | public void setOnItemLongClickListener(AbsListView.OnItemLongClickListener listener) { 53 | onItemLongClickListener = listener; 54 | } 55 | 56 | @Override public void onItemClick(@NonNull AdapterView parent, @NonNull View view, 57 | int position, long id) { 58 | if (onItemClickListener != null) { 59 | // TODO: Sending incorrect view` and `id` args below. 60 | onItemClickListener.onItemClick(parent, view, 61 | position - getClickPositionOffset(parent), id); 62 | } 63 | } 64 | 65 | @Override 66 | public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { 67 | if (onItemLongClickListener != null) { 68 | // TODO: Sending incorrect view` and `id` args below. 69 | onItemLongClickListener.onItemLongClick(parent, view, 70 | position - getClickPositionOffset(parent), id); 71 | } 72 | return false; 73 | } 74 | 75 | private int getClickPositionOffset(AdapterView parent) { 76 | if (parent instanceof ListView) { 77 | return 1; 78 | } 79 | 80 | // TODO: getNumColumns may return AUTO_FIT. 81 | // TODO: Need fallback for Gingerbread. 82 | if (parent instanceof GridView) { 83 | return ((GridView) parent).getNumColumns(); 84 | } 85 | 86 | return 0; 87 | } 88 | } -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/CompositeAbsListViewOnScrollListener.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library; 2 | 3 | import android.widget.AbsListView; 4 | 5 | import java.util.ArrayList; 6 | 7 | public class CompositeAbsListViewOnScrollListener 8 | extends ArrayList 9 | implements AbsListView.OnScrollListener { 10 | 11 | public void registerOnScrollListener(final AbsListView.OnScrollListener listener) { 12 | add(listener); 13 | } 14 | 15 | public void unregisterOnScrollListener(final AbsListView.OnScrollListener listener) { 16 | remove(listener); 17 | } 18 | 19 | @Override 20 | public void onScrollStateChanged(final AbsListView view, final int scrollState) { 21 | for (AbsListView.OnScrollListener listener : this) { 22 | listener.onScrollStateChanged(view, scrollState); 23 | } 24 | } 25 | 26 | @Override 27 | public void onScroll(final AbsListView view, final int firstVisibleItem, 28 | final int visibleItemCount, final int totalItemCount) { 29 | for (AbsListView.OnScrollListener listener : this) { 30 | listener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/CompositeOnScrollListener.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library; 2 | 3 | import com.felipecsl.quickreturn.library.widget.ObservableScrollView; 4 | 5 | import java.util.ArrayList; 6 | 7 | public class CompositeOnScrollListener extends ArrayList 8 | implements ObservableScrollView.OnScrollListener { 9 | 10 | public void registerOnScrollListener(final ObservableScrollView.OnScrollListener listener) { 11 | add(listener); 12 | } 13 | 14 | public void unregisterOnScrollListener(final ObservableScrollView.OnScrollListener listener) { 15 | remove(listener); 16 | } 17 | 18 | @Override 19 | public void onScrollChanged(final int scrollY) { 20 | for (ObservableScrollView.OnScrollListener listener : this) { 21 | listener.onScrollChanged(scrollY); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/QuickReturnAttacher.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library; 2 | 3 | import android.view.View; 4 | import android.view.ViewGroup; 5 | import android.widget.AbsListView; 6 | import android.widget.ScrollView; 7 | 8 | import com.felipecsl.quickreturn.library.widget.ObservableScrollView; 9 | import com.felipecsl.quickreturn.library.widget.QuickReturnTargetView; 10 | 11 | public abstract class QuickReturnAttacher { 12 | public static QuickReturnAttacher forView(ViewGroup viewGroup) { 13 | if (viewGroup instanceof AbsListView) { 14 | return new AbsListViewQuickReturnAttacher((AbsListView) viewGroup); 15 | } 16 | if (viewGroup instanceof ScrollView) { 17 | return new ScrollViewQuickReturnAttacher((ObservableScrollView) viewGroup); 18 | } 19 | 20 | throw new UnsupportedOperationException( 21 | "Invalid viewGroup instance. It must be a subclass of AbsListView or ObservableScrollView"); 22 | } 23 | 24 | public abstract QuickReturnTargetView addTargetView(View view, int position); 25 | 26 | public abstract QuickReturnTargetView addTargetView(View view, int position, int viewHeight); 27 | } 28 | -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/QuickReturnStateTransition.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library; 2 | 3 | public interface QuickReturnStateTransition { 4 | int determineState(int rawY, int quickReturnHeight); 5 | } 6 | -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/ScrollViewQuickReturnAttacher.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library; 2 | 3 | import android.view.View; 4 | 5 | import com.felipecsl.quickreturn.library.widget.ObservableScrollView; 6 | import com.felipecsl.quickreturn.library.widget.QuickReturnTargetView; 7 | import com.felipecsl.quickreturn.library.widget.ScrollViewScrollTarget; 8 | 9 | public class ScrollViewQuickReturnAttacher extends QuickReturnAttacher { 10 | private final CompositeOnScrollListener onScrollListener = new CompositeOnScrollListener(); 11 | private final ObservableScrollView scrollView; 12 | 13 | public ScrollViewQuickReturnAttacher(final ObservableScrollView scrollView) { 14 | this.scrollView = scrollView; 15 | scrollView.setOnScrollListener(onScrollListener); 16 | } 17 | 18 | public QuickReturnTargetView addTargetView(final View view, final int position) { 19 | return addTargetView(view, position, 0); 20 | } 21 | 22 | public QuickReturnTargetView addTargetView( 23 | final View view, final int position, final int viewHeight) { 24 | ScrollViewScrollTarget targetView = new ScrollViewScrollTarget(scrollView, view, position); 25 | onScrollListener.registerOnScrollListener(targetView); 26 | 27 | return targetView; 28 | } 29 | 30 | public void removeTargetView(final ScrollViewScrollTarget targetView) { 31 | onScrollListener.unregisterOnScrollListener(targetView); 32 | } 33 | 34 | public void addOnScrollListener(final ObservableScrollView.OnScrollListener listener) { 35 | onScrollListener.registerOnScrollListener(listener); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/SimpleAnimationListener.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library; 2 | 3 | import android.view.animation.Animation; 4 | 5 | public abstract class SimpleAnimationListener implements Animation.AnimationListener { 6 | 7 | @Override public void onAnimationStart(Animation animation) { 8 | } 9 | 10 | @Override public void onAnimationRepeat(Animation animation) { 11 | } 12 | 13 | @Override public void onAnimationEnd(Animation animation) { 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/widget/AbsListViewScrollTarget.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library.widget; 2 | 3 | import android.annotation.TargetApi; 4 | import android.os.Build; 5 | import android.support.annotation.NonNull; 6 | import android.view.View; 7 | import android.widget.AbsListView; 8 | import android.widget.GridView; 9 | import android.widget.ListAdapter; 10 | import android.widget.ListView; 11 | import android.widget.WrapperListAdapter; 12 | 13 | public class AbsListViewScrollTarget extends QuickReturnTargetView 14 | implements AbsListView.OnScrollListener { 15 | 16 | private final AbsListView listView; 17 | 18 | public AbsListViewScrollTarget(AbsListView listView, View targetView, int position) { 19 | this(listView, targetView, position, 0); 20 | } 21 | 22 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 23 | public AbsListViewScrollTarget( 24 | AbsListView listView, View targetView, int position, int targetViewHeight) { 25 | super(targetView, position); 26 | 27 | this.listView = listView; 28 | QuickReturnAdapter adapter = getAdapter(); 29 | 30 | if (adapter == null) { 31 | throw new UnsupportedOperationException( 32 | "You need to set the listView adapter before adding a targetView"); 33 | } 34 | 35 | if (position == POSITION_TOP) { 36 | adapter.setTargetViewHeight(targetViewHeight); 37 | } 38 | 39 | if (listView instanceof ListView) { 40 | adapter.setVerticalSpacing(((ListView) listView).getDividerHeight()); 41 | } else if (listView instanceof GridView 42 | && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 43 | adapter.setVerticalSpacing(((GridView) listView).getVerticalSpacing()); 44 | } 45 | } 46 | 47 | @Override protected int getComputedScrollY() { 48 | if (listView.getChildCount() == 0 || listView.getAdapter() == null) { 49 | return 0; 50 | } 51 | 52 | int pos = listView.getFirstVisiblePosition(); 53 | View view = listView.getChildAt(0); 54 | return getAdapter().getPositionVerticalOffset(pos) - view.getTop(); 55 | } 56 | 57 | private QuickReturnAdapter getAdapter() { 58 | ListAdapter adapter = listView.getAdapter(); 59 | 60 | if (adapter instanceof WrapperListAdapter) { 61 | adapter = ((WrapperListAdapter) adapter).getWrappedAdapter(); 62 | } 63 | 64 | if (!(adapter instanceof QuickReturnAdapter)) { 65 | throw new UnsupportedOperationException( 66 | "Your QuickReturn ListView adapter must be an instance of QuickReturnAdapter."); 67 | } 68 | 69 | return (QuickReturnAdapter) adapter; 70 | } 71 | 72 | @Override public void onScrollStateChanged(AbsListView view, int scrollState) { 73 | } 74 | 75 | @Override public void onScroll(@NonNull AbsListView view, int firstVisibleItem, 76 | int visibleItemCount, int totalItemCount) { 77 | if (listView.getAdapter() == null || quickReturnView == null) { 78 | return; 79 | } 80 | 81 | int maxVerticalOffset = getAdapter().getMaxVerticalOffset(); 82 | int listViewHeight = listView.getHeight(); 83 | int rawY = -Math.min(maxVerticalOffset > listViewHeight 84 | ? maxVerticalOffset - listViewHeight 85 | : listViewHeight, getComputedScrollY()); 86 | 87 | int translationY = currentTransition.determineState(rawY, quickReturnView.getHeight()); 88 | 89 | translateTo(translationY); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/widget/ObservableScrollView.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library.widget; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.widget.ScrollView; 6 | 7 | /** A custom ScrollView that can accept a scroll listener. */ 8 | public class ObservableScrollView extends ScrollView { 9 | 10 | private OnScrollListener mCallbacks; 11 | 12 | public ObservableScrollView(Context context, AttributeSet attrs) { 13 | super(context, attrs); 14 | } 15 | 16 | @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { 17 | super.onScrollChanged(l, t, oldl, oldt); 18 | if (mCallbacks != null) { 19 | mCallbacks.onScrollChanged(t); 20 | } 21 | } 22 | 23 | @Override public int computeVerticalScrollRange() { 24 | return super.computeVerticalScrollRange(); 25 | } 26 | 27 | public void setOnScrollListener(OnScrollListener listener) { 28 | mCallbacks = listener; 29 | } 30 | 31 | public interface OnScrollListener { 32 | void onScrollChanged(int scrollY); 33 | } 34 | } -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/widget/QuickReturnAdapter.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library.widget; 2 | 3 | import android.database.DataSetObserver; 4 | import android.support.annotation.NonNull; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.AbsListView; 8 | import android.widget.ListAdapter; 9 | 10 | import java.util.ArrayList; 11 | import java.util.Collections; 12 | import java.util.List; 13 | 14 | public class QuickReturnAdapter extends DataSetObserver implements ListAdapter { 15 | 16 | private static final String TAG = "QuickReturnAdapter"; 17 | private final ListAdapter wrappedAdapter; 18 | private final int heightMeasureSpec; 19 | private int[] itemsVerticalOffset; 20 | private final int numColumns; 21 | private int targetViewHeight; 22 | private int verticalSpacing; 23 | 24 | public QuickReturnAdapter(ListAdapter wrappedAdapter) { 25 | this(wrappedAdapter, 1); 26 | } 27 | 28 | public QuickReturnAdapter(ListAdapter wrappedAdapter, int numColumns) { 29 | this.wrappedAdapter = wrappedAdapter; 30 | this.numColumns = numColumns; 31 | heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 32 | itemsVerticalOffset = new int[wrappedAdapter.getCount() + numColumns]; 33 | wrappedAdapter.registerDataSetObserver(this); 34 | } 35 | 36 | @Override public boolean areAllItemsEnabled() { 37 | return wrappedAdapter.areAllItemsEnabled(); 38 | } 39 | 40 | @Override public boolean isEnabled(int position) { 41 | if (position < numColumns) { 42 | return true; 43 | } 44 | return wrappedAdapter.isEnabled(position - numColumns); 45 | } 46 | 47 | @Override public void registerDataSetObserver(DataSetObserver observer) { 48 | wrappedAdapter.registerDataSetObserver(observer); 49 | } 50 | 51 | @Override public void unregisterDataSetObserver(DataSetObserver observer) { 52 | wrappedAdapter.unregisterDataSetObserver(observer); 53 | } 54 | 55 | @Override 56 | public int getCount() { 57 | return wrappedAdapter.getCount() + numColumns; 58 | } 59 | 60 | @Override 61 | public Object getItem(int position) { 62 | if (position < numColumns) { 63 | return null; 64 | } 65 | return wrappedAdapter.getItem(position - numColumns); 66 | } 67 | 68 | @Override public long getItemId(int position) { 69 | if (position < numColumns) { 70 | return 0; 71 | } 72 | return wrappedAdapter.getItemId(position - numColumns); 73 | } 74 | 75 | @Override public boolean hasStableIds() { 76 | return wrappedAdapter.hasStableIds(); 77 | } 78 | 79 | @Override public View getView(int position, View convertView, @NonNull ViewGroup parent) { 80 | View v; 81 | int finalHeight; 82 | if (position < numColumns) { 83 | if (convertView == null) { 84 | v = new View(parent.getContext()); 85 | } else { 86 | v = convertView; 87 | } 88 | v.setLayoutParams(new AbsListView.LayoutParams( 89 | AbsListView.LayoutParams.MATCH_PARENT, 90 | targetViewHeight)); 91 | 92 | finalHeight = targetViewHeight; 93 | } else { 94 | v = wrappedAdapter.getView(position - numColumns, convertView, parent); 95 | //fixes NullPointerException when v.measure() is called 96 | v.setLayoutParams(new AbsListView.LayoutParams( 97 | AbsListView.LayoutParams.MATCH_PARENT, 98 | AbsListView.LayoutParams.WRAP_CONTENT)); 99 | v.measure(View.MeasureSpec 100 | .makeMeasureSpec(parent.getWidth() / numColumns, View.MeasureSpec.AT_MOST), 101 | heightMeasureSpec); 102 | finalHeight = v.getMeasuredHeight(); 103 | } 104 | 105 | if (position + numColumns < itemsVerticalOffset.length) { 106 | itemsVerticalOffset[position + numColumns] = 107 | itemsVerticalOffset[position] + finalHeight + verticalSpacing; 108 | } 109 | 110 | return v; 111 | } 112 | 113 | @Override public int getItemViewType(int position) { 114 | if (position < numColumns) { 115 | return wrappedAdapter.getViewTypeCount(); 116 | } 117 | return wrappedAdapter.getItemViewType(position - numColumns); 118 | } 119 | 120 | @Override public int getViewTypeCount() { 121 | return wrappedAdapter.getViewTypeCount() + 1; 122 | } 123 | 124 | @Override public boolean isEmpty() { 125 | return wrappedAdapter.isEmpty(); 126 | } 127 | 128 | public int getPositionVerticalOffset(int position) { 129 | if (position >= itemsVerticalOffset.length) { 130 | return 0; 131 | } 132 | 133 | return itemsVerticalOffset[position]; 134 | } 135 | 136 | public int getMaxVerticalOffset() { 137 | if (isEmpty()) { 138 | return 0; 139 | } 140 | 141 | List items = new ArrayList<>(itemsVerticalOffset.length); 142 | for (int aMItemOffsetY : itemsVerticalOffset) { 143 | items.add(aMItemOffsetY); 144 | } 145 | return Collections.max(items); 146 | } 147 | 148 | @Override public void onChanged() { 149 | super.onChanged(); 150 | 151 | if (wrappedAdapter.getCount() < itemsVerticalOffset.length) { 152 | return; 153 | } 154 | 155 | int[] newArray = new int[wrappedAdapter.getCount() + numColumns]; 156 | System.arraycopy(itemsVerticalOffset, 0, newArray, 0, 157 | Math.min(itemsVerticalOffset.length, newArray.length)); 158 | itemsVerticalOffset = newArray; 159 | } 160 | 161 | public void setTargetViewHeight(int targetViewHeight) { 162 | this.targetViewHeight = targetViewHeight; 163 | } 164 | 165 | public void setVerticalSpacing(int verticalSpacing) { 166 | this.verticalSpacing = verticalSpacing; 167 | } 168 | } -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/widget/QuickReturnTargetView.java: -------------------------------------------------------------------------------- 1 | package com.felipecsl.quickreturn.library.widget; 2 | 3 | import android.annotation.TargetApi; 4 | import android.os.Build; 5 | import android.util.Log; 6 | import android.view.View; 7 | import android.view.animation.Animation; 8 | import android.view.animation.TranslateAnimation; 9 | 10 | import com.felipecsl.quickreturn.library.QuickReturnStateTransition; 11 | import com.felipecsl.quickreturn.library.SimpleAnimationListener; 12 | 13 | public abstract class QuickReturnTargetView { 14 | protected static final String TAG = "QuickReturnTargetView"; 15 | 16 | protected static final int STATE_ONSCREEN = 0; 17 | protected static final int STATE_OFFSCREEN = 1; 18 | protected static final int STATE_RETURNING = 2; 19 | protected static final int STATE_EXPANDED = 3; 20 | 21 | public static final int POSITION_TOP = 0; 22 | public static final int POSITION_BOTTOM = 1; 23 | 24 | protected int currentState = STATE_ONSCREEN; 25 | protected int minRawY; 26 | protected View quickReturnView; 27 | protected boolean noAnimation; 28 | 29 | protected final SimpleQuickReturnStateTransition defaultTransition = 30 | new SimpleQuickReturnStateTransition(); 31 | protected final AnimatedQuickReturnStateTransition animatedTransition = 32 | new AnimatedQuickReturnStateTransition(); 33 | protected final BottomQuickReturnStateTransition bottomTransition = 34 | new BottomQuickReturnStateTransition(); 35 | 36 | protected QuickReturnStateTransition currentTransition; 37 | 38 | protected abstract int getComputedScrollY(); 39 | 40 | public QuickReturnTargetView(final View targetView, final int position) { 41 | quickReturnView = targetView; 42 | setPosition(position); 43 | } 44 | 45 | /** 46 | * Returns the quick return target view alignment. Will be either 0 (POSITION_TOP) or 1 47 | * (POSITION_BOTTOM) 48 | */ 49 | public int getPosition() { 50 | if (currentTransition.equals(defaultTransition) || 51 | currentTransition.equals(animatedTransition)) { 52 | return POSITION_TOP; 53 | } 54 | 55 | return POSITION_BOTTOM; 56 | } 57 | 58 | /** 59 | * Sets the quick return target view alignment to either 0 (POSITION_TOP) or 1 (POSITION_BOTTOM). 60 | * 61 | * @param newPosition The new position 62 | */ 63 | public void setPosition(int newPosition) { 64 | currentTransition = newPosition == POSITION_TOP 65 | ? defaultTransition 66 | : bottomTransition; 67 | } 68 | 69 | public boolean isAnimatedTransition() { 70 | return currentTransition.equals(animatedTransition); 71 | } 72 | 73 | public void setAnimatedTransition(final boolean isAnimatedTransition) { 74 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 75 | Log.w(TAG, "Animated QuickReturn is only supported by Android API Level 11+"); 76 | return; 77 | } 78 | this.currentTransition = isAnimatedTransition ? animatedTransition : defaultTransition; 79 | currentState = STATE_ONSCREEN; 80 | minRawY = 0; 81 | } 82 | 83 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 84 | protected void translateTo(final int translationY) { 85 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) { 86 | final TranslateAnimation anim = new TranslateAnimation(0, 0, translationY, translationY); 87 | anim.setFillAfter(true); 88 | anim.setDuration(0); 89 | quickReturnView.startAnimation(anim); 90 | } else { 91 | quickReturnView.setTranslationY(translationY); 92 | } 93 | } 94 | 95 | class SimpleQuickReturnStateTransition implements QuickReturnStateTransition { 96 | public int determineState(int rawY, int quickReturnHeight) { 97 | int translationY = 0; 98 | 99 | switch (currentState) { 100 | case STATE_OFFSCREEN: 101 | if (rawY <= minRawY) { 102 | minRawY = rawY; 103 | } else { 104 | currentState = STATE_RETURNING; 105 | } 106 | 107 | translationY = rawY; 108 | break; 109 | 110 | case STATE_ONSCREEN: 111 | if (rawY < -quickReturnHeight) { 112 | currentState = STATE_OFFSCREEN; 113 | minRawY = rawY; 114 | } 115 | translationY = rawY; 116 | break; 117 | 118 | case STATE_RETURNING: 119 | translationY = (rawY - minRawY) - quickReturnHeight; 120 | if (translationY > 0) { 121 | translationY = 0; 122 | minRawY = rawY - quickReturnHeight; 123 | } 124 | 125 | if (rawY > 0) { 126 | currentState = STATE_ONSCREEN; 127 | translationY = rawY; 128 | } 129 | 130 | if (translationY < -quickReturnHeight) { 131 | currentState = STATE_OFFSCREEN; 132 | minRawY = rawY; 133 | } 134 | break; 135 | } 136 | return translationY; 137 | } 138 | } 139 | 140 | class AnimatedQuickReturnStateTransition implements QuickReturnStateTransition { 141 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 142 | public int determineState(final int rawY, int quickReturnHeight) { 143 | int translationY = 0; 144 | switch (currentState) { 145 | case STATE_OFFSCREEN: 146 | if (rawY <= minRawY) { 147 | minRawY = rawY; 148 | } else { 149 | currentState = STATE_RETURNING; 150 | } 151 | 152 | translationY = rawY; 153 | break; 154 | 155 | case STATE_ONSCREEN: 156 | if (rawY < -quickReturnHeight) { 157 | currentState = STATE_OFFSCREEN; 158 | minRawY = rawY; 159 | } 160 | translationY = rawY; 161 | break; 162 | 163 | case STATE_RETURNING: 164 | if (translationY > 0) { 165 | translationY = 0; 166 | minRawY = rawY - quickReturnHeight; 167 | } else if (rawY > 0) { 168 | currentState = STATE_ONSCREEN; 169 | translationY = rawY; 170 | } else if (translationY < -quickReturnHeight) { 171 | currentState = STATE_OFFSCREEN; 172 | minRawY = rawY; 173 | 174 | } else if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB 175 | && quickReturnView.getTranslationY() != 0) && 176 | !noAnimation) { 177 | 178 | noAnimation = true; 179 | final TranslateAnimation anim = new TranslateAnimation(0, 0, -quickReturnHeight, 0); 180 | anim.setFillAfter(true); 181 | anim.setDuration(250); 182 | quickReturnView.startAnimation(anim); 183 | anim.setAnimationListener(new SimpleAnimationListener() { 184 | @Override 185 | public void onAnimationEnd(Animation animation) { 186 | noAnimation = false; 187 | minRawY = rawY; 188 | currentState = STATE_EXPANDED; 189 | } 190 | }); 191 | } 192 | break; 193 | 194 | case STATE_EXPANDED: 195 | if (rawY < minRawY - 2 && !noAnimation) { 196 | noAnimation = true; 197 | TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -quickReturnHeight); 198 | anim.setFillAfter(true); 199 | anim.setDuration(250); 200 | anim.setAnimationListener(new SimpleAnimationListener() { 201 | @Override 202 | public void onAnimationEnd(Animation animation) { 203 | noAnimation = false; 204 | currentState = STATE_OFFSCREEN; 205 | } 206 | }); 207 | quickReturnView.startAnimation(anim); 208 | } else if (translationY > 0) { 209 | translationY = 0; 210 | minRawY = rawY - quickReturnHeight; 211 | } else if (rawY > 0) { 212 | currentState = STATE_ONSCREEN; 213 | translationY = rawY; 214 | } else if (translationY < -quickReturnHeight) { 215 | currentState = STATE_OFFSCREEN; 216 | minRawY = rawY; 217 | } else { 218 | minRawY = rawY; 219 | } 220 | } 221 | return translationY; 222 | } 223 | } 224 | 225 | class BottomQuickReturnStateTransition implements QuickReturnStateTransition { 226 | public int determineState(final int _rawY, int quickReturnHeight) { 227 | int rawY = getComputedScrollY(); 228 | int translationY = 0; 229 | 230 | switch (currentState) { 231 | case STATE_OFFSCREEN: 232 | if (rawY >= minRawY) { 233 | minRawY = rawY; 234 | } else { 235 | currentState = STATE_RETURNING; 236 | } 237 | 238 | translationY = rawY; 239 | break; 240 | 241 | case STATE_ONSCREEN: 242 | if (rawY > quickReturnHeight) { 243 | currentState = STATE_OFFSCREEN; 244 | minRawY = rawY; 245 | } 246 | translationY = rawY; 247 | break; 248 | 249 | case STATE_RETURNING: 250 | translationY = (rawY - minRawY) + quickReturnHeight; 251 | 252 | if (translationY < 0) { 253 | translationY = 0; 254 | minRawY = rawY + quickReturnHeight; 255 | } 256 | 257 | if (rawY == 0) { 258 | currentState = STATE_ONSCREEN; 259 | translationY = 0; 260 | } 261 | 262 | if (translationY > quickReturnHeight) { 263 | currentState = STATE_OFFSCREEN; 264 | minRawY = rawY; 265 | } 266 | break; 267 | } 268 | 269 | return translationY; 270 | } 271 | } 272 | } 273 | -------------------------------------------------------------------------------- /library/src/main/java/com/felipecsl/quickreturn/library/widget/ScrollViewScrollTarget.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.felipecsl.quickreturn.library.widget; 18 | 19 | import android.view.View; 20 | import android.view.ViewTreeObserver; 21 | 22 | public class ScrollViewScrollTarget extends QuickReturnTargetView 23 | implements ObservableScrollView.OnScrollListener { 24 | 25 | private final ObservableScrollView scrollView; 26 | private int maxScrollY; 27 | 28 | public ScrollViewScrollTarget(final ObservableScrollView scrollView, View targetView, 29 | int position) { 30 | super(targetView, position); 31 | this.scrollView = scrollView; 32 | scrollView.getViewTreeObserver().addOnGlobalLayoutListener( 33 | new ViewTreeObserver.OnGlobalLayoutListener() { 34 | @Override public void onGlobalLayout() { 35 | onScrollChanged(scrollView.getScrollY()); 36 | maxScrollY = scrollView.computeVerticalScrollRange() - scrollView.getHeight(); 37 | } 38 | } 39 | ); 40 | } 41 | 42 | @Override 43 | protected int getComputedScrollY() { 44 | return scrollView.getScrollY(); 45 | } 46 | 47 | @Override 48 | public void onScrollChanged(int scrollY) { 49 | if (quickReturnView == null) { 50 | return; 51 | } 52 | 53 | scrollY = Math.min(maxScrollY, scrollY); 54 | 55 | int rawY = -scrollY; 56 | 57 | translateTo(currentTransition.determineState(rawY, quickReturnView.getHeight())); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', 'library' 2 | --------------------------------------------------------------------------------