├── README.md ├── VegaLayoutManager ├── .gitignore ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── stone │ │ │ └── vega │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── stone │ │ │ │ └── vega │ │ │ │ ├── MainActivity.java │ │ │ │ ├── StockEntity.java │ │ │ │ └── Utils.java │ │ └── res │ │ │ ├── drawable-xxhdpi │ │ │ ├── down_green.png │ │ │ └── up_red.png │ │ │ ├── layout │ │ │ ├── activity_main.xml │ │ │ └── recycler_item.xml │ │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── values-v21 │ │ │ └── dimens.xml │ │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── stone │ │ └── vega │ │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── stone │ │ │ └── vega │ │ │ └── library │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── stone │ │ │ │ └── vega │ │ │ │ └── library │ │ │ │ ├── StartSnapHelper.java │ │ │ │ └── VegaLayoutManager.java │ │ └── res │ │ │ └── values │ │ │ └── strings.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── stone │ │ └── vega │ │ └── library │ │ └── ExampleUnitTest.java └── settings.gradle ├── app-debug.apk ├── capture.gif └── capture2.png /README.md: -------------------------------------------------------------------------------- 1 | # VegaLayoutManager 2 | a customized LayoutManager - fade and shrink the head itemView when scrolling. 3 | 4 | ### 实现效果 5 | 6 | Dribbble设计:[链接](https://dribbble.com/shots/3793079-iPhone-8-iOS-11)
7 | IOS实现:[VegaScroll](https://github.com/AppliKeySolutions/VegaScroll) 8 | 9 | ### 代码思路 10 | RecyclerView最顶部的itemView,会随着手指滑动实现收缩隐藏与放大显示,并伴随recycler的回收与复用。

11 | 代码比较简单粗暴,使用自定义的LayoutManger,内置SnapHelper。
12 | 由于想要在任意时刻都能snap到第一个子View,所以在LayoutManager中用了比较讨巧的方法去设定scroll的最大值。 13 | 14 | ### 使用方法 15 | 1. gradle引入 16 | ```gradle 17 | compile 'com.stone.vega.library:VegaLayoutManager:1.0.1' 18 | ``` 19 | 2. java文件中设定LayoutManager 20 | ```java 21 | recyclerView.setLayoutManager(new VegaLayoutManager()); 22 | ``` 23 | 24 | ### demo下载 25 | [点击下载](https://github.com/xmuSistone/VegaLayoutManager/blob/master/app-debug.apk?raw=true) 26 | 27 | 28 | ### License 29 | 30 | Copyright 2017, xmuSistone 31 | 32 | Licensed under the Apache License, Version 2.0 (the "License"); 33 | you may not use this file except in compliance with the License. 34 | You may obtain a copy of the License at 35 | 36 | http://www.apache.org/licenses/LICENSE-2.0 37 | 38 | Unless required by applicable law or agreed to in writing, software 39 | distributed under the License is distributed on an "AS IS" BASIS, 40 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 41 | See the License for the specific language governing permissions and 42 | limitations under the License. 43 | -------------------------------------------------------------------------------- /VegaLayoutManager/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | defaultConfig { 7 | applicationId "com.stone.vega" 8 | minSdkVersion 14 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.1' 28 | compile 'com.android.support:design:25.3.1' 29 | compile 'com.android.support:cardview-v7:25.3.1' 30 | testCompile 'junit:junit:4.12' 31 | compile project(':library') 32 | } 33 | -------------------------------------------------------------------------------- /VegaLayoutManager/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 D:\adt-bundle-windows-x86-20130917\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 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/androidTest/java/com/stone/vega/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.stone.vega; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.stone.vega", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/java/com/stone/vega/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.stone.vega; 2 | 3 | import android.graphics.Color; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.support.v7.widget.Toolbar; 8 | import android.view.LayoutInflater; 9 | import android.view.MenuItem; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.ImageView; 13 | import android.widget.TextView; 14 | 15 | import com.stone.vega.library.VegaLayoutManager; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | public class MainActivity extends AppCompatActivity { 21 | 22 | private List dataList = new ArrayList<>(); 23 | private int redColor, greenColor; 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_main); 29 | 30 | // 1. 沉浸式状态栏 + dark模式 31 | View positionView = findViewById(R.id.main_position_view); 32 | boolean immerse = Utils.immerseStatusBar(this); 33 | boolean darkMode = Utils.setDarkMode(this); 34 | if (immerse) { 35 | ViewGroup.LayoutParams lp = positionView.getLayoutParams(); 36 | lp.height = Utils.getStatusBarHeight(this); 37 | positionView.setLayoutParams(lp); 38 | if (!darkMode) { 39 | positionView.setBackgroundColor(Color.BLACK); 40 | } 41 | } else { 42 | positionView.setVisibility(View.GONE); 43 | } 44 | 45 | // 2. toolbar 46 | Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar); 47 | setSupportActionBar(toolbar); 48 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 49 | 50 | // 3. recyclerView数据填充 51 | RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_recycler_view); 52 | recyclerView.setLayoutManager(new VegaLayoutManager()); 53 | RecyclerView.Adapter adapter = getAdapter(); 54 | recyclerView.setAdapter(adapter); 55 | 56 | redColor = getResources().getColor(R.color.red); 57 | greenColor = getResources().getColor(R.color.green); 58 | 59 | prepareDataList(); 60 | adapter.notifyDataSetChanged(); 61 | } 62 | 63 | private void prepareDataList() { 64 | for (int i = 0; i < 5; i++) { 65 | dataList.add(new StockEntity("Google Inc.", 921.59f, 1, "+6.59 (+0.72%)")); 66 | dataList.add(new StockEntity("Apple Inc.", 158.73f, 1, "+0.06 (+0.04%)")); 67 | dataList.add(new StockEntity("Vmware Inc.", 109.74f, -1, "-0.24 (-0.22%)")); 68 | dataList.add(new StockEntity("Microsoft Inc.", 75.44f, 1, "+0.28 (+0.37%)")); 69 | dataList.add(new StockEntity("Facebook Inc.", 172.52f, 1, "+2.51 (+1.48%)")); 70 | dataList.add(new StockEntity("IBM Inc.", 144.40f, -1, "-0.15 (-0.10%)")); 71 | dataList.add(new StockEntity("Alibaba Inc.", 180.04f, 1, "+0.06 (+0.03%)")); 72 | dataList.add(new StockEntity("Tencent Inc.", 346.400f, 1, "+2.200 (+0.64%)")); 73 | dataList.add(new StockEntity("Baidu Inc.", 237.92f, -1, "-1.15 (-0.48%)")); 74 | dataList.add(new StockEntity("Amazon Inc.", 969.47f, -1, "-4.72 (-0.48%)")); 75 | dataList.add(new StockEntity("Oracle Inc.", 48.03f, -1, "-0.30 (-0.62%)")); 76 | dataList.add(new StockEntity("Intel Inc.", 37.22f, 1, "+0.22 (+0.61%)")); 77 | dataList.add(new StockEntity("Cisco Systems Inc.", 32.49f, -1, "-0.03 (-0.08%)")); 78 | dataList.add(new StockEntity("Qualcomm Inc.", 52.30f, 1, "+0.05 (+0.10%)")); 79 | dataList.add(new StockEntity("Sony Inc.", 37.65f, -1, "-0.74 (-1.93%)")); 80 | } 81 | } 82 | 83 | @Override 84 | public boolean onOptionsItemSelected(MenuItem item) { 85 | switch (item.getItemId()) { 86 | case android.R.id.home:// 点击返回图标事件 87 | finish(); 88 | default: 89 | return super.onOptionsItemSelected(item); 90 | } 91 | } 92 | 93 | private RecyclerView.Adapter getAdapter() { 94 | final LayoutInflater inflater = LayoutInflater.from(this); 95 | RecyclerView.Adapter adapter = new RecyclerView.Adapter() { 96 | @Override 97 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 98 | View view = inflater.inflate(R.layout.recycler_item, parent, false); 99 | return new MyViewHolder(view); 100 | } 101 | 102 | @Override 103 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 104 | MyViewHolder myHolder = (MyViewHolder) holder; 105 | myHolder.bindData(dataList.get(position)); 106 | } 107 | 108 | @Override 109 | public int getItemCount() { 110 | return dataList.size(); 111 | } 112 | }; 113 | return adapter; 114 | } 115 | 116 | class MyViewHolder extends RecyclerView.ViewHolder { 117 | 118 | TextView nameTv; 119 | TextView currentPriceTv; 120 | ImageView trendFlagIv; 121 | TextView grossTv; 122 | 123 | public MyViewHolder(View itemView) { 124 | super(itemView); 125 | this.nameTv = (TextView) itemView.findViewById(R.id.item_name_tv); 126 | this.currentPriceTv = (TextView) itemView.findViewById(R.id.item_current_price); 127 | this.trendFlagIv = (ImageView) itemView.findViewById(R.id.item_trend_flag); 128 | this.grossTv = (TextView) itemView.findViewById(R.id.item_gross); 129 | } 130 | 131 | public void bindData(StockEntity stockEntity) { 132 | nameTv.setText(stockEntity.getName()); 133 | currentPriceTv.setText("$" + stockEntity.getPrice()); 134 | trendFlagIv.setImageResource(stockEntity.getFlag() > 0 ? R.drawable.up_red : R.drawable.down_green); 135 | grossTv.setText(stockEntity.getGross()); 136 | grossTv.setTextColor(stockEntity.getFlag() > 0 ? redColor : greenColor); 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/java/com/stone/vega/StockEntity.java: -------------------------------------------------------------------------------- 1 | package com.stone.vega; 2 | 3 | /** 4 | * Created by xmuSistone on 2017/9/20. 5 | */ 6 | 7 | public class StockEntity { 8 | 9 | private String name; 10 | private float price; 11 | private int flag; 12 | private String gross; 13 | 14 | public StockEntity(String name, float price, int flag, String gross) { 15 | this.name = name; 16 | this.price = price; 17 | this.flag = flag; 18 | this.gross = gross; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public float getPrice() { 26 | return price; 27 | } 28 | 29 | public int getFlag() { 30 | return flag; 31 | } 32 | 33 | public String getGross() { 34 | return gross; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/java/com/stone/vega/Utils.java: -------------------------------------------------------------------------------- 1 | package com.stone.vega; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.graphics.Color; 6 | import android.os.Build; 7 | import android.view.View; 8 | import android.view.Window; 9 | import android.view.WindowManager; 10 | 11 | import java.lang.reflect.Field; 12 | import java.lang.reflect.Method; 13 | 14 | public class Utils { 15 | 16 | public static int getStatusBarHeight(Context context) { 17 | Class c = null; 18 | Object obj = null; 19 | Field field = null; 20 | int x = 0, statusBarHeight = 0; 21 | try { 22 | c = Class.forName("com.android.internal.R$dimen"); 23 | obj = c.newInstance(); 24 | field = c.getField("status_bar_height"); 25 | x = Integer.parseInt(field.get(obj).toString()); 26 | statusBarHeight = context.getResources().getDimensionPixelSize(x); 27 | } catch (Exception e1) { 28 | e1.printStackTrace(); 29 | } 30 | return statusBarHeight; 31 | } 32 | 33 | 34 | /******************************以下代码段是为处理顶部状态栏的************************************/ 35 | 36 | /** 37 | * 沉浸式状态栏 38 | */ 39 | public static boolean immerseStatusBar(Activity activity) { 40 | boolean success = false; 41 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 42 | success = true; 43 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT); 44 | activity.getWindow() 45 | .getDecorView() 46 | .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 47 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 48 | success = true; 49 | activity.getWindow() 50 | .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 51 | } 52 | return success; 53 | } 54 | 55 | public static boolean setDarkMode(Activity activity) { 56 | return setDarkMode(activity, true); 57 | } 58 | 59 | /** 60 | * darkMode设置 61 | * 62 | * @return 是否成功 63 | */ 64 | public static boolean setDarkMode(Activity activity, boolean darkMode) { 65 | String brand = Build.BRAND; 66 | boolean success = false; 67 | if (brand.contains("Xiaomi")) { 68 | success = setXiaomiDarkMode(activity, darkMode); 69 | } else if (brand.contains("Meizu")) { 70 | success = setMeizuDarkMode(activity, darkMode); 71 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 72 | final int lFlags = activity.getWindow().getDecorView().getSystemUiVisibility(); 73 | activity.getWindow().getDecorView().setSystemUiVisibility(darkMode ? (lFlags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) : (lFlags & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)); 74 | success = true; 75 | } 76 | 77 | return success; 78 | } 79 | 80 | private static boolean setXiaomiDarkMode(Activity activity, boolean darkmode) { 81 | Class clazz = activity.getWindow().getClass(); 82 | try { 83 | int darkModeFlag = 0; 84 | Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); 85 | Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); 86 | darkModeFlag = field.getInt(layoutParams); 87 | Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); 88 | extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag); 89 | return true; 90 | } catch (Exception e) { 91 | e.printStackTrace(); 92 | } 93 | return false; 94 | } 95 | 96 | private static boolean setMeizuDarkMode(Activity activity, boolean dark) { 97 | boolean result = false; 98 | if (activity != null) { 99 | try { 100 | WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); 101 | Field darkFlag = WindowManager.LayoutParams.class 102 | .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); 103 | Field meizuFlags = WindowManager.LayoutParams.class 104 | .getDeclaredField("meizuFlags"); 105 | darkFlag.setAccessible(true); 106 | meizuFlags.setAccessible(true); 107 | int bit = darkFlag.getInt(null); 108 | int value = meizuFlags.getInt(lp); 109 | if (dark) { 110 | value |= bit; 111 | } else { 112 | value &= ~bit; 113 | } 114 | meizuFlags.setInt(lp, value); 115 | activity.getWindow().setAttributes(lp); 116 | result = true; 117 | } catch (Exception e) { 118 | } 119 | } 120 | return result; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/drawable-xxhdpi/down_green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/VegaLayoutManager/app/src/main/res/drawable-xxhdpi/down_green.png -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/drawable-xxhdpi/up_red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/VegaLayoutManager/app/src/main/res/drawable-xxhdpi/up_red.png -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 20 | 21 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/layout/recycler_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 16 | 17 | 24 | 25 | 28 | 29 | 36 | 37 | 45 | 46 | 54 | 55 | 56 | 57 | 61 | 62 | 69 | 70 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/VegaLayoutManager/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/VegaLayoutManager/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/VegaLayoutManager/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/VegaLayoutManager/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/VegaLayoutManager/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/values-v21/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 6dp 6 | 10dp 7 | 8 | 9 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #17bb0b 8 | #e50b1f 9 | 10 | 11 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10dp 4 | 1dp 5 | 2dp 6 | 7 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | VegaLayoutManager 3 | 4 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /VegaLayoutManager/app/src/test/java/com/stone/vega/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.stone.vega; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /VegaLayoutManager/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:2.2.3' 9 | 10 | classpath 'com.android.tools.build:gradle:1.2.3' 11 | classpath 'com.novoda:bintray-release:0.3.4' 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | jcenter() 21 | } 22 | } 23 | 24 | task clean(type: Delete) { 25 | delete rootProject.buildDir 26 | } 27 | 28 | tasks.getByPath(":library:mavenAndroidJavadocs").enabled = false -------------------------------------------------------------------------------- /VegaLayoutManager/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 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 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /VegaLayoutManager/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/VegaLayoutManager/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /VegaLayoutManager/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 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.14.1-all.zip 7 | -------------------------------------------------------------------------------- /VegaLayoutManager/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 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /VegaLayoutManager/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 | -------------------------------------------------------------------------------- /VegaLayoutManager/library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /VegaLayoutManager/library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.novoda.bintray-release' 3 | 4 | android { 5 | compileSdkVersion 25 6 | buildToolsVersion "25.0.3" 7 | 8 | defaultConfig { 9 | minSdkVersion 14 10 | targetSdkVersion 25 11 | versionCode 101 12 | versionName "1.0.1" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 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 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 28 | exclude group: 'com.android.support', module: 'support-annotations' 29 | }) 30 | compile 'com.android.support:appcompat-v7:25.3.1' 31 | compile 'com.android.support:design:25.3.1' 32 | testCompile 'junit:junit:4.12' 33 | } 34 | 35 | 36 | 37 | publish { 38 | userOrg = 'xmuzll' 39 | groupId = 'com.stone.vega.library' 40 | artifactId = 'VegaLayoutManager' 41 | publishVersion = '1.0.1' 42 | desc = 'Vega Layout' 43 | website = 'https://github.com/xmuSistone/VegaLayoutManager' 44 | } 45 | 46 | // .gradlew clean build bintrayUpload -PbintrayUser=xmuzll -PbintrayKey=xxxxxxxxxxxxxxxxxxxxxx -PdryRun=false 47 | 48 | 49 | -------------------------------------------------------------------------------- /VegaLayoutManager/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 D:\adt-bundle-windows-x86-20130917\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 | -------------------------------------------------------------------------------- /VegaLayoutManager/library/src/androidTest/java/com/stone/vega/library/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.stone.vega.library; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.stone.vega.library.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /VegaLayoutManager/library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /VegaLayoutManager/library/src/main/java/com/stone/vega/library/StartSnapHelper.java: -------------------------------------------------------------------------------- 1 | package com.stone.vega.library; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.v7.widget.LinearSnapHelper; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.View; 7 | 8 | /** 9 | * 定位到第一个子View的SnapHelper 10 | * Created by xmuSistone on 2017/9/19. 11 | */ 12 | public class StartSnapHelper extends LinearSnapHelper { 13 | 14 | @Override 15 | public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, 16 | @NonNull View targetView) { 17 | int[] out = new int[2]; 18 | out[0] = 0; 19 | out[1] = ((VegaLayoutManager) layoutManager).getSnapHeight(); 20 | return out; 21 | } 22 | 23 | @Override 24 | public View findSnapView(RecyclerView.LayoutManager layoutManager) { 25 | VegaLayoutManager custLayoutManager = (VegaLayoutManager) layoutManager; 26 | return custLayoutManager.findSnapView(); 27 | } 28 | } -------------------------------------------------------------------------------- /VegaLayoutManager/library/src/main/java/com/stone/vega/library/VegaLayoutManager.java: -------------------------------------------------------------------------------- 1 | package com.stone.vega.library; 2 | 3 | import android.graphics.Rect; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.util.SparseArray; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | /** 10 | * Created by xmuSistone on 2017/9/20. 11 | */ 12 | public class VegaLayoutManager extends RecyclerView.LayoutManager { 13 | 14 | private int mDecoratedMeasuredWidth; 15 | private int mDecoratedMeasuredHeight; 16 | private int scroll = 0; 17 | private SparseArray locationRects = new SparseArray<>(); 18 | private SparseArray attachedItems = new SparseArray<>(); 19 | private boolean needSnap = false; 20 | private int lastDy = 0; 21 | private int maxScroll = -1; 22 | 23 | @Override 24 | public RecyclerView.LayoutParams generateDefaultLayoutParams() { 25 | return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 26 | } 27 | 28 | @Override 29 | public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { 30 | super.onLayoutChildren(recycler, state); 31 | int itemCount = getItemCount(); 32 | if (itemCount <= 0 || state.isPreLayout()) { 33 | return; 34 | } 35 | 36 | if (getChildCount() == 0) { 37 | // 通过第一个itemView,获取一些中间变量 38 | View itemView = recycler.getViewForPosition(0); 39 | addView(itemView); 40 | measureChildWithMargins(itemView, 0, 0); 41 | mDecoratedMeasuredWidth = getDecoratedMeasuredWidth(itemView); 42 | mDecoratedMeasuredHeight = getDecoratedMeasuredHeight(itemView); 43 | } 44 | 45 | int tempPosition = getPaddingTop(); 46 | for (int i = 0; i < itemCount; i++) { 47 | Rect rect = new Rect(); 48 | rect.left = getPaddingLeft(); 49 | rect.top = tempPosition; 50 | rect.right = mDecoratedMeasuredWidth - getPaddingRight(); 51 | rect.bottom = rect.top + mDecoratedMeasuredHeight; 52 | locationRects.put(i, rect); 53 | attachedItems.put(i, false); 54 | 55 | tempPosition = tempPosition + mDecoratedMeasuredHeight; 56 | } 57 | 58 | // 得到中间变量后,第一个View先回收放到缓存,后面会再次统一layout 59 | detachAndScrapAttachedViews(recycler); 60 | layoutItemsOnCreate(recycler); 61 | computeMaxScroll(); 62 | } 63 | 64 | 65 | /** 66 | * 对外提供接口,找到第一个可视view的index 67 | */ 68 | public int findFirstVisibleItemPosition() { 69 | int count = locationRects.size(); 70 | Rect displayRect = new Rect(0, scroll, getWidth(), getHeight() + scroll); 71 | for (int i = 0; i < count; i++) { 72 | if (Rect.intersects(displayRect, locationRects.get(i)) && 73 | attachedItems.get(i)) { 74 | return i; 75 | } 76 | } 77 | return 0; 78 | } 79 | 80 | /** 81 | * 计算可滑动的最大值 82 | */ 83 | private void computeMaxScroll() { 84 | maxScroll = locationRects.get(locationRects.size() - 1).bottom - getHeight(); 85 | if (maxScroll < 0) { 86 | maxScroll = 0; 87 | return; 88 | } 89 | 90 | int childCount = getChildCount(); 91 | int screenFilledHeight = 0; 92 | for (int i = childCount - 1; i >= 0; i--) { 93 | Rect rect = locationRects.get(i); 94 | screenFilledHeight = screenFilledHeight + (rect.bottom - rect.top); 95 | if (screenFilledHeight > getHeight()) { 96 | int extraSnapHeight = getHeight() - (screenFilledHeight - (rect.bottom - rect.top)); 97 | maxScroll = maxScroll + extraSnapHeight; 98 | break; 99 | } 100 | } 101 | } 102 | 103 | /** 104 | * 初始化的时候,layout子View 105 | */ 106 | private void layoutItemsOnCreate(RecyclerView.Recycler recycler) { 107 | int itemCount = getItemCount(); 108 | 109 | for (int i = 0; i < itemCount; i++) { 110 | View childView = recycler.getViewForPosition(i); 111 | addView(childView); 112 | measureChildWithMargins(childView, 0, 0); 113 | layoutItem(childView, locationRects.get(i)); 114 | attachedItems.put(i, true); 115 | childView.setPivotY(0); 116 | childView.setPivotX(childView.getMeasuredWidth() / 2); 117 | 118 | if (locationRects.get(i).top > getHeight()) { 119 | break; 120 | } 121 | } 122 | } 123 | 124 | 125 | /** 126 | * 初始化的时候,layout子View 127 | */ 128 | private void layoutItemsOnScroll(RecyclerView.Recycler recycler, RecyclerView.State state, int dy) { 129 | int childCount = getChildCount(); 130 | if (state.isPreLayout() || childCount == 0) { 131 | return; 132 | } 133 | 134 | // 1. 已经在屏幕上显示的child 135 | int itemCount = getItemCount(); 136 | Rect displayRect = new Rect(0, scroll, getWidth(), getHeight() + scroll); 137 | for (int i = 0; i < childCount; i++) { 138 | View child = getChildAt(i); 139 | if (child == null) { 140 | continue; 141 | } 142 | int position = getPosition(child); 143 | if (!Rect.intersects(displayRect, locationRects.get(position))) { 144 | // 回收滑出屏幕的View 145 | removeAndRecycleView(child, recycler); 146 | attachedItems.put(position, false); 147 | } else { 148 | // Item还在显示区域内,更新滑动后Item的位置 149 | layoutItem(child, locationRects.get(position)); //更新Item位置 150 | } 151 | } 152 | 153 | // 2. 复用View添加 154 | for (int i = 0; i < itemCount; i++) { 155 | if (Rect.intersects(displayRect, locationRects.get(i)) && 156 | !attachedItems.get(i)) { 157 | // 重新加载可见范围内的Item 158 | View scrap = recycler.getViewForPosition(i); 159 | measureChildWithMargins(scrap, 0, 0); 160 | scrap.setPivotY(0); 161 | scrap.setPivotX(scrap.getMeasuredWidth() / 2); 162 | if (dy > 0) { 163 | addView(scrap); 164 | } else { 165 | addView(scrap, 0); 166 | } 167 | // 将这个Item布局出来 168 | layoutItem(scrap, locationRects.get(i)); 169 | attachedItems.put(i, true); 170 | } 171 | } 172 | } 173 | 174 | private void layoutItem(View child, Rect rect) { 175 | int topDistance = scroll - rect.top; 176 | int layoutTop, layoutBottom; 177 | if (topDistance < mDecoratedMeasuredHeight && topDistance >= 0) { 178 | float rate1 = (float) topDistance / mDecoratedMeasuredHeight; 179 | float rate2 = 1 - rate1 * rate1 / 3; 180 | float rate3 = 1 - rate1 * rate1; 181 | child.setScaleX(rate2); 182 | child.setScaleY(rate2); 183 | child.setAlpha(rate3); 184 | 185 | layoutTop = 0; 186 | layoutBottom = mDecoratedMeasuredHeight; 187 | } else { 188 | child.setScaleX(1); 189 | child.setScaleY(1); 190 | child.setAlpha(1); 191 | 192 | layoutTop = rect.top - scroll; 193 | layoutBottom = rect.bottom - scroll; 194 | } 195 | layoutDecorated(child, rect.left, layoutTop, rect.right, layoutBottom); 196 | } 197 | 198 | @Override 199 | public boolean canScrollVertically() { 200 | return true; 201 | } 202 | 203 | @Override 204 | public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { 205 | if (getItemCount() == 0 || dy == 0) { 206 | return 0; 207 | } 208 | int travel = dy; 209 | if (dy + scroll < 0) { 210 | travel = -scroll; 211 | } else if (dy + scroll > maxScroll) { 212 | travel = maxScroll - scroll; 213 | } 214 | scroll += travel; //累计偏移量 215 | lastDy = dy; 216 | layoutItemsOnScroll(recycler, state, dy); 217 | return travel; 218 | } 219 | 220 | @Override 221 | public void onAttachedToWindow(RecyclerView view) { 222 | super.onAttachedToWindow(view); 223 | new StartSnapHelper().attachToRecyclerView(view); 224 | } 225 | 226 | @Override 227 | public void onScrollStateChanged(int state) { 228 | if (state == RecyclerView.SCROLL_STATE_DRAGGING) { 229 | needSnap = true; 230 | } 231 | super.onScrollStateChanged(state); 232 | } 233 | 234 | public int getSnapHeight() { 235 | if (!needSnap) { 236 | return 0; 237 | } 238 | needSnap = false; 239 | 240 | Rect displayRect = new Rect(0, scroll, getWidth(), getHeight() + scroll); 241 | int itemCount = getItemCount(); 242 | for (int i = 0; i < itemCount; i++) { 243 | Rect itemRect = locationRects.get(i); 244 | if (displayRect.intersect(itemRect)) { 245 | 246 | if (lastDy > 0) { 247 | // scroll变大,属于列表往下走,往下找下一个为snapView 248 | if (i < itemCount - 1) { 249 | Rect nextRect = locationRects.get(i + 1); 250 | return nextRect.top - displayRect.top; 251 | } 252 | } 253 | return itemRect.top - displayRect.top; 254 | } 255 | } 256 | return 0; 257 | } 258 | 259 | public View findSnapView() { 260 | if (getChildCount() > 0) { 261 | return getChildAt(0); 262 | } 263 | return null; 264 | } 265 | } 266 | -------------------------------------------------------------------------------- /VegaLayoutManager/library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /VegaLayoutManager/library/src/test/java/com/stone/vega/library/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.stone.vega.library; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /VegaLayoutManager/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':library' 2 | -------------------------------------------------------------------------------- /app-debug.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/app-debug.apk -------------------------------------------------------------------------------- /capture.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/capture.gif -------------------------------------------------------------------------------- /capture2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iammert/VegaLayoutManager/413ef925713d1eaa14f9451110d0f6a8e2680edf/capture2.png --------------------------------------------------------------------------------