From 24238acf0e6900d8016f6024e885b2b14f2849fb Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Fri, 21 Oct 2016 16:44:40 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E6=A3=80=E6=9F=A5for=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E4=B8=AD=E7=9A=84remove=EF=BC=8C=E6=A3=80=E6=9F=A5response?= =?UTF-8?q?=E7=9A=84length=20=EF=BC=81=3D=200?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/gh/base/DetailActivity.java | 17 ++- .../java/com/gh/common/util/FileUtils.java | 15 --- .../com/gh/common/util/GameViewUtils.java | 16 --- .../common/view/ChildLinearLayoutManager.java | 67 ----------- .../com/gh/common/view/DownloadDialog.java | 3 + .../com/gh/gamecenter/GameDetailActivity.java | 16 ++- .../java/com/gh/gamecenter/MainActivity.java | 110 ++++++++++-------- .../com/gh/gamecenter/NewsDetailActivity.java | 48 +++++--- .../com/gh/gamecenter/PluginActivity.java | 2 +- .../gh/gamecenter/SplashScreenActivity.java | 45 +++---- .../com/gh/gamecenter/SuggestionActivity.java | 22 ++-- .../com/gh/gamecenter/ViewImageActivity.java | 7 +- .../gh/gamecenter/adapter/ConcernAdapter.java | 4 +- .../adapter/ConcernRecommendAdapter.java | 8 +- .../download/GameUpdateAdapter.java | 2 +- .../download/GameUpdateFragment.java | 2 - .../com/gh/gamecenter/game/Game1Fragment.java | 3 +- .../gamecenter/game/Game1FragmentAdapter.java | 7 +- .../gamecenter/game/Game2FragmentAdapter.java | 4 +- .../gamecenter/game/Game3FragmentAdapter.java | 2 - .../gamedetail/GameDetailAdapter.java | 94 +++++++-------- .../manager/DataCollectionManager.java | 2 - .../gh/gamecenter/manager/FilterManager.java | 5 +- .../gamecenter/news/News1FragmentAdapter.java | 1 - .../gamecenter/news/News2FragmentAdapter.java | 3 +- .../gamecenter/news/News3FragmentAdapter.java | 3 - .../com/gh/gamecenter/news/News4Fragment.java | 16 +-- .../gamecenter/news/News4FragmentAdapter.java | 3 +- .../newsdetail/NewsDetailAdapter.java | 1 - .../personal/ConcernFragmentAdapter.java | 4 +- .../personal/InstallFragmentAdapter.java | 5 +- .../search/SearchGameListFragmentAdapter.java | 1 + 32 files changed, 231 insertions(+), 307 deletions(-) delete mode 100644 app/src/main/java/com/gh/common/view/ChildLinearLayoutManager.java diff --git a/app/src/main/java/com/gh/base/DetailActivity.java b/app/src/main/java/com/gh/base/DetailActivity.java index 11d68890f5..44a008c6e7 100644 --- a/app/src/main/java/com/gh/base/DetailActivity.java +++ b/app/src/main/java/com/gh/base/DetailActivity.java @@ -240,16 +240,13 @@ public abstract class DetailActivity extends BaseActivity implements View.OnClic && gameEntity.getApk() != null && gameEntity.getApk().size() == 1) { String url = gameEntity.getApk().get(0).getUrl(); - List list = DownloadManager.getInstance(getApplicationContext()).getAll(); - for (DownloadEntity entry : list) { - if (url.equals(entry.getUrl())) { - mDownloadEntity = entry; - detail_tv_download.setVisibility(View.GONE); - detail_pb_progressbar.setVisibility(View.VISIBLE); - detail_tv_per.setVisibility(View.VISIBLE); - invalidate(); - break; - } + DownloadEntity downloadEntity = DownloadManager.getInstance(getApplicationContext()).get(url); + if (downloadEntity != null) { + mDownloadEntity = downloadEntity; + detail_tv_download.setVisibility(View.GONE); + detail_pb_progressbar.setVisibility(View.VISIBLE); + detail_tv_per.setVisibility(View.VISIBLE); + invalidate(); } } } diff --git a/app/src/main/java/com/gh/common/util/FileUtils.java b/app/src/main/java/com/gh/common/util/FileUtils.java index 1874f9ef93..2fba49aa9b 100644 --- a/app/src/main/java/com/gh/common/util/FileUtils.java +++ b/app/src/main/java/com/gh/common/util/FileUtils.java @@ -96,21 +96,6 @@ public class FileUtils { } return true; } - - public static void checkDirExists(String dirName) { - File file = Environment.getExternalStorageDirectory(); - if (file.isDirectory()) { - File[] fs = file.listFiles(); - for (int i = 0; i < fs.length; i++) { - if (fs[i].isDirectory() - && fs[i].getName().equalsIgnoreCase(dirName) - && !fs[i].getName().equals(dirName)) { - fs[i].delete(); - break; - } - } - } - } public static boolean isMounted() { return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); diff --git a/app/src/main/java/com/gh/common/util/GameViewUtils.java b/app/src/main/java/com/gh/common/util/GameViewUtils.java index ca52a764d1..81177b2e62 100644 --- a/app/src/main/java/com/gh/common/util/GameViewUtils.java +++ b/app/src/main/java/com/gh/common/util/GameViewUtils.java @@ -41,22 +41,6 @@ public class GameViewUtils { } } - // 获取游戏标签列表视图 - public static void setLabelList(Context context, LinearLayout labelLayout, String tag) { - labelLayout.removeAllViews(); - // 添加tag标签 - if (tag != null && !tag.isEmpty()) { - String[] tags = tag.split(","); - for (int i = 0; i < tags.length; i++) { - if (i == tags.length - 1) { - labelLayout.addView(getGameTagView(context, tags[i], 0)); - } else { - labelLayout.addView(getGameTagView(context, tags[i], DisplayUtils.dip2px(context, 5))); - } - } - } - } - public static TextView getGameTagView(Context context, String tagStr, int rightMargin) { LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); diff --git a/app/src/main/java/com/gh/common/view/ChildLinearLayoutManager.java b/app/src/main/java/com/gh/common/view/ChildLinearLayoutManager.java deleted file mode 100644 index d1fd68b4bf..0000000000 --- a/app/src/main/java/com/gh/common/view/ChildLinearLayoutManager.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.gh.common.view; - -import android.content.Context; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.view.View; -import android.view.ViewGroup; - -/** - * Created by khy on 2016/8/9. - */ -public class ChildLinearLayoutManager extends LinearLayoutManager { - - public ChildLinearLayoutManager(Context context) { - super(context); - } - - public ChildLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { - super(context, orientation, reverseLayout); - } - @Override - public boolean canScrollVertically() { - return false; - } - private int[] mMeasuredDimension = new int[1]; - - @Override - public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, - int widthSpec, int heightSpec) { - - final int heightMode = View.MeasureSpec.getMode(heightSpec); - final int heightSize = View.MeasureSpec.getSize(heightSpec); - - int height = 0; - for (int i = 0; i < getItemCount(); i++) { - measureScrapChild(recycler, i, - View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), - View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), - mMeasuredDimension); - height = height + mMeasuredDimension[0]; - - } - if (heightMode == View.MeasureSpec.EXACTLY){ - height = heightSize; - } - - setMeasuredDimension(View.MeasureSpec.getSize(widthSpec), height); - } - - private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec, - int heightSpec, int[] measuredDimension) { - View view = recycler.getViewForPosition(position); - if (view.getVisibility() == View.GONE) { - measuredDimension[0] = 0; - return; - } - super.measureChildWithMargins(view, 0, 0); - RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams(); - int childHeightSpec = ViewGroup.getChildMeasureSpec( - heightSpec, - getPaddingTop() + getPaddingBottom() + getDecoratedTop(view) + getDecoratedBottom(view), - p.height); - view.measure(0, childHeightSpec); - measuredDimension[0] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin; - recycler.recycleView(view); - } -} \ No newline at end of file diff --git a/app/src/main/java/com/gh/common/view/DownloadDialog.java b/app/src/main/java/com/gh/common/view/DownloadDialog.java index 1b6bb30711..9720657efc 100644 --- a/app/src/main/java/com/gh/common/view/DownloadDialog.java +++ b/app/src/main/java/com/gh/common/view/DownloadDialog.java @@ -105,6 +105,9 @@ public class DownloadDialog implements OnCollectionCallBackListener { public void showPopupWindow(View view, GameEntity gameEntity, String entrance, String location) { + if (isShow && (popupWindow == null || !popupWindow.isShowing())) { + isShow = false; + } if (isShow) { return; } diff --git a/app/src/main/java/com/gh/gamecenter/GameDetailActivity.java b/app/src/main/java/com/gh/gamecenter/GameDetailActivity.java index a84fbb51d9..eed81bd549 100644 --- a/app/src/main/java/com/gh/gamecenter/GameDetailActivity.java +++ b/app/src/main/java/com/gh/gamecenter/GameDetailActivity.java @@ -142,12 +142,16 @@ public class GameDetailActivity extends DetailActivity implements View.OnClickLi new Response.Listener() { @Override public void onResponse(JSONObject response) { - Gson gson = new Gson(); - gameEntity = gson.fromJson(response.toString(), GameEntity.class); - title = gameEntity.getName(); - actionbar_tv_title.setText(gameEntity.getName()); - adapter.setGameEntity(gameEntity); - adapter.getGameDetail(); + if (response.length() != 0) { + Gson gson = new Gson(); + gameEntity = gson.fromJson(response.toString(), GameEntity.class); + title = gameEntity.getName(); + actionbar_tv_title.setText(gameEntity.getName()); + adapter.setGameEntity(gameEntity); + adapter.getGameDetail(); + } else { + reuse_no_connection.setVisibility(View.VISIBLE); + } } }, new Response.ErrorListener() { @Override diff --git a/app/src/main/java/com/gh/gamecenter/MainActivity.java b/app/src/main/java/com/gh/gamecenter/MainActivity.java index 69eefb2de4..471a5141c4 100644 --- a/app/src/main/java/com/gh/gamecenter/MainActivity.java +++ b/app/src/main/java/com/gh/gamecenter/MainActivity.java @@ -581,29 +581,31 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene new Response.Listener() { @Override public void onResponse(JSONObject response) { - Gson gson = new Gson(); - GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class); - ConcernInfo concernInfo = concernManager.findConcernById(gameEntity.getId()); - if (concernInfo != null && gameEntity.getApk() != null - && gameEntity.getApk().size() != 0) { - HashMap packageNames = new HashMap<>(); - String packageName; - for (int i = 0, size = gameEntity.getApk().size(); i < size; i++) { - packageName = gameEntity.getApk().get(i).getPackageName(); - if (PackageManager.isInstalled(packageName)) { - packageNames.put(packageName, true); - } else { - packageNames.put(packageName, false); + if (response.length() != 0) { + Gson gson = new Gson(); + GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class); + ConcernInfo concernInfo = concernManager.findConcernById(gameEntity.getId()); + if (concernInfo != null && gameEntity.getApk() != null + && gameEntity.getApk().size() != 0) { + HashMap packageNames = new HashMap<>(); + String packageName; + for (int i = 0, size = gameEntity.getApk().size(); i < size; i++) { + packageName = gameEntity.getApk().get(i).getPackageName(); + if (PackageManager.isInstalled(packageName)) { + packageNames.put(packageName, true); + } else { + packageNames.put(packageName, false); + } } + concernInfo.setTime(System.currentTimeMillis()); + concernInfo.setPackageNames(packageNames); + concernManager.updateByConcern(concernInfo); } - concernInfo.setTime(System.currentTimeMillis()); - concernInfo.setPackageNames(packageNames); - concernManager.updateByConcern(concernInfo); - } - if (isNewFirstLaunch) { - //默认安装即为关注 - if (!concernManager.isConcern(gameEntity.getId())) { - concernManager.addByEntity(gameEntity); + if (isNewFirstLaunch) { + //默认安装即为关注 + if (!concernManager.isConcern(gameEntity.getId())) { + concernManager.addByEntity(gameEntity); + } } } addConcernCount(); @@ -774,9 +776,11 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene new Response.Listener() { @Override public void onResponse(JSONObject response) { - Gson gson = new Gson(); - GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class); - list.add(gameEntity); + if (response.length() != 0) { + Gson gson = new Gson(); + GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class); + list.add(gameEntity); + } addCount(); if (count == size) { processPluginData(list); @@ -913,18 +917,20 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene new Response.Listener() { @Override public void onResponse(JSONObject response) { - try { - boolean isShow = response.getBoolean("isShow"); - sp.edit().putBoolean("isShowDisclaimer", isShow).apply(); - if (isShow) { - String content = response.getString("content"); - sp.edit().putString("disclaimer", content).apply(); - if (isFirst) { - DialogUtils.showDisclaimerDialog(MainActivity.this, content); + if (response.length() != 0) { + try { + boolean isShow = response.getBoolean("isShow"); + sp.edit().putBoolean("isShowDisclaimer", isShow).apply(); + if (isShow) { + String content = response.getString("content"); + sp.edit().putString("disclaimer", content).apply(); + if (isFirst) { + DialogUtils.showDisclaimerDialog(MainActivity.this, content); + } } + } catch (JSONException e) { + e.printStackTrace(); } - } catch (JSONException e) { - e.printStackTrace(); } } }, null); @@ -1470,23 +1476,25 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene new Response.Listener() { @Override public void onResponse(JSONObject response) { - Gson gson = new Gson(); - GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class); - GameManager manager = new GameManager(getApplicationContext()); - manager.addOrUpdate(gameEntity.getApk(), gameEntity.getId(), gameEntity.getName()); - if (!concernManager.isConcern(id)) { - concernManager.addByEntity(gameEntity); - } - // 检查是否能插件化 - if (gameEntity.getTag() != null && gameEntity.getTag().size() != 0 - && gameEntity.getApk() != null) { - for (ApkEntity apkEntity : gameEntity.getApk()) { - if (apkEntity.getPackageName().equals(packageName) - && !TextUtils.isEmpty(apkEntity.getGhVersion()) - && !PackageUtils.isSignature(getApplicationContext(), apkEntity.getPackageName())) { - PackageManager.addUpdate(getGameUpdateEntity(gameEntity, apkEntity)); - EventBus.getDefault().post(new EBDownloadStatus("plugin")); - break; + if (response.length() != 0) { + Gson gson = new Gson(); + GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class); + GameManager manager = new GameManager(getApplicationContext()); + manager.addOrUpdate(gameEntity.getApk(), gameEntity.getId(), gameEntity.getName()); + if (!concernManager.isConcern(id)) { + concernManager.addByEntity(gameEntity); + } + // 检查是否能插件化 + if (gameEntity.getTag() != null && gameEntity.getTag().size() != 0 + && gameEntity.getApk() != null) { + for (ApkEntity apkEntity : gameEntity.getApk()) { + if (apkEntity.getPackageName().equals(packageName) + && !TextUtils.isEmpty(apkEntity.getGhVersion()) + && !PackageUtils.isSignature(getApplicationContext(), apkEntity.getPackageName())) { + PackageManager.addUpdate(getGameUpdateEntity(gameEntity, apkEntity)); + EventBus.getDefault().post(new EBDownloadStatus("plugin")); + break; + } } } } diff --git a/app/src/main/java/com/gh/gamecenter/NewsDetailActivity.java b/app/src/main/java/com/gh/gamecenter/NewsDetailActivity.java index e0e1cec5ce..7a684cced9 100644 --- a/app/src/main/java/com/gh/gamecenter/NewsDetailActivity.java +++ b/app/src/main/java/com/gh/gamecenter/NewsDetailActivity.java @@ -224,20 +224,28 @@ public class NewsDetailActivity extends DetailActivity implements OnClickListene new Response.Listener() { @Override public void onResponse(JSONObject response) { - Gson gson = new Gson(); - NewsEntity newsEntity = gson.fromJson(response.toString(), NewsEntity.class); - if (newsEntity.getType() != null) { - actionbar_tv_title.setText(newsEntity.getType()); + if (response.length() != 0) { + Gson gson = new Gson(); + NewsEntity newsEntity = gson.fromJson(response.toString(), NewsEntity.class); + if (newsEntity.getType() != null) { + actionbar_tv_title.setText(newsEntity.getType()); + } + + adapter.setId(news_id); + adapter.setType(newsEntity.getType()); + adapter.setTitle(newsEntity.getTitle()); + adapter.getNewsDetail(); + + title = newsEntity.getTitle(); + + iv_share.setVisibility(View.VISIBLE); + } else { + detail_rv_show.setVisibility(View.GONE); + reuse_ll_loading.setVisibility(View.GONE); + detail_ll_bottom.setVisibility(View.GONE); + detail_rv_show.setPadding(0, 0, 0, 0); + reuse_no_connection.setVisibility(View.VISIBLE); } - - adapter.setId(news_id); - adapter.setType(newsEntity.getType()); - adapter.setTitle(newsEntity.getTitle()); - adapter.getNewsDetail(); - - title = newsEntity.getTitle(); - - iv_share.setVisibility(View.VISIBLE); } }, new Response.ErrorListener() { @Override @@ -325,12 +333,14 @@ public class NewsDetailActivity extends DetailActivity implements OnClickListene new Response.Listener() { @Override public void onResponse(JSONObject response) { - Gson gson = new Gson(); - gameEntity = gson.fromJson(response.toString(), GameEntity.class); - adapter.setGameEntity(gameEntity); //出现空指针 找不到原因 - adapter.notifyItemInserted(1); - downloadAddWord = gameEntity.getDownloadAddWord(); - initDownload(true); + if (response.length() != 0) { + Gson gson = new Gson(); + gameEntity = gson.fromJson(response.toString(), GameEntity.class); + adapter.setGameEntity(gameEntity); + adapter.notifyItemInserted(1); + downloadAddWord = gameEntity.getDownloadAddWord(); + initDownload(true); + } } }, null); AppController.addToRequestQueue(gameRequest, TAG); diff --git a/app/src/main/java/com/gh/gamecenter/PluginActivity.java b/app/src/main/java/com/gh/gamecenter/PluginActivity.java index 4238a5d625..8c6db5a379 100644 --- a/app/src/main/java/com/gh/gamecenter/PluginActivity.java +++ b/app/src/main/java/com/gh/gamecenter/PluginActivity.java @@ -136,12 +136,12 @@ public class PluginActivity extends BaseActivity { if (list.get(i).getApk().get(0).getPackageName().equals(busFour.getPackageName())) { list.remove(i); adapter.notifyItemRemoved(location); - adapter.initLocationMap(); break; } } } } + adapter.initLocationMap(); } } diff --git a/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java b/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java index 32a1da3e50..b6285c9d8f 100644 --- a/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java +++ b/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java @@ -295,18 +295,19 @@ public class SplashScreenActivity extends BaseActivity { new Response.Listener() { @Override public void onResponse(JSONObject response) { - Utils.log(response.toString()); - try { - Editor editor = sp.edit(); - editor.putInt("download_box_row", - response.getJSONObject("download_box").getInt("row")); - editor.putInt("download_box_column", - response.getJSONObject("download_box").getInt("column")); - editor.putInt("game_detail_news_type_tab_column", - response.getJSONObject("game_detail_news_type_tab").getInt("column")); - editor.apply(); - } catch (JSONException e) { - e.printStackTrace(); + if (response.length() != 0) { + try { + Editor editor = sp.edit(); + editor.putInt("download_box_row", + response.getJSONObject("download_box").getInt("row")); + editor.putInt("download_box_column", + response.getJSONObject("download_box").getInt("column")); + editor.putInt("game_detail_news_type_tab_column", + response.getJSONObject("game_detail_news_type_tab").getInt("column")); + editor.apply(); + } catch (JSONException e) { + e.printStackTrace(); + } } } }, null); @@ -325,16 +326,18 @@ public class SplashScreenActivity extends BaseActivity { new Response.Listener() { @Override public void onResponse(JSONObject response) { - try { - String status = response.getString("status"); - if ("on".equals(status)) { - sp.edit().putBoolean("isShow", true).apply(); - } else { - sp.edit().putBoolean("isShow", false).apply(); + if (response.length() != 0) { + try { + String status = response.getString("status"); + if ("on".equals(status)) { + sp.edit().putBoolean("isShow", true).apply(); + } else { + sp.edit().putBoolean("isShow", false).apply(); + } + EventBus.getDefault().post(new EBReuse("Refresh")); + } catch (JSONException e) { + e.printStackTrace(); } - EventBus.getDefault().post(new EBReuse("Refresh")); - } catch (JSONException e) { - e.printStackTrace(); } } }, null); diff --git a/app/src/main/java/com/gh/gamecenter/SuggestionActivity.java b/app/src/main/java/com/gh/gamecenter/SuggestionActivity.java index d8cb202d78..8d2bef6caf 100644 --- a/app/src/main/java/com/gh/gamecenter/SuggestionActivity.java +++ b/app/src/main/java/com/gh/gamecenter/SuggestionActivity.java @@ -194,19 +194,23 @@ public class SuggestionActivity extends BaseActivity implements OnClickListener Config.HOST + "support/suggestion", new JSONObject(map), new Response.Listener() { @Override - public void onResponse(JSONObject object) { + public void onResponse(JSONObject response) { isShowing = false; dialog.dismiss(); - try { - if ("ok".equals(object.getString("status"))) { - toast("提交成功,感谢您的反馈!"); - finish(); - } else { - toast("提交失败,请稍后尝试!"); + if (response.length() != 0) { + try { + if ("ok".equals(response.getString("status"))) { + toast("提交成功,感谢您的反馈!"); + finish(); + } else { + toast("提交失败,请稍后尝试!"); + } + } catch (JSONException e) { + e.printStackTrace(); } - } catch (JSONException e) { - e.printStackTrace(); + } else { + toast("提交失败,请稍后尝试!"); } } diff --git a/app/src/main/java/com/gh/gamecenter/ViewImageActivity.java b/app/src/main/java/com/gh/gamecenter/ViewImageActivity.java index 9a699ddf11..6169fe88f9 100644 --- a/app/src/main/java/com/gh/gamecenter/ViewImageActivity.java +++ b/app/src/main/java/com/gh/gamecenter/ViewImageActivity.java @@ -276,12 +276,7 @@ public class ViewImageActivity extends BaseActivity implements connection.setReadTimeout(5 * 1000); connection.connect(); int code = connection.getResponseCode(); - if (code == 200) { - //图片存在 - if (urls == null) { - return; - } - //urls出现空指针 + if (code == 200 && urls != null) { for (int i = 0, size = urls.size(); i < size; i++) { if (urls.get(i).equals(url)) { urls.set(i, newUrl); diff --git a/app/src/main/java/com/gh/gamecenter/adapter/ConcernAdapter.java b/app/src/main/java/com/gh/gamecenter/adapter/ConcernAdapter.java index 334ceec242..41e306faf7 100644 --- a/app/src/main/java/com/gh/gamecenter/adapter/ConcernAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/adapter/ConcernAdapter.java @@ -76,8 +76,10 @@ public class ConcernAdapter extends RecyclerView.Adapter { new Response.Listener() { @Override public void onResponse(JSONObject response) { + if (response.length() != 0) { + result.add(response); + } addConcernCount(); - result.add(response); if (cCount == count) { processingConcernGame(result); } diff --git a/app/src/main/java/com/gh/gamecenter/adapter/ConcernRecommendAdapter.java b/app/src/main/java/com/gh/gamecenter/adapter/ConcernRecommendAdapter.java index b672862fa8..bd18334c7a 100644 --- a/app/src/main/java/com/gh/gamecenter/adapter/ConcernRecommendAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/adapter/ConcernRecommendAdapter.java @@ -78,9 +78,11 @@ public class ConcernRecommendAdapter extends RecyclerView.Adapter() { @Override public void onResponse(JSONObject response) { - Gson gson = new Gson(); - GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class); - gameList.add(gameEntity); + if (response.length() != 0) { + Gson gson = new Gson(); + GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class); + gameList.add(gameEntity); + } addCount(); if (count == size) { initRecommendGame(); diff --git a/app/src/main/java/com/gh/gamecenter/download/GameUpdateAdapter.java b/app/src/main/java/com/gh/gamecenter/download/GameUpdateAdapter.java index 1a20d4d95c..ac5726d23f 100644 --- a/app/src/main/java/com/gh/gamecenter/download/GameUpdateAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/download/GameUpdateAdapter.java @@ -147,8 +147,8 @@ public class GameUpdateAdapter extends RecyclerView.Adapter list = adapter.getPluginList(); - for (int i = 0, size = list.size(); i < size; i++) { + for (int i = 0; i < list.size(); i++) { if (list.get(i).getApk().get(0).getPackageName().equals(busFour.getPackageName())) { if ("卸载".equals(busFour.getType()) && DownloadManager.getInstance(getActivity()).get( diff --git a/app/src/main/java/com/gh/gamecenter/game/Game1FragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/game/Game1FragmentAdapter.java index 61810dbf69..ef7ef90278 100644 --- a/app/src/main/java/com/gh/gamecenter/game/Game1FragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/game/Game1FragmentAdapter.java @@ -199,6 +199,7 @@ public class Game1FragmentAdapter extends RecyclerView.Adapter() { @Override public void onResponse(JSONArray response) { - Type listType = new TypeToken>() {}.getType(); Gson gson = new Gson(); List list = gson.fromJson(response.toString(), listType); @@ -102,6 +101,7 @@ public class Game2FragmentAdapter extends RecyclerView.Adapter offset && position <= subjectList.get(i).getData().size() + offset) { - int index = position -offset-1; + int index = position - offset - 1; if (index<0){ index = 0; } diff --git a/app/src/main/java/com/gh/gamecenter/game/Game3FragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/game/Game3FragmentAdapter.java index 192cc7c55b..eeb834100f 100644 --- a/app/src/main/java/com/gh/gamecenter/game/Game3FragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/game/Game3FragmentAdapter.java @@ -98,7 +98,6 @@ public class Game3FragmentAdapter extends RecyclerView.Adapter() { - @Override public void onResponse(JSONArray response) { processingData(response, offset); @@ -106,7 +105,6 @@ public class Game3FragmentAdapter extends RecyclerView.Adapter() { @Override public void onResponse(JSONObject response) { - Gson gson = new Gson(); - gameDetailEntity = gson.fromJson(response.toString(), GameDetailEntity.class); + if (response.length() != 0) { + Gson gson = new Gson(); + gameDetailEntity = gson.fromJson(response.toString(), GameDetailEntity.class); - getGameNews(); + getGameNews(); - getNewsServer(); + getNewsServer(); + } else if (listener != null) { + listener.loadError(); + } } }, new Response.ErrorListener() { @Override @@ -160,51 +164,49 @@ public class GameDetailAdapter extends RecyclerView.Adapter { new Response.Listener() { @Override public void onResponse(JSONArray response) { - if (response.length() != 0) { - try { - ArrayList serverInfo = new ArrayList<>(); - SimpleDateFormat format = new SimpleDateFormat( - "Mdd", Locale.getDefault()); - int today = Integer.valueOf(format.format(new Date())); - for (int j = 0, sizej = response.length(); j < sizej; j++) { - ServerEntity entity = new ServerEntity(); - JSONObject jsonObject2; - jsonObject2 = response.getJSONObject(j); - String server = jsonObject2.getString("server"); - if (server.length() > 4) { - server = server.substring(0, 4); - } - entity.setServer(server); - entity.setTime(Long.valueOf(jsonObject2.getString("time") + "000")); - int day = Integer.valueOf(format.format(new Date(entity.getTime()))); - if (day == today + 1) { - entity.setTag("明天"); - serverInfo.add(entity); - } else if (day == today - 1) { - entity.setTag("昨天"); - serverInfo.add(entity); - } else if (day == today) { - entity.setTag("今天"); - serverInfo.add(entity); - } + try { + ArrayList serverInfo = new ArrayList<>(); + SimpleDateFormat format = new SimpleDateFormat( + "Mdd", Locale.getDefault()); + int today = Integer.valueOf(format.format(new Date())); + for (int i = 0, size = response.length(); i < size; i++) { + ServerEntity entity = new ServerEntity(); + JSONObject jsonObject2; + jsonObject2 = response.getJSONObject(i); + String server = jsonObject2.getString("server"); + if (server.length() > 4) { + server = server.substring(0, 4); } - - Comparator comparator = new Comparator() { - @Override - public int compare(ServerEntity lhs, ServerEntity rhs) { - return (int) (lhs.getTime() - rhs.getTime()); - } - }; - Collections.sort(serverInfo, comparator); - - gameDetailEntity.setServerInfo(serverInfo); - initPosition(); - if (position_newsserver != -1) { - notifyItemInserted(position_newsserver); + entity.setServer(server); + entity.setTime(Long.valueOf(jsonObject2.getString("time") + "000")); + int day = Integer.valueOf(format.format(new Date(entity.getTime()))); + if (day == today + 1) { + entity.setTag("明天"); + serverInfo.add(entity); + } else if (day == today - 1) { + entity.setTag("昨天"); + serverInfo.add(entity); + } else if (day == today) { + entity.setTag("今天"); + serverInfo.add(entity); } - } catch (JSONException e) { - e.printStackTrace(); } + + Comparator comparator = new Comparator() { + @Override + public int compare(ServerEntity lhs, ServerEntity rhs) { + return (int) (lhs.getTime() - rhs.getTime()); + } + }; + Collections.sort(serverInfo, comparator); + + gameDetailEntity.setServerInfo(serverInfo); + initPosition(); + if (position_newsserver != -1) { + notifyItemInserted(position_newsserver); + } + } catch (JSONException e) { + e.printStackTrace(); } } }, null); diff --git a/app/src/main/java/com/gh/gamecenter/manager/DataCollectionManager.java b/app/src/main/java/com/gh/gamecenter/manager/DataCollectionManager.java index f0419259a9..3875eb6a66 100644 --- a/app/src/main/java/com/gh/gamecenter/manager/DataCollectionManager.java +++ b/app/src/main/java/com/gh/gamecenter/manager/DataCollectionManager.java @@ -247,7 +247,6 @@ public class DataCollectionManager { @Override public void onResponse(JSONObject response) { isUploading = false; - Utils.log(response); try { if ("success".equals(response.getString("status"))) { // 上传成功,删除本地数据 @@ -262,7 +261,6 @@ public class DataCollectionManager { public void onErrorResponse(VolleyError error) { isUploading = false; // 上传失败,检查网络状态,继续上传 - Utils.log(error); if (error.networkResponse != null) { Utils.log(new String(error.networkResponse.data)); } diff --git a/app/src/main/java/com/gh/gamecenter/manager/FilterManager.java b/app/src/main/java/com/gh/gamecenter/manager/FilterManager.java index dbe2b14d3f..37022575e6 100644 --- a/app/src/main/java/com/gh/gamecenter/manager/FilterManager.java +++ b/app/src/main/java/com/gh/gamecenter/manager/FilterManager.java @@ -56,11 +56,8 @@ public class FilterManager { new Response.Listener() { @Override public void onResponse(JSONArray response) { - - Utils.log("getFilterFromServer=" + response.toString()); - try { - List list = new ArrayList(); + List list = new ArrayList<>(); for (int i = 0, size = response.length(); i < size; i++) { list.add(new FilterInfo(response.getString(i))); } diff --git a/app/src/main/java/com/gh/gamecenter/news/News1FragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/news/News1FragmentAdapter.java index fa1619d51b..4d59856fed 100644 --- a/app/src/main/java/com/gh/gamecenter/news/News1FragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/news/News1FragmentAdapter.java @@ -182,7 +182,6 @@ public class News1FragmentAdapter extends RecyclerView.Adapter() { @Override public void onResponse(JSONArray response) { - Utils.log("response = " + response); try { JSONObject jsonObject; for (int i = 0, size = response.length(); i < size; i++) { diff --git a/app/src/main/java/com/gh/gamecenter/news/News2FragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/news/News2FragmentAdapter.java index 906a852c44..7492b0b0d2 100644 --- a/app/src/main/java/com/gh/gamecenter/news/News2FragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/news/News2FragmentAdapter.java @@ -183,7 +183,6 @@ public class News2FragmentAdapter extends RecyclerView.Adapter() { @Override public void onResponse(JSONArray response) { - Utils.log("response = " + response); try { JSONObject jsonObject; for (int i = 0, size = response.length(); i < size; i++) { @@ -456,7 +455,7 @@ public class News2FragmentAdapter extends RecyclerView.Adapter() { - @Override public void onResponse(JSONArray response) { - Type listType = new TypeToken>() {}.getType(); Gson gson = new Gson(); List list = gson.fromJson(response.toString(), listType); @@ -188,7 +186,6 @@ public class News3FragmentAdapter extends RecyclerView.Adapter() { @Override public void onResponse(JSONObject response) { - Gson gson = new Gson(); - GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class); + if (response.length() != 0) { + Gson gson = new Gson(); + GameEntity gameEntity = gson.fromJson(response.toString(), GameEntity.class); - if (finalI == size - 1){ - recommendGameList.add(gameEntity); - }else { - installGameList.add(gameEntity); + if (finalI == size - 1){ + recommendGameList.add(gameEntity); + }else { + installGameList.add(gameEntity); + } } - addCount(); if (count == size) { initConcernRecommend(); @@ -335,6 +336,7 @@ public class News4Fragment extends BaseFragment implements SwipeRefreshLayout.On if (installGameList.get(j).getId().equals(gameList.get(i).getId())) { recommendGameList.add(gameList.get(i)); installGameList.remove(j); + j--; } } } diff --git a/app/src/main/java/com/gh/gamecenter/news/News4FragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/news/News4FragmentAdapter.java index 56fadfc5de..5a52de2470 100644 --- a/app/src/main/java/com/gh/gamecenter/news/News4FragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/news/News4FragmentAdapter.java @@ -150,6 +150,7 @@ public class News4FragmentAdapter extends RecyclerView.Adapter>() {}.getType(); Gson gson = new Gson(); List list = gson.fromJson(response.toString(), listType); @@ -218,6 +219,7 @@ public class News4FragmentAdapter extends RecyclerView.Adapter>() {}.getType(); Gson gson = new Gson(); List list = gson.fromJson(response.toString(), listType); @@ -326,7 +328,6 @@ public class News4FragmentAdapter extends RecyclerView.Adapter() { @Override public void onResponse(JSONArray response) { - Utils.log("response = " + response); try { JSONObject jsonObject; for (int i = 0, size = response.length(); i < size; i++) { diff --git a/app/src/main/java/com/gh/gamecenter/newsdetail/NewsDetailAdapter.java b/app/src/main/java/com/gh/gamecenter/newsdetail/NewsDetailAdapter.java index c385732935..7f0c9a750e 100644 --- a/app/src/main/java/com/gh/gamecenter/newsdetail/NewsDetailAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/newsdetail/NewsDetailAdapter.java @@ -106,7 +106,6 @@ public class NewsDetailAdapter extends RecyclerView.Adapter { listener.loadError(); } } - }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { diff --git a/app/src/main/java/com/gh/gamecenter/personal/ConcernFragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/personal/ConcernFragmentAdapter.java index 753a39b1aa..6021eaf4ae 100644 --- a/app/src/main/java/com/gh/gamecenter/personal/ConcernFragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/personal/ConcernFragmentAdapter.java @@ -94,7 +94,9 @@ public class ConcernFragmentAdapter extends RecyclerView.Adapter() { @Override public void onResponse(JSONObject response) { - result.add(response); + if (response.length() != 0) { + result.add(response); + } addCount(); if (count == size) { processingConcernGame(result); diff --git a/app/src/main/java/com/gh/gamecenter/personal/InstallFragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/personal/InstallFragmentAdapter.java index 4aff36b98a..163747b366 100644 --- a/app/src/main/java/com/gh/gamecenter/personal/InstallFragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/personal/InstallFragmentAdapter.java @@ -226,7 +226,9 @@ public class InstallFragmentAdapter extends RecyclerView.Adapter() { @Override public void onResponse(JSONObject response) { - result.add(response); + if (response.length() != 0) { + result.add(response); + } addCount(); if (count == size) { processingData(result); @@ -309,7 +311,6 @@ public class InstallFragmentAdapter extends RecyclerView.Adapter list; for (int i = 0; i < gameList.size(); i++) { gameEntity = gameList.get(i); - // TODO ERROR IndexOutOfBoundsException InstallFragmentAdapter initLocationMap 290 if (gameEntity.getApk() != null && gameEntity.getApk().size() != 0) { for (ApkEntity apkEntity : gameEntity.getApk()) { list = locationMap.get(apkEntity.getPackageName()); diff --git a/app/src/main/java/com/gh/gamecenter/search/SearchGameListFragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/search/SearchGameListFragmentAdapter.java index f00923fd4b..4ceb0a5dac 100644 --- a/app/src/main/java/com/gh/gamecenter/search/SearchGameListFragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/search/SearchGameListFragmentAdapter.java @@ -87,6 +87,7 @@ public class SearchGameListFragmentAdapter extends RecyclerView.Adapter>() {}.getType(); Gson gson = new Gson(); List list = gson.fromJson(response.toString(), listType); From 2c6fd5d0d8ed36f1ec817d50973c6c251b983f32 Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Fri, 21 Oct 2016 17:38:14 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0BitmapUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/gh/common/util/BitmapUtils.java | 87 +++++++++++++++++++ .../com/gh/gamecenter/CropImageActivity.java | 35 ++++++-- .../gh/gamecenter/db/DataCollectionDao.java | 1 - .../newsdetail/NewsDetailAdapter.java | 4 +- .../gamecenter/personal/PersonalFragment.java | 1 - 5 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/com/gh/common/util/BitmapUtils.java diff --git a/app/src/main/java/com/gh/common/util/BitmapUtils.java b/app/src/main/java/com/gh/common/util/BitmapUtils.java new file mode 100644 index 0000000000..3c16288b34 --- /dev/null +++ b/app/src/main/java/com/gh/common/util/BitmapUtils.java @@ -0,0 +1,87 @@ +package com.gh.common.util; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.media.ExifInterface; + +import java.io.IOException; + +/** + * Created by LGT on 2016/10/21. + */ +public class BitmapUtils { + + /** + * 根据文件路径返回bitmap + * @param filepath 文件路径 + * @param w 宽 + * @param h 高 + * @return bitmap + */ + public static Bitmap getBitmapByFile(String filepath, int w, int h) { + try { + BitmapFactory.Options options = new BitmapFactory.Options(); + // 设置为ture只获取图片大小 + options.inJustDecodeBounds = true; + options.inPreferredConfig = Bitmap.Config.RGB_565; + // 返回为空 + BitmapFactory.decodeFile(filepath, options); + int width = options.outWidth; + int height = options.outHeight; + float scaleWidth = 0.f, scaleHeight = 0.f; + if (width > w || height > h) { + // 缩放 + scaleWidth = ((float) width) / w; + scaleHeight = ((float) height) / h; + } + options.inJustDecodeBounds = false; + int scale = (int) Math.ceil(Math.max(scaleWidth, scaleHeight)); + if (scale % 2 == 1) { + scale += 1; + } + options.inSampleSize = scale; + Bitmap bitmap = BitmapFactory.decodeFile(filepath, options); + bitmap = rotatePicture(filepath, bitmap); + if (bitmap != null) { + return bitmap; + } + return null; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static Bitmap rotatePicture(String path, Bitmap bitmap) { + int rotate = 0; + try { + ExifInterface exifInterface = new ExifInterface(path); + int orientation = exifInterface.getAttributeInt( + ExifInterface.TAG_ORIENTATION, + ExifInterface.ORIENTATION_NORMAL); + switch (orientation) { + case ExifInterface.ORIENTATION_ROTATE_90: + rotate = 90; + break; + case ExifInterface.ORIENTATION_ROTATE_180: + rotate = 180; + break; + case ExifInterface.ORIENTATION_ROTATE_270: + rotate = 270; + break; + } + } catch (IOException e) { + e.printStackTrace(); + } + if (rotate != 0) { + Matrix matrix = new Matrix(); + matrix.setRotate(rotate); + return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), + bitmap.getHeight(), matrix, true); + } else { + return bitmap; + } + } + +} diff --git a/app/src/main/java/com/gh/gamecenter/CropImageActivity.java b/app/src/main/java/com/gh/gamecenter/CropImageActivity.java index b136393519..f8e6a6fe8b 100644 --- a/app/src/main/java/com/gh/gamecenter/CropImageActivity.java +++ b/app/src/main/java/com/gh/gamecenter/CropImageActivity.java @@ -2,34 +2,42 @@ package com.gh.gamecenter; import android.app.Dialog; import android.content.Intent; +import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup.LayoutParams; +import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.gh.base.BaseActivity; import com.gh.common.constant.Config; +import com.gh.common.util.BitmapUtils; import com.gh.common.util.DialogUtils; import com.gh.common.util.DisplayUtils; import com.gh.common.util.FileUtils; import com.gh.common.util.ImageUtils; import com.gh.common.util.TokenUtils; +import com.gh.common.util.Utils; import com.gh.common.view.CropImageCustom; +import com.gh.common.view.CropImageZoomView; import org.apache.http.HttpStatus; import org.json.JSONException; import org.json.JSONObject; import java.io.File; +import java.lang.ref.SoftReference; public class CropImageActivity extends BaseActivity { private CropImageCustom cropimage_custom; + private SoftReference reference; + private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { @@ -107,12 +115,27 @@ public class CropImageActivity extends BaseActivity { rparams.addRule(RelativeLayout.CENTER_VERTICAL); confirm.setLayoutParams(rparams); reuse_actionbar.addView(confirm); - - String path = getIntent().getStringExtra("path"); - - ImageUtils.getInstance(getApplicationContext()).displayFile( - "file://" + path, cropimage_custom.getCropImageZoomView()); - } + @Override + public void onWindowFocusChanged(boolean hasFocus) { + super.onWindowFocusChanged(hasFocus); + if (hasFocus && (reference == null || reference.get() == null)) { + ImageView imageView = cropimage_custom.getCropImageZoomView(); + Bitmap bitmap = BitmapUtils.getBitmapByFile(getIntent().getStringExtra("path"), + imageView.getWidth(), imageView.getHeight()); + if (bitmap != null) { + reference = new SoftReference<>(bitmap); + imageView.setImageBitmap(reference.get()); + } + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (reference != null) { + reference.get().recycle(); + } + } } diff --git a/app/src/main/java/com/gh/gamecenter/db/DataCollectionDao.java b/app/src/main/java/com/gh/gamecenter/db/DataCollectionDao.java index 9bcc230b09..ce3e51a964 100644 --- a/app/src/main/java/com/gh/gamecenter/db/DataCollectionDao.java +++ b/app/src/main/java/com/gh/gamecenter/db/DataCollectionDao.java @@ -29,7 +29,6 @@ public class DataCollectionDao { try { return dao.queryForEq("type", type); } catch (SQLException e) { - // TODO Auto-generated catch block e.printStackTrace(); } return null; diff --git a/app/src/main/java/com/gh/gamecenter/newsdetail/NewsDetailAdapter.java b/app/src/main/java/com/gh/gamecenter/newsdetail/NewsDetailAdapter.java index 7f0c9a750e..1f3eda841b 100644 --- a/app/src/main/java/com/gh/gamecenter/newsdetail/NewsDetailAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/newsdetail/NewsDetailAdapter.java @@ -136,7 +136,7 @@ public class NewsDetailAdapter extends RecyclerView.Adapter { } } List more = new ArrayList<>(); - // TODO 随机三篇文章 + // 随机三篇文章 int[] index = RandomUtils.getRandomArray(list.size() > 3 ? 3 : list.size(), list.size()); for (int i : index) { more.add(list.get(i)); @@ -239,7 +239,7 @@ public class NewsDetailAdapter extends RecyclerView.Adapter { } private void initGameDetailTopViewHolder(GameDetailTopViewHolder viewHolder) { - ImageUtils.getInstance(context).displayFile(gameEntity.getIcon(), viewHolder.gamedetail_iv_thumb); + ImageUtils.getInstance(context).display(gameEntity.getIcon(), viewHolder.gamedetail_iv_thumb); viewHolder.gamedetail_tv_name.setText(gameEntity.getName()); if (gameEntity.getApk() != null && gameEntity.getApk().size() != 0) { for (int i = 0, size = gameEntity.getApk().size(); i < size; i++) { diff --git a/app/src/main/java/com/gh/gamecenter/personal/PersonalFragment.java b/app/src/main/java/com/gh/gamecenter/personal/PersonalFragment.java index 477b8fc2e8..5c31debeaa 100644 --- a/app/src/main/java/com/gh/gamecenter/personal/PersonalFragment.java +++ b/app/src/main/java/com/gh/gamecenter/personal/PersonalFragment.java @@ -293,7 +293,6 @@ public class PersonalFragment extends Fragment implements View.OnClickListener, super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0x123 && data != null) { Uri selectedImage = data.getData(); - // TODO 华为手机选择图片后有自带预览,如在预览时取消,getData()返回为null if (selectedImage == null) { return; } From b7564109820cf1c3d0880dee3f55e9c4737b2933 Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Fri, 21 Oct 2016 18:03:31 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E7=94=A8impor?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/gh/base/DetailActivity.java | 1 - app/src/main/java/com/gh/gamecenter/CropImageActivity.java | 3 +-- app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java | 1 - app/src/main/java/com/gh/gamecenter/manager/FilterManager.java | 1 - app/src/main/java/com/gh/gamecenter/news/News4Fragment.java | 2 +- 5 files changed, 2 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/gh/base/DetailActivity.java b/app/src/main/java/com/gh/base/DetailActivity.java index 44a008c6e7..e199e5680a 100644 --- a/app/src/main/java/com/gh/base/DetailActivity.java +++ b/app/src/main/java/com/gh/base/DetailActivity.java @@ -36,7 +36,6 @@ import com.gh.gamecenter.manager.PackageManager; import com.tencent.tauth.Tencent; import java.util.HashMap; -import java.util.List; import java.util.Map; /** diff --git a/app/src/main/java/com/gh/gamecenter/CropImageActivity.java b/app/src/main/java/com/gh/gamecenter/CropImageActivity.java index 2e28646e6d..7f64a83829 100644 --- a/app/src/main/java/com/gh/gamecenter/CropImageActivity.java +++ b/app/src/main/java/com/gh/gamecenter/CropImageActivity.java @@ -2,6 +2,7 @@ package com.gh.gamecenter; import android.app.Dialog; import android.content.Intent; +import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -19,9 +20,7 @@ import com.gh.common.util.DialogUtils; import com.gh.common.util.DisplayUtils; import com.gh.common.util.FileUtils; import com.gh.common.util.TokenUtils; -import com.gh.common.util.Utils; import com.gh.common.view.CropImageCustom; -import com.gh.common.view.CropImageZoomView; import org.apache.http.HttpStatus; import org.json.JSONException; diff --git a/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java b/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java index b6285c9d8f..fc2ba4b17f 100644 --- a/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java +++ b/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java @@ -26,7 +26,6 @@ import com.gh.common.constant.Config; import com.gh.common.util.FileUtils; import com.gh.common.util.PackageUtils; import com.gh.common.util.TimestampUtils; -import com.gh.common.util.Utils; import com.gh.download.DownloadManager; import com.gh.download.DownloadService; import com.gh.gamecenter.db.info.FilterInfo; diff --git a/app/src/main/java/com/gh/gamecenter/manager/FilterManager.java b/app/src/main/java/com/gh/gamecenter/manager/FilterManager.java index 37022575e6..4ca4aff30a 100644 --- a/app/src/main/java/com/gh/gamecenter/manager/FilterManager.java +++ b/app/src/main/java/com/gh/gamecenter/manager/FilterManager.java @@ -7,7 +7,6 @@ import com.android.volley.Request; import com.android.volley.Response; import com.gh.base.AppController; import com.gh.common.constant.Config; -import com.gh.common.util.Utils; import com.gh.gamecenter.db.FilterDao; import com.gh.gamecenter.db.info.FilterInfo; import com.gh.gamecenter.volley.extended.JsonArrayExtendedRequest; diff --git a/app/src/main/java/com/gh/gamecenter/news/News4Fragment.java b/app/src/main/java/com/gh/gamecenter/news/News4Fragment.java index 70ce03af77..531cd66228 100644 --- a/app/src/main/java/com/gh/gamecenter/news/News4Fragment.java +++ b/app/src/main/java/com/gh/gamecenter/news/News4Fragment.java @@ -325,7 +325,7 @@ public class News4Fragment extends BaseFragment implements SwipeRefreshLayout.On if (finalI == size - 1) { recommendGameList.add(gameEntity); - } else if (gameEntity.isExists() { + } else if (gameEntity.isExists()) { installGameList.add(gameEntity); } } From f3ee2d2cf00c9506fd3815b468418d1ad1eb0c4e Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Tue, 25 Oct 2016 09:32:39 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0downloadOffText?= =?UTF-8?q?=EF=BC=8C=E6=A3=80=E6=9F=A5=E4=B8=8B=E8=BD=BD=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=98=AF=E5=90=A6=E5=AE=8C=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 1 - .../main/java/com/gh/base/DetailActivity.java | 7 +- .../java/com/gh/common/util/DialogUtils.java | 4 +- .../java/com/gh/common/util/FileUtils.java | 9 +- .../com/gh/common/util/PlatformUtils.java | 4 +- .../java/com/gh/download/DownloadThread.java | 89 ++++++++----------- .../com/gh/gamecenter/CropImageActivity.java | 6 +- .../com/gh/gamecenter/GameDetailActivity.java | 1 + .../java/com/gh/gamecenter/MainActivity.java | 8 +- .../com/gh/gamecenter/NewsDetailActivity.java | 1 + .../com/gh/gamecenter/ViewImageActivity.java | 1 - .../adapter/ConcernRecommendAdapter.java | 4 +- .../gamecenter/adapter/ImagePagerAdapter.java | 4 +- .../gh/gamecenter/adapter/SubjectAdapter.java | 10 +-- .../download/GameUpdateAdapter.java | 2 +- .../gamecenter/entity/GameDetailEntity.java | 10 +++ .../com/gh/gamecenter/entity/GameEntity.java | 39 ++++++-- .../gamecenter/game/Game2FragmentAdapter.java | 8 +- .../com/gh/gamecenter/news/News4Fragment.java | 2 +- .../gamecenter/news/News4FragmentAdapter.java | 4 +- .../gamecenter/personal/PersonalFragment.java | 4 +- .../volley/extended/ExtendedRequest.java | 7 +- .../main/res/layout/viewimage_gif_item.xml | 20 ----- 23 files changed, 125 insertions(+), 120 deletions(-) delete mode 100644 app/src/main/res/layout/viewimage_gif_item.xml diff --git a/app/build.gradle b/app/build.gradle index 99f1da1327..1796971109 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -118,7 +118,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.16' compile 'com.android.support:cardview-v7:21.0.0' compile ('com.facebook.fresco:fresco:0.12.0') { diff --git a/app/src/main/java/com/gh/base/DetailActivity.java b/app/src/main/java/com/gh/base/DetailActivity.java index e199e5680a..6171178436 100644 --- a/app/src/main/java/com/gh/base/DetailActivity.java +++ b/app/src/main/java/com/gh/base/DetailActivity.java @@ -60,6 +60,7 @@ public abstract class DetailActivity extends BaseActivity implements View.OnClic protected String name; protected String title; protected String downloadAddWord; + protected String downloadOffText; protected Handler handler = new Handler(); @@ -166,7 +167,11 @@ public abstract class DetailActivity extends BaseActivity implements View.OnClic detail_tv_download.setVisibility(View.VISIBLE); detail_pb_progressbar.setVisibility(View.GONE); detail_tv_per.setVisibility(View.GONE); - detail_tv_download.setText("暂无下载"); + if (TextUtils.isEmpty(downloadOffText)) { + detail_tv_download.setText("暂无下载"); + } else { + detail_tv_download.setText(downloadOffText); + } detail_tv_download.setBackgroundResource(R.drawable.game_item_btn_pause_style); detail_tv_download.setTextColor(0xFF999999); detail_tv_download.setClickable(false); diff --git a/app/src/main/java/com/gh/common/util/DialogUtils.java b/app/src/main/java/com/gh/common/util/DialogUtils.java index c5f29f2576..ba1967b681 100644 --- a/app/src/main/java/com/gh/common/util/DialogUtils.java +++ b/app/src/main/java/com/gh/common/util/DialogUtils.java @@ -101,8 +101,8 @@ public class DialogUtils { } // 打开QQ客户端,创建临时会话 - public static void showQqSessionDialog(final Context context, String qq){ - if (qq == null){ + public static void showQqSessionDialog(final Context context, String qq) { + if (qq == null) { qq = "2586716223"; } final String finalQq = qq; diff --git a/app/src/main/java/com/gh/common/util/FileUtils.java b/app/src/main/java/com/gh/common/util/FileUtils.java index 2fba49aa9b..59096ae2d3 100644 --- a/app/src/main/java/com/gh/common/util/FileUtils.java +++ b/app/src/main/java/com/gh/common/util/FileUtils.java @@ -5,7 +5,6 @@ import android.os.Environment; import android.os.StatFs; import android.os.StrictMode; -import org.apache.http.HttpStatus; import org.json.JSONObject; import java.io.DataInputStream; @@ -271,14 +270,14 @@ public class FileUtils { int statusCode = connection.getResponseCode(); Utils.log("statusCode = " + statusCode); - if (statusCode == HttpStatus.SC_OK) { + if (statusCode == HttpURLConnection.HTTP_OK) { // {"icon":"http:\/\/gh-test-1.oss-cn-qingdao.aliyuncs.com\/pic\/57e4f4d58a3200042d29492f.jpg"} JSONObject response = new JSONObject(b.toString().trim()); - response.put("statusCode", HttpStatus.SC_OK); + response.put("statusCode", HttpURLConnection.HTTP_OK); return response; - } else if (statusCode == HttpStatus.SC_FORBIDDEN) { + } else if (statusCode == HttpURLConnection.HTTP_FORBIDDEN) { JSONObject response = new JSONObject(b.toString().trim()); - response.put("statusCode", HttpStatus.SC_FORBIDDEN); + response.put("statusCode", HttpURLConnection.HTTP_FORBIDDEN); return response; } } catch (Exception e) { diff --git a/app/src/main/java/com/gh/common/util/PlatformUtils.java b/app/src/main/java/com/gh/common/util/PlatformUtils.java index 7359311748..494f7cff63 100644 --- a/app/src/main/java/com/gh/common/util/PlatformUtils.java +++ b/app/src/main/java/com/gh/common/util/PlatformUtils.java @@ -15,13 +15,13 @@ import com.gh.gamecenter.R; import com.gh.gamecenter.eventbus.EBReuse; import com.gh.gamecenter.volley.extended.JsonArrayExtendedRequest; -import org.apache.http.HttpStatus; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.io.IOException; +import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.HashSet; import java.util.Properties; @@ -175,7 +175,7 @@ public class PlatformUtils { + url.substring(url.lastIndexOf("/") + 1); try { int code = FileUtils.downloadFile(url, savePath); - if (code == HttpStatus.SC_OK) { + if (code == HttpURLConnection.HTTP_OK) { success++; } } catch (IOException e) { diff --git a/app/src/main/java/com/gh/download/DownloadThread.java b/app/src/main/java/com/gh/download/DownloadThread.java index 27ec069a5f..f4f012908a 100644 --- a/app/src/main/java/com/gh/download/DownloadThread.java +++ b/app/src/main/java/com/gh/download/DownloadThread.java @@ -7,8 +7,6 @@ import android.util.Log; import com.gh.common.util.HttpsUtils; import com.gh.common.util.Utils; -import org.apache.http.HttpStatus; - import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; @@ -52,66 +50,34 @@ public class DownloadThread extends Thread { if (TextUtils.isEmpty(entry.getUrl())) { listener.onStatusChanged(DownloadStatus.notfound); - Utils.log(DownloadThread.class.getSimpleName(), - "error-->url is empty"); + Utils.log(DownloadThread.class.getSimpleName(), "error-->url is empty"); return; } - URL url = new URL(entry.getUrl()); + Utils.log("url = " + entry.getUrl()); - HttpURLConnection connection; - if ("https".equals(url.getProtocol())) { - connection = HttpsUtils.getHttpsURLConnection(url); - } else { - connection = (HttpURLConnection) url.openConnection(); - } + HttpURLConnection connection = openConnection(new URL(entry.getUrl()), targetFile.length()); - connection.setRequestMethod("GET"); - connection.setConnectTimeout(CONNECT_TIME); - connection.setReadTimeout(READ_TIME); - connection.setRequestProperty("RANGE", - "bytes=" + targetFile.length() + "-"); - Utils.log(DownloadThread.class.getSimpleName(), - "startPosition-->" + targetFile.length()); - //设置自动重定向 - connection.setInstanceFollowRedirects(true); + Utils.log(DownloadThread.class.getSimpleName(), "startPosition-->" + targetFile.length()); int code = connection.getResponseCode(); Utils.log("code = " +code); - if (code == HttpStatus.SC_MOVED_PERMANENTLY - || code == HttpStatus.SC_MOVED_TEMPORARILY) { + if (code == HttpURLConnection.HTTP_MOVED_PERM + || code == HttpURLConnection.HTTP_MOVED_TEMP) { //未自动重定向 String location = connection.getHeaderField("Location"); Utils.log("location = " + location); - url = new URL(location); - connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); - connection.setConnectTimeout(CONNECT_TIME); - connection.setReadTimeout(READ_TIME); - connection.setRequestProperty("RANGE", - "bytes=" + targetFile.length() + "-"); - Utils.log(DownloadThread.class.getSimpleName(), - "startPosition-->" + targetFile.length()); - //设置自动重定向 - connection.setInstanceFollowRedirects(true); + connection = openConnection(new URL(location), targetFile.length()); code = connection.getResponseCode(); } - if (code == HttpStatus.SC_NOT_FOUND) { + if (code == HttpURLConnection.HTTP_NOT_FOUND) { // 404 Not Found listener.onStatusChanged(DownloadStatus.notfound); - Utils.log(DownloadThread.class.getSimpleName(), - "error-->404 Not Found"); + Utils.log(DownloadThread.class.getSimpleName(), "error-->404 Not Found"); return; } - bis = new BufferedInputStream(connection.getInputStream()); - if (targetFile.length() > 0) { - bos = new BufferedOutputStream(new FileOutputStream(entry.getPath(), true)); - } else { - bos = new BufferedOutputStream(new FileOutputStream(entry.getPath())); - } - String eTag = connection.getHeaderField("ETag"); if (!TextUtils.isEmpty(eTag) && eTag.startsWith("\"") && eTag.endsWith("\"")) { eTag = eTag.substring(1, eTag.length() - 1); @@ -122,8 +88,7 @@ public class DownloadThread extends Thread { Utils.log("eTag = " + eTag); Utils.log("eTag2 = " + eTag2); listener.onStatusChanged(DownloadStatus.hijack); - Utils.log(DownloadThread.class.getSimpleName(), - "error-->链接被劫持"); + Utils.log(DownloadThread.class.getSimpleName(), "error-->链接被劫持"); return; } @@ -132,14 +97,20 @@ public class DownloadThread extends Thread { if (entry.getSize() == 0) { entry.setSize(conentLength); DownloadDao.getInstance(context).newOrUpdate(entry); - Utils.log(DownloadThread.class.getSimpleName(), - "记录第一次长度"); + Utils.log(DownloadThread.class.getSimpleName(), "记录第一次长度"); } Utils.log(DownloadThread.class.getSimpleName(), "progress:" + entry.getProgress() + "/curfilesize:" + targetFile.length() + "=====contentLength:" + conentLength + "/ totalSize:" + entry.getSize()); + bis = new BufferedInputStream(connection.getInputStream()); + if (targetFile.length() > 0) { + bos = new BufferedOutputStream(new FileOutputStream(entry.getPath(), true)); + } else { + bos = new BufferedOutputStream(new FileOutputStream(entry.getPath())); + } + byte[] buffer = new byte[2048]; int len; while ((len = bis.read(buffer)) != -1) { @@ -160,16 +131,15 @@ public class DownloadThread extends Thread { listener.onStatusChanged(DownloadStatus.done); } } catch (Exception e) { - String errorMsg = Log.getStackTraceString(e); + // TODO 默认第一次错误自动恢复一次下载 - //e.getMessage() will null error + String errorMsg = Log.getStackTraceString(e); if (!TextUtils.isEmpty(e.getMessage()) && e.getMessage().contains("connection timeout")) { listener.onStatusChanged(DownloadStatus.timeout, errorMsg); } else { listener.onStatusChanged(DownloadStatus.neterror, errorMsg); } - Utils.log(DownloadThread.class.getSimpleName(), - "exception-->" + e.toString()); + Utils.log(DownloadThread.class.getSimpleName(), "exception-->" + e.toString()); } finally { if (bis != null) { try { @@ -188,6 +158,23 @@ public class DownloadThread extends Thread { } } + private HttpURLConnection openConnection(URL url, long range) throws Exception { + HttpURLConnection connection; + if ("https".equals(url.getProtocol())) { + connection = HttpsUtils.getHttpsURLConnection(url); + } else { + connection = (HttpURLConnection) url.openConnection(); + } + connection.setRequestMethod("GET"); + connection.setConnectTimeout(CONNECT_TIME); + connection.setReadTimeout(READ_TIME); + connection.setDoInput(true); + connection.setRequestProperty("RANGE", "bytes=" + range + "-"); + //设置自动重定向 + connection.setInstanceFollowRedirects(true); + return connection; + } + public void setStatus(DownloadStatus status) { this.status = status; } diff --git a/app/src/main/java/com/gh/gamecenter/CropImageActivity.java b/app/src/main/java/com/gh/gamecenter/CropImageActivity.java index 7f64a83829..681c8c2125 100644 --- a/app/src/main/java/com/gh/gamecenter/CropImageActivity.java +++ b/app/src/main/java/com/gh/gamecenter/CropImageActivity.java @@ -22,12 +22,12 @@ import com.gh.common.util.FileUtils; import com.gh.common.util.TokenUtils; import com.gh.common.view.CropImageCustom; -import org.apache.http.HttpStatus; import org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.lang.ref.SoftReference; +import java.net.HttpURLConnection; public class CropImageActivity extends BaseActivity { @@ -82,13 +82,13 @@ public class CropImageActivity extends BaseActivity { if (result != null) { try { int statusCode = result.getInt("statusCode"); - if (statusCode == HttpStatus.SC_OK) { + if (statusCode == HttpURLConnection.HTTP_OK) { Intent data = new Intent(); data.putExtra("url", result.getString("icon")); setResult(RESULT_OK, data); finish(); handler.sendEmptyMessage(0); - } else if (statusCode == HttpStatus.SC_FORBIDDEN + } else if (statusCode == HttpURLConnection.HTTP_FORBIDDEN && "too frequent".equals(result.getString("detail"))) { handler.sendEmptyMessage(2); } diff --git a/app/src/main/java/com/gh/gamecenter/GameDetailActivity.java b/app/src/main/java/com/gh/gamecenter/GameDetailActivity.java index eed81bd549..9ffcac43f7 100644 --- a/app/src/main/java/com/gh/gamecenter/GameDetailActivity.java +++ b/app/src/main/java/com/gh/gamecenter/GameDetailActivity.java @@ -91,6 +91,7 @@ public class GameDetailActivity extends DetailActivity implements View.OnClickLi iv_share.setVisibility(View.VISIBLE); } downloadAddWord = adapter.getGameDetailEntity().getDownloadAddWord(); + downloadOffText = gameEntity.getDownloadOffText(); initDownload(true); } diff --git a/app/src/main/java/com/gh/gamecenter/MainActivity.java b/app/src/main/java/com/gh/gamecenter/MainActivity.java index 471a5141c4..9bfed1c9ce 100644 --- a/app/src/main/java/com/gh/gamecenter/MainActivity.java +++ b/app/src/main/java/com/gh/gamecenter/MainActivity.java @@ -962,7 +962,7 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene Intent toIntent = new Intent(MainActivity.this, clazz); if ("NewsActivity".equals(to) || "NewsDetailActivity".equals(to)) { toIntent.putExtra("newsId", getIntent().getExtras().getString("newsId")); - toIntent.putExtra("entrance", getIntent().getExtras().getString("entrance")); + toIntent.putExtra("entrance", "(插件跳转)"); } else if("DownloadManagerActivity".equals(to)) { String packageName = getIntent().getExtras().getString("packageName"); if (packageName != null) { @@ -971,7 +971,7 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene } } else if ("GameDetailsActivity".equals(to) || "GameDetailActivity".equals(to)) { toIntent.putExtra("gameId", getIntent().getExtras().getString("gameId")); - toIntent.putExtra("entrance", getIntent().getExtras().getString("entrance")); + toIntent.putExtra("entrance", "(插件跳转)"); } else if ("SubjectActivity".equals(to)) { toIntent.putExtra("id", getIntent().getExtras().getString("id")); toIntent.putExtra("name", getIntent().getExtras().getString("name")); @@ -993,12 +993,12 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene startActivity(intent); } else if (from.equals("mipush_news")) { Intent intent = new Intent(MainActivity.this, NewsDetailActivity.class); - intent.putExtra("entrance", "小米推送"); + intent.putExtra("entrance", "(小米推送)"); intent.putExtra("newsId", getIntent().getStringExtra("newsId")); startActivity(intent); } else if (from.equals("mipush_new_game")) { Intent intent = new Intent(MainActivity.this, GameDetailActivity.class); - intent.putExtra("entrance", "小米推送"); + intent.putExtra("entrance", "(小米推送)"); startActivity(intent); } else if (from.equals("mipush_plugin")) { Intent intent = new Intent(MainActivity.this, DownloadManagerActivity.class); diff --git a/app/src/main/java/com/gh/gamecenter/NewsDetailActivity.java b/app/src/main/java/com/gh/gamecenter/NewsDetailActivity.java index 7a684cced9..ed2cb4307e 100644 --- a/app/src/main/java/com/gh/gamecenter/NewsDetailActivity.java +++ b/app/src/main/java/com/gh/gamecenter/NewsDetailActivity.java @@ -339,6 +339,7 @@ public class NewsDetailActivity extends DetailActivity implements OnClickListene adapter.setGameEntity(gameEntity); adapter.notifyItemInserted(1); downloadAddWord = gameEntity.getDownloadAddWord(); + downloadOffText = gameEntity.getDownloadOffText(); initDownload(true); } } diff --git a/app/src/main/java/com/gh/gamecenter/ViewImageActivity.java b/app/src/main/java/com/gh/gamecenter/ViewImageActivity.java index d7e0721e9d..c8ed9f24cc 100644 --- a/app/src/main/java/com/gh/gamecenter/ViewImageActivity.java +++ b/app/src/main/java/com/gh/gamecenter/ViewImageActivity.java @@ -26,7 +26,6 @@ import com.gc.materialdesign.views.ProgressBarCircularIndeterminate; import com.gh.base.BaseActivity; import com.gh.common.util.DisplayUtils; import com.gh.common.util.ImageUtils; -import com.gh.common.util.Utils; import com.gh.common.view.Gh_RelativeLayout; import com.gh.common.view.Gh_RelativeLayout.OnSingleTapListener; diff --git a/app/src/main/java/com/gh/gamecenter/adapter/ConcernRecommendAdapter.java b/app/src/main/java/com/gh/gamecenter/adapter/ConcernRecommendAdapter.java index 727454a137..e4aba641a5 100644 --- a/app/src/main/java/com/gh/gamecenter/adapter/ConcernRecommendAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/adapter/ConcernRecommendAdapter.java @@ -80,7 +80,9 @@ public class ConcernRecommendAdapter extends RecyclerView.Adapter articleTypes; @SerializedName("share_code") private String shareCode; + @SerializedName("download_off_text") + private String downloadOffText; + + public void setDownloadOffText(String downloadOffText) { + this.downloadOffText = downloadOffText; + } + + public String getDownloadOffText() { + return downloadOffText; + } public ArrayList getArticleTypes() { return articleTypes; diff --git a/app/src/main/java/com/gh/gamecenter/entity/GameEntity.java b/app/src/main/java/com/gh/gamecenter/entity/GameEntity.java index eb65261ee7..d69cbb21fc 100644 --- a/app/src/main/java/com/gh/gamecenter/entity/GameEntity.java +++ b/app/src/main/java/com/gh/gamecenter/entity/GameEntity.java @@ -42,14 +42,25 @@ public class GameEntity { private String link; @SerializedName("concern_article_exists") - private boolean exists = true; + private boolean newsExists = true; - public boolean isExists() { - return exists; + @SerializedName("download_off_text") + private String downloadOffText; + + public void setDownloadOffText(String downloadOffText) { + this.downloadOffText = downloadOffText; } - public void setExists(boolean exists) { - this.exists = exists; + public String getDownloadOffText() { + return downloadOffText; + } + + public boolean isNewsExists() { + return newsExists; + } + + public void setNewsExists(boolean newsExists) { + this.newsExists = newsExists; } public String getLink() { @@ -181,15 +192,27 @@ public class GameEntity { gameEntity.setIcon(icon); gameEntity.setName(name); gameEntity.setBrief(brief); - gameEntity.setTag(new ArrayList<>(tag)); - gameEntity.setApk(new ArrayList<>(apk)); + if (tag != null) { + gameEntity.setTag(new ArrayList<>(tag)); + } + if (apk != null) { + gameEntity.setApk(new ArrayList<>(apk)); + } + if (collection != null) { + gameEntity.setCollection(new ArrayList<>(collection)); + } gameEntity.setSlide(slide); gameEntity.setTest(test); gameEntity.setDownloadAddWord(downloadAddWord); - gameEntity.setEntryMap(new ArrayMap(entryMap)); + if (entryMap != null) { + gameEntity.setEntryMap(new ArrayMap(entryMap)); + } gameEntity.setImage(image); gameEntity.setType(type); gameEntity.setPluggable(isPluggable); + gameEntity.setLink(link); + gameEntity.setNewsExists(newsExists); + gameEntity.setDownloadOffText(downloadOffText); return gameEntity; } diff --git a/app/src/main/java/com/gh/gamecenter/game/Game2FragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/game/Game2FragmentAdapter.java index 996c1c32f8..583ef67d8f 100644 --- a/app/src/main/java/com/gh/gamecenter/game/Game2FragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/game/Game2FragmentAdapter.java @@ -211,17 +211,17 @@ public class Game2FragmentAdapter extends RecyclerView.Adapter= offset && position <= subjectList.get(i).getData().size() + offset) { - int index = position -offset-1; + int index = position - offset - 1; if (index < 0) { index = 0; } gameEntity = subjectList.get(i).getData().get(index); - if (position == offset && !TextUtils.isEmpty(gameEntity.getImage())){ + if (position == offset && !TextUtils.isEmpty(gameEntity.getImage())) { return ItemViewType.GAME_IMAGE; - }else if (position == offset){ + } else if (position == offset) { return ItemViewType.COLUMN_HEADER; } - if (position == offset+1 && !TextUtils.isEmpty(subjectList.get(i).getData().get(0).getImage())) { + if (position == offset + 1 && !TextUtils.isEmpty(subjectList.get(i).getData().get(0).getImage())) { return ItemViewType.COLUMN_HEADER; } if (gameEntity.getTest() != null) { diff --git a/app/src/main/java/com/gh/gamecenter/news/News4Fragment.java b/app/src/main/java/com/gh/gamecenter/news/News4Fragment.java index 470218b399..c0e1d1fa3a 100644 --- a/app/src/main/java/com/gh/gamecenter/news/News4Fragment.java +++ b/app/src/main/java/com/gh/gamecenter/news/News4Fragment.java @@ -328,7 +328,7 @@ public class News4Fragment extends BaseFragment implements SwipeRefreshLayout.On if (finalI == size - 1) { recommendGameList.add(gameEntity); - } else if (gameEntity.isExists()) { + } else if (gameEntity.isNewsExists()) { installGameList.add(gameEntity); } } diff --git a/app/src/main/java/com/gh/gamecenter/news/News4FragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/news/News4FragmentAdapter.java index 1105719ca1..67eb2544b8 100644 --- a/app/src/main/java/com/gh/gamecenter/news/News4FragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/news/News4FragmentAdapter.java @@ -46,13 +46,13 @@ import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; -import org.apache.http.HttpStatus; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.lang.reflect.Type; +import java.net.HttpURLConnection; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -186,7 +186,7 @@ public class News4FragmentAdapter extends RecyclerView.Adapter extends Request { String responseString = null; String charset = HttpHeaderParser.parseCharset(response.headers); - if (HttpStatus.SC_NOT_MODIFIED == response.statusCode + if (HttpURLConnection.HTTP_NOT_MODIFIED == response.statusCode || (mGzipEnabled && isGzipped(response.headers)) - || HttpStatus.SC_OK == response.statusCode) { + || HttpURLConnection.HTTP_OK == response.statusCode) { try { byte[] data = GzipUtils.decompressBytes(response.data); responseString = new String(data, charset); diff --git a/app/src/main/res/layout/viewimage_gif_item.xml b/app/src/main/res/layout/viewimage_gif_item.xml deleted file mode 100644 index 10eac33703..0000000000 --- a/app/src/main/res/layout/viewimage_gif_item.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - \ No newline at end of file From 1578925613a734f9a33e97be151b3f60afd3e2f3 Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Tue, 25 Oct 2016 11:23:27 +0800 Subject: [PATCH 05/11] ... --- .../support/v7/util/AsyncListUtil.java | 592 ++++++++++++++++++ 1 file changed, 592 insertions(+) create mode 100644 app/src/main/java/android/support/v7/util/AsyncListUtil.java diff --git a/app/src/main/java/android/support/v7/util/AsyncListUtil.java b/app/src/main/java/android/support/v7/util/AsyncListUtil.java new file mode 100644 index 0000000000..c2a66b4fe8 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/AsyncListUtil.java @@ -0,0 +1,592 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import android.support.annotation.UiThread; +import android.support.annotation.WorkerThread; +import android.util.Log; +import android.util.SparseBooleanArray; +import android.util.SparseIntArray; + +/** + * A utility class that supports asynchronous content loading. + *

+ * It can be used to load Cursor data in chunks without querying the Cursor on the UI Thread while + * keeping UI and cache synchronous for better user experience. + *

+ * It loads the data on a background thread and keeps only a limited number of fixed sized + * chunks in memory at all times. + *

+ * {@link AsyncListUtil} queries the currently visible range through {@link ViewCallback}, + * loads the required data items in the background through {@link DataCallback}, and notifies a + * {@link ViewCallback} when the data is loaded. It may load some extra items for smoother + * scrolling. + *

+ * Note that this class uses a single thread to load the data, so it suitable to load data from + * secondary storage such as disk, but not from network. + *

+ * This class is designed to work with {@link android.support.v7.widget.RecyclerView}, but it does + * not depend on it and can be used with other list views. + * + */ +public class AsyncListUtil { + private static final String TAG = "AsyncListUtil"; + + private static final boolean DEBUG = false; + + final Class mTClass; + final int mTileSize; + final DataCallback mDataCallback; + final ViewCallback mViewCallback; + + final TileList mTileList; + + final ThreadUtil.MainThreadCallback mMainThreadProxy; + final ThreadUtil.BackgroundCallback mBackgroundProxy; + + final int[] mTmpRange = new int[2]; + final int[] mPrevRange = new int[2]; + final int[] mTmpRangeExtended = new int[2]; + + private boolean mAllowScrollHints; + private int mScrollHint = ViewCallback.HINT_SCROLL_NONE; + + private int mItemCount = 0; + + int mDisplayedGeneration = 0; + int mRequestedGeneration = mDisplayedGeneration; + + final private SparseIntArray mMissingPositions = new SparseIntArray(); + + private void log(String s, Object... args) { + Log.d(TAG, "[MAIN] " + String.format(s, args)); + } + + /** + * Creates an AsyncListUtil. + * + * @param klass Class of the data item. + * @param tileSize Number of item per chunk loaded at once. + * @param dataCallback Data access callback. + * @param viewCallback Callback for querying visible item range and update notifications. + */ + public AsyncListUtil(Class klass, int tileSize, DataCallback dataCallback, + ViewCallback viewCallback) { + mTClass = klass; + mTileSize = tileSize; + mDataCallback = dataCallback; + mViewCallback = viewCallback; + + mTileList = new TileList(mTileSize); + + ThreadUtil threadUtil = new MessageThreadUtil(); + mMainThreadProxy = threadUtil.getMainThreadProxy(mMainThreadCallback); + mBackgroundProxy = threadUtil.getBackgroundProxy(mBackgroundCallback); + + refresh(); + } + + private boolean isRefreshPending() { + return mRequestedGeneration != mDisplayedGeneration; + } + + /** + * Updates the currently visible item range. + * + *

+ * Identifies the data items that have not been loaded yet and initiates loading them in the + * background. Should be called from the view's scroll listener (such as + * {@link android.support.v7.widget.RecyclerView.OnScrollListener#onScrolled}). + */ + public void onRangeChanged() { + if (isRefreshPending()) { + return; // Will update range will the refresh result arrives. + } + updateRange(); + mAllowScrollHints = true; + } + + /** + * Forces reloading the data. + *

+ * Discards all the cached data and reloads all required data items for the currently visible + * range. To be called when the data item count and/or contents has changed. + */ + public void refresh() { + mMissingPositions.clear(); + mBackgroundProxy.refresh(++mRequestedGeneration); + } + + /** + * Returns the data item at the given position or null if it has not been loaded + * yet. + * + *

+ * If this method has been called for a specific position and returned null, then + * {@link ViewCallback#onItemLoaded(int)} will be called when it finally loads. Note that if + * this position stays outside of the cached item range (as defined by + * {@link ViewCallback#extendRangeInto} method), then the callback will never be called for + * this position. + * + * @param position Item position. + * + * @return The data item at the given position or null if it has not been loaded + * yet. + */ + public T getItem(int position) { + if (position < 0 || position >= mItemCount) { + throw new IndexOutOfBoundsException(position + " is not within 0 and " + mItemCount); + } + T item = mTileList.getItemAt(position); + if (item == null && !isRefreshPending()) { + mMissingPositions.put(position, 0); + } + return item; + } + + /** + * Returns the number of items in the data set. + * + *

+ * This is the number returned by a recent call to + * {@link DataCallback#refreshData()}. + * + * @return Number of items. + */ + public int getItemCount() { + return mItemCount; + } + + private void updateRange() { + mViewCallback.getItemRangeInto(mTmpRange); + if (mTmpRange[0] > mTmpRange[1] || mTmpRange[0] < 0) { + return; + } + if (mTmpRange[1] >= mItemCount) { + // Invalid range may arrive soon after the refresh. + return; + } + + if (!mAllowScrollHints) { + mScrollHint = ViewCallback.HINT_SCROLL_NONE; + } else if (mTmpRange[0] > mPrevRange[1] || mPrevRange[0] > mTmpRange[1]) { + // Ranges do not intersect, long leap not a scroll. + mScrollHint = ViewCallback.HINT_SCROLL_NONE; + } else if (mTmpRange[0] < mPrevRange[0]) { + mScrollHint = ViewCallback.HINT_SCROLL_DESC; + } else if (mTmpRange[0] > mPrevRange[0]) { + mScrollHint = ViewCallback.HINT_SCROLL_ASC; + } + + mPrevRange[0] = mTmpRange[0]; + mPrevRange[1] = mTmpRange[1]; + + mViewCallback.extendRangeInto(mTmpRange, mTmpRangeExtended, mScrollHint); + mTmpRangeExtended[0] = Math.min(mTmpRange[0], Math.max(mTmpRangeExtended[0], 0)); + mTmpRangeExtended[1] = + Math.max(mTmpRange[1], Math.min(mTmpRangeExtended[1], mItemCount - 1)); + + mBackgroundProxy.updateRange(mTmpRange[0], mTmpRange[1], + mTmpRangeExtended[0], mTmpRangeExtended[1], mScrollHint); + } + + private final ThreadUtil.MainThreadCallback + mMainThreadCallback = new ThreadUtil.MainThreadCallback() { + @Override + public void updateItemCount(int generation, int itemCount) { + if (DEBUG) { + log("updateItemCount: size=%d, gen #%d", itemCount, generation); + } + if (!isRequestedGeneration(generation)) { + return; + } + mItemCount = itemCount; + mViewCallback.onDataRefresh(); + mDisplayedGeneration = mRequestedGeneration; + recycleAllTiles(); + + mAllowScrollHints = false; // Will be set to true after a first real scroll. + // There will be no scroll event if the size change does not affect the current range. + updateRange(); + } + + @Override + public void addTile(int generation, TileList.Tile tile) { + if (!isRequestedGeneration(generation)) { + if (DEBUG) { + log("recycling an older generation tile @%d", tile.mStartPosition); + } + mBackgroundProxy.recycleTile(tile); + return; + } + TileList.Tile duplicate = mTileList.addOrReplace(tile); + if (duplicate != null) { + Log.e(TAG, "duplicate tile @" + duplicate.mStartPosition); + mBackgroundProxy.recycleTile(duplicate); + } + if (DEBUG) { + log("gen #%d, added tile @%d, total tiles: %d", + generation, tile.mStartPosition, mTileList.size()); + } + int endPosition = tile.mStartPosition + tile.mItemCount; + int index = 0; + while (index < mMissingPositions.size()) { + final int position = mMissingPositions.keyAt(index); + if (tile.mStartPosition <= position && position < endPosition) { + mMissingPositions.removeAt(index); + mViewCallback.onItemLoaded(position); + } else { + index++; + } + } + } + + @Override + public void removeTile(int generation, int position) { + if (!isRequestedGeneration(generation)) { + return; + } + TileList.Tile tile = mTileList.removeAtPos(position); + if (tile == null) { + Log.e(TAG, "tile not found @" + position); + return; + } + if (DEBUG) { + log("recycling tile @%d, total tiles: %d", tile.mStartPosition, mTileList.size()); + } + mBackgroundProxy.recycleTile(tile); + } + + private void recycleAllTiles() { + if (DEBUG) { + log("recycling all %d tiles", mTileList.size()); + } + for (int i = 0; i < mTileList.size(); i++) { + mBackgroundProxy.recycleTile(mTileList.getAtIndex(i)); + } + mTileList.clear(); + } + + private boolean isRequestedGeneration(int generation) { + return generation == mRequestedGeneration; + } + }; + + private final ThreadUtil.BackgroundCallback + mBackgroundCallback = new ThreadUtil.BackgroundCallback() { + + private TileList.Tile mRecycledRoot; + + final SparseBooleanArray mLoadedTiles = new SparseBooleanArray(); + + private int mGeneration; + private int mItemCount; + + private int mFirstRequiredTileStart; + private int mLastRequiredTileStart; + + @Override + public void refresh(int generation) { + mGeneration = generation; + mLoadedTiles.clear(); + mItemCount = mDataCallback.refreshData(); + mMainThreadProxy.updateItemCount(mGeneration, mItemCount); + } + + @Override + public void updateRange(int rangeStart, int rangeEnd, int extRangeStart, int extRangeEnd, + int scrollHint) { + if (DEBUG) { + log("updateRange: %d..%d extended to %d..%d, scroll hint: %d", + rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint); + } + + if (rangeStart > rangeEnd) { + return; + } + + final int firstVisibleTileStart = getTileStart(rangeStart); + final int lastVisibleTileStart = getTileStart(rangeEnd); + + mFirstRequiredTileStart = getTileStart(extRangeStart); + mLastRequiredTileStart = getTileStart(extRangeEnd); + if (DEBUG) { + log("requesting tile range: %d..%d", + mFirstRequiredTileStart, mLastRequiredTileStart); + } + + // All pending tile requests are removed by ThreadUtil at this point. + // Re-request all required tiles in the most optimal order. + if (scrollHint == ViewCallback.HINT_SCROLL_DESC) { + requestTiles(mFirstRequiredTileStart, lastVisibleTileStart, scrollHint, true); + requestTiles(lastVisibleTileStart + mTileSize, mLastRequiredTileStart, scrollHint, + false); + } else { + requestTiles(firstVisibleTileStart, mLastRequiredTileStart, scrollHint, false); + requestTiles(mFirstRequiredTileStart, firstVisibleTileStart - mTileSize, scrollHint, + true); + } + } + + private int getTileStart(int position) { + return position - position % mTileSize; + } + + private void requestTiles(int firstTileStart, int lastTileStart, int scrollHint, + boolean backwards) { + for (int i = firstTileStart; i <= lastTileStart; i += mTileSize) { + int tileStart = backwards ? (lastTileStart + firstTileStart - i) : i; + if (DEBUG) { + log("requesting tile @%d", tileStart); + } + mBackgroundProxy.loadTile(tileStart, scrollHint); + } + } + + @Override + public void loadTile(int position, int scrollHint) { + if (isTileLoaded(position)) { + if (DEBUG) { + log("already loaded tile @%d", position); + } + return; + } + TileList.Tile tile = acquireTile(); + tile.mStartPosition = position; + tile.mItemCount = Math.min(mTileSize, mItemCount - tile.mStartPosition); + mDataCallback.fillData(tile.mItems, tile.mStartPosition, tile.mItemCount); + flushTileCache(scrollHint); + addTile(tile); + } + + @Override + public void recycleTile(TileList.Tile tile) { + if (DEBUG) { + log("recycling tile @%d", tile.mStartPosition); + } + mDataCallback.recycleData(tile.mItems, tile.mItemCount); + + tile.mNext = mRecycledRoot; + mRecycledRoot = tile; + } + + private TileList.Tile acquireTile() { + if (mRecycledRoot != null) { + TileList.Tile result = mRecycledRoot; + mRecycledRoot = mRecycledRoot.mNext; + return result; + } + return new TileList.Tile(mTClass, mTileSize); + } + + private boolean isTileLoaded(int position) { + return mLoadedTiles.get(position); + } + + private void addTile(TileList.Tile tile) { + mLoadedTiles.put(tile.mStartPosition, true); + mMainThreadProxy.addTile(mGeneration, tile); + if (DEBUG) { + log("loaded tile @%d, total tiles: %d", tile.mStartPosition, mLoadedTiles.size()); + } + } + + private void removeTile(int position) { + mLoadedTiles.delete(position); + mMainThreadProxy.removeTile(mGeneration, position); + if (DEBUG) { + log("flushed tile @%d, total tiles: %s", position, mLoadedTiles.size()); + } + } + + private void flushTileCache(int scrollHint) { + final int cacheSizeLimit = mDataCallback.getMaxCachedTiles(); + while (mLoadedTiles.size() >= cacheSizeLimit) { + int firstLoadedTileStart = mLoadedTiles.keyAt(0); + int lastLoadedTileStart = mLoadedTiles.keyAt(mLoadedTiles.size() - 1); + int startMargin = mFirstRequiredTileStart - firstLoadedTileStart; + int endMargin = lastLoadedTileStart - mLastRequiredTileStart; + if (startMargin > 0 && (startMargin >= endMargin || + (scrollHint == ViewCallback.HINT_SCROLL_ASC))) { + removeTile(firstLoadedTileStart); + } else if (endMargin > 0 && (startMargin < endMargin || + (scrollHint == ViewCallback.HINT_SCROLL_DESC))){ + removeTile(lastLoadedTileStart); + } else { + // Could not flush on either side, bail out. + return; + } + } + } + + private void log(String s, Object... args) { + Log.d(TAG, "[BKGR] " + String.format(s, args)); + } + }; + + /** + * The callback that provides data access for {@link AsyncListUtil}. + * + *

+ * All methods are called on the background thread. + */ + public static abstract class DataCallback { + + /** + * Refresh the data set and return the new data item count. + * + *

+ * If the data is being accessed through {@link android.database.Cursor} this is where + * the new cursor should be created. + * + * @return Data item count. + */ + @WorkerThread + public abstract int refreshData(); + + /** + * Fill the given tile. + * + *

+ * The provided tile might be a recycled tile, in which case it will already have objects. + * It is suggested to re-use these objects if possible in your use case. + * + * @param startPosition The start position in the list. + * @param itemCount The data item count. + * @param data The data item array to fill into. Should not be accessed beyond + * itemCount. + */ + @WorkerThread + public abstract void fillData(T[] data, int startPosition, int itemCount); + + /** + * Recycle the objects created in {@link #fillData} if necessary. + * + * + * @param data Array of data items. Should not be accessed beyond itemCount. + * @param itemCount The data item count. + */ + @WorkerThread + public void recycleData(T[] data, int itemCount) { + } + + /** + * Returns tile cache size limit (in tiles). + * + *

+ * The actual number of cached tiles will be the maximum of this value and the number of + * tiles that is required to cover the range returned by + * {@link ViewCallback#extendRangeInto(int[], int[], int)}. + *

+ * For example, if this method returns 10, and the most + * recent call to {@link ViewCallback#extendRangeInto(int[], int[], int)} returned + * {100, 179}, and the tile size is 5, then the maximum number of cached tiles will be 16. + *

+ * However, if the tile size is 20, then the maximum number of cached tiles will be 10. + *

+ * The default implementation returns 10. + * + * @return Maximum cache size. + */ + @WorkerThread + public int getMaxCachedTiles() { + return 10; + } + } + + /** + * The callback that links {@link AsyncListUtil} with the list view. + * + *

+ * All methods are called on the main thread. + */ + public static abstract class ViewCallback { + + /** + * No scroll direction hint available. + */ + public static final int HINT_SCROLL_NONE = 0; + + /** + * Scrolling in descending order (from higher to lower positions in the order of the backing + * storage). + */ + public static final int HINT_SCROLL_DESC = 1; + + /** + * Scrolling in ascending order (from lower to higher positions in the order of the backing + * storage). + */ + public static final int HINT_SCROLL_ASC = 2; + + /** + * Compute the range of visible item positions. + *

+ * outRange[0] is the position of the first visible item (in the order of the backing + * storage). + *

+ * outRange[1] is the position of the last visible item (in the order of the backing + * storage). + *

+ * Negative positions and positions greater or equal to {@link #getItemCount} are invalid. + * If the returned range contains invalid positions it is ignored (no item will be loaded). + * + * @param outRange The visible item range. + */ + @UiThread + public abstract void getItemRangeInto(int[] outRange); + + /** + * Compute a wider range of items that will be loaded for smoother scrolling. + * + *

+ * If there is no scroll hint, the default implementation extends the visible range by half + * its length in both directions. If there is a scroll hint, the range is extended by + * its full length in the scroll direction, and by half in the other direction. + *

+ * For example, if range is {100, 200} and scrollHint + * is {@link #HINT_SCROLL_ASC}, then outRange will be {50, 300}. + *

+ * However, if scrollHint is {@link #HINT_SCROLL_NONE}, then + * outRange will be {50, 250} + * + * @param range Visible item range. + * @param outRange Extended range. + * @param scrollHint The scroll direction hint. + */ + @UiThread + public void extendRangeInto(int[] range, int[] outRange, int scrollHint) { + final int fullRange = range[1] - range[0] + 1; + final int halfRange = fullRange / 2; + outRange[0] = range[0] - (scrollHint == HINT_SCROLL_DESC ? fullRange : halfRange); + outRange[1] = range[1] + (scrollHint == HINT_SCROLL_ASC ? fullRange : halfRange); + } + + /** + * Called when the entire data set has changed. + */ + @UiThread + public abstract void onDataRefresh(); + + /** + * Called when an item at the given position is loaded. + * @param position Item position. + */ + @UiThread + public abstract void onItemLoaded(int position); + } +} From 95a3d7b494d239feb5efa2d6e00630442bf4c968 Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Tue, 25 Oct 2016 11:23:48 +0800 Subject: [PATCH 06/11] ... --- .../support/v7/util/AsyncListUtil.java | 592 ------------------ 1 file changed, 592 deletions(-) delete mode 100644 app/src/main/java/android/support/v7/util/AsyncListUtil.java diff --git a/app/src/main/java/android/support/v7/util/AsyncListUtil.java b/app/src/main/java/android/support/v7/util/AsyncListUtil.java deleted file mode 100644 index c2a66b4fe8..0000000000 --- a/app/src/main/java/android/support/v7/util/AsyncListUtil.java +++ /dev/null @@ -1,592 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import android.support.annotation.UiThread; -import android.support.annotation.WorkerThread; -import android.util.Log; -import android.util.SparseBooleanArray; -import android.util.SparseIntArray; - -/** - * A utility class that supports asynchronous content loading. - *

- * It can be used to load Cursor data in chunks without querying the Cursor on the UI Thread while - * keeping UI and cache synchronous for better user experience. - *

- * It loads the data on a background thread and keeps only a limited number of fixed sized - * chunks in memory at all times. - *

- * {@link AsyncListUtil} queries the currently visible range through {@link ViewCallback}, - * loads the required data items in the background through {@link DataCallback}, and notifies a - * {@link ViewCallback} when the data is loaded. It may load some extra items for smoother - * scrolling. - *

- * Note that this class uses a single thread to load the data, so it suitable to load data from - * secondary storage such as disk, but not from network. - *

- * This class is designed to work with {@link android.support.v7.widget.RecyclerView}, but it does - * not depend on it and can be used with other list views. - * - */ -public class AsyncListUtil { - private static final String TAG = "AsyncListUtil"; - - private static final boolean DEBUG = false; - - final Class mTClass; - final int mTileSize; - final DataCallback mDataCallback; - final ViewCallback mViewCallback; - - final TileList mTileList; - - final ThreadUtil.MainThreadCallback mMainThreadProxy; - final ThreadUtil.BackgroundCallback mBackgroundProxy; - - final int[] mTmpRange = new int[2]; - final int[] mPrevRange = new int[2]; - final int[] mTmpRangeExtended = new int[2]; - - private boolean mAllowScrollHints; - private int mScrollHint = ViewCallback.HINT_SCROLL_NONE; - - private int mItemCount = 0; - - int mDisplayedGeneration = 0; - int mRequestedGeneration = mDisplayedGeneration; - - final private SparseIntArray mMissingPositions = new SparseIntArray(); - - private void log(String s, Object... args) { - Log.d(TAG, "[MAIN] " + String.format(s, args)); - } - - /** - * Creates an AsyncListUtil. - * - * @param klass Class of the data item. - * @param tileSize Number of item per chunk loaded at once. - * @param dataCallback Data access callback. - * @param viewCallback Callback for querying visible item range and update notifications. - */ - public AsyncListUtil(Class klass, int tileSize, DataCallback dataCallback, - ViewCallback viewCallback) { - mTClass = klass; - mTileSize = tileSize; - mDataCallback = dataCallback; - mViewCallback = viewCallback; - - mTileList = new TileList(mTileSize); - - ThreadUtil threadUtil = new MessageThreadUtil(); - mMainThreadProxy = threadUtil.getMainThreadProxy(mMainThreadCallback); - mBackgroundProxy = threadUtil.getBackgroundProxy(mBackgroundCallback); - - refresh(); - } - - private boolean isRefreshPending() { - return mRequestedGeneration != mDisplayedGeneration; - } - - /** - * Updates the currently visible item range. - * - *

- * Identifies the data items that have not been loaded yet and initiates loading them in the - * background. Should be called from the view's scroll listener (such as - * {@link android.support.v7.widget.RecyclerView.OnScrollListener#onScrolled}). - */ - public void onRangeChanged() { - if (isRefreshPending()) { - return; // Will update range will the refresh result arrives. - } - updateRange(); - mAllowScrollHints = true; - } - - /** - * Forces reloading the data. - *

- * Discards all the cached data and reloads all required data items for the currently visible - * range. To be called when the data item count and/or contents has changed. - */ - public void refresh() { - mMissingPositions.clear(); - mBackgroundProxy.refresh(++mRequestedGeneration); - } - - /** - * Returns the data item at the given position or null if it has not been loaded - * yet. - * - *

- * If this method has been called for a specific position and returned null, then - * {@link ViewCallback#onItemLoaded(int)} will be called when it finally loads. Note that if - * this position stays outside of the cached item range (as defined by - * {@link ViewCallback#extendRangeInto} method), then the callback will never be called for - * this position. - * - * @param position Item position. - * - * @return The data item at the given position or null if it has not been loaded - * yet. - */ - public T getItem(int position) { - if (position < 0 || position >= mItemCount) { - throw new IndexOutOfBoundsException(position + " is not within 0 and " + mItemCount); - } - T item = mTileList.getItemAt(position); - if (item == null && !isRefreshPending()) { - mMissingPositions.put(position, 0); - } - return item; - } - - /** - * Returns the number of items in the data set. - * - *

- * This is the number returned by a recent call to - * {@link DataCallback#refreshData()}. - * - * @return Number of items. - */ - public int getItemCount() { - return mItemCount; - } - - private void updateRange() { - mViewCallback.getItemRangeInto(mTmpRange); - if (mTmpRange[0] > mTmpRange[1] || mTmpRange[0] < 0) { - return; - } - if (mTmpRange[1] >= mItemCount) { - // Invalid range may arrive soon after the refresh. - return; - } - - if (!mAllowScrollHints) { - mScrollHint = ViewCallback.HINT_SCROLL_NONE; - } else if (mTmpRange[0] > mPrevRange[1] || mPrevRange[0] > mTmpRange[1]) { - // Ranges do not intersect, long leap not a scroll. - mScrollHint = ViewCallback.HINT_SCROLL_NONE; - } else if (mTmpRange[0] < mPrevRange[0]) { - mScrollHint = ViewCallback.HINT_SCROLL_DESC; - } else if (mTmpRange[0] > mPrevRange[0]) { - mScrollHint = ViewCallback.HINT_SCROLL_ASC; - } - - mPrevRange[0] = mTmpRange[0]; - mPrevRange[1] = mTmpRange[1]; - - mViewCallback.extendRangeInto(mTmpRange, mTmpRangeExtended, mScrollHint); - mTmpRangeExtended[0] = Math.min(mTmpRange[0], Math.max(mTmpRangeExtended[0], 0)); - mTmpRangeExtended[1] = - Math.max(mTmpRange[1], Math.min(mTmpRangeExtended[1], mItemCount - 1)); - - mBackgroundProxy.updateRange(mTmpRange[0], mTmpRange[1], - mTmpRangeExtended[0], mTmpRangeExtended[1], mScrollHint); - } - - private final ThreadUtil.MainThreadCallback - mMainThreadCallback = new ThreadUtil.MainThreadCallback() { - @Override - public void updateItemCount(int generation, int itemCount) { - if (DEBUG) { - log("updateItemCount: size=%d, gen #%d", itemCount, generation); - } - if (!isRequestedGeneration(generation)) { - return; - } - mItemCount = itemCount; - mViewCallback.onDataRefresh(); - mDisplayedGeneration = mRequestedGeneration; - recycleAllTiles(); - - mAllowScrollHints = false; // Will be set to true after a first real scroll. - // There will be no scroll event if the size change does not affect the current range. - updateRange(); - } - - @Override - public void addTile(int generation, TileList.Tile tile) { - if (!isRequestedGeneration(generation)) { - if (DEBUG) { - log("recycling an older generation tile @%d", tile.mStartPosition); - } - mBackgroundProxy.recycleTile(tile); - return; - } - TileList.Tile duplicate = mTileList.addOrReplace(tile); - if (duplicate != null) { - Log.e(TAG, "duplicate tile @" + duplicate.mStartPosition); - mBackgroundProxy.recycleTile(duplicate); - } - if (DEBUG) { - log("gen #%d, added tile @%d, total tiles: %d", - generation, tile.mStartPosition, mTileList.size()); - } - int endPosition = tile.mStartPosition + tile.mItemCount; - int index = 0; - while (index < mMissingPositions.size()) { - final int position = mMissingPositions.keyAt(index); - if (tile.mStartPosition <= position && position < endPosition) { - mMissingPositions.removeAt(index); - mViewCallback.onItemLoaded(position); - } else { - index++; - } - } - } - - @Override - public void removeTile(int generation, int position) { - if (!isRequestedGeneration(generation)) { - return; - } - TileList.Tile tile = mTileList.removeAtPos(position); - if (tile == null) { - Log.e(TAG, "tile not found @" + position); - return; - } - if (DEBUG) { - log("recycling tile @%d, total tiles: %d", tile.mStartPosition, mTileList.size()); - } - mBackgroundProxy.recycleTile(tile); - } - - private void recycleAllTiles() { - if (DEBUG) { - log("recycling all %d tiles", mTileList.size()); - } - for (int i = 0; i < mTileList.size(); i++) { - mBackgroundProxy.recycleTile(mTileList.getAtIndex(i)); - } - mTileList.clear(); - } - - private boolean isRequestedGeneration(int generation) { - return generation == mRequestedGeneration; - } - }; - - private final ThreadUtil.BackgroundCallback - mBackgroundCallback = new ThreadUtil.BackgroundCallback() { - - private TileList.Tile mRecycledRoot; - - final SparseBooleanArray mLoadedTiles = new SparseBooleanArray(); - - private int mGeneration; - private int mItemCount; - - private int mFirstRequiredTileStart; - private int mLastRequiredTileStart; - - @Override - public void refresh(int generation) { - mGeneration = generation; - mLoadedTiles.clear(); - mItemCount = mDataCallback.refreshData(); - mMainThreadProxy.updateItemCount(mGeneration, mItemCount); - } - - @Override - public void updateRange(int rangeStart, int rangeEnd, int extRangeStart, int extRangeEnd, - int scrollHint) { - if (DEBUG) { - log("updateRange: %d..%d extended to %d..%d, scroll hint: %d", - rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint); - } - - if (rangeStart > rangeEnd) { - return; - } - - final int firstVisibleTileStart = getTileStart(rangeStart); - final int lastVisibleTileStart = getTileStart(rangeEnd); - - mFirstRequiredTileStart = getTileStart(extRangeStart); - mLastRequiredTileStart = getTileStart(extRangeEnd); - if (DEBUG) { - log("requesting tile range: %d..%d", - mFirstRequiredTileStart, mLastRequiredTileStart); - } - - // All pending tile requests are removed by ThreadUtil at this point. - // Re-request all required tiles in the most optimal order. - if (scrollHint == ViewCallback.HINT_SCROLL_DESC) { - requestTiles(mFirstRequiredTileStart, lastVisibleTileStart, scrollHint, true); - requestTiles(lastVisibleTileStart + mTileSize, mLastRequiredTileStart, scrollHint, - false); - } else { - requestTiles(firstVisibleTileStart, mLastRequiredTileStart, scrollHint, false); - requestTiles(mFirstRequiredTileStart, firstVisibleTileStart - mTileSize, scrollHint, - true); - } - } - - private int getTileStart(int position) { - return position - position % mTileSize; - } - - private void requestTiles(int firstTileStart, int lastTileStart, int scrollHint, - boolean backwards) { - for (int i = firstTileStart; i <= lastTileStart; i += mTileSize) { - int tileStart = backwards ? (lastTileStart + firstTileStart - i) : i; - if (DEBUG) { - log("requesting tile @%d", tileStart); - } - mBackgroundProxy.loadTile(tileStart, scrollHint); - } - } - - @Override - public void loadTile(int position, int scrollHint) { - if (isTileLoaded(position)) { - if (DEBUG) { - log("already loaded tile @%d", position); - } - return; - } - TileList.Tile tile = acquireTile(); - tile.mStartPosition = position; - tile.mItemCount = Math.min(mTileSize, mItemCount - tile.mStartPosition); - mDataCallback.fillData(tile.mItems, tile.mStartPosition, tile.mItemCount); - flushTileCache(scrollHint); - addTile(tile); - } - - @Override - public void recycleTile(TileList.Tile tile) { - if (DEBUG) { - log("recycling tile @%d", tile.mStartPosition); - } - mDataCallback.recycleData(tile.mItems, tile.mItemCount); - - tile.mNext = mRecycledRoot; - mRecycledRoot = tile; - } - - private TileList.Tile acquireTile() { - if (mRecycledRoot != null) { - TileList.Tile result = mRecycledRoot; - mRecycledRoot = mRecycledRoot.mNext; - return result; - } - return new TileList.Tile(mTClass, mTileSize); - } - - private boolean isTileLoaded(int position) { - return mLoadedTiles.get(position); - } - - private void addTile(TileList.Tile tile) { - mLoadedTiles.put(tile.mStartPosition, true); - mMainThreadProxy.addTile(mGeneration, tile); - if (DEBUG) { - log("loaded tile @%d, total tiles: %d", tile.mStartPosition, mLoadedTiles.size()); - } - } - - private void removeTile(int position) { - mLoadedTiles.delete(position); - mMainThreadProxy.removeTile(mGeneration, position); - if (DEBUG) { - log("flushed tile @%d, total tiles: %s", position, mLoadedTiles.size()); - } - } - - private void flushTileCache(int scrollHint) { - final int cacheSizeLimit = mDataCallback.getMaxCachedTiles(); - while (mLoadedTiles.size() >= cacheSizeLimit) { - int firstLoadedTileStart = mLoadedTiles.keyAt(0); - int lastLoadedTileStart = mLoadedTiles.keyAt(mLoadedTiles.size() - 1); - int startMargin = mFirstRequiredTileStart - firstLoadedTileStart; - int endMargin = lastLoadedTileStart - mLastRequiredTileStart; - if (startMargin > 0 && (startMargin >= endMargin || - (scrollHint == ViewCallback.HINT_SCROLL_ASC))) { - removeTile(firstLoadedTileStart); - } else if (endMargin > 0 && (startMargin < endMargin || - (scrollHint == ViewCallback.HINT_SCROLL_DESC))){ - removeTile(lastLoadedTileStart); - } else { - // Could not flush on either side, bail out. - return; - } - } - } - - private void log(String s, Object... args) { - Log.d(TAG, "[BKGR] " + String.format(s, args)); - } - }; - - /** - * The callback that provides data access for {@link AsyncListUtil}. - * - *

- * All methods are called on the background thread. - */ - public static abstract class DataCallback { - - /** - * Refresh the data set and return the new data item count. - * - *

- * If the data is being accessed through {@link android.database.Cursor} this is where - * the new cursor should be created. - * - * @return Data item count. - */ - @WorkerThread - public abstract int refreshData(); - - /** - * Fill the given tile. - * - *

- * The provided tile might be a recycled tile, in which case it will already have objects. - * It is suggested to re-use these objects if possible in your use case. - * - * @param startPosition The start position in the list. - * @param itemCount The data item count. - * @param data The data item array to fill into. Should not be accessed beyond - * itemCount. - */ - @WorkerThread - public abstract void fillData(T[] data, int startPosition, int itemCount); - - /** - * Recycle the objects created in {@link #fillData} if necessary. - * - * - * @param data Array of data items. Should not be accessed beyond itemCount. - * @param itemCount The data item count. - */ - @WorkerThread - public void recycleData(T[] data, int itemCount) { - } - - /** - * Returns tile cache size limit (in tiles). - * - *

- * The actual number of cached tiles will be the maximum of this value and the number of - * tiles that is required to cover the range returned by - * {@link ViewCallback#extendRangeInto(int[], int[], int)}. - *

- * For example, if this method returns 10, and the most - * recent call to {@link ViewCallback#extendRangeInto(int[], int[], int)} returned - * {100, 179}, and the tile size is 5, then the maximum number of cached tiles will be 16. - *

- * However, if the tile size is 20, then the maximum number of cached tiles will be 10. - *

- * The default implementation returns 10. - * - * @return Maximum cache size. - */ - @WorkerThread - public int getMaxCachedTiles() { - return 10; - } - } - - /** - * The callback that links {@link AsyncListUtil} with the list view. - * - *

- * All methods are called on the main thread. - */ - public static abstract class ViewCallback { - - /** - * No scroll direction hint available. - */ - public static final int HINT_SCROLL_NONE = 0; - - /** - * Scrolling in descending order (from higher to lower positions in the order of the backing - * storage). - */ - public static final int HINT_SCROLL_DESC = 1; - - /** - * Scrolling in ascending order (from lower to higher positions in the order of the backing - * storage). - */ - public static final int HINT_SCROLL_ASC = 2; - - /** - * Compute the range of visible item positions. - *

- * outRange[0] is the position of the first visible item (in the order of the backing - * storage). - *

- * outRange[1] is the position of the last visible item (in the order of the backing - * storage). - *

- * Negative positions and positions greater or equal to {@link #getItemCount} are invalid. - * If the returned range contains invalid positions it is ignored (no item will be loaded). - * - * @param outRange The visible item range. - */ - @UiThread - public abstract void getItemRangeInto(int[] outRange); - - /** - * Compute a wider range of items that will be loaded for smoother scrolling. - * - *

- * If there is no scroll hint, the default implementation extends the visible range by half - * its length in both directions. If there is a scroll hint, the range is extended by - * its full length in the scroll direction, and by half in the other direction. - *

- * For example, if range is {100, 200} and scrollHint - * is {@link #HINT_SCROLL_ASC}, then outRange will be {50, 300}. - *

- * However, if scrollHint is {@link #HINT_SCROLL_NONE}, then - * outRange will be {50, 250} - * - * @param range Visible item range. - * @param outRange Extended range. - * @param scrollHint The scroll direction hint. - */ - @UiThread - public void extendRangeInto(int[] range, int[] outRange, int scrollHint) { - final int fullRange = range[1] - range[0] + 1; - final int halfRange = fullRange / 2; - outRange[0] = range[0] - (scrollHint == HINT_SCROLL_DESC ? fullRange : halfRange); - outRange[1] = range[1] + (scrollHint == HINT_SCROLL_ASC ? fullRange : halfRange); - } - - /** - * Called when the entire data set has changed. - */ - @UiThread - public abstract void onDataRefresh(); - - /** - * Called when an item at the given position is loaded. - * @param position Item position. - */ - @UiThread - public abstract void onItemLoaded(int position); - } -} From e873b539eb32dfea49d06bb7008cbb8ab12f862f Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Tue, 25 Oct 2016 11:24:31 +0800 Subject: [PATCH 07/11] ... --- .../support/v7/util/AsyncListUtil.java | 592 ++++++++++++ .../v7/util/BatchingListUpdateCallback.java | 123 +++ .../android/support/v7/util/DiffUtil.java | 856 ++++++++++++++++++ .../support/v7/util/ListUpdateCallback.java | 55 ++ .../support/v7/util/MessageThreadUtil.java | 283 ++++++ .../android/support/v7/util/SortedList.java | 821 +++++++++++++++++ .../android/support/v7/util/ThreadUtil.java | 45 + .../android/support/v7/util/TileList.java | 105 +++ 8 files changed, 2880 insertions(+) create mode 100644 app/src/main/java/android/support/v7/util/AsyncListUtil.java create mode 100644 app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java create mode 100644 app/src/main/java/android/support/v7/util/DiffUtil.java create mode 100644 app/src/main/java/android/support/v7/util/ListUpdateCallback.java create mode 100644 app/src/main/java/android/support/v7/util/MessageThreadUtil.java create mode 100644 app/src/main/java/android/support/v7/util/SortedList.java create mode 100644 app/src/main/java/android/support/v7/util/ThreadUtil.java create mode 100644 app/src/main/java/android/support/v7/util/TileList.java diff --git a/app/src/main/java/android/support/v7/util/AsyncListUtil.java b/app/src/main/java/android/support/v7/util/AsyncListUtil.java new file mode 100644 index 0000000000..c2a66b4fe8 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/AsyncListUtil.java @@ -0,0 +1,592 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import android.support.annotation.UiThread; +import android.support.annotation.WorkerThread; +import android.util.Log; +import android.util.SparseBooleanArray; +import android.util.SparseIntArray; + +/** + * A utility class that supports asynchronous content loading. + *

+ * It can be used to load Cursor data in chunks without querying the Cursor on the UI Thread while + * keeping UI and cache synchronous for better user experience. + *

+ * It loads the data on a background thread and keeps only a limited number of fixed sized + * chunks in memory at all times. + *

+ * {@link AsyncListUtil} queries the currently visible range through {@link ViewCallback}, + * loads the required data items in the background through {@link DataCallback}, and notifies a + * {@link ViewCallback} when the data is loaded. It may load some extra items for smoother + * scrolling. + *

+ * Note that this class uses a single thread to load the data, so it suitable to load data from + * secondary storage such as disk, but not from network. + *

+ * This class is designed to work with {@link android.support.v7.widget.RecyclerView}, but it does + * not depend on it and can be used with other list views. + * + */ +public class AsyncListUtil { + private static final String TAG = "AsyncListUtil"; + + private static final boolean DEBUG = false; + + final Class mTClass; + final int mTileSize; + final DataCallback mDataCallback; + final ViewCallback mViewCallback; + + final TileList mTileList; + + final ThreadUtil.MainThreadCallback mMainThreadProxy; + final ThreadUtil.BackgroundCallback mBackgroundProxy; + + final int[] mTmpRange = new int[2]; + final int[] mPrevRange = new int[2]; + final int[] mTmpRangeExtended = new int[2]; + + private boolean mAllowScrollHints; + private int mScrollHint = ViewCallback.HINT_SCROLL_NONE; + + private int mItemCount = 0; + + int mDisplayedGeneration = 0; + int mRequestedGeneration = mDisplayedGeneration; + + final private SparseIntArray mMissingPositions = new SparseIntArray(); + + private void log(String s, Object... args) { + Log.d(TAG, "[MAIN] " + String.format(s, args)); + } + + /** + * Creates an AsyncListUtil. + * + * @param klass Class of the data item. + * @param tileSize Number of item per chunk loaded at once. + * @param dataCallback Data access callback. + * @param viewCallback Callback for querying visible item range and update notifications. + */ + public AsyncListUtil(Class klass, int tileSize, DataCallback dataCallback, + ViewCallback viewCallback) { + mTClass = klass; + mTileSize = tileSize; + mDataCallback = dataCallback; + mViewCallback = viewCallback; + + mTileList = new TileList(mTileSize); + + ThreadUtil threadUtil = new MessageThreadUtil(); + mMainThreadProxy = threadUtil.getMainThreadProxy(mMainThreadCallback); + mBackgroundProxy = threadUtil.getBackgroundProxy(mBackgroundCallback); + + refresh(); + } + + private boolean isRefreshPending() { + return mRequestedGeneration != mDisplayedGeneration; + } + + /** + * Updates the currently visible item range. + * + *

+ * Identifies the data items that have not been loaded yet and initiates loading them in the + * background. Should be called from the view's scroll listener (such as + * {@link android.support.v7.widget.RecyclerView.OnScrollListener#onScrolled}). + */ + public void onRangeChanged() { + if (isRefreshPending()) { + return; // Will update range will the refresh result arrives. + } + updateRange(); + mAllowScrollHints = true; + } + + /** + * Forces reloading the data. + *

+ * Discards all the cached data and reloads all required data items for the currently visible + * range. To be called when the data item count and/or contents has changed. + */ + public void refresh() { + mMissingPositions.clear(); + mBackgroundProxy.refresh(++mRequestedGeneration); + } + + /** + * Returns the data item at the given position or null if it has not been loaded + * yet. + * + *

+ * If this method has been called for a specific position and returned null, then + * {@link ViewCallback#onItemLoaded(int)} will be called when it finally loads. Note that if + * this position stays outside of the cached item range (as defined by + * {@link ViewCallback#extendRangeInto} method), then the callback will never be called for + * this position. + * + * @param position Item position. + * + * @return The data item at the given position or null if it has not been loaded + * yet. + */ + public T getItem(int position) { + if (position < 0 || position >= mItemCount) { + throw new IndexOutOfBoundsException(position + " is not within 0 and " + mItemCount); + } + T item = mTileList.getItemAt(position); + if (item == null && !isRefreshPending()) { + mMissingPositions.put(position, 0); + } + return item; + } + + /** + * Returns the number of items in the data set. + * + *

+ * This is the number returned by a recent call to + * {@link DataCallback#refreshData()}. + * + * @return Number of items. + */ + public int getItemCount() { + return mItemCount; + } + + private void updateRange() { + mViewCallback.getItemRangeInto(mTmpRange); + if (mTmpRange[0] > mTmpRange[1] || mTmpRange[0] < 0) { + return; + } + if (mTmpRange[1] >= mItemCount) { + // Invalid range may arrive soon after the refresh. + return; + } + + if (!mAllowScrollHints) { + mScrollHint = ViewCallback.HINT_SCROLL_NONE; + } else if (mTmpRange[0] > mPrevRange[1] || mPrevRange[0] > mTmpRange[1]) { + // Ranges do not intersect, long leap not a scroll. + mScrollHint = ViewCallback.HINT_SCROLL_NONE; + } else if (mTmpRange[0] < mPrevRange[0]) { + mScrollHint = ViewCallback.HINT_SCROLL_DESC; + } else if (mTmpRange[0] > mPrevRange[0]) { + mScrollHint = ViewCallback.HINT_SCROLL_ASC; + } + + mPrevRange[0] = mTmpRange[0]; + mPrevRange[1] = mTmpRange[1]; + + mViewCallback.extendRangeInto(mTmpRange, mTmpRangeExtended, mScrollHint); + mTmpRangeExtended[0] = Math.min(mTmpRange[0], Math.max(mTmpRangeExtended[0], 0)); + mTmpRangeExtended[1] = + Math.max(mTmpRange[1], Math.min(mTmpRangeExtended[1], mItemCount - 1)); + + mBackgroundProxy.updateRange(mTmpRange[0], mTmpRange[1], + mTmpRangeExtended[0], mTmpRangeExtended[1], mScrollHint); + } + + private final ThreadUtil.MainThreadCallback + mMainThreadCallback = new ThreadUtil.MainThreadCallback() { + @Override + public void updateItemCount(int generation, int itemCount) { + if (DEBUG) { + log("updateItemCount: size=%d, gen #%d", itemCount, generation); + } + if (!isRequestedGeneration(generation)) { + return; + } + mItemCount = itemCount; + mViewCallback.onDataRefresh(); + mDisplayedGeneration = mRequestedGeneration; + recycleAllTiles(); + + mAllowScrollHints = false; // Will be set to true after a first real scroll. + // There will be no scroll event if the size change does not affect the current range. + updateRange(); + } + + @Override + public void addTile(int generation, TileList.Tile tile) { + if (!isRequestedGeneration(generation)) { + if (DEBUG) { + log("recycling an older generation tile @%d", tile.mStartPosition); + } + mBackgroundProxy.recycleTile(tile); + return; + } + TileList.Tile duplicate = mTileList.addOrReplace(tile); + if (duplicate != null) { + Log.e(TAG, "duplicate tile @" + duplicate.mStartPosition); + mBackgroundProxy.recycleTile(duplicate); + } + if (DEBUG) { + log("gen #%d, added tile @%d, total tiles: %d", + generation, tile.mStartPosition, mTileList.size()); + } + int endPosition = tile.mStartPosition + tile.mItemCount; + int index = 0; + while (index < mMissingPositions.size()) { + final int position = mMissingPositions.keyAt(index); + if (tile.mStartPosition <= position && position < endPosition) { + mMissingPositions.removeAt(index); + mViewCallback.onItemLoaded(position); + } else { + index++; + } + } + } + + @Override + public void removeTile(int generation, int position) { + if (!isRequestedGeneration(generation)) { + return; + } + TileList.Tile tile = mTileList.removeAtPos(position); + if (tile == null) { + Log.e(TAG, "tile not found @" + position); + return; + } + if (DEBUG) { + log("recycling tile @%d, total tiles: %d", tile.mStartPosition, mTileList.size()); + } + mBackgroundProxy.recycleTile(tile); + } + + private void recycleAllTiles() { + if (DEBUG) { + log("recycling all %d tiles", mTileList.size()); + } + for (int i = 0; i < mTileList.size(); i++) { + mBackgroundProxy.recycleTile(mTileList.getAtIndex(i)); + } + mTileList.clear(); + } + + private boolean isRequestedGeneration(int generation) { + return generation == mRequestedGeneration; + } + }; + + private final ThreadUtil.BackgroundCallback + mBackgroundCallback = new ThreadUtil.BackgroundCallback() { + + private TileList.Tile mRecycledRoot; + + final SparseBooleanArray mLoadedTiles = new SparseBooleanArray(); + + private int mGeneration; + private int mItemCount; + + private int mFirstRequiredTileStart; + private int mLastRequiredTileStart; + + @Override + public void refresh(int generation) { + mGeneration = generation; + mLoadedTiles.clear(); + mItemCount = mDataCallback.refreshData(); + mMainThreadProxy.updateItemCount(mGeneration, mItemCount); + } + + @Override + public void updateRange(int rangeStart, int rangeEnd, int extRangeStart, int extRangeEnd, + int scrollHint) { + if (DEBUG) { + log("updateRange: %d..%d extended to %d..%d, scroll hint: %d", + rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint); + } + + if (rangeStart > rangeEnd) { + return; + } + + final int firstVisibleTileStart = getTileStart(rangeStart); + final int lastVisibleTileStart = getTileStart(rangeEnd); + + mFirstRequiredTileStart = getTileStart(extRangeStart); + mLastRequiredTileStart = getTileStart(extRangeEnd); + if (DEBUG) { + log("requesting tile range: %d..%d", + mFirstRequiredTileStart, mLastRequiredTileStart); + } + + // All pending tile requests are removed by ThreadUtil at this point. + // Re-request all required tiles in the most optimal order. + if (scrollHint == ViewCallback.HINT_SCROLL_DESC) { + requestTiles(mFirstRequiredTileStart, lastVisibleTileStart, scrollHint, true); + requestTiles(lastVisibleTileStart + mTileSize, mLastRequiredTileStart, scrollHint, + false); + } else { + requestTiles(firstVisibleTileStart, mLastRequiredTileStart, scrollHint, false); + requestTiles(mFirstRequiredTileStart, firstVisibleTileStart - mTileSize, scrollHint, + true); + } + } + + private int getTileStart(int position) { + return position - position % mTileSize; + } + + private void requestTiles(int firstTileStart, int lastTileStart, int scrollHint, + boolean backwards) { + for (int i = firstTileStart; i <= lastTileStart; i += mTileSize) { + int tileStart = backwards ? (lastTileStart + firstTileStart - i) : i; + if (DEBUG) { + log("requesting tile @%d", tileStart); + } + mBackgroundProxy.loadTile(tileStart, scrollHint); + } + } + + @Override + public void loadTile(int position, int scrollHint) { + if (isTileLoaded(position)) { + if (DEBUG) { + log("already loaded tile @%d", position); + } + return; + } + TileList.Tile tile = acquireTile(); + tile.mStartPosition = position; + tile.mItemCount = Math.min(mTileSize, mItemCount - tile.mStartPosition); + mDataCallback.fillData(tile.mItems, tile.mStartPosition, tile.mItemCount); + flushTileCache(scrollHint); + addTile(tile); + } + + @Override + public void recycleTile(TileList.Tile tile) { + if (DEBUG) { + log("recycling tile @%d", tile.mStartPosition); + } + mDataCallback.recycleData(tile.mItems, tile.mItemCount); + + tile.mNext = mRecycledRoot; + mRecycledRoot = tile; + } + + private TileList.Tile acquireTile() { + if (mRecycledRoot != null) { + TileList.Tile result = mRecycledRoot; + mRecycledRoot = mRecycledRoot.mNext; + return result; + } + return new TileList.Tile(mTClass, mTileSize); + } + + private boolean isTileLoaded(int position) { + return mLoadedTiles.get(position); + } + + private void addTile(TileList.Tile tile) { + mLoadedTiles.put(tile.mStartPosition, true); + mMainThreadProxy.addTile(mGeneration, tile); + if (DEBUG) { + log("loaded tile @%d, total tiles: %d", tile.mStartPosition, mLoadedTiles.size()); + } + } + + private void removeTile(int position) { + mLoadedTiles.delete(position); + mMainThreadProxy.removeTile(mGeneration, position); + if (DEBUG) { + log("flushed tile @%d, total tiles: %s", position, mLoadedTiles.size()); + } + } + + private void flushTileCache(int scrollHint) { + final int cacheSizeLimit = mDataCallback.getMaxCachedTiles(); + while (mLoadedTiles.size() >= cacheSizeLimit) { + int firstLoadedTileStart = mLoadedTiles.keyAt(0); + int lastLoadedTileStart = mLoadedTiles.keyAt(mLoadedTiles.size() - 1); + int startMargin = mFirstRequiredTileStart - firstLoadedTileStart; + int endMargin = lastLoadedTileStart - mLastRequiredTileStart; + if (startMargin > 0 && (startMargin >= endMargin || + (scrollHint == ViewCallback.HINT_SCROLL_ASC))) { + removeTile(firstLoadedTileStart); + } else if (endMargin > 0 && (startMargin < endMargin || + (scrollHint == ViewCallback.HINT_SCROLL_DESC))){ + removeTile(lastLoadedTileStart); + } else { + // Could not flush on either side, bail out. + return; + } + } + } + + private void log(String s, Object... args) { + Log.d(TAG, "[BKGR] " + String.format(s, args)); + } + }; + + /** + * The callback that provides data access for {@link AsyncListUtil}. + * + *

+ * All methods are called on the background thread. + */ + public static abstract class DataCallback { + + /** + * Refresh the data set and return the new data item count. + * + *

+ * If the data is being accessed through {@link android.database.Cursor} this is where + * the new cursor should be created. + * + * @return Data item count. + */ + @WorkerThread + public abstract int refreshData(); + + /** + * Fill the given tile. + * + *

+ * The provided tile might be a recycled tile, in which case it will already have objects. + * It is suggested to re-use these objects if possible in your use case. + * + * @param startPosition The start position in the list. + * @param itemCount The data item count. + * @param data The data item array to fill into. Should not be accessed beyond + * itemCount. + */ + @WorkerThread + public abstract void fillData(T[] data, int startPosition, int itemCount); + + /** + * Recycle the objects created in {@link #fillData} if necessary. + * + * + * @param data Array of data items. Should not be accessed beyond itemCount. + * @param itemCount The data item count. + */ + @WorkerThread + public void recycleData(T[] data, int itemCount) { + } + + /** + * Returns tile cache size limit (in tiles). + * + *

+ * The actual number of cached tiles will be the maximum of this value and the number of + * tiles that is required to cover the range returned by + * {@link ViewCallback#extendRangeInto(int[], int[], int)}. + *

+ * For example, if this method returns 10, and the most + * recent call to {@link ViewCallback#extendRangeInto(int[], int[], int)} returned + * {100, 179}, and the tile size is 5, then the maximum number of cached tiles will be 16. + *

+ * However, if the tile size is 20, then the maximum number of cached tiles will be 10. + *

+ * The default implementation returns 10. + * + * @return Maximum cache size. + */ + @WorkerThread + public int getMaxCachedTiles() { + return 10; + } + } + + /** + * The callback that links {@link AsyncListUtil} with the list view. + * + *

+ * All methods are called on the main thread. + */ + public static abstract class ViewCallback { + + /** + * No scroll direction hint available. + */ + public static final int HINT_SCROLL_NONE = 0; + + /** + * Scrolling in descending order (from higher to lower positions in the order of the backing + * storage). + */ + public static final int HINT_SCROLL_DESC = 1; + + /** + * Scrolling in ascending order (from lower to higher positions in the order of the backing + * storage). + */ + public static final int HINT_SCROLL_ASC = 2; + + /** + * Compute the range of visible item positions. + *

+ * outRange[0] is the position of the first visible item (in the order of the backing + * storage). + *

+ * outRange[1] is the position of the last visible item (in the order of the backing + * storage). + *

+ * Negative positions and positions greater or equal to {@link #getItemCount} are invalid. + * If the returned range contains invalid positions it is ignored (no item will be loaded). + * + * @param outRange The visible item range. + */ + @UiThread + public abstract void getItemRangeInto(int[] outRange); + + /** + * Compute a wider range of items that will be loaded for smoother scrolling. + * + *

+ * If there is no scroll hint, the default implementation extends the visible range by half + * its length in both directions. If there is a scroll hint, the range is extended by + * its full length in the scroll direction, and by half in the other direction. + *

+ * For example, if range is {100, 200} and scrollHint + * is {@link #HINT_SCROLL_ASC}, then outRange will be {50, 300}. + *

+ * However, if scrollHint is {@link #HINT_SCROLL_NONE}, then + * outRange will be {50, 250} + * + * @param range Visible item range. + * @param outRange Extended range. + * @param scrollHint The scroll direction hint. + */ + @UiThread + public void extendRangeInto(int[] range, int[] outRange, int scrollHint) { + final int fullRange = range[1] - range[0] + 1; + final int halfRange = fullRange / 2; + outRange[0] = range[0] - (scrollHint == HINT_SCROLL_DESC ? fullRange : halfRange); + outRange[1] = range[1] + (scrollHint == HINT_SCROLL_ASC ? fullRange : halfRange); + } + + /** + * Called when the entire data set has changed. + */ + @UiThread + public abstract void onDataRefresh(); + + /** + * Called when an item at the given position is loaded. + * @param position Item position. + */ + @UiThread + public abstract void onItemLoaded(int position); + } +} diff --git a/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java b/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java new file mode 100644 index 0000000000..c8bc1a4b8b --- /dev/null +++ b/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.support.v7.util; + +/** + * Wraps a {@link ListUpdateCallback} callback and batches operations that can be merged. + *

+ * For instance, when 2 add operations comes that adds 2 consecutive elements, + * BatchingListUpdateCallback merges them and calls the wrapped callback only once. + *

+ * This is a general purpose class and is also used by + * {@link android.support.v7.util.DiffUtil.DiffResult DiffResult} and + * {@link SortedList} to minimize the number of updates that are dispatched. + *

+ * If you use this class to batch updates, you must call {@link #dispatchLastEvent()} when the + * stream of update events drain. + */ +public class BatchingListUpdateCallback implements ListUpdateCallback { + private static final int TYPE_NONE = 0; + private static final int TYPE_ADD = 1; + private static final int TYPE_REMOVE = 2; + private static final int TYPE_CHANGE = 3; + + final ListUpdateCallback mWrapped; + + int mLastEventType = TYPE_NONE; + int mLastEventPosition = -1; + int mLastEventCount = -1; + Object mLastEventPayload = null; + + public BatchingListUpdateCallback(ListUpdateCallback callback) { + mWrapped = callback; + } + + /** + * BatchingListUpdateCallback holds onto the last event to see if it can be merged with the + * next one. When stream of events finish, you should call this method to dispatch the last + * event. + */ + public void dispatchLastEvent() { + if (mLastEventType == TYPE_NONE) { + return; + } + switch (mLastEventType) { + case TYPE_ADD: + mWrapped.onInserted(mLastEventPosition, mLastEventCount); + break; + case TYPE_REMOVE: + mWrapped.onRemoved(mLastEventPosition, mLastEventCount); + break; + case TYPE_CHANGE: + mWrapped.onChanged(mLastEventPosition, mLastEventCount, mLastEventPayload); + break; + } + mLastEventPayload = null; + mLastEventType = TYPE_NONE; + } + + @Override + public void onInserted(int position, int count) { + if (mLastEventType == TYPE_ADD && position >= mLastEventPosition + && position <= mLastEventPosition + mLastEventCount) { + mLastEventCount += count; + mLastEventPosition = Math.min(position, mLastEventPosition); + return; + } + dispatchLastEvent(); + mLastEventPosition = position; + mLastEventCount = count; + mLastEventType = TYPE_ADD; + } + + @Override + public void onRemoved(int position, int count) { + if (mLastEventType == TYPE_REMOVE && mLastEventPosition >= position && + mLastEventPosition <= position + count) { + mLastEventCount += count; + mLastEventPosition = position; + return; + } + dispatchLastEvent(); + mLastEventPosition = position; + mLastEventCount = count; + mLastEventType = TYPE_REMOVE; + } + + @Override + public void onMoved(int fromPosition, int toPosition) { + dispatchLastEvent(); // moves are not merged + mWrapped.onMoved(fromPosition, toPosition); + } + + @Override + public void onChanged(int position, int count, Object payload) { + if (mLastEventType == TYPE_CHANGE && + !(position > mLastEventPosition + mLastEventCount + || position + count < mLastEventPosition || mLastEventPayload != payload)) { + // take potential overlap into account + int previousEnd = mLastEventPosition + mLastEventCount; + mLastEventPosition = Math.min(position, mLastEventPosition); + mLastEventCount = Math.max(previousEnd, position + count) - mLastEventPosition; + return; + } + dispatchLastEvent(); + mLastEventPosition = position; + mLastEventCount = count; + mLastEventPayload = payload; + mLastEventType = TYPE_CHANGE; + } +} diff --git a/app/src/main/java/android/support/v7/util/DiffUtil.java b/app/src/main/java/android/support/v7/util/DiffUtil.java new file mode 100644 index 0000000000..6f0a078fa7 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/DiffUtil.java @@ -0,0 +1,856 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; +import android.support.v7.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * DiffUtil is a utility class that can calculate the difference between two lists and output a + * list of update operations that converts the first list into the second one. + *

+ * It can be used to calculate updates for a RecyclerView Adapter. + *

+ * DiffUtil uses Eugene W. Myers's difference algorithm to calculate the minimal number of updates + * to convert one list into another. Myers's algorithm does not handle items that are moved so + * DiffUtil runs a second pass on the result to detect items that were moved. + *

+ * If the lists are large, this operation may take significant time so you are advised to run this + * on a background thread, get the {@link DiffResult} then apply it on the RecyclerView on the main + * thread. + *

+ * This algorithm is optimized for space and uses O(N) space to find the minimal + * number of addition and removal operations between the two lists. It has O(N + D^2) expected time + * performance where D is the length of the edit script. + *

+ * If move detection is enabled, it takes an additional O(N^2) time where N is the total number of + * added and removed items. If your lists are already sorted by the same constraint (e.g. a created + * timestamp for a list of posts), you can disable move detection to improve performance. + *

+ * The actual runtime of the algorithm significantly depends on the number of changes in the list + * and the cost of your comparison methods. Below are some average run times for reference: + * (The test list is composed of random UUID Strings and the tests are run on Nexus 5X with M) + *

    + *
  • 100 items and 10 modifications: avg: 0.39 ms, median: 0.35 ms + *
  • 100 items and 100 modifications: 3.82 ms, median: 3.75 ms + *
  • 100 items and 100 modifications without moves: 2.09 ms, median: 2.06 ms + *
  • 1000 items and 50 modifications: avg: 4.67 ms, median: 4.59 ms + *
  • 1000 items and 50 modifications without moves: avg: 3.59 ms, median: 3.50 ms + *
  • 1000 items and 200 modifications: 27.07 ms, median: 26.92 ms + *
  • 1000 items and 200 modifications without moves: 13.54 ms, median: 13.36 ms + *
+ *

+ * Due to implementation constraints, the max size of the list can be 2^26. + */ +public class DiffUtil { + + private DiffUtil() { + // utility class, no instance. + } + + private static final Comparator SNAKE_COMPARATOR = new Comparator() { + @Override + public int compare(Snake o1, Snake o2) { + int cmpX = o1.x - o2.x; + return cmpX == 0 ? o1.y - o2.y : cmpX; + } + }; + + // Myers' algorithm uses two lists as axis labels. In DiffUtil's implementation, `x` axis is + // used for old list and `y` axis is used for new list. + + /** + * Calculates the list of update operations that can covert one list into the other one. + * + * @param cb The callback that acts as a gateway to the backing list data + * + * @return A DiffResult that contains the information about the edit sequence to convert the + * old list into the new list. + */ + public static DiffResult calculateDiff(Callback cb) { + return calculateDiff(cb, true); + } + + /** + * Calculates the list of update operations that can covert one list into the other one. + *

+ * If your old and new lists are sorted by the same constraint and items never move (swap + * positions), you can disable move detection which takes O(N^2) time where + * N is the number of added, moved, removed items. + * + * @param cb The callback that acts as a gateway to the backing list data + * @param detectMoves True if DiffUtil should try to detect moved items, false otherwise. + * + * @return A DiffResult that contains the information about the edit sequence to convert the + * old list into the new list. + */ + public static DiffResult calculateDiff(Callback cb, boolean detectMoves) { + final int oldSize = cb.getOldListSize(); + final int newSize = cb.getNewListSize(); + + final List snakes = new ArrayList<>(); + + // instead of a recursive implementation, we keep our own stack to avoid potential stack + // overflow exceptions + final List stack = new ArrayList<>(); + + stack.add(new Range(0, oldSize, 0, newSize)); + + final int max = oldSize + newSize + Math.abs(oldSize - newSize); + // allocate forward and backward k-lines. K lines are diagonal lines in the matrix. (see the + // paper for details) + // These arrays lines keep the max reachable position for each k-line. + final int[] forward = new int[max * 2]; + final int[] backward = new int[max * 2]; + + // We pool the ranges to avoid allocations for each recursive call. + final List rangePool = new ArrayList<>(); + while (!stack.isEmpty()) { + final Range range = stack.remove(stack.size() - 1); + final Snake snake = diffPartial(cb, range.oldListStart, range.oldListEnd, + range.newListStart, range.newListEnd, forward, backward, max); + if (snake != null) { + if (snake.size > 0) { + snakes.add(snake); + } + // offset the snake to convert its coordinates from the Range's area to global + snake.x += range.oldListStart; + snake.y += range.newListStart; + + // add new ranges for left and right + final Range left = rangePool.isEmpty() ? new Range() : rangePool.remove( + rangePool.size() - 1); + left.oldListStart = range.oldListStart; + left.newListStart = range.newListStart; + if (snake.reverse) { + left.oldListEnd = snake.x; + left.newListEnd = snake.y; + } else { + if (snake.removal) { + left.oldListEnd = snake.x - 1; + left.newListEnd = snake.y; + } else { + left.oldListEnd = snake.x; + left.newListEnd = snake.y - 1; + } + } + stack.add(left); + + // re-use range for right + //noinspection UnnecessaryLocalVariable + final Range right = range; + if (snake.reverse) { + if (snake.removal) { + right.oldListStart = snake.x + snake.size + 1; + right.newListStart = snake.y + snake.size; + } else { + right.oldListStart = snake.x + snake.size; + right.newListStart = snake.y + snake.size + 1; + } + } else { + right.oldListStart = snake.x + snake.size; + right.newListStart = snake.y + snake.size; + } + stack.add(right); + } else { + rangePool.add(range); + } + + } + // sort snakes + Collections.sort(snakes, SNAKE_COMPARATOR); + + return new DiffResult(cb, snakes, forward, backward, detectMoves); + + } + + private static Snake diffPartial(Callback cb, int startOld, int endOld, + int startNew, int endNew, int[] forward, int[] backward, int kOffset) { + final int oldSize = endOld - startOld; + final int newSize = endNew - startNew; + + if (endOld - startOld < 1 || endNew - startNew < 1) { + return null; + } + + final int delta = oldSize - newSize; + final int dLimit = (oldSize + newSize + 1) / 2; + Arrays.fill(forward, kOffset - dLimit - 1, kOffset + dLimit + 1, 0); + Arrays.fill(backward, kOffset - dLimit - 1 + delta, kOffset + dLimit + 1 + delta, oldSize); + final boolean checkInFwd = delta % 2 != 0; + for (int d = 0; d <= dLimit; d++) { + for (int k = -d; k <= d; k += 2) { + // find forward path + // we can reach k from k - 1 or k + 1. Check which one is further in the graph + int x; + final boolean removal; + if (k == -d || k != d && forward[kOffset + k - 1] < forward[kOffset + k + 1]) { + x = forward[kOffset + k + 1]; + removal = false; + } else { + x = forward[kOffset + k - 1] + 1; + removal = true; + } + // set y based on x + int y = x - k; + // move diagonal as long as items match + while (x < oldSize && y < newSize + && cb.areItemsTheSame(startOld + x, startNew + y)) { + x++; + y++; + } + forward[kOffset + k] = x; + if (checkInFwd && k >= delta - d + 1 && k <= delta + d - 1) { + if (forward[kOffset + k] >= backward[kOffset + k]) { + Snake outSnake = new Snake(); + outSnake.x = backward[kOffset + k]; + outSnake.y = outSnake.x - k; + outSnake.size = forward[kOffset + k] - backward[kOffset + k]; + outSnake.removal = removal; + outSnake.reverse = false; + return outSnake; + } + } + } + for (int k = -d; k <= d; k += 2) { + // find reverse path at k + delta, in reverse + final int backwardK = k + delta; + int x; + final boolean removal; + if (backwardK == d + delta || backwardK != -d + delta + && backward[kOffset + backwardK - 1] < backward[kOffset + backwardK + 1]) { + x = backward[kOffset + backwardK - 1]; + removal = false; + } else { + x = backward[kOffset + backwardK + 1] - 1; + removal = true; + } + + // set y based on x + int y = x - backwardK; + // move diagonal as long as items match + while (x > 0 && y > 0 + && cb.areItemsTheSame(startOld + x - 1, startNew + y - 1)) { + x--; + y--; + } + backward[kOffset + backwardK] = x; + if (!checkInFwd && k + delta >= -d && k + delta <= d) { + if (forward[kOffset + backwardK] >= backward[kOffset + backwardK]) { + Snake outSnake = new Snake(); + outSnake.x = backward[kOffset + backwardK]; + outSnake.y = outSnake.x - backwardK; + outSnake.size = + forward[kOffset + backwardK] - backward[kOffset + backwardK]; + outSnake.removal = removal; + outSnake.reverse = true; + return outSnake; + } + } + } + } + throw new IllegalStateException("DiffUtil hit an unexpected case while trying to calculate" + + " the optimal path. Please make sure your data is not changing during the" + + " diff calculation."); + } + + /** + * A Callback class used by DiffUtil while calculating the diff between two lists. + */ + public abstract static class Callback { + /** + * Returns the size of the old list. + * + * @return The size of the old list. + */ + public abstract int getOldListSize(); + + /** + * Returns the size of the new list. + * + * @return The size of the new list. + */ + public abstract int getNewListSize(); + + /** + * Called by the DiffUtil to decide whether two object represent the same Item. + *

+ * For example, if your items have unique ids, this method should check their id equality. + * + * @param oldItemPosition The position of the item in the old list + * @param newItemPosition The position of the item in the new list + * @return True if the two items represent the same object or false if they are different. + */ + public abstract boolean areItemsTheSame(int oldItemPosition, int newItemPosition); + + /** + * Called by the DiffUtil when it wants to check whether two items have the same data. + * DiffUtil uses this information to detect if the contents of an item has changed. + *

+ * DiffUtil uses this method to check equality instead of {@link Object#equals(Object)} + * so that you can change its behavior depending on your UI. + * For example, if you are using DiffUtil with a + * {@link android.support.v7.widget.RecyclerView.Adapter RecyclerView.Adapter}, you should + * return whether the items' visual representations are the same. + *

+ * This method is called only if {@link #areItemsTheSame(int, int)} returns + * {@code true} for these items. + * + * @param oldItemPosition The position of the item in the old list + * @param newItemPosition The position of the item in the new list which replaces the + * oldItem + * @return True if the contents of the items are the same or false if they are different. + */ + public abstract boolean areContentsTheSame(int oldItemPosition, int newItemPosition); + + /** + * When {@link #areItemsTheSame(int, int)} returns {@code true} for two items and + * {@link #areContentsTheSame(int, int)} returns false for them, DiffUtil + * calls this method to get a payload about the change. + *

+ * For example, if you are using DiffUtil with {@link RecyclerView}, you can return the + * particular field that changed in the item and your + * {@link android.support.v7.widget.RecyclerView.ItemAnimator ItemAnimator} can use that + * information to run the correct animation. + *

+ * Default implementation returns {@code null}. + * + * @param oldItemPosition The position of the item in the old list + * @param newItemPosition The position of the item in the new list + * + * @return A payload object that represents the change between the two items. + */ + @Nullable + public Object getChangePayload(int oldItemPosition, int newItemPosition) { + return null; + } + } + + /** + * Snakes represent a match between two lists. It is optionally prefixed or postfixed with an + * add or remove operation. See the Myers' paper for details. + */ + static class Snake { + /** + * Position in the old list + */ + int x; + + /** + * Position in the new list + */ + int y; + + /** + * Number of matches. Might be 0. + */ + int size; + + /** + * If true, this is a removal from the original list followed by {@code size} matches. + * If false, this is an addition from the new list followed by {@code size} matches. + */ + boolean removal; + + /** + * If true, the addition or removal is at the end of the snake. + * If false, the addition or removal is at the beginning of the snake. + */ + boolean reverse; + } + + /** + * Represents a range in two lists that needs to be solved. + *

+ * This internal class is used when running Myers' algorithm without recursion. + */ + static class Range { + + int oldListStart, oldListEnd; + + int newListStart, newListEnd; + + public Range() { + } + + public Range(int oldListStart, int oldListEnd, int newListStart, int newListEnd) { + this.oldListStart = oldListStart; + this.oldListEnd = oldListEnd; + this.newListStart = newListStart; + this.newListEnd = newListEnd; + } + } + + /** + * This class holds the information about the result of a + * {@link DiffUtil#calculateDiff(Callback, boolean)} call. + *

+ * You can consume the updates in a DiffResult via + * {@link #dispatchUpdatesTo(ListUpdateCallback)} or directly stream the results into a + * {@link RecyclerView.Adapter} via {@link #dispatchUpdatesTo(RecyclerView.Adapter)}. + */ + public static class DiffResult { + /** + * While reading the flags below, keep in mind that when multiple items move in a list, + * Myers's may pick any of them as the anchor item and consider that one NOT_CHANGED while + * picking others as additions and removals. This is completely fine as we later detect + * all moves. + *

+ * Below, when an item is mentioned to stay in the same "location", it means we won't + * dispatch a move/add/remove for it, it DOES NOT mean the item is still in the same + * position. + */ + // item stayed the same. + private static final int FLAG_NOT_CHANGED = 1; + // item stayed in the same location but changed. + private static final int FLAG_CHANGED = FLAG_NOT_CHANGED << 1; + // Item has moved and also changed. + private static final int FLAG_MOVED_CHANGED = FLAG_CHANGED << 1; + // Item has moved but did not change. + private static final int FLAG_MOVED_NOT_CHANGED = FLAG_MOVED_CHANGED << 1; + // Ignore this update. + // If this is an addition from the new list, it means the item is actually removed from an + // earlier position and its move will be dispatched when we process the matching removal + // from the old list. + // If this is a removal from the old list, it means the item is actually added back to an + // earlier index in the new list and we'll dispatch its move when we are processing that + // addition. + private static final int FLAG_IGNORE = FLAG_MOVED_NOT_CHANGED << 1; + + // since we are re-using the int arrays that were created in the Myers' step, we mask + // change flags + private static final int FLAG_OFFSET = 5; + + private static final int FLAG_MASK = (1 << FLAG_OFFSET) - 1; + + // The Myers' snakes. At this point, we only care about their diagonal sections. + private final List mSnakes; + + // The list to keep oldItemStatuses. As we traverse old items, we assign flags to them + // which also includes whether they were a real removal or a move (and its new index). + private final int[] mOldItemStatuses; + // The list to keep newItemStatuses. As we traverse new items, we assign flags to them + // which also includes whether they were a real addition or a move(and its old index). + private final int[] mNewItemStatuses; + // The callback that was given to calcualte diff method. + private final Callback mCallback; + + private final int mOldListSize; + + private final int mNewListSize; + + private final boolean mDetectMoves; + + /** + * @param callback The callback that was used to calculate the diff + * @param snakes The list of Myers' snakes + * @param oldItemStatuses An int[] that can be re-purposed to keep metadata + * @param newItemStatuses An int[] that can be re-purposed to keep metadata + * @param detectMoves True if this DiffResult will try to detect moved items + */ + DiffResult(Callback callback, List snakes, int[] oldItemStatuses, + int[] newItemStatuses, boolean detectMoves) { + mSnakes = snakes; + mOldItemStatuses = oldItemStatuses; + mNewItemStatuses = newItemStatuses; + Arrays.fill(mOldItemStatuses, 0); + Arrays.fill(mNewItemStatuses, 0); + mCallback = callback; + mOldListSize = callback.getOldListSize(); + mNewListSize = callback.getNewListSize(); + mDetectMoves = detectMoves; + addRootSnake(); + findMatchingItems(); + } + + /** + * We always add a Snake to 0/0 so that we can run loops from end to beginning and be done + * when we run out of snakes. + */ + private void addRootSnake() { + Snake firstSnake = mSnakes.isEmpty() ? null : mSnakes.get(0); + if (firstSnake == null || firstSnake.x != 0 || firstSnake.y != 0) { + Snake root = new Snake(); + root.x = 0; + root.y = 0; + root.removal = false; + root.size = 0; + root.reverse = false; + mSnakes.add(0, root); + } + } + + /** + * This method traverses each addition / removal and tries to match it to a previous + * removal / addition. This is how we detect move operations. + *

+ * This class also flags whether an item has been changed or not. + *

+ * DiffUtil does this pre-processing so that if it is running on a big list, it can be moved + * to background thread where most of the expensive stuff will be calculated and kept in + * the statuses maps. DiffResult uses this pre-calculated information while dispatching + * the updates (which is probably being called on the main thread). + */ + private void findMatchingItems() { + int posOld = mOldListSize; + int posNew = mNewListSize; + // traverse the matrix from right bottom to 0,0. + for (int i = mSnakes.size() - 1; i >= 0; i--) { + final Snake snake = mSnakes.get(i); + final int endX = snake.x + snake.size; + final int endY = snake.y + snake.size; + if (mDetectMoves) { + while (posOld > endX) { + // this is a removal. Check remaining snakes to see if this was added before + findAddition(posOld, posNew, i); + posOld--; + } + while (posNew > endY) { + // this is an addition. Check remaining snakes to see if this was removed + // before + findRemoval(posOld, posNew, i); + posNew--; + } + } + for (int j = 0; j < snake.size; j++) { + // matching items. Check if it is changed or not + final int oldItemPos = snake.x + j; + final int newItemPos = snake.y + j; + final boolean theSame = mCallback + .areContentsTheSame(oldItemPos, newItemPos); + final int changeFlag = theSame ? FLAG_NOT_CHANGED : FLAG_CHANGED; + mOldItemStatuses[oldItemPos] = (newItemPos << FLAG_OFFSET) | changeFlag; + mNewItemStatuses[newItemPos] = (oldItemPos << FLAG_OFFSET) | changeFlag; + } + posOld = snake.x; + posNew = snake.y; + } + } + + private void findAddition(int x, int y, int snakeIndex) { + if (mOldItemStatuses[x - 1] != 0) { + return; // already set by a latter item + } + findMatchingItem(x, y, snakeIndex, false); + } + + private void findRemoval(int x, int y, int snakeIndex) { + if (mNewItemStatuses[y - 1] != 0) { + return; // already set by a latter item + } + findMatchingItem(x, y, snakeIndex, true); + } + + /** + * Finds a matching item that is before the given coordinates in the matrix + * (before : left and above). + * + * @param x The x position in the matrix (position in the old list) + * @param y The y position in the matrix (position in the new list) + * @param snakeIndex The current snake index + * @param removal True if we are looking for a removal, false otherwise + * + * @return True if such item is found. + */ + private boolean findMatchingItem(final int x, final int y, final int snakeIndex, + final boolean removal) { + final int myItemPos; + int curX; + int curY; + if (removal) { + myItemPos = y - 1; + curX = x; + curY = y - 1; + } else { + myItemPos = x - 1; + curX = x - 1; + curY = y; + } + for (int i = snakeIndex; i >= 0; i--) { + final Snake snake = mSnakes.get(i); + final int endX = snake.x + snake.size; + final int endY = snake.y + snake.size; + if (removal) { + // check removals for a match + for (int pos = curX - 1; pos >= endX; pos--) { + if (mCallback.areItemsTheSame(pos, myItemPos)) { + // found! + final boolean theSame = mCallback.areContentsTheSame(pos, myItemPos); + final int changeFlag = theSame ? FLAG_MOVED_NOT_CHANGED + : FLAG_MOVED_CHANGED; + mNewItemStatuses[myItemPos] = (pos << FLAG_OFFSET) | FLAG_IGNORE; + mOldItemStatuses[pos] = (myItemPos << FLAG_OFFSET) | changeFlag; + return true; + } + } + } else { + // check for additions for a match + for (int pos = curY - 1; pos >= endY; pos--) { + if (mCallback.areItemsTheSame(myItemPos, pos)) { + // found + final boolean theSame = mCallback.areContentsTheSame(myItemPos, pos); + final int changeFlag = theSame ? FLAG_MOVED_NOT_CHANGED + : FLAG_MOVED_CHANGED; + mOldItemStatuses[x - 1] = (pos << FLAG_OFFSET) | FLAG_IGNORE; + mNewItemStatuses[pos] = ((x - 1) << FLAG_OFFSET) | changeFlag; + return true; + } + } + } + curX = snake.x; + curY = snake.y; + } + return false; + } + + /** + * Dispatches the update events to the given adapter. + *

+ * For example, if you have an {@link android.support.v7.widget.RecyclerView.Adapter Adapter} + * that is backed by a {@link List}, you can swap the list with the new one then call this + * method to dispatch all updates to the RecyclerView. + *

+         *     List oldList = mAdapter.getData();
+         *     DiffResult result = DiffUtil.calculateDiff(new MyCallback(oldList, newList));
+         *     mAdapter.setData(newList);
+         *     result.dispatchUpdatesTo(mAdapter);
+         * 
+ *

+ * Note that the RecyclerView requires you to dispatch adapter updates immediately when you + * change the data (you cannot defer {@code notify*} calls). The usage above adheres to this + * rule because updates are sent to the adapter right after the backing data is changed, + * before RecyclerView tries to read it. + *

+ * On the other hand, if you have another + * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver AdapterDataObserver} + * that tries to process events synchronously, this may confuse that observer because the + * list is instantly moved to its final state while the adapter updates are dispatched later + * on, one by one. If you have such an + * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver AdapterDataObserver}, + * you can use + * {@link #dispatchUpdatesTo(ListUpdateCallback)} to handle each modification + * manually. + * + * @param adapter A RecyclerView adapter which was displaying the old list and will start + * displaying the new list. + */ + public void dispatchUpdatesTo(final RecyclerView.Adapter adapter) { + dispatchUpdatesTo(new ListUpdateCallback() { + @Override + public void onInserted(int position, int count) { + adapter.notifyItemRangeInserted(position, count); + } + + @Override + public void onRemoved(int position, int count) { + adapter.notifyItemRangeRemoved(position, count); + } + + @Override + public void onMoved(int fromPosition, int toPosition) { + adapter.notifyItemMoved(fromPosition, toPosition); + } + + @Override + public void onChanged(int position, int count, Object payload) { + adapter.notifyItemRangeChanged(position, count, payload); + } + }); + } + + /** + * Dispatches update operations to the given Callback. + *

+ * These updates are atomic such that the first update call effects every update call that + * comes after it (the same as RecyclerView). + * + * @param updateCallback The callback to receive the update operations. + * @see #dispatchUpdatesTo(RecyclerView.Adapter) + */ + public void dispatchUpdatesTo(ListUpdateCallback updateCallback) { + final BatchingListUpdateCallback batchingCallback; + if (updateCallback instanceof BatchingListUpdateCallback) { + batchingCallback = (BatchingListUpdateCallback) updateCallback; + } else { + batchingCallback = new BatchingListUpdateCallback(updateCallback); + // replace updateCallback with a batching callback and override references to + // updateCallback so that we don't call it directly by mistake + //noinspection UnusedAssignment + updateCallback = batchingCallback; + } + // These are add/remove ops that are converted to moves. We track their positions until + // their respective update operations are processed. + final List postponedUpdates = new ArrayList<>(); + int posOld = mOldListSize; + int posNew = mNewListSize; + for (int snakeIndex = mSnakes.size() - 1; snakeIndex >= 0; snakeIndex--) { + final Snake snake = mSnakes.get(snakeIndex); + final int snakeSize = snake.size; + final int endX = snake.x + snakeSize; + final int endY = snake.y + snakeSize; + if (endX < posOld) { + dispatchRemovals(postponedUpdates, batchingCallback, endX, posOld - endX, endX); + } + + if (endY < posNew) { + dispatchAdditions(postponedUpdates, batchingCallback, endX, posNew - endY, + endY); + } + for (int i = snakeSize - 1; i >= 0; i--) { + if ((mOldItemStatuses[snake.x + i] & FLAG_MASK) == FLAG_CHANGED) { + batchingCallback.onChanged(snake.x + i, 1, + mCallback.getChangePayload(snake.x + i, snake.y + i)); + } + } + posOld = snake.x; + posNew = snake.y; + } + batchingCallback.dispatchLastEvent(); + } + + private static PostponedUpdate removePostponedUpdate(List updates, + int pos, boolean removal) { + for (int i = updates.size() - 1; i >= 0; i--) { + final PostponedUpdate update = updates.get(i); + if (update.posInOwnerList == pos && update.removal == removal) { + updates.remove(i); + for (int j = i; j < updates.size(); j++) { + // offset other ops since they swapped positions + updates.get(j).currentPos += removal ? 1 : -1; + } + return update; + } + } + return null; + } + + private void dispatchAdditions(List postponedUpdates, + ListUpdateCallback updateCallback, int start, int count, int globalIndex) { + if (!mDetectMoves) { + updateCallback.onInserted(start, count); + return; + } + for (int i = count - 1; i >= 0; i--) { + int status = mNewItemStatuses[globalIndex + i] & FLAG_MASK; + switch (status) { + case 0: // real addition + updateCallback.onInserted(start, 1); + for (PostponedUpdate update : postponedUpdates) { + update.currentPos += 1; + } + break; + case FLAG_MOVED_CHANGED: + case FLAG_MOVED_NOT_CHANGED: + final int pos = mNewItemStatuses[globalIndex + i] >> FLAG_OFFSET; + final PostponedUpdate update = removePostponedUpdate(postponedUpdates, pos, + true); + // the item was moved from that position + //noinspection ConstantConditions + updateCallback.onMoved(update.currentPos, start); + if (status == FLAG_MOVED_CHANGED) { + // also dispatch a change + updateCallback.onChanged(start, 1, + mCallback.getChangePayload(pos, globalIndex + i)); + } + break; + case FLAG_IGNORE: // ignoring this + postponedUpdates.add(new PostponedUpdate(globalIndex + i, start, false)); + break; + default: + throw new IllegalStateException( + "unknown flag for pos " + (globalIndex + i) + " " + Long + .toBinaryString(status)); + } + } + } + + private void dispatchRemovals(List postponedUpdates, + ListUpdateCallback updateCallback, int start, int count, int globalIndex) { + if (!mDetectMoves) { + updateCallback.onRemoved(start, count); + return; + } + for (int i = count - 1; i >= 0; i--) { + final int status = mOldItemStatuses[globalIndex + i] & FLAG_MASK; + switch (status) { + case 0: // real removal + updateCallback.onRemoved(start + i, 1); + for (PostponedUpdate update : postponedUpdates) { + update.currentPos -= 1; + } + break; + case FLAG_MOVED_CHANGED: + case FLAG_MOVED_NOT_CHANGED: + final int pos = mOldItemStatuses[globalIndex + i] >> FLAG_OFFSET; + final PostponedUpdate update = removePostponedUpdate(postponedUpdates, pos, + false); + // the item was moved to that position. we do -1 because this is a move not + // add and removing current item offsets the target move by 1 + //noinspection ConstantConditions + updateCallback.onMoved(start + i, update.currentPos - 1); + if (status == FLAG_MOVED_CHANGED) { + // also dispatch a change + updateCallback.onChanged(update.currentPos - 1, 1, + mCallback.getChangePayload(globalIndex + i, pos)); + } + break; + case FLAG_IGNORE: // ignoring this + postponedUpdates.add(new PostponedUpdate(globalIndex + i, start + i, true)); + break; + default: + throw new IllegalStateException( + "unknown flag for pos " + (globalIndex + i) + " " + Long + .toBinaryString(status)); + } + } + } + + @VisibleForTesting + List getSnakes() { + return mSnakes; + } + } + + /** + * Represents an update that we skipped because it was a move. + *

+ * When an update is skipped, it is tracked as other updates are dispatched until the matching + * add/remove operation is found at which point the tracked position is used to dispatch the + * update. + */ + private static class PostponedUpdate { + + int posInOwnerList; + + int currentPos; + + boolean removal; + + public PostponedUpdate(int posInOwnerList, int currentPos, boolean removal) { + this.posInOwnerList = posInOwnerList; + this.currentPos = currentPos; + this.removal = removal; + } + } +} diff --git a/app/src/main/java/android/support/v7/util/ListUpdateCallback.java b/app/src/main/java/android/support/v7/util/ListUpdateCallback.java new file mode 100644 index 0000000000..2136202958 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/ListUpdateCallback.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.support.v7.util; + +/** + * An interface that can receive Update operations that are applied to a list. + *

+ * This class can be used together with DiffUtil to detect changes between two lists. + */ +public interface ListUpdateCallback { + /** + * Called when {@code count} number of items are inserted at the given position. + * + * @param position The position of the new item. + * @param count The number of items that have been added. + */ + void onInserted(int position, int count); + + /** + * Called when {@code count} number of items are removed from the given position. + * + * @param position The position of the item which has been removed. + * @param count The number of items which have been removed. + */ + void onRemoved(int position, int count); + + /** + * Called when an item changes its position in the list. + * + * @param fromPosition The previous position of the item before the move. + * @param toPosition The new position of the item. + */ + void onMoved(int fromPosition, int toPosition); + + /** + * Called when {@code count} number of items are updated at the given position. + * + * @param position The position of the item which has been updated. + * @param count The number of items which has changed. + */ + void onChanged(int position, int count, Object payload); +} diff --git a/app/src/main/java/android/support/v7/util/MessageThreadUtil.java b/app/src/main/java/android/support/v7/util/MessageThreadUtil.java new file mode 100644 index 0000000000..8aa9eda764 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/MessageThreadUtil.java @@ -0,0 +1,283 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import android.os.Handler; +import android.os.Looper; +import android.support.v4.content.ParallelExecutorCompat; +import android.util.Log; + +import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; + +class MessageThreadUtil implements ThreadUtil { + + @Override + public MainThreadCallback getMainThreadProxy(final MainThreadCallback callback) { + return new MainThreadCallback() { + final private MessageQueue mQueue = new MessageQueue(); + final private Handler mMainThreadHandler = new Handler(Looper.getMainLooper()); + + private static final int UPDATE_ITEM_COUNT = 1; + private static final int ADD_TILE = 2; + private static final int REMOVE_TILE = 3; + + @Override + public void updateItemCount(int generation, int itemCount) { + sendMessage(SyncQueueItem.obtainMessage(UPDATE_ITEM_COUNT, generation, itemCount)); + } + + @Override + public void addTile(int generation, TileList.Tile tile) { + sendMessage(SyncQueueItem.obtainMessage(ADD_TILE, generation, tile)); + } + + @Override + public void removeTile(int generation, int position) { + sendMessage(SyncQueueItem.obtainMessage(REMOVE_TILE, generation, position)); + } + + private void sendMessage(SyncQueueItem msg) { + mQueue.sendMessage(msg); + mMainThreadHandler.post(mMainThreadRunnable); + } + + private Runnable mMainThreadRunnable = new Runnable() { + @Override + public void run() { + SyncQueueItem msg = mQueue.next(); + while (msg != null) { + switch (msg.what) { + case UPDATE_ITEM_COUNT: + callback.updateItemCount(msg.arg1, msg.arg2); + break; + case ADD_TILE: + //noinspection unchecked + callback.addTile(msg.arg1, (TileList.Tile) msg.data); + break; + case REMOVE_TILE: + callback.removeTile(msg.arg1, msg.arg2); + break; + default: + Log.e("ThreadUtil", "Unsupported message, what=" + msg.what); + } + msg = mQueue.next(); + } + } + }; + }; + } + + @Override + public BackgroundCallback getBackgroundProxy(final BackgroundCallback callback) { + return new BackgroundCallback() { + final private MessageQueue mQueue = new MessageQueue(); + final private Executor mExecutor = ParallelExecutorCompat.getParallelExecutor(); + AtomicBoolean mBackgroundRunning = new AtomicBoolean(false); + + private static final int REFRESH = 1; + private static final int UPDATE_RANGE = 2; + private static final int LOAD_TILE = 3; + private static final int RECYCLE_TILE = 4; + + @Override + public void refresh(int generation) { + sendMessageAtFrontOfQueue(SyncQueueItem.obtainMessage(REFRESH, generation, null)); + } + + @Override + public void updateRange(int rangeStart, int rangeEnd, + int extRangeStart, int extRangeEnd, int scrollHint) { + sendMessageAtFrontOfQueue(SyncQueueItem.obtainMessage(UPDATE_RANGE, + rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint, null)); + } + + @Override + public void loadTile(int position, int scrollHint) { + sendMessage(SyncQueueItem.obtainMessage(LOAD_TILE, position, scrollHint)); + } + + @Override + public void recycleTile(TileList.Tile tile) { + sendMessage(SyncQueueItem.obtainMessage(RECYCLE_TILE, 0, tile)); + } + + private void sendMessage(SyncQueueItem msg) { + mQueue.sendMessage(msg); + maybeExecuteBackgroundRunnable(); + } + + private void sendMessageAtFrontOfQueue(SyncQueueItem msg) { + mQueue.sendMessageAtFrontOfQueue(msg); + maybeExecuteBackgroundRunnable(); + } + + private void maybeExecuteBackgroundRunnable() { + if (mBackgroundRunning.compareAndSet(false, true)) { + mExecutor.execute(mBackgroundRunnable); + } + } + + private Runnable mBackgroundRunnable = new Runnable() { + @Override + public void run() { + while (true) { + SyncQueueItem msg = mQueue.next(); + if (msg == null) { + break; + } + switch (msg.what) { + case REFRESH: + mQueue.removeMessages(REFRESH); + callback.refresh(msg.arg1); + break; + case UPDATE_RANGE: + mQueue.removeMessages(UPDATE_RANGE); + mQueue.removeMessages(LOAD_TILE); + callback.updateRange( + msg.arg1, msg.arg2, msg.arg3, msg.arg4, msg.arg5); + break; + case LOAD_TILE: + callback.loadTile(msg.arg1, msg.arg2); + break; + case RECYCLE_TILE: + //noinspection unchecked + callback.recycleTile((TileList.Tile) msg.data); + break; + default: + Log.e("ThreadUtil", "Unsupported message, what=" + msg.what); + } + } + mBackgroundRunning.set(false); + } + }; + }; + } + + /** + * Replica of android.os.Message. Unfortunately, cannot use it without a Handler and don't want + * to create a thread just for this component. + */ + static class SyncQueueItem { + + private static SyncQueueItem sPool; + private static final Object sPoolLock = new Object(); + private SyncQueueItem next; + public int what; + public int arg1; + public int arg2; + public int arg3; + public int arg4; + public int arg5; + public Object data; + + void recycle() { + next = null; + what = arg1 = arg2 = arg3 = arg4 = arg5 = 0; + data = null; + synchronized (sPoolLock) { + if (sPool != null) { + next = sPool; + } + sPool = this; + } + } + + static SyncQueueItem obtainMessage(int what, int arg1, int arg2, int arg3, int arg4, + int arg5, Object data) { + synchronized (sPoolLock) { + final SyncQueueItem item; + if (sPool == null) { + item = new SyncQueueItem(); + } else { + item = sPool; + sPool = sPool.next; + item.next = null; + } + item.what = what; + item.arg1 = arg1; + item.arg2 = arg2; + item.arg3 = arg3; + item.arg4 = arg4; + item.arg5 = arg5; + item.data = data; + return item; + } + } + + static SyncQueueItem obtainMessage(int what, int arg1, int arg2) { + return obtainMessage(what, arg1, arg2, 0, 0, 0, null); + } + + static SyncQueueItem obtainMessage(int what, int arg1, Object data) { + return obtainMessage(what, arg1, 0, 0, 0, 0, data); + } + } + + static class MessageQueue { + + private SyncQueueItem mRoot; + + synchronized SyncQueueItem next() { + if (mRoot == null) { + return null; + } + final SyncQueueItem next = mRoot; + mRoot = mRoot.next; + return next; + } + + synchronized void sendMessageAtFrontOfQueue(SyncQueueItem item) { + item.next = mRoot; + mRoot = item; + } + + synchronized void sendMessage(SyncQueueItem item) { + if (mRoot == null) { + mRoot = item; + return; + } + SyncQueueItem last = mRoot; + while (last.next != null) { + last = last.next; + } + last.next = item; + } + + synchronized void removeMessages(int what) { + while (mRoot != null && mRoot.what == what) { + SyncQueueItem item = mRoot; + mRoot = mRoot.next; + item.recycle(); + } + if (mRoot != null) { + SyncQueueItem prev = mRoot; + SyncQueueItem item = prev.next; + while (item != null) { + SyncQueueItem next = item.next; + if (item.what == what) { + prev.next = next; + item.recycle(); + } else { + prev = item; + } + item = next; + } + } + } + } +} diff --git a/app/src/main/java/android/support/v7/util/SortedList.java b/app/src/main/java/android/support/v7/util/SortedList.java new file mode 100644 index 0000000000..1d48468191 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/SortedList.java @@ -0,0 +1,821 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; + +/** + * A Sorted list implementation that can keep items in order and also notify for changes in the + * list + * such that it can be bound to a {@link android.support.v7.widget.RecyclerView.Adapter + * RecyclerView.Adapter}. + *

+ * It keeps items ordered using the {@link Callback#compare(Object, Object)} method and uses + * binary search to retrieve items. If the sorting criteria of your items may change, make sure you + * call appropriate methods while editing them to avoid data inconsistencies. + *

+ * You can control the order of items and change notifications via the {@link Callback} parameter. + */ +@SuppressWarnings("unchecked") +public class SortedList { + + /** + * Used by {@link #indexOf(Object)} when he item cannot be found in the list. + */ + public static final int INVALID_POSITION = -1; + + private static final int MIN_CAPACITY = 10; + private static final int CAPACITY_GROWTH = MIN_CAPACITY; + private static final int INSERTION = 1; + private static final int DELETION = 1 << 1; + private static final int LOOKUP = 1 << 2; + T[] mData; + + /** + * A copy of the previous list contents used during the merge phase of addAll. + */ + private T[] mOldData; + private int mOldDataStart; + private int mOldDataSize; + + /** + * The size of the valid portion of mData during the merge phase of addAll. + */ + private int mMergedSize; + + + /** + * The callback instance that controls the behavior of the SortedList and get notified when + * changes happen. + */ + private Callback mCallback; + + private BatchedCallback mBatchedCallback; + + private int mSize; + private final Class mTClass; + + /** + * Creates a new SortedList of type T. + * + * @param klass The class of the contents of the SortedList. + * @param callback The callback that controls the behavior of SortedList. + */ + public SortedList(Class klass, Callback callback) { + this(klass, callback, MIN_CAPACITY); + } + + /** + * Creates a new SortedList of type T. + * + * @param klass The class of the contents of the SortedList. + * @param callback The callback that controls the behavior of SortedList. + * @param initialCapacity The initial capacity to hold items. + */ + public SortedList(Class klass, Callback callback, int initialCapacity) { + mTClass = klass; + mData = (T[]) Array.newInstance(klass, initialCapacity); + mCallback = callback; + mSize = 0; + } + + /** + * The number of items in the list. + * + * @return The number of items in the list. + */ + public int size() { + return mSize; + } + + /** + * Adds the given item to the list. If this is a new item, SortedList calls + * {@link Callback#onInserted(int, int)}. + *

+ * If the item already exists in the list and its sorting criteria is not changed, it is + * replaced with the existing Item. SortedList uses + * {@link Callback#areItemsTheSame(Object, Object)} to check if two items are the same item + * and uses {@link Callback#areContentsTheSame(Object, Object)} to decide whether it should + * call {@link Callback#onChanged(int, int)} or not. In both cases, it always removes the + * reference to the old item and puts the new item into the backing array even if + * {@link Callback#areContentsTheSame(Object, Object)} returns false. + *

+ * If the sorting criteria of the item is changed, SortedList won't be able to find + * its duplicate in the list which will result in having a duplicate of the Item in the list. + * If you need to update sorting criteria of an item that already exists in the list, + * use {@link #updateItemAt(int, Object)}. You can find the index of the item using + * {@link #indexOf(Object)} before you update the object. + * + * @param item The item to be added into the list. + * + * @return The index of the newly added item. + * @see {@link Callback#compare(Object, Object)} + * @see {@link Callback#areItemsTheSame(Object, Object)} + * @see {@link Callback#areContentsTheSame(Object, Object)}} + */ + public int add(T item) { + throwIfMerging(); + return add(item, true); + } + + /** + * Adds the given items to the list. Equivalent to calling {@link SortedList#add} in a loop, + * except the callback events may be in a different order/granularity since addAll can batch + * them for better performance. + *

+ * If allowed, may modify the input array and even take the ownership over it in order + * to avoid extra memory allocation during sorting and deduplication. + *

+ * @param items Array of items to be added into the list. + * @param mayModifyInput If true, SortedList is allowed to modify the input. + * @see {@link SortedList#addAll(Object[] items)}. + */ + public void addAll(T[] items, boolean mayModifyInput) { + throwIfMerging(); + if (items.length == 0) { + return; + } + if (mayModifyInput) { + addAllInternal(items); + } else { + T[] copy = (T[]) Array.newInstance(mTClass, items.length); + System.arraycopy(items, 0, copy, 0, items.length); + addAllInternal(copy); + } + + } + + /** + * Adds the given items to the list. Does not modify the input. + * + * @see {@link SortedList#addAll(T[] items, boolean mayModifyInput)} + * + * @param items Array of items to be added into the list. + */ + public void addAll(T... items) { + addAll(items, false); + } + + /** + * Adds the given items to the list. Does not modify the input. + * + * @see {@link SortedList#addAll(T[] items, boolean mayModifyInput)} + * + * @param items Collection of items to be added into the list. + */ + public void addAll(Collection items) { + T[] copy = (T[]) Array.newInstance(mTClass, items.size()); + addAll(items.toArray(copy), true); + } + + private void addAllInternal(T[] newItems) { + final boolean forceBatchedUpdates = !(mCallback instanceof BatchedCallback); + if (forceBatchedUpdates) { + beginBatchedUpdates(); + } + + mOldData = mData; + mOldDataStart = 0; + mOldDataSize = mSize; + + Arrays.sort(newItems, mCallback); // Arrays.sort is stable. + + final int newSize = deduplicate(newItems); + if (mSize == 0) { + mData = newItems; + mSize = newSize; + mMergedSize = newSize; + mCallback.onInserted(0, newSize); + } else { + merge(newItems, newSize); + } + + mOldData = null; + + if (forceBatchedUpdates) { + endBatchedUpdates(); + } + } + + /** + * Remove duplicate items, leaving only the last item from each group of "same" items. + * Move the remaining items to the beginning of the array. + * + * @return Number of deduplicated items at the beginning of the array. + */ + private int deduplicate(T[] items) { + if (items.length == 0) { + throw new IllegalArgumentException("Input array must be non-empty"); + } + + // Keep track of the range of equal items at the end of the output. + // Start with the range containing just the first item. + int rangeStart = 0; + int rangeEnd = 1; + + for (int i = 1; i < items.length; ++i) { + T currentItem = items[i]; + + int compare = mCallback.compare(items[rangeStart], currentItem); + if (compare > 0) { + throw new IllegalArgumentException("Input must be sorted in ascending order."); + } + + if (compare == 0) { + // The range of equal items continues, update it. + final int sameItemPos = findSameItem(currentItem, items, rangeStart, rangeEnd); + if (sameItemPos != INVALID_POSITION) { + // Replace the duplicate item. + items[sameItemPos] = currentItem; + } else { + // Expand the range. + if (rangeEnd != i) { // Avoid redundant copy. + items[rangeEnd] = currentItem; + } + rangeEnd++; + } + } else { + // The range has ended. Reset it to contain just the current item. + if (rangeEnd != i) { // Avoid redundant copy. + items[rangeEnd] = currentItem; + } + rangeStart = rangeEnd++; + } + } + return rangeEnd; + } + + + private int findSameItem(T item, T[] items, int from, int to) { + for (int pos = from; pos < to; pos++) { + if (mCallback.areItemsTheSame(items[pos], item)) { + return pos; + } + } + return INVALID_POSITION; + } + + /** + * This method assumes that newItems are sorted and deduplicated. + */ + private void merge(T[] newData, int newDataSize) { + final int mergedCapacity = mSize + newDataSize + CAPACITY_GROWTH; + mData = (T[]) Array.newInstance(mTClass, mergedCapacity); + mMergedSize = 0; + + int newDataStart = 0; + while (mOldDataStart < mOldDataSize || newDataStart < newDataSize) { + if (mOldDataStart == mOldDataSize) { + // No more old items, copy the remaining new items. + int itemCount = newDataSize - newDataStart; + System.arraycopy(newData, newDataStart, mData, mMergedSize, itemCount); + mMergedSize += itemCount; + mSize += itemCount; + mCallback.onInserted(mMergedSize - itemCount, itemCount); + break; + } + + if (newDataStart == newDataSize) { + // No more new items, copy the remaining old items. + int itemCount = mOldDataSize - mOldDataStart; + System.arraycopy(mOldData, mOldDataStart, mData, mMergedSize, itemCount); + mMergedSize += itemCount; + break; + } + + T oldItem = mOldData[mOldDataStart]; + T newItem = newData[newDataStart]; + int compare = mCallback.compare(oldItem, newItem); + if (compare > 0) { + // New item is lower, output it. + mData[mMergedSize++] = newItem; + mSize++; + newDataStart++; + mCallback.onInserted(mMergedSize - 1, 1); + } else if (compare == 0 && mCallback.areItemsTheSame(oldItem, newItem)) { + // Items are the same. Output the new item, but consume both. + mData[mMergedSize++] = newItem; + newDataStart++; + mOldDataStart++; + if (!mCallback.areContentsTheSame(oldItem, newItem)) { + mCallback.onChanged(mMergedSize - 1, 1); + } + } else { + // Old item is lower than or equal to (but not the same as the new). Output it. + // New item with the same sort order will be inserted later. + mData[mMergedSize++] = oldItem; + mOldDataStart++; + } + } + } + + private void throwIfMerging() { + if (mOldData != null) { + throw new IllegalStateException("Cannot call this method from within addAll"); + } + } + + /** + * Batches adapter updates that happen between calling this method until calling + * {@link #endBatchedUpdates()}. For example, if you add multiple items in a loop + * and they are placed into consecutive indices, SortedList calls + * {@link Callback#onInserted(int, int)} only once with the proper item count. If an event + * cannot be merged with the previous event, the previous event is dispatched + * to the callback instantly. + *

+ * After running your data updates, you must call {@link #endBatchedUpdates()} + * which will dispatch any deferred data change event to the current callback. + *

+ * A sample implementation may look like this: + *

+     *     mSortedList.beginBatchedUpdates();
+     *     try {
+     *         mSortedList.add(item1)
+     *         mSortedList.add(item2)
+     *         mSortedList.remove(item3)
+     *         ...
+     *     } finally {
+     *         mSortedList.endBatchedUpdates();
+     *     }
+     * 
+ *

+ * Instead of using this method to batch calls, you can use a Callback that extends + * {@link BatchedCallback}. In that case, you must make sure that you are manually calling + * {@link BatchedCallback#dispatchLastEvent()} right after you complete your data changes. + * Failing to do so may create data inconsistencies with the Callback. + *

+ * If the current Callback in an instance of {@link BatchedCallback}, calling this method + * has no effect. + */ + public void beginBatchedUpdates() { + throwIfMerging(); + if (mCallback instanceof BatchedCallback) { + return; + } + if (mBatchedCallback == null) { + mBatchedCallback = new BatchedCallback(mCallback); + } + mCallback = mBatchedCallback; + } + + /** + * Ends the update transaction and dispatches any remaining event to the callback. + */ + public void endBatchedUpdates() { + throwIfMerging(); + if (mCallback instanceof BatchedCallback) { + ((BatchedCallback) mCallback).dispatchLastEvent(); + } + if (mCallback == mBatchedCallback) { + mCallback = mBatchedCallback.mWrappedCallback; + } + } + + private int add(T item, boolean notify) { + int index = findIndexOf(item, mData, 0, mSize, INSERTION); + if (index == INVALID_POSITION) { + index = 0; + } else if (index < mSize) { + T existing = mData[index]; + if (mCallback.areItemsTheSame(existing, item)) { + if (mCallback.areContentsTheSame(existing, item)) { + //no change but still replace the item + mData[index] = item; + return index; + } else { + mData[index] = item; + mCallback.onChanged(index, 1); + return index; + } + } + } + addToData(index, item); + if (notify) { + mCallback.onInserted(index, 1); + } + return index; + } + + /** + * Removes the provided item from the list and calls {@link Callback#onRemoved(int, int)}. + * + * @param item The item to be removed from the list. + * + * @return True if item is removed, false if item cannot be found in the list. + */ + public boolean remove(T item) { + throwIfMerging(); + return remove(item, true); + } + + /** + * Removes the item at the given index and calls {@link Callback#onRemoved(int, int)}. + * + * @param index The index of the item to be removed. + * + * @return The removed item. + */ + public T removeItemAt(int index) { + throwIfMerging(); + T item = get(index); + removeItemAtIndex(index, true); + return item; + } + + private boolean remove(T item, boolean notify) { + int index = findIndexOf(item, mData, 0, mSize, DELETION); + if (index == INVALID_POSITION) { + return false; + } + removeItemAtIndex(index, notify); + return true; + } + + private void removeItemAtIndex(int index, boolean notify) { + System.arraycopy(mData, index + 1, mData, index, mSize - index - 1); + mSize--; + mData[mSize] = null; + if (notify) { + mCallback.onRemoved(index, 1); + } + } + + /** + * Updates the item at the given index and calls {@link Callback#onChanged(int, int)} and/or + * {@link Callback#onMoved(int, int)} if necessary. + *

+ * You can use this method if you need to change an existing Item such that its position in the + * list may change. + *

+ * If the new object is a different object (get(index) != item) and + * {@link Callback#areContentsTheSame(Object, Object)} returns true, SortedList + * avoids calling {@link Callback#onChanged(int, int)} otherwise it calls + * {@link Callback#onChanged(int, int)}. + *

+ * If the new position of the item is different than the provided index, + * SortedList + * calls {@link Callback#onMoved(int, int)}. + * + * @param index The index of the item to replace + * @param item The item to replace the item at the given Index. + * @see #add(Object) + */ + public void updateItemAt(int index, T item) { + throwIfMerging(); + final T existing = get(index); + // assume changed if the same object is given back + boolean contentsChanged = existing == item || !mCallback.areContentsTheSame(existing, item); + if (existing != item) { + // different items, we can use comparison and may avoid lookup + final int cmp = mCallback.compare(existing, item); + if (cmp == 0) { + mData[index] = item; + if (contentsChanged) { + mCallback.onChanged(index, 1); + } + return; + } + } + if (contentsChanged) { + mCallback.onChanged(index, 1); + } + // TODO this done in 1 pass to avoid shifting twice. + removeItemAtIndex(index, false); + int newIndex = add(item, false); + if (index != newIndex) { + mCallback.onMoved(index, newIndex); + } + } + + /** + * This method can be used to recalculate the position of the item at the given index, without + * triggering an {@link Callback#onChanged(int, int)} callback. + *

+ * If you are editing objects in the list such that their position in the list may change but + * you don't want to trigger an onChange animation, you can use this method to re-position it. + * If the item changes position, SortedList will call {@link Callback#onMoved(int, int)} + * without + * calling {@link Callback#onChanged(int, int)}. + *

+ * A sample usage may look like: + * + *

+     *     final int position = mSortedList.indexOf(item);
+     *     item.incrementPriority(); // assume items are sorted by priority
+     *     mSortedList.recalculatePositionOfItemAt(position);
+     * 
+ * In the example above, because the sorting criteria of the item has been changed, + * mSortedList.indexOf(item) will not be able to find the item. This is why the code above + * first + * gets the position before editing the item, edits it and informs the SortedList that item + * should be repositioned. + * + * @param index The current index of the Item whose position should be re-calculated. + * @see #updateItemAt(int, Object) + * @see #add(Object) + */ + public void recalculatePositionOfItemAt(int index) { + throwIfMerging(); + // TODO can be improved + final T item = get(index); + removeItemAtIndex(index, false); + int newIndex = add(item, false); + if (index != newIndex) { + mCallback.onMoved(index, newIndex); + } + } + + /** + * Returns the item at the given index. + * + * @param index The index of the item to retrieve. + * + * @return The item at the given index. + * @throws java.lang.IndexOutOfBoundsException if provided index is negative or larger than the + * size of the list. + */ + public T get(int index) throws IndexOutOfBoundsException { + if (index >= mSize || index < 0) { + throw new IndexOutOfBoundsException("Asked to get item at " + index + " but size is " + + mSize); + } + if (mOldData != null) { + // The call is made from a callback during addAll execution. The data is split + // between mData and mOldData. + if (index >= mMergedSize) { + return mOldData[index - mMergedSize + mOldDataStart]; + } + } + return mData[index]; + } + + /** + * Returns the position of the provided item. + * + * @param item The item to query for position. + * + * @return The position of the provided item or {@link #INVALID_POSITION} if item is not in the + * list. + */ + public int indexOf(T item) { + if (mOldData != null) { + int index = findIndexOf(item, mData, 0, mMergedSize, LOOKUP); + if (index != INVALID_POSITION) { + return index; + } + index = findIndexOf(item, mOldData, mOldDataStart, mOldDataSize, LOOKUP); + if (index != INVALID_POSITION) { + return index - mOldDataStart + mMergedSize; + } + return INVALID_POSITION; + } + return findIndexOf(item, mData, 0, mSize, LOOKUP); + } + + private int findIndexOf(T item, T[] mData, int left, int right, int reason) { + while (left < right) { + final int middle = (left + right) / 2; + T myItem = mData[middle]; + final int cmp = mCallback.compare(myItem, item); + if (cmp < 0) { + left = middle + 1; + } else if (cmp == 0) { + if (mCallback.areItemsTheSame(myItem, item)) { + return middle; + } else { + int exact = linearEqualitySearch(item, middle, left, right); + if (reason == INSERTION) { + return exact == INVALID_POSITION ? middle : exact; + } else { + return exact; + } + } + } else { + right = middle; + } + } + return reason == INSERTION ? left : INVALID_POSITION; + } + + private int linearEqualitySearch(T item, int middle, int left, int right) { + // go left + for (int next = middle - 1; next >= left; next--) { + T nextItem = mData[next]; + int cmp = mCallback.compare(nextItem, item); + if (cmp != 0) { + break; + } + if (mCallback.areItemsTheSame(nextItem, item)) { + return next; + } + } + for (int next = middle + 1; next < right; next++) { + T nextItem = mData[next]; + int cmp = mCallback.compare(nextItem, item); + if (cmp != 0) { + break; + } + if (mCallback.areItemsTheSame(nextItem, item)) { + return next; + } + } + return INVALID_POSITION; + } + + private void addToData(int index, T item) { + if (index > mSize) { + throw new IndexOutOfBoundsException( + "cannot add item to " + index + " because size is " + mSize); + } + if (mSize == mData.length) { + // we are at the limit enlarge + T[] newData = (T[]) Array.newInstance(mTClass, mData.length + CAPACITY_GROWTH); + System.arraycopy(mData, 0, newData, 0, index); + newData[index] = item; + System.arraycopy(mData, index, newData, index + 1, mSize - index); + mData = newData; + } else { + // just shift, we fit + System.arraycopy(mData, index, mData, index + 1, mSize - index); + mData[index] = item; + } + mSize++; + } + + /** + * Removes all items from the SortedList. + */ + public void clear() { + throwIfMerging(); + if (mSize == 0) { + return; + } + final int prevSize = mSize; + Arrays.fill(mData, 0, prevSize, null); + mSize = 0; + mCallback.onRemoved(0, prevSize); + } + + /** + * The class that controls the behavior of the {@link SortedList}. + *

+ * It defines how items should be sorted and how duplicates should be handled. + *

+ * SortedList calls the callback methods on this class to notify changes about the underlying + * data. + */ + public static abstract class Callback implements Comparator, ListUpdateCallback { + + /** + * Similar to {@link java.util.Comparator#compare(Object, Object)}, should compare two and + * return how they should be ordered. + * + * @param o1 The first object to compare. + * @param o2 The second object to compare. + * + * @return a negative integer, zero, or a positive integer as the + * first argument is less than, equal to, or greater than the + * second. + */ + @Override + abstract public int compare(T2 o1, T2 o2); + + /** + * Called by the SortedList when the item at the given position is updated. + * + * @param position The position of the item which has been updated. + * @param count The number of items which has changed. + */ + abstract public void onChanged(int position, int count); + + @Override + public void onChanged(int position, int count, Object payload) { + onChanged(position, count); + } + + /** + * Called by the SortedList when it wants to check whether two items have the same data + * or not. SortedList uses this information to decide whether it should call + * {@link #onChanged(int, int)} or not. + *

+ * SortedList uses this method to check equality instead of {@link Object#equals(Object)} + * so + * that you can change its behavior depending on your UI. + *

+ * For example, if you are using SortedList with a {@link android.support.v7.widget.RecyclerView.Adapter + * RecyclerView.Adapter}, you should + * return whether the items' visual representations are the same or not. + * + * @param oldItem The previous representation of the object. + * @param newItem The new object that replaces the previous one. + * + * @return True if the contents of the items are the same or false if they are different. + */ + abstract public boolean areContentsTheSame(T2 oldItem, T2 newItem); + + /** + * Called by the SortedList to decide whether two object represent the same Item or not. + *

+ * For example, if your items have unique ids, this method should check their equality. + * + * @param item1 The first item to check. + * @param item2 The second item to check. + * + * @return True if the two items represent the same object or false if they are different. + */ + abstract public boolean areItemsTheSame(T2 item1, T2 item2); + } + + /** + * A callback implementation that can batch notify events dispatched by the SortedList. + *

+ * This class can be useful if you want to do multiple operations on a SortedList but don't + * want to dispatch each event one by one, which may result in a performance issue. + *

+ * For example, if you are going to add multiple items to a SortedList, BatchedCallback call + * convert individual onInserted(index, 1) calls into one + * onInserted(index, N) if items are added into consecutive indices. This change + * can help RecyclerView resolve changes much more easily. + *

+ * If consecutive changes in the SortedList are not suitable for batching, BatchingCallback + * dispatches them as soon as such case is detected. After your edits on the SortedList is + * complete, you must always call {@link BatchedCallback#dispatchLastEvent()} to flush + * all changes to the Callback. + */ + public static class BatchedCallback extends Callback { + + private final Callback mWrappedCallback; + private final BatchingListUpdateCallback mBatchingListUpdateCallback; + /** + * Creates a new BatchedCallback that wraps the provided Callback. + * + * @param wrappedCallback The Callback which should received the data change callbacks. + * Other method calls (e.g. {@link #compare(Object, Object)} from + * the SortedList are directly forwarded to this Callback. + */ + public BatchedCallback(Callback wrappedCallback) { + mWrappedCallback = wrappedCallback; + mBatchingListUpdateCallback = new BatchingListUpdateCallback(mWrappedCallback); + } + + @Override + public int compare(T2 o1, T2 o2) { + return mWrappedCallback.compare(o1, o2); + } + + @Override + public void onInserted(int position, int count) { + mBatchingListUpdateCallback.onInserted(position, count); + } + + @Override + public void onRemoved(int position, int count) { + mBatchingListUpdateCallback.onRemoved(position, count); + } + + @Override + public void onMoved(int fromPosition, int toPosition) { + mBatchingListUpdateCallback.onInserted(fromPosition, toPosition); + } + + @Override + public void onChanged(int position, int count) { + mBatchingListUpdateCallback.onChanged(position, count, null); + } + + @Override + public boolean areContentsTheSame(T2 oldItem, T2 newItem) { + return mWrappedCallback.areContentsTheSame(oldItem, newItem); + } + + @Override + public boolean areItemsTheSame(T2 item1, T2 item2) { + return mWrappedCallback.areItemsTheSame(item1, item2); + } + + /** + * This method dispatches any pending event notifications to the wrapped Callback. + * You must always call this method after you are done with editing the SortedList. + */ + public void dispatchLastEvent() { + mBatchingListUpdateCallback.dispatchLastEvent(); + } + } +} diff --git a/app/src/main/java/android/support/v7/util/ThreadUtil.java b/app/src/main/java/android/support/v7/util/ThreadUtil.java new file mode 100644 index 0000000000..05db034259 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/ThreadUtil.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +interface ThreadUtil { + + interface MainThreadCallback { + + void updateItemCount(int generation, int itemCount); + + void addTile(int generation, TileList.Tile tile); + + void removeTile(int generation, int position); + } + + interface BackgroundCallback { + + void refresh(int generation); + + void updateRange(int rangeStart, int rangeEnd, int extRangeStart, int extRangeEnd, + int scrollHint); + + void loadTile(int position, int scrollHint); + + void recycleTile(TileList.Tile tile); + } + + MainThreadCallback getMainThreadProxy(MainThreadCallback callback); + + BackgroundCallback getBackgroundProxy(BackgroundCallback callback); +} diff --git a/app/src/main/java/android/support/v7/util/TileList.java b/app/src/main/java/android/support/v7/util/TileList.java new file mode 100644 index 0000000000..f686a31c24 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/TileList.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import android.util.SparseArray; + +import java.lang.reflect.Array; + +/** + * A sparse collection of tiles sorted for efficient access. + */ +class TileList { + + final int mTileSize; + + // Keyed by start position. + private final SparseArray> mTiles = new SparseArray>(10); + + Tile mLastAccessedTile; + + public TileList(int tileSize) { + mTileSize = tileSize; + } + + public T getItemAt(int pos) { + if (mLastAccessedTile == null || !mLastAccessedTile.containsPosition(pos)) { + final int startPosition = pos - (pos % mTileSize); + final int index = mTiles.indexOfKey(startPosition); + if (index < 0) { + return null; + } + mLastAccessedTile = mTiles.valueAt(index); + } + return mLastAccessedTile.getByPosition(pos); + } + + public int size() { + return mTiles.size(); + } + + public void clear() { + mTiles.clear(); + } + + public Tile getAtIndex(int index) { + return mTiles.valueAt(index); + } + + public Tile addOrReplace(Tile newTile) { + final int index = mTiles.indexOfKey(newTile.mStartPosition); + if (index < 0) { + mTiles.put(newTile.mStartPosition, newTile); + return null; + } + Tile oldTile = mTiles.valueAt(index); + mTiles.setValueAt(index, newTile); + if (mLastAccessedTile == oldTile) { + mLastAccessedTile = newTile; + } + return oldTile; + } + + public Tile removeAtPos(int startPosition) { + Tile tile = mTiles.get(startPosition); + if (mLastAccessedTile == tile) { + mLastAccessedTile = null; + } + mTiles.delete(startPosition); + return tile; + } + + public static class Tile { + public final T[] mItems; + public int mStartPosition; + public int mItemCount; + Tile mNext; // Used only for pooling recycled tiles. + + public Tile(Class klass, int size) { + //noinspection unchecked + mItems = (T[]) Array.newInstance(klass, size); + } + + boolean containsPosition(int pos) { + return mStartPosition <= pos && pos < mStartPosition + mItemCount; + } + + T getByPosition(int pos) { + return mItems[pos - mStartPosition]; + } + } +} From f6758d4d3aba1e52c3d5ae6e3c7ec1e6e926a0c3 Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Tue, 25 Oct 2016 11:25:06 +0800 Subject: [PATCH 08/11] ... --- .../support/v7/util/AsyncListUtil.java | 592 ------------ .../v7/util/BatchingListUpdateCallback.java | 123 --- .../android/support/v7/util/DiffUtil.java | 856 ------------------ .../support/v7/util/ListUpdateCallback.java | 55 -- .../support/v7/util/MessageThreadUtil.java | 283 ------ .../android/support/v7/util/SortedList.java | 821 ----------------- .../android/support/v7/util/ThreadUtil.java | 45 - .../android/support/v7/util/TileList.java | 105 --- 8 files changed, 2880 deletions(-) delete mode 100644 app/src/main/java/android/support/v7/util/AsyncListUtil.java delete mode 100644 app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java delete mode 100644 app/src/main/java/android/support/v7/util/DiffUtil.java delete mode 100644 app/src/main/java/android/support/v7/util/ListUpdateCallback.java delete mode 100644 app/src/main/java/android/support/v7/util/MessageThreadUtil.java delete mode 100644 app/src/main/java/android/support/v7/util/SortedList.java delete mode 100644 app/src/main/java/android/support/v7/util/ThreadUtil.java delete mode 100644 app/src/main/java/android/support/v7/util/TileList.java diff --git a/app/src/main/java/android/support/v7/util/AsyncListUtil.java b/app/src/main/java/android/support/v7/util/AsyncListUtil.java deleted file mode 100644 index c2a66b4fe8..0000000000 --- a/app/src/main/java/android/support/v7/util/AsyncListUtil.java +++ /dev/null @@ -1,592 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import android.support.annotation.UiThread; -import android.support.annotation.WorkerThread; -import android.util.Log; -import android.util.SparseBooleanArray; -import android.util.SparseIntArray; - -/** - * A utility class that supports asynchronous content loading. - *

- * It can be used to load Cursor data in chunks without querying the Cursor on the UI Thread while - * keeping UI and cache synchronous for better user experience. - *

- * It loads the data on a background thread and keeps only a limited number of fixed sized - * chunks in memory at all times. - *

- * {@link AsyncListUtil} queries the currently visible range through {@link ViewCallback}, - * loads the required data items in the background through {@link DataCallback}, and notifies a - * {@link ViewCallback} when the data is loaded. It may load some extra items for smoother - * scrolling. - *

- * Note that this class uses a single thread to load the data, so it suitable to load data from - * secondary storage such as disk, but not from network. - *

- * This class is designed to work with {@link android.support.v7.widget.RecyclerView}, but it does - * not depend on it and can be used with other list views. - * - */ -public class AsyncListUtil { - private static final String TAG = "AsyncListUtil"; - - private static final boolean DEBUG = false; - - final Class mTClass; - final int mTileSize; - final DataCallback mDataCallback; - final ViewCallback mViewCallback; - - final TileList mTileList; - - final ThreadUtil.MainThreadCallback mMainThreadProxy; - final ThreadUtil.BackgroundCallback mBackgroundProxy; - - final int[] mTmpRange = new int[2]; - final int[] mPrevRange = new int[2]; - final int[] mTmpRangeExtended = new int[2]; - - private boolean mAllowScrollHints; - private int mScrollHint = ViewCallback.HINT_SCROLL_NONE; - - private int mItemCount = 0; - - int mDisplayedGeneration = 0; - int mRequestedGeneration = mDisplayedGeneration; - - final private SparseIntArray mMissingPositions = new SparseIntArray(); - - private void log(String s, Object... args) { - Log.d(TAG, "[MAIN] " + String.format(s, args)); - } - - /** - * Creates an AsyncListUtil. - * - * @param klass Class of the data item. - * @param tileSize Number of item per chunk loaded at once. - * @param dataCallback Data access callback. - * @param viewCallback Callback for querying visible item range and update notifications. - */ - public AsyncListUtil(Class klass, int tileSize, DataCallback dataCallback, - ViewCallback viewCallback) { - mTClass = klass; - mTileSize = tileSize; - mDataCallback = dataCallback; - mViewCallback = viewCallback; - - mTileList = new TileList(mTileSize); - - ThreadUtil threadUtil = new MessageThreadUtil(); - mMainThreadProxy = threadUtil.getMainThreadProxy(mMainThreadCallback); - mBackgroundProxy = threadUtil.getBackgroundProxy(mBackgroundCallback); - - refresh(); - } - - private boolean isRefreshPending() { - return mRequestedGeneration != mDisplayedGeneration; - } - - /** - * Updates the currently visible item range. - * - *

- * Identifies the data items that have not been loaded yet and initiates loading them in the - * background. Should be called from the view's scroll listener (such as - * {@link android.support.v7.widget.RecyclerView.OnScrollListener#onScrolled}). - */ - public void onRangeChanged() { - if (isRefreshPending()) { - return; // Will update range will the refresh result arrives. - } - updateRange(); - mAllowScrollHints = true; - } - - /** - * Forces reloading the data. - *

- * Discards all the cached data and reloads all required data items for the currently visible - * range. To be called when the data item count and/or contents has changed. - */ - public void refresh() { - mMissingPositions.clear(); - mBackgroundProxy.refresh(++mRequestedGeneration); - } - - /** - * Returns the data item at the given position or null if it has not been loaded - * yet. - * - *

- * If this method has been called for a specific position and returned null, then - * {@link ViewCallback#onItemLoaded(int)} will be called when it finally loads. Note that if - * this position stays outside of the cached item range (as defined by - * {@link ViewCallback#extendRangeInto} method), then the callback will never be called for - * this position. - * - * @param position Item position. - * - * @return The data item at the given position or null if it has not been loaded - * yet. - */ - public T getItem(int position) { - if (position < 0 || position >= mItemCount) { - throw new IndexOutOfBoundsException(position + " is not within 0 and " + mItemCount); - } - T item = mTileList.getItemAt(position); - if (item == null && !isRefreshPending()) { - mMissingPositions.put(position, 0); - } - return item; - } - - /** - * Returns the number of items in the data set. - * - *

- * This is the number returned by a recent call to - * {@link DataCallback#refreshData()}. - * - * @return Number of items. - */ - public int getItemCount() { - return mItemCount; - } - - private void updateRange() { - mViewCallback.getItemRangeInto(mTmpRange); - if (mTmpRange[0] > mTmpRange[1] || mTmpRange[0] < 0) { - return; - } - if (mTmpRange[1] >= mItemCount) { - // Invalid range may arrive soon after the refresh. - return; - } - - if (!mAllowScrollHints) { - mScrollHint = ViewCallback.HINT_SCROLL_NONE; - } else if (mTmpRange[0] > mPrevRange[1] || mPrevRange[0] > mTmpRange[1]) { - // Ranges do not intersect, long leap not a scroll. - mScrollHint = ViewCallback.HINT_SCROLL_NONE; - } else if (mTmpRange[0] < mPrevRange[0]) { - mScrollHint = ViewCallback.HINT_SCROLL_DESC; - } else if (mTmpRange[0] > mPrevRange[0]) { - mScrollHint = ViewCallback.HINT_SCROLL_ASC; - } - - mPrevRange[0] = mTmpRange[0]; - mPrevRange[1] = mTmpRange[1]; - - mViewCallback.extendRangeInto(mTmpRange, mTmpRangeExtended, mScrollHint); - mTmpRangeExtended[0] = Math.min(mTmpRange[0], Math.max(mTmpRangeExtended[0], 0)); - mTmpRangeExtended[1] = - Math.max(mTmpRange[1], Math.min(mTmpRangeExtended[1], mItemCount - 1)); - - mBackgroundProxy.updateRange(mTmpRange[0], mTmpRange[1], - mTmpRangeExtended[0], mTmpRangeExtended[1], mScrollHint); - } - - private final ThreadUtil.MainThreadCallback - mMainThreadCallback = new ThreadUtil.MainThreadCallback() { - @Override - public void updateItemCount(int generation, int itemCount) { - if (DEBUG) { - log("updateItemCount: size=%d, gen #%d", itemCount, generation); - } - if (!isRequestedGeneration(generation)) { - return; - } - mItemCount = itemCount; - mViewCallback.onDataRefresh(); - mDisplayedGeneration = mRequestedGeneration; - recycleAllTiles(); - - mAllowScrollHints = false; // Will be set to true after a first real scroll. - // There will be no scroll event if the size change does not affect the current range. - updateRange(); - } - - @Override - public void addTile(int generation, TileList.Tile tile) { - if (!isRequestedGeneration(generation)) { - if (DEBUG) { - log("recycling an older generation tile @%d", tile.mStartPosition); - } - mBackgroundProxy.recycleTile(tile); - return; - } - TileList.Tile duplicate = mTileList.addOrReplace(tile); - if (duplicate != null) { - Log.e(TAG, "duplicate tile @" + duplicate.mStartPosition); - mBackgroundProxy.recycleTile(duplicate); - } - if (DEBUG) { - log("gen #%d, added tile @%d, total tiles: %d", - generation, tile.mStartPosition, mTileList.size()); - } - int endPosition = tile.mStartPosition + tile.mItemCount; - int index = 0; - while (index < mMissingPositions.size()) { - final int position = mMissingPositions.keyAt(index); - if (tile.mStartPosition <= position && position < endPosition) { - mMissingPositions.removeAt(index); - mViewCallback.onItemLoaded(position); - } else { - index++; - } - } - } - - @Override - public void removeTile(int generation, int position) { - if (!isRequestedGeneration(generation)) { - return; - } - TileList.Tile tile = mTileList.removeAtPos(position); - if (tile == null) { - Log.e(TAG, "tile not found @" + position); - return; - } - if (DEBUG) { - log("recycling tile @%d, total tiles: %d", tile.mStartPosition, mTileList.size()); - } - mBackgroundProxy.recycleTile(tile); - } - - private void recycleAllTiles() { - if (DEBUG) { - log("recycling all %d tiles", mTileList.size()); - } - for (int i = 0; i < mTileList.size(); i++) { - mBackgroundProxy.recycleTile(mTileList.getAtIndex(i)); - } - mTileList.clear(); - } - - private boolean isRequestedGeneration(int generation) { - return generation == mRequestedGeneration; - } - }; - - private final ThreadUtil.BackgroundCallback - mBackgroundCallback = new ThreadUtil.BackgroundCallback() { - - private TileList.Tile mRecycledRoot; - - final SparseBooleanArray mLoadedTiles = new SparseBooleanArray(); - - private int mGeneration; - private int mItemCount; - - private int mFirstRequiredTileStart; - private int mLastRequiredTileStart; - - @Override - public void refresh(int generation) { - mGeneration = generation; - mLoadedTiles.clear(); - mItemCount = mDataCallback.refreshData(); - mMainThreadProxy.updateItemCount(mGeneration, mItemCount); - } - - @Override - public void updateRange(int rangeStart, int rangeEnd, int extRangeStart, int extRangeEnd, - int scrollHint) { - if (DEBUG) { - log("updateRange: %d..%d extended to %d..%d, scroll hint: %d", - rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint); - } - - if (rangeStart > rangeEnd) { - return; - } - - final int firstVisibleTileStart = getTileStart(rangeStart); - final int lastVisibleTileStart = getTileStart(rangeEnd); - - mFirstRequiredTileStart = getTileStart(extRangeStart); - mLastRequiredTileStart = getTileStart(extRangeEnd); - if (DEBUG) { - log("requesting tile range: %d..%d", - mFirstRequiredTileStart, mLastRequiredTileStart); - } - - // All pending tile requests are removed by ThreadUtil at this point. - // Re-request all required tiles in the most optimal order. - if (scrollHint == ViewCallback.HINT_SCROLL_DESC) { - requestTiles(mFirstRequiredTileStart, lastVisibleTileStart, scrollHint, true); - requestTiles(lastVisibleTileStart + mTileSize, mLastRequiredTileStart, scrollHint, - false); - } else { - requestTiles(firstVisibleTileStart, mLastRequiredTileStart, scrollHint, false); - requestTiles(mFirstRequiredTileStart, firstVisibleTileStart - mTileSize, scrollHint, - true); - } - } - - private int getTileStart(int position) { - return position - position % mTileSize; - } - - private void requestTiles(int firstTileStart, int lastTileStart, int scrollHint, - boolean backwards) { - for (int i = firstTileStart; i <= lastTileStart; i += mTileSize) { - int tileStart = backwards ? (lastTileStart + firstTileStart - i) : i; - if (DEBUG) { - log("requesting tile @%d", tileStart); - } - mBackgroundProxy.loadTile(tileStart, scrollHint); - } - } - - @Override - public void loadTile(int position, int scrollHint) { - if (isTileLoaded(position)) { - if (DEBUG) { - log("already loaded tile @%d", position); - } - return; - } - TileList.Tile tile = acquireTile(); - tile.mStartPosition = position; - tile.mItemCount = Math.min(mTileSize, mItemCount - tile.mStartPosition); - mDataCallback.fillData(tile.mItems, tile.mStartPosition, tile.mItemCount); - flushTileCache(scrollHint); - addTile(tile); - } - - @Override - public void recycleTile(TileList.Tile tile) { - if (DEBUG) { - log("recycling tile @%d", tile.mStartPosition); - } - mDataCallback.recycleData(tile.mItems, tile.mItemCount); - - tile.mNext = mRecycledRoot; - mRecycledRoot = tile; - } - - private TileList.Tile acquireTile() { - if (mRecycledRoot != null) { - TileList.Tile result = mRecycledRoot; - mRecycledRoot = mRecycledRoot.mNext; - return result; - } - return new TileList.Tile(mTClass, mTileSize); - } - - private boolean isTileLoaded(int position) { - return mLoadedTiles.get(position); - } - - private void addTile(TileList.Tile tile) { - mLoadedTiles.put(tile.mStartPosition, true); - mMainThreadProxy.addTile(mGeneration, tile); - if (DEBUG) { - log("loaded tile @%d, total tiles: %d", tile.mStartPosition, mLoadedTiles.size()); - } - } - - private void removeTile(int position) { - mLoadedTiles.delete(position); - mMainThreadProxy.removeTile(mGeneration, position); - if (DEBUG) { - log("flushed tile @%d, total tiles: %s", position, mLoadedTiles.size()); - } - } - - private void flushTileCache(int scrollHint) { - final int cacheSizeLimit = mDataCallback.getMaxCachedTiles(); - while (mLoadedTiles.size() >= cacheSizeLimit) { - int firstLoadedTileStart = mLoadedTiles.keyAt(0); - int lastLoadedTileStart = mLoadedTiles.keyAt(mLoadedTiles.size() - 1); - int startMargin = mFirstRequiredTileStart - firstLoadedTileStart; - int endMargin = lastLoadedTileStart - mLastRequiredTileStart; - if (startMargin > 0 && (startMargin >= endMargin || - (scrollHint == ViewCallback.HINT_SCROLL_ASC))) { - removeTile(firstLoadedTileStart); - } else if (endMargin > 0 && (startMargin < endMargin || - (scrollHint == ViewCallback.HINT_SCROLL_DESC))){ - removeTile(lastLoadedTileStart); - } else { - // Could not flush on either side, bail out. - return; - } - } - } - - private void log(String s, Object... args) { - Log.d(TAG, "[BKGR] " + String.format(s, args)); - } - }; - - /** - * The callback that provides data access for {@link AsyncListUtil}. - * - *

- * All methods are called on the background thread. - */ - public static abstract class DataCallback { - - /** - * Refresh the data set and return the new data item count. - * - *

- * If the data is being accessed through {@link android.database.Cursor} this is where - * the new cursor should be created. - * - * @return Data item count. - */ - @WorkerThread - public abstract int refreshData(); - - /** - * Fill the given tile. - * - *

- * The provided tile might be a recycled tile, in which case it will already have objects. - * It is suggested to re-use these objects if possible in your use case. - * - * @param startPosition The start position in the list. - * @param itemCount The data item count. - * @param data The data item array to fill into. Should not be accessed beyond - * itemCount. - */ - @WorkerThread - public abstract void fillData(T[] data, int startPosition, int itemCount); - - /** - * Recycle the objects created in {@link #fillData} if necessary. - * - * - * @param data Array of data items. Should not be accessed beyond itemCount. - * @param itemCount The data item count. - */ - @WorkerThread - public void recycleData(T[] data, int itemCount) { - } - - /** - * Returns tile cache size limit (in tiles). - * - *

- * The actual number of cached tiles will be the maximum of this value and the number of - * tiles that is required to cover the range returned by - * {@link ViewCallback#extendRangeInto(int[], int[], int)}. - *

- * For example, if this method returns 10, and the most - * recent call to {@link ViewCallback#extendRangeInto(int[], int[], int)} returned - * {100, 179}, and the tile size is 5, then the maximum number of cached tiles will be 16. - *

- * However, if the tile size is 20, then the maximum number of cached tiles will be 10. - *

- * The default implementation returns 10. - * - * @return Maximum cache size. - */ - @WorkerThread - public int getMaxCachedTiles() { - return 10; - } - } - - /** - * The callback that links {@link AsyncListUtil} with the list view. - * - *

- * All methods are called on the main thread. - */ - public static abstract class ViewCallback { - - /** - * No scroll direction hint available. - */ - public static final int HINT_SCROLL_NONE = 0; - - /** - * Scrolling in descending order (from higher to lower positions in the order of the backing - * storage). - */ - public static final int HINT_SCROLL_DESC = 1; - - /** - * Scrolling in ascending order (from lower to higher positions in the order of the backing - * storage). - */ - public static final int HINT_SCROLL_ASC = 2; - - /** - * Compute the range of visible item positions. - *

- * outRange[0] is the position of the first visible item (in the order of the backing - * storage). - *

- * outRange[1] is the position of the last visible item (in the order of the backing - * storage). - *

- * Negative positions and positions greater or equal to {@link #getItemCount} are invalid. - * If the returned range contains invalid positions it is ignored (no item will be loaded). - * - * @param outRange The visible item range. - */ - @UiThread - public abstract void getItemRangeInto(int[] outRange); - - /** - * Compute a wider range of items that will be loaded for smoother scrolling. - * - *

- * If there is no scroll hint, the default implementation extends the visible range by half - * its length in both directions. If there is a scroll hint, the range is extended by - * its full length in the scroll direction, and by half in the other direction. - *

- * For example, if range is {100, 200} and scrollHint - * is {@link #HINT_SCROLL_ASC}, then outRange will be {50, 300}. - *

- * However, if scrollHint is {@link #HINT_SCROLL_NONE}, then - * outRange will be {50, 250} - * - * @param range Visible item range. - * @param outRange Extended range. - * @param scrollHint The scroll direction hint. - */ - @UiThread - public void extendRangeInto(int[] range, int[] outRange, int scrollHint) { - final int fullRange = range[1] - range[0] + 1; - final int halfRange = fullRange / 2; - outRange[0] = range[0] - (scrollHint == HINT_SCROLL_DESC ? fullRange : halfRange); - outRange[1] = range[1] + (scrollHint == HINT_SCROLL_ASC ? fullRange : halfRange); - } - - /** - * Called when the entire data set has changed. - */ - @UiThread - public abstract void onDataRefresh(); - - /** - * Called when an item at the given position is loaded. - * @param position Item position. - */ - @UiThread - public abstract void onItemLoaded(int position); - } -} diff --git a/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java b/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java deleted file mode 100644 index c8bc1a4b8b..0000000000 --- a/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package android.support.v7.util; - -/** - * Wraps a {@link ListUpdateCallback} callback and batches operations that can be merged. - *

- * For instance, when 2 add operations comes that adds 2 consecutive elements, - * BatchingListUpdateCallback merges them and calls the wrapped callback only once. - *

- * This is a general purpose class and is also used by - * {@link android.support.v7.util.DiffUtil.DiffResult DiffResult} and - * {@link SortedList} to minimize the number of updates that are dispatched. - *

- * If you use this class to batch updates, you must call {@link #dispatchLastEvent()} when the - * stream of update events drain. - */ -public class BatchingListUpdateCallback implements ListUpdateCallback { - private static final int TYPE_NONE = 0; - private static final int TYPE_ADD = 1; - private static final int TYPE_REMOVE = 2; - private static final int TYPE_CHANGE = 3; - - final ListUpdateCallback mWrapped; - - int mLastEventType = TYPE_NONE; - int mLastEventPosition = -1; - int mLastEventCount = -1; - Object mLastEventPayload = null; - - public BatchingListUpdateCallback(ListUpdateCallback callback) { - mWrapped = callback; - } - - /** - * BatchingListUpdateCallback holds onto the last event to see if it can be merged with the - * next one. When stream of events finish, you should call this method to dispatch the last - * event. - */ - public void dispatchLastEvent() { - if (mLastEventType == TYPE_NONE) { - return; - } - switch (mLastEventType) { - case TYPE_ADD: - mWrapped.onInserted(mLastEventPosition, mLastEventCount); - break; - case TYPE_REMOVE: - mWrapped.onRemoved(mLastEventPosition, mLastEventCount); - break; - case TYPE_CHANGE: - mWrapped.onChanged(mLastEventPosition, mLastEventCount, mLastEventPayload); - break; - } - mLastEventPayload = null; - mLastEventType = TYPE_NONE; - } - - @Override - public void onInserted(int position, int count) { - if (mLastEventType == TYPE_ADD && position >= mLastEventPosition - && position <= mLastEventPosition + mLastEventCount) { - mLastEventCount += count; - mLastEventPosition = Math.min(position, mLastEventPosition); - return; - } - dispatchLastEvent(); - mLastEventPosition = position; - mLastEventCount = count; - mLastEventType = TYPE_ADD; - } - - @Override - public void onRemoved(int position, int count) { - if (mLastEventType == TYPE_REMOVE && mLastEventPosition >= position && - mLastEventPosition <= position + count) { - mLastEventCount += count; - mLastEventPosition = position; - return; - } - dispatchLastEvent(); - mLastEventPosition = position; - mLastEventCount = count; - mLastEventType = TYPE_REMOVE; - } - - @Override - public void onMoved(int fromPosition, int toPosition) { - dispatchLastEvent(); // moves are not merged - mWrapped.onMoved(fromPosition, toPosition); - } - - @Override - public void onChanged(int position, int count, Object payload) { - if (mLastEventType == TYPE_CHANGE && - !(position > mLastEventPosition + mLastEventCount - || position + count < mLastEventPosition || mLastEventPayload != payload)) { - // take potential overlap into account - int previousEnd = mLastEventPosition + mLastEventCount; - mLastEventPosition = Math.min(position, mLastEventPosition); - mLastEventCount = Math.max(previousEnd, position + count) - mLastEventPosition; - return; - } - dispatchLastEvent(); - mLastEventPosition = position; - mLastEventCount = count; - mLastEventPayload = payload; - mLastEventType = TYPE_CHANGE; - } -} diff --git a/app/src/main/java/android/support/v7/util/DiffUtil.java b/app/src/main/java/android/support/v7/util/DiffUtil.java deleted file mode 100644 index 6f0a078fa7..0000000000 --- a/app/src/main/java/android/support/v7/util/DiffUtil.java +++ /dev/null @@ -1,856 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import android.support.annotation.Nullable; -import android.support.annotation.VisibleForTesting; -import android.support.v7.widget.RecyclerView; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * DiffUtil is a utility class that can calculate the difference between two lists and output a - * list of update operations that converts the first list into the second one. - *

- * It can be used to calculate updates for a RecyclerView Adapter. - *

- * DiffUtil uses Eugene W. Myers's difference algorithm to calculate the minimal number of updates - * to convert one list into another. Myers's algorithm does not handle items that are moved so - * DiffUtil runs a second pass on the result to detect items that were moved. - *

- * If the lists are large, this operation may take significant time so you are advised to run this - * on a background thread, get the {@link DiffResult} then apply it on the RecyclerView on the main - * thread. - *

- * This algorithm is optimized for space and uses O(N) space to find the minimal - * number of addition and removal operations between the two lists. It has O(N + D^2) expected time - * performance where D is the length of the edit script. - *

- * If move detection is enabled, it takes an additional O(N^2) time where N is the total number of - * added and removed items. If your lists are already sorted by the same constraint (e.g. a created - * timestamp for a list of posts), you can disable move detection to improve performance. - *

- * The actual runtime of the algorithm significantly depends on the number of changes in the list - * and the cost of your comparison methods. Below are some average run times for reference: - * (The test list is composed of random UUID Strings and the tests are run on Nexus 5X with M) - *

    - *
  • 100 items and 10 modifications: avg: 0.39 ms, median: 0.35 ms - *
  • 100 items and 100 modifications: 3.82 ms, median: 3.75 ms - *
  • 100 items and 100 modifications without moves: 2.09 ms, median: 2.06 ms - *
  • 1000 items and 50 modifications: avg: 4.67 ms, median: 4.59 ms - *
  • 1000 items and 50 modifications without moves: avg: 3.59 ms, median: 3.50 ms - *
  • 1000 items and 200 modifications: 27.07 ms, median: 26.92 ms - *
  • 1000 items and 200 modifications without moves: 13.54 ms, median: 13.36 ms - *
- *

- * Due to implementation constraints, the max size of the list can be 2^26. - */ -public class DiffUtil { - - private DiffUtil() { - // utility class, no instance. - } - - private static final Comparator SNAKE_COMPARATOR = new Comparator() { - @Override - public int compare(Snake o1, Snake o2) { - int cmpX = o1.x - o2.x; - return cmpX == 0 ? o1.y - o2.y : cmpX; - } - }; - - // Myers' algorithm uses two lists as axis labels. In DiffUtil's implementation, `x` axis is - // used for old list and `y` axis is used for new list. - - /** - * Calculates the list of update operations that can covert one list into the other one. - * - * @param cb The callback that acts as a gateway to the backing list data - * - * @return A DiffResult that contains the information about the edit sequence to convert the - * old list into the new list. - */ - public static DiffResult calculateDiff(Callback cb) { - return calculateDiff(cb, true); - } - - /** - * Calculates the list of update operations that can covert one list into the other one. - *

- * If your old and new lists are sorted by the same constraint and items never move (swap - * positions), you can disable move detection which takes O(N^2) time where - * N is the number of added, moved, removed items. - * - * @param cb The callback that acts as a gateway to the backing list data - * @param detectMoves True if DiffUtil should try to detect moved items, false otherwise. - * - * @return A DiffResult that contains the information about the edit sequence to convert the - * old list into the new list. - */ - public static DiffResult calculateDiff(Callback cb, boolean detectMoves) { - final int oldSize = cb.getOldListSize(); - final int newSize = cb.getNewListSize(); - - final List snakes = new ArrayList<>(); - - // instead of a recursive implementation, we keep our own stack to avoid potential stack - // overflow exceptions - final List stack = new ArrayList<>(); - - stack.add(new Range(0, oldSize, 0, newSize)); - - final int max = oldSize + newSize + Math.abs(oldSize - newSize); - // allocate forward and backward k-lines. K lines are diagonal lines in the matrix. (see the - // paper for details) - // These arrays lines keep the max reachable position for each k-line. - final int[] forward = new int[max * 2]; - final int[] backward = new int[max * 2]; - - // We pool the ranges to avoid allocations for each recursive call. - final List rangePool = new ArrayList<>(); - while (!stack.isEmpty()) { - final Range range = stack.remove(stack.size() - 1); - final Snake snake = diffPartial(cb, range.oldListStart, range.oldListEnd, - range.newListStart, range.newListEnd, forward, backward, max); - if (snake != null) { - if (snake.size > 0) { - snakes.add(snake); - } - // offset the snake to convert its coordinates from the Range's area to global - snake.x += range.oldListStart; - snake.y += range.newListStart; - - // add new ranges for left and right - final Range left = rangePool.isEmpty() ? new Range() : rangePool.remove( - rangePool.size() - 1); - left.oldListStart = range.oldListStart; - left.newListStart = range.newListStart; - if (snake.reverse) { - left.oldListEnd = snake.x; - left.newListEnd = snake.y; - } else { - if (snake.removal) { - left.oldListEnd = snake.x - 1; - left.newListEnd = snake.y; - } else { - left.oldListEnd = snake.x; - left.newListEnd = snake.y - 1; - } - } - stack.add(left); - - // re-use range for right - //noinspection UnnecessaryLocalVariable - final Range right = range; - if (snake.reverse) { - if (snake.removal) { - right.oldListStart = snake.x + snake.size + 1; - right.newListStart = snake.y + snake.size; - } else { - right.oldListStart = snake.x + snake.size; - right.newListStart = snake.y + snake.size + 1; - } - } else { - right.oldListStart = snake.x + snake.size; - right.newListStart = snake.y + snake.size; - } - stack.add(right); - } else { - rangePool.add(range); - } - - } - // sort snakes - Collections.sort(snakes, SNAKE_COMPARATOR); - - return new DiffResult(cb, snakes, forward, backward, detectMoves); - - } - - private static Snake diffPartial(Callback cb, int startOld, int endOld, - int startNew, int endNew, int[] forward, int[] backward, int kOffset) { - final int oldSize = endOld - startOld; - final int newSize = endNew - startNew; - - if (endOld - startOld < 1 || endNew - startNew < 1) { - return null; - } - - final int delta = oldSize - newSize; - final int dLimit = (oldSize + newSize + 1) / 2; - Arrays.fill(forward, kOffset - dLimit - 1, kOffset + dLimit + 1, 0); - Arrays.fill(backward, kOffset - dLimit - 1 + delta, kOffset + dLimit + 1 + delta, oldSize); - final boolean checkInFwd = delta % 2 != 0; - for (int d = 0; d <= dLimit; d++) { - for (int k = -d; k <= d; k += 2) { - // find forward path - // we can reach k from k - 1 or k + 1. Check which one is further in the graph - int x; - final boolean removal; - if (k == -d || k != d && forward[kOffset + k - 1] < forward[kOffset + k + 1]) { - x = forward[kOffset + k + 1]; - removal = false; - } else { - x = forward[kOffset + k - 1] + 1; - removal = true; - } - // set y based on x - int y = x - k; - // move diagonal as long as items match - while (x < oldSize && y < newSize - && cb.areItemsTheSame(startOld + x, startNew + y)) { - x++; - y++; - } - forward[kOffset + k] = x; - if (checkInFwd && k >= delta - d + 1 && k <= delta + d - 1) { - if (forward[kOffset + k] >= backward[kOffset + k]) { - Snake outSnake = new Snake(); - outSnake.x = backward[kOffset + k]; - outSnake.y = outSnake.x - k; - outSnake.size = forward[kOffset + k] - backward[kOffset + k]; - outSnake.removal = removal; - outSnake.reverse = false; - return outSnake; - } - } - } - for (int k = -d; k <= d; k += 2) { - // find reverse path at k + delta, in reverse - final int backwardK = k + delta; - int x; - final boolean removal; - if (backwardK == d + delta || backwardK != -d + delta - && backward[kOffset + backwardK - 1] < backward[kOffset + backwardK + 1]) { - x = backward[kOffset + backwardK - 1]; - removal = false; - } else { - x = backward[kOffset + backwardK + 1] - 1; - removal = true; - } - - // set y based on x - int y = x - backwardK; - // move diagonal as long as items match - while (x > 0 && y > 0 - && cb.areItemsTheSame(startOld + x - 1, startNew + y - 1)) { - x--; - y--; - } - backward[kOffset + backwardK] = x; - if (!checkInFwd && k + delta >= -d && k + delta <= d) { - if (forward[kOffset + backwardK] >= backward[kOffset + backwardK]) { - Snake outSnake = new Snake(); - outSnake.x = backward[kOffset + backwardK]; - outSnake.y = outSnake.x - backwardK; - outSnake.size = - forward[kOffset + backwardK] - backward[kOffset + backwardK]; - outSnake.removal = removal; - outSnake.reverse = true; - return outSnake; - } - } - } - } - throw new IllegalStateException("DiffUtil hit an unexpected case while trying to calculate" - + " the optimal path. Please make sure your data is not changing during the" - + " diff calculation."); - } - - /** - * A Callback class used by DiffUtil while calculating the diff between two lists. - */ - public abstract static class Callback { - /** - * Returns the size of the old list. - * - * @return The size of the old list. - */ - public abstract int getOldListSize(); - - /** - * Returns the size of the new list. - * - * @return The size of the new list. - */ - public abstract int getNewListSize(); - - /** - * Called by the DiffUtil to decide whether two object represent the same Item. - *

- * For example, if your items have unique ids, this method should check their id equality. - * - * @param oldItemPosition The position of the item in the old list - * @param newItemPosition The position of the item in the new list - * @return True if the two items represent the same object or false if they are different. - */ - public abstract boolean areItemsTheSame(int oldItemPosition, int newItemPosition); - - /** - * Called by the DiffUtil when it wants to check whether two items have the same data. - * DiffUtil uses this information to detect if the contents of an item has changed. - *

- * DiffUtil uses this method to check equality instead of {@link Object#equals(Object)} - * so that you can change its behavior depending on your UI. - * For example, if you are using DiffUtil with a - * {@link android.support.v7.widget.RecyclerView.Adapter RecyclerView.Adapter}, you should - * return whether the items' visual representations are the same. - *

- * This method is called only if {@link #areItemsTheSame(int, int)} returns - * {@code true} for these items. - * - * @param oldItemPosition The position of the item in the old list - * @param newItemPosition The position of the item in the new list which replaces the - * oldItem - * @return True if the contents of the items are the same or false if they are different. - */ - public abstract boolean areContentsTheSame(int oldItemPosition, int newItemPosition); - - /** - * When {@link #areItemsTheSame(int, int)} returns {@code true} for two items and - * {@link #areContentsTheSame(int, int)} returns false for them, DiffUtil - * calls this method to get a payload about the change. - *

- * For example, if you are using DiffUtil with {@link RecyclerView}, you can return the - * particular field that changed in the item and your - * {@link android.support.v7.widget.RecyclerView.ItemAnimator ItemAnimator} can use that - * information to run the correct animation. - *

- * Default implementation returns {@code null}. - * - * @param oldItemPosition The position of the item in the old list - * @param newItemPosition The position of the item in the new list - * - * @return A payload object that represents the change between the two items. - */ - @Nullable - public Object getChangePayload(int oldItemPosition, int newItemPosition) { - return null; - } - } - - /** - * Snakes represent a match between two lists. It is optionally prefixed or postfixed with an - * add or remove operation. See the Myers' paper for details. - */ - static class Snake { - /** - * Position in the old list - */ - int x; - - /** - * Position in the new list - */ - int y; - - /** - * Number of matches. Might be 0. - */ - int size; - - /** - * If true, this is a removal from the original list followed by {@code size} matches. - * If false, this is an addition from the new list followed by {@code size} matches. - */ - boolean removal; - - /** - * If true, the addition or removal is at the end of the snake. - * If false, the addition or removal is at the beginning of the snake. - */ - boolean reverse; - } - - /** - * Represents a range in two lists that needs to be solved. - *

- * This internal class is used when running Myers' algorithm without recursion. - */ - static class Range { - - int oldListStart, oldListEnd; - - int newListStart, newListEnd; - - public Range() { - } - - public Range(int oldListStart, int oldListEnd, int newListStart, int newListEnd) { - this.oldListStart = oldListStart; - this.oldListEnd = oldListEnd; - this.newListStart = newListStart; - this.newListEnd = newListEnd; - } - } - - /** - * This class holds the information about the result of a - * {@link DiffUtil#calculateDiff(Callback, boolean)} call. - *

- * You can consume the updates in a DiffResult via - * {@link #dispatchUpdatesTo(ListUpdateCallback)} or directly stream the results into a - * {@link RecyclerView.Adapter} via {@link #dispatchUpdatesTo(RecyclerView.Adapter)}. - */ - public static class DiffResult { - /** - * While reading the flags below, keep in mind that when multiple items move in a list, - * Myers's may pick any of them as the anchor item and consider that one NOT_CHANGED while - * picking others as additions and removals. This is completely fine as we later detect - * all moves. - *

- * Below, when an item is mentioned to stay in the same "location", it means we won't - * dispatch a move/add/remove for it, it DOES NOT mean the item is still in the same - * position. - */ - // item stayed the same. - private static final int FLAG_NOT_CHANGED = 1; - // item stayed in the same location but changed. - private static final int FLAG_CHANGED = FLAG_NOT_CHANGED << 1; - // Item has moved and also changed. - private static final int FLAG_MOVED_CHANGED = FLAG_CHANGED << 1; - // Item has moved but did not change. - private static final int FLAG_MOVED_NOT_CHANGED = FLAG_MOVED_CHANGED << 1; - // Ignore this update. - // If this is an addition from the new list, it means the item is actually removed from an - // earlier position and its move will be dispatched when we process the matching removal - // from the old list. - // If this is a removal from the old list, it means the item is actually added back to an - // earlier index in the new list and we'll dispatch its move when we are processing that - // addition. - private static final int FLAG_IGNORE = FLAG_MOVED_NOT_CHANGED << 1; - - // since we are re-using the int arrays that were created in the Myers' step, we mask - // change flags - private static final int FLAG_OFFSET = 5; - - private static final int FLAG_MASK = (1 << FLAG_OFFSET) - 1; - - // The Myers' snakes. At this point, we only care about their diagonal sections. - private final List mSnakes; - - // The list to keep oldItemStatuses. As we traverse old items, we assign flags to them - // which also includes whether they were a real removal or a move (and its new index). - private final int[] mOldItemStatuses; - // The list to keep newItemStatuses. As we traverse new items, we assign flags to them - // which also includes whether they were a real addition or a move(and its old index). - private final int[] mNewItemStatuses; - // The callback that was given to calcualte diff method. - private final Callback mCallback; - - private final int mOldListSize; - - private final int mNewListSize; - - private final boolean mDetectMoves; - - /** - * @param callback The callback that was used to calculate the diff - * @param snakes The list of Myers' snakes - * @param oldItemStatuses An int[] that can be re-purposed to keep metadata - * @param newItemStatuses An int[] that can be re-purposed to keep metadata - * @param detectMoves True if this DiffResult will try to detect moved items - */ - DiffResult(Callback callback, List snakes, int[] oldItemStatuses, - int[] newItemStatuses, boolean detectMoves) { - mSnakes = snakes; - mOldItemStatuses = oldItemStatuses; - mNewItemStatuses = newItemStatuses; - Arrays.fill(mOldItemStatuses, 0); - Arrays.fill(mNewItemStatuses, 0); - mCallback = callback; - mOldListSize = callback.getOldListSize(); - mNewListSize = callback.getNewListSize(); - mDetectMoves = detectMoves; - addRootSnake(); - findMatchingItems(); - } - - /** - * We always add a Snake to 0/0 so that we can run loops from end to beginning and be done - * when we run out of snakes. - */ - private void addRootSnake() { - Snake firstSnake = mSnakes.isEmpty() ? null : mSnakes.get(0); - if (firstSnake == null || firstSnake.x != 0 || firstSnake.y != 0) { - Snake root = new Snake(); - root.x = 0; - root.y = 0; - root.removal = false; - root.size = 0; - root.reverse = false; - mSnakes.add(0, root); - } - } - - /** - * This method traverses each addition / removal and tries to match it to a previous - * removal / addition. This is how we detect move operations. - *

- * This class also flags whether an item has been changed or not. - *

- * DiffUtil does this pre-processing so that if it is running on a big list, it can be moved - * to background thread where most of the expensive stuff will be calculated and kept in - * the statuses maps. DiffResult uses this pre-calculated information while dispatching - * the updates (which is probably being called on the main thread). - */ - private void findMatchingItems() { - int posOld = mOldListSize; - int posNew = mNewListSize; - // traverse the matrix from right bottom to 0,0. - for (int i = mSnakes.size() - 1; i >= 0; i--) { - final Snake snake = mSnakes.get(i); - final int endX = snake.x + snake.size; - final int endY = snake.y + snake.size; - if (mDetectMoves) { - while (posOld > endX) { - // this is a removal. Check remaining snakes to see if this was added before - findAddition(posOld, posNew, i); - posOld--; - } - while (posNew > endY) { - // this is an addition. Check remaining snakes to see if this was removed - // before - findRemoval(posOld, posNew, i); - posNew--; - } - } - for (int j = 0; j < snake.size; j++) { - // matching items. Check if it is changed or not - final int oldItemPos = snake.x + j; - final int newItemPos = snake.y + j; - final boolean theSame = mCallback - .areContentsTheSame(oldItemPos, newItemPos); - final int changeFlag = theSame ? FLAG_NOT_CHANGED : FLAG_CHANGED; - mOldItemStatuses[oldItemPos] = (newItemPos << FLAG_OFFSET) | changeFlag; - mNewItemStatuses[newItemPos] = (oldItemPos << FLAG_OFFSET) | changeFlag; - } - posOld = snake.x; - posNew = snake.y; - } - } - - private void findAddition(int x, int y, int snakeIndex) { - if (mOldItemStatuses[x - 1] != 0) { - return; // already set by a latter item - } - findMatchingItem(x, y, snakeIndex, false); - } - - private void findRemoval(int x, int y, int snakeIndex) { - if (mNewItemStatuses[y - 1] != 0) { - return; // already set by a latter item - } - findMatchingItem(x, y, snakeIndex, true); - } - - /** - * Finds a matching item that is before the given coordinates in the matrix - * (before : left and above). - * - * @param x The x position in the matrix (position in the old list) - * @param y The y position in the matrix (position in the new list) - * @param snakeIndex The current snake index - * @param removal True if we are looking for a removal, false otherwise - * - * @return True if such item is found. - */ - private boolean findMatchingItem(final int x, final int y, final int snakeIndex, - final boolean removal) { - final int myItemPos; - int curX; - int curY; - if (removal) { - myItemPos = y - 1; - curX = x; - curY = y - 1; - } else { - myItemPos = x - 1; - curX = x - 1; - curY = y; - } - for (int i = snakeIndex; i >= 0; i--) { - final Snake snake = mSnakes.get(i); - final int endX = snake.x + snake.size; - final int endY = snake.y + snake.size; - if (removal) { - // check removals for a match - for (int pos = curX - 1; pos >= endX; pos--) { - if (mCallback.areItemsTheSame(pos, myItemPos)) { - // found! - final boolean theSame = mCallback.areContentsTheSame(pos, myItemPos); - final int changeFlag = theSame ? FLAG_MOVED_NOT_CHANGED - : FLAG_MOVED_CHANGED; - mNewItemStatuses[myItemPos] = (pos << FLAG_OFFSET) | FLAG_IGNORE; - mOldItemStatuses[pos] = (myItemPos << FLAG_OFFSET) | changeFlag; - return true; - } - } - } else { - // check for additions for a match - for (int pos = curY - 1; pos >= endY; pos--) { - if (mCallback.areItemsTheSame(myItemPos, pos)) { - // found - final boolean theSame = mCallback.areContentsTheSame(myItemPos, pos); - final int changeFlag = theSame ? FLAG_MOVED_NOT_CHANGED - : FLAG_MOVED_CHANGED; - mOldItemStatuses[x - 1] = (pos << FLAG_OFFSET) | FLAG_IGNORE; - mNewItemStatuses[pos] = ((x - 1) << FLAG_OFFSET) | changeFlag; - return true; - } - } - } - curX = snake.x; - curY = snake.y; - } - return false; - } - - /** - * Dispatches the update events to the given adapter. - *

- * For example, if you have an {@link android.support.v7.widget.RecyclerView.Adapter Adapter} - * that is backed by a {@link List}, you can swap the list with the new one then call this - * method to dispatch all updates to the RecyclerView. - *

-         *     List oldList = mAdapter.getData();
-         *     DiffResult result = DiffUtil.calculateDiff(new MyCallback(oldList, newList));
-         *     mAdapter.setData(newList);
-         *     result.dispatchUpdatesTo(mAdapter);
-         * 
- *

- * Note that the RecyclerView requires you to dispatch adapter updates immediately when you - * change the data (you cannot defer {@code notify*} calls). The usage above adheres to this - * rule because updates are sent to the adapter right after the backing data is changed, - * before RecyclerView tries to read it. - *

- * On the other hand, if you have another - * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver AdapterDataObserver} - * that tries to process events synchronously, this may confuse that observer because the - * list is instantly moved to its final state while the adapter updates are dispatched later - * on, one by one. If you have such an - * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver AdapterDataObserver}, - * you can use - * {@link #dispatchUpdatesTo(ListUpdateCallback)} to handle each modification - * manually. - * - * @param adapter A RecyclerView adapter which was displaying the old list and will start - * displaying the new list. - */ - public void dispatchUpdatesTo(final RecyclerView.Adapter adapter) { - dispatchUpdatesTo(new ListUpdateCallback() { - @Override - public void onInserted(int position, int count) { - adapter.notifyItemRangeInserted(position, count); - } - - @Override - public void onRemoved(int position, int count) { - adapter.notifyItemRangeRemoved(position, count); - } - - @Override - public void onMoved(int fromPosition, int toPosition) { - adapter.notifyItemMoved(fromPosition, toPosition); - } - - @Override - public void onChanged(int position, int count, Object payload) { - adapter.notifyItemRangeChanged(position, count, payload); - } - }); - } - - /** - * Dispatches update operations to the given Callback. - *

- * These updates are atomic such that the first update call effects every update call that - * comes after it (the same as RecyclerView). - * - * @param updateCallback The callback to receive the update operations. - * @see #dispatchUpdatesTo(RecyclerView.Adapter) - */ - public void dispatchUpdatesTo(ListUpdateCallback updateCallback) { - final BatchingListUpdateCallback batchingCallback; - if (updateCallback instanceof BatchingListUpdateCallback) { - batchingCallback = (BatchingListUpdateCallback) updateCallback; - } else { - batchingCallback = new BatchingListUpdateCallback(updateCallback); - // replace updateCallback with a batching callback and override references to - // updateCallback so that we don't call it directly by mistake - //noinspection UnusedAssignment - updateCallback = batchingCallback; - } - // These are add/remove ops that are converted to moves. We track their positions until - // their respective update operations are processed. - final List postponedUpdates = new ArrayList<>(); - int posOld = mOldListSize; - int posNew = mNewListSize; - for (int snakeIndex = mSnakes.size() - 1; snakeIndex >= 0; snakeIndex--) { - final Snake snake = mSnakes.get(snakeIndex); - final int snakeSize = snake.size; - final int endX = snake.x + snakeSize; - final int endY = snake.y + snakeSize; - if (endX < posOld) { - dispatchRemovals(postponedUpdates, batchingCallback, endX, posOld - endX, endX); - } - - if (endY < posNew) { - dispatchAdditions(postponedUpdates, batchingCallback, endX, posNew - endY, - endY); - } - for (int i = snakeSize - 1; i >= 0; i--) { - if ((mOldItemStatuses[snake.x + i] & FLAG_MASK) == FLAG_CHANGED) { - batchingCallback.onChanged(snake.x + i, 1, - mCallback.getChangePayload(snake.x + i, snake.y + i)); - } - } - posOld = snake.x; - posNew = snake.y; - } - batchingCallback.dispatchLastEvent(); - } - - private static PostponedUpdate removePostponedUpdate(List updates, - int pos, boolean removal) { - for (int i = updates.size() - 1; i >= 0; i--) { - final PostponedUpdate update = updates.get(i); - if (update.posInOwnerList == pos && update.removal == removal) { - updates.remove(i); - for (int j = i; j < updates.size(); j++) { - // offset other ops since they swapped positions - updates.get(j).currentPos += removal ? 1 : -1; - } - return update; - } - } - return null; - } - - private void dispatchAdditions(List postponedUpdates, - ListUpdateCallback updateCallback, int start, int count, int globalIndex) { - if (!mDetectMoves) { - updateCallback.onInserted(start, count); - return; - } - for (int i = count - 1; i >= 0; i--) { - int status = mNewItemStatuses[globalIndex + i] & FLAG_MASK; - switch (status) { - case 0: // real addition - updateCallback.onInserted(start, 1); - for (PostponedUpdate update : postponedUpdates) { - update.currentPos += 1; - } - break; - case FLAG_MOVED_CHANGED: - case FLAG_MOVED_NOT_CHANGED: - final int pos = mNewItemStatuses[globalIndex + i] >> FLAG_OFFSET; - final PostponedUpdate update = removePostponedUpdate(postponedUpdates, pos, - true); - // the item was moved from that position - //noinspection ConstantConditions - updateCallback.onMoved(update.currentPos, start); - if (status == FLAG_MOVED_CHANGED) { - // also dispatch a change - updateCallback.onChanged(start, 1, - mCallback.getChangePayload(pos, globalIndex + i)); - } - break; - case FLAG_IGNORE: // ignoring this - postponedUpdates.add(new PostponedUpdate(globalIndex + i, start, false)); - break; - default: - throw new IllegalStateException( - "unknown flag for pos " + (globalIndex + i) + " " + Long - .toBinaryString(status)); - } - } - } - - private void dispatchRemovals(List postponedUpdates, - ListUpdateCallback updateCallback, int start, int count, int globalIndex) { - if (!mDetectMoves) { - updateCallback.onRemoved(start, count); - return; - } - for (int i = count - 1; i >= 0; i--) { - final int status = mOldItemStatuses[globalIndex + i] & FLAG_MASK; - switch (status) { - case 0: // real removal - updateCallback.onRemoved(start + i, 1); - for (PostponedUpdate update : postponedUpdates) { - update.currentPos -= 1; - } - break; - case FLAG_MOVED_CHANGED: - case FLAG_MOVED_NOT_CHANGED: - final int pos = mOldItemStatuses[globalIndex + i] >> FLAG_OFFSET; - final PostponedUpdate update = removePostponedUpdate(postponedUpdates, pos, - false); - // the item was moved to that position. we do -1 because this is a move not - // add and removing current item offsets the target move by 1 - //noinspection ConstantConditions - updateCallback.onMoved(start + i, update.currentPos - 1); - if (status == FLAG_MOVED_CHANGED) { - // also dispatch a change - updateCallback.onChanged(update.currentPos - 1, 1, - mCallback.getChangePayload(globalIndex + i, pos)); - } - break; - case FLAG_IGNORE: // ignoring this - postponedUpdates.add(new PostponedUpdate(globalIndex + i, start + i, true)); - break; - default: - throw new IllegalStateException( - "unknown flag for pos " + (globalIndex + i) + " " + Long - .toBinaryString(status)); - } - } - } - - @VisibleForTesting - List getSnakes() { - return mSnakes; - } - } - - /** - * Represents an update that we skipped because it was a move. - *

- * When an update is skipped, it is tracked as other updates are dispatched until the matching - * add/remove operation is found at which point the tracked position is used to dispatch the - * update. - */ - private static class PostponedUpdate { - - int posInOwnerList; - - int currentPos; - - boolean removal; - - public PostponedUpdate(int posInOwnerList, int currentPos, boolean removal) { - this.posInOwnerList = posInOwnerList; - this.currentPos = currentPos; - this.removal = removal; - } - } -} diff --git a/app/src/main/java/android/support/v7/util/ListUpdateCallback.java b/app/src/main/java/android/support/v7/util/ListUpdateCallback.java deleted file mode 100644 index 2136202958..0000000000 --- a/app/src/main/java/android/support/v7/util/ListUpdateCallback.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package android.support.v7.util; - -/** - * An interface that can receive Update operations that are applied to a list. - *

- * This class can be used together with DiffUtil to detect changes between two lists. - */ -public interface ListUpdateCallback { - /** - * Called when {@code count} number of items are inserted at the given position. - * - * @param position The position of the new item. - * @param count The number of items that have been added. - */ - void onInserted(int position, int count); - - /** - * Called when {@code count} number of items are removed from the given position. - * - * @param position The position of the item which has been removed. - * @param count The number of items which have been removed. - */ - void onRemoved(int position, int count); - - /** - * Called when an item changes its position in the list. - * - * @param fromPosition The previous position of the item before the move. - * @param toPosition The new position of the item. - */ - void onMoved(int fromPosition, int toPosition); - - /** - * Called when {@code count} number of items are updated at the given position. - * - * @param position The position of the item which has been updated. - * @param count The number of items which has changed. - */ - void onChanged(int position, int count, Object payload); -} diff --git a/app/src/main/java/android/support/v7/util/MessageThreadUtil.java b/app/src/main/java/android/support/v7/util/MessageThreadUtil.java deleted file mode 100644 index 8aa9eda764..0000000000 --- a/app/src/main/java/android/support/v7/util/MessageThreadUtil.java +++ /dev/null @@ -1,283 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import android.os.Handler; -import android.os.Looper; -import android.support.v4.content.ParallelExecutorCompat; -import android.util.Log; - -import java.util.concurrent.Executor; -import java.util.concurrent.atomic.AtomicBoolean; - -class MessageThreadUtil implements ThreadUtil { - - @Override - public MainThreadCallback getMainThreadProxy(final MainThreadCallback callback) { - return new MainThreadCallback() { - final private MessageQueue mQueue = new MessageQueue(); - final private Handler mMainThreadHandler = new Handler(Looper.getMainLooper()); - - private static final int UPDATE_ITEM_COUNT = 1; - private static final int ADD_TILE = 2; - private static final int REMOVE_TILE = 3; - - @Override - public void updateItemCount(int generation, int itemCount) { - sendMessage(SyncQueueItem.obtainMessage(UPDATE_ITEM_COUNT, generation, itemCount)); - } - - @Override - public void addTile(int generation, TileList.Tile tile) { - sendMessage(SyncQueueItem.obtainMessage(ADD_TILE, generation, tile)); - } - - @Override - public void removeTile(int generation, int position) { - sendMessage(SyncQueueItem.obtainMessage(REMOVE_TILE, generation, position)); - } - - private void sendMessage(SyncQueueItem msg) { - mQueue.sendMessage(msg); - mMainThreadHandler.post(mMainThreadRunnable); - } - - private Runnable mMainThreadRunnable = new Runnable() { - @Override - public void run() { - SyncQueueItem msg = mQueue.next(); - while (msg != null) { - switch (msg.what) { - case UPDATE_ITEM_COUNT: - callback.updateItemCount(msg.arg1, msg.arg2); - break; - case ADD_TILE: - //noinspection unchecked - callback.addTile(msg.arg1, (TileList.Tile) msg.data); - break; - case REMOVE_TILE: - callback.removeTile(msg.arg1, msg.arg2); - break; - default: - Log.e("ThreadUtil", "Unsupported message, what=" + msg.what); - } - msg = mQueue.next(); - } - } - }; - }; - } - - @Override - public BackgroundCallback getBackgroundProxy(final BackgroundCallback callback) { - return new BackgroundCallback() { - final private MessageQueue mQueue = new MessageQueue(); - final private Executor mExecutor = ParallelExecutorCompat.getParallelExecutor(); - AtomicBoolean mBackgroundRunning = new AtomicBoolean(false); - - private static final int REFRESH = 1; - private static final int UPDATE_RANGE = 2; - private static final int LOAD_TILE = 3; - private static final int RECYCLE_TILE = 4; - - @Override - public void refresh(int generation) { - sendMessageAtFrontOfQueue(SyncQueueItem.obtainMessage(REFRESH, generation, null)); - } - - @Override - public void updateRange(int rangeStart, int rangeEnd, - int extRangeStart, int extRangeEnd, int scrollHint) { - sendMessageAtFrontOfQueue(SyncQueueItem.obtainMessage(UPDATE_RANGE, - rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint, null)); - } - - @Override - public void loadTile(int position, int scrollHint) { - sendMessage(SyncQueueItem.obtainMessage(LOAD_TILE, position, scrollHint)); - } - - @Override - public void recycleTile(TileList.Tile tile) { - sendMessage(SyncQueueItem.obtainMessage(RECYCLE_TILE, 0, tile)); - } - - private void sendMessage(SyncQueueItem msg) { - mQueue.sendMessage(msg); - maybeExecuteBackgroundRunnable(); - } - - private void sendMessageAtFrontOfQueue(SyncQueueItem msg) { - mQueue.sendMessageAtFrontOfQueue(msg); - maybeExecuteBackgroundRunnable(); - } - - private void maybeExecuteBackgroundRunnable() { - if (mBackgroundRunning.compareAndSet(false, true)) { - mExecutor.execute(mBackgroundRunnable); - } - } - - private Runnable mBackgroundRunnable = new Runnable() { - @Override - public void run() { - while (true) { - SyncQueueItem msg = mQueue.next(); - if (msg == null) { - break; - } - switch (msg.what) { - case REFRESH: - mQueue.removeMessages(REFRESH); - callback.refresh(msg.arg1); - break; - case UPDATE_RANGE: - mQueue.removeMessages(UPDATE_RANGE); - mQueue.removeMessages(LOAD_TILE); - callback.updateRange( - msg.arg1, msg.arg2, msg.arg3, msg.arg4, msg.arg5); - break; - case LOAD_TILE: - callback.loadTile(msg.arg1, msg.arg2); - break; - case RECYCLE_TILE: - //noinspection unchecked - callback.recycleTile((TileList.Tile) msg.data); - break; - default: - Log.e("ThreadUtil", "Unsupported message, what=" + msg.what); - } - } - mBackgroundRunning.set(false); - } - }; - }; - } - - /** - * Replica of android.os.Message. Unfortunately, cannot use it without a Handler and don't want - * to create a thread just for this component. - */ - static class SyncQueueItem { - - private static SyncQueueItem sPool; - private static final Object sPoolLock = new Object(); - private SyncQueueItem next; - public int what; - public int arg1; - public int arg2; - public int arg3; - public int arg4; - public int arg5; - public Object data; - - void recycle() { - next = null; - what = arg1 = arg2 = arg3 = arg4 = arg5 = 0; - data = null; - synchronized (sPoolLock) { - if (sPool != null) { - next = sPool; - } - sPool = this; - } - } - - static SyncQueueItem obtainMessage(int what, int arg1, int arg2, int arg3, int arg4, - int arg5, Object data) { - synchronized (sPoolLock) { - final SyncQueueItem item; - if (sPool == null) { - item = new SyncQueueItem(); - } else { - item = sPool; - sPool = sPool.next; - item.next = null; - } - item.what = what; - item.arg1 = arg1; - item.arg2 = arg2; - item.arg3 = arg3; - item.arg4 = arg4; - item.arg5 = arg5; - item.data = data; - return item; - } - } - - static SyncQueueItem obtainMessage(int what, int arg1, int arg2) { - return obtainMessage(what, arg1, arg2, 0, 0, 0, null); - } - - static SyncQueueItem obtainMessage(int what, int arg1, Object data) { - return obtainMessage(what, arg1, 0, 0, 0, 0, data); - } - } - - static class MessageQueue { - - private SyncQueueItem mRoot; - - synchronized SyncQueueItem next() { - if (mRoot == null) { - return null; - } - final SyncQueueItem next = mRoot; - mRoot = mRoot.next; - return next; - } - - synchronized void sendMessageAtFrontOfQueue(SyncQueueItem item) { - item.next = mRoot; - mRoot = item; - } - - synchronized void sendMessage(SyncQueueItem item) { - if (mRoot == null) { - mRoot = item; - return; - } - SyncQueueItem last = mRoot; - while (last.next != null) { - last = last.next; - } - last.next = item; - } - - synchronized void removeMessages(int what) { - while (mRoot != null && mRoot.what == what) { - SyncQueueItem item = mRoot; - mRoot = mRoot.next; - item.recycle(); - } - if (mRoot != null) { - SyncQueueItem prev = mRoot; - SyncQueueItem item = prev.next; - while (item != null) { - SyncQueueItem next = item.next; - if (item.what == what) { - prev.next = next; - item.recycle(); - } else { - prev = item; - } - item = next; - } - } - } - } -} diff --git a/app/src/main/java/android/support/v7/util/SortedList.java b/app/src/main/java/android/support/v7/util/SortedList.java deleted file mode 100644 index 1d48468191..0000000000 --- a/app/src/main/java/android/support/v7/util/SortedList.java +++ /dev/null @@ -1,821 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.Collection; -import java.util.Comparator; - -/** - * A Sorted list implementation that can keep items in order and also notify for changes in the - * list - * such that it can be bound to a {@link android.support.v7.widget.RecyclerView.Adapter - * RecyclerView.Adapter}. - *

- * It keeps items ordered using the {@link Callback#compare(Object, Object)} method and uses - * binary search to retrieve items. If the sorting criteria of your items may change, make sure you - * call appropriate methods while editing them to avoid data inconsistencies. - *

- * You can control the order of items and change notifications via the {@link Callback} parameter. - */ -@SuppressWarnings("unchecked") -public class SortedList { - - /** - * Used by {@link #indexOf(Object)} when he item cannot be found in the list. - */ - public static final int INVALID_POSITION = -1; - - private static final int MIN_CAPACITY = 10; - private static final int CAPACITY_GROWTH = MIN_CAPACITY; - private static final int INSERTION = 1; - private static final int DELETION = 1 << 1; - private static final int LOOKUP = 1 << 2; - T[] mData; - - /** - * A copy of the previous list contents used during the merge phase of addAll. - */ - private T[] mOldData; - private int mOldDataStart; - private int mOldDataSize; - - /** - * The size of the valid portion of mData during the merge phase of addAll. - */ - private int mMergedSize; - - - /** - * The callback instance that controls the behavior of the SortedList and get notified when - * changes happen. - */ - private Callback mCallback; - - private BatchedCallback mBatchedCallback; - - private int mSize; - private final Class mTClass; - - /** - * Creates a new SortedList of type T. - * - * @param klass The class of the contents of the SortedList. - * @param callback The callback that controls the behavior of SortedList. - */ - public SortedList(Class klass, Callback callback) { - this(klass, callback, MIN_CAPACITY); - } - - /** - * Creates a new SortedList of type T. - * - * @param klass The class of the contents of the SortedList. - * @param callback The callback that controls the behavior of SortedList. - * @param initialCapacity The initial capacity to hold items. - */ - public SortedList(Class klass, Callback callback, int initialCapacity) { - mTClass = klass; - mData = (T[]) Array.newInstance(klass, initialCapacity); - mCallback = callback; - mSize = 0; - } - - /** - * The number of items in the list. - * - * @return The number of items in the list. - */ - public int size() { - return mSize; - } - - /** - * Adds the given item to the list. If this is a new item, SortedList calls - * {@link Callback#onInserted(int, int)}. - *

- * If the item already exists in the list and its sorting criteria is not changed, it is - * replaced with the existing Item. SortedList uses - * {@link Callback#areItemsTheSame(Object, Object)} to check if two items are the same item - * and uses {@link Callback#areContentsTheSame(Object, Object)} to decide whether it should - * call {@link Callback#onChanged(int, int)} or not. In both cases, it always removes the - * reference to the old item and puts the new item into the backing array even if - * {@link Callback#areContentsTheSame(Object, Object)} returns false. - *

- * If the sorting criteria of the item is changed, SortedList won't be able to find - * its duplicate in the list which will result in having a duplicate of the Item in the list. - * If you need to update sorting criteria of an item that already exists in the list, - * use {@link #updateItemAt(int, Object)}. You can find the index of the item using - * {@link #indexOf(Object)} before you update the object. - * - * @param item The item to be added into the list. - * - * @return The index of the newly added item. - * @see {@link Callback#compare(Object, Object)} - * @see {@link Callback#areItemsTheSame(Object, Object)} - * @see {@link Callback#areContentsTheSame(Object, Object)}} - */ - public int add(T item) { - throwIfMerging(); - return add(item, true); - } - - /** - * Adds the given items to the list. Equivalent to calling {@link SortedList#add} in a loop, - * except the callback events may be in a different order/granularity since addAll can batch - * them for better performance. - *

- * If allowed, may modify the input array and even take the ownership over it in order - * to avoid extra memory allocation during sorting and deduplication. - *

- * @param items Array of items to be added into the list. - * @param mayModifyInput If true, SortedList is allowed to modify the input. - * @see {@link SortedList#addAll(Object[] items)}. - */ - public void addAll(T[] items, boolean mayModifyInput) { - throwIfMerging(); - if (items.length == 0) { - return; - } - if (mayModifyInput) { - addAllInternal(items); - } else { - T[] copy = (T[]) Array.newInstance(mTClass, items.length); - System.arraycopy(items, 0, copy, 0, items.length); - addAllInternal(copy); - } - - } - - /** - * Adds the given items to the list. Does not modify the input. - * - * @see {@link SortedList#addAll(T[] items, boolean mayModifyInput)} - * - * @param items Array of items to be added into the list. - */ - public void addAll(T... items) { - addAll(items, false); - } - - /** - * Adds the given items to the list. Does not modify the input. - * - * @see {@link SortedList#addAll(T[] items, boolean mayModifyInput)} - * - * @param items Collection of items to be added into the list. - */ - public void addAll(Collection items) { - T[] copy = (T[]) Array.newInstance(mTClass, items.size()); - addAll(items.toArray(copy), true); - } - - private void addAllInternal(T[] newItems) { - final boolean forceBatchedUpdates = !(mCallback instanceof BatchedCallback); - if (forceBatchedUpdates) { - beginBatchedUpdates(); - } - - mOldData = mData; - mOldDataStart = 0; - mOldDataSize = mSize; - - Arrays.sort(newItems, mCallback); // Arrays.sort is stable. - - final int newSize = deduplicate(newItems); - if (mSize == 0) { - mData = newItems; - mSize = newSize; - mMergedSize = newSize; - mCallback.onInserted(0, newSize); - } else { - merge(newItems, newSize); - } - - mOldData = null; - - if (forceBatchedUpdates) { - endBatchedUpdates(); - } - } - - /** - * Remove duplicate items, leaving only the last item from each group of "same" items. - * Move the remaining items to the beginning of the array. - * - * @return Number of deduplicated items at the beginning of the array. - */ - private int deduplicate(T[] items) { - if (items.length == 0) { - throw new IllegalArgumentException("Input array must be non-empty"); - } - - // Keep track of the range of equal items at the end of the output. - // Start with the range containing just the first item. - int rangeStart = 0; - int rangeEnd = 1; - - for (int i = 1; i < items.length; ++i) { - T currentItem = items[i]; - - int compare = mCallback.compare(items[rangeStart], currentItem); - if (compare > 0) { - throw new IllegalArgumentException("Input must be sorted in ascending order."); - } - - if (compare == 0) { - // The range of equal items continues, update it. - final int sameItemPos = findSameItem(currentItem, items, rangeStart, rangeEnd); - if (sameItemPos != INVALID_POSITION) { - // Replace the duplicate item. - items[sameItemPos] = currentItem; - } else { - // Expand the range. - if (rangeEnd != i) { // Avoid redundant copy. - items[rangeEnd] = currentItem; - } - rangeEnd++; - } - } else { - // The range has ended. Reset it to contain just the current item. - if (rangeEnd != i) { // Avoid redundant copy. - items[rangeEnd] = currentItem; - } - rangeStart = rangeEnd++; - } - } - return rangeEnd; - } - - - private int findSameItem(T item, T[] items, int from, int to) { - for (int pos = from; pos < to; pos++) { - if (mCallback.areItemsTheSame(items[pos], item)) { - return pos; - } - } - return INVALID_POSITION; - } - - /** - * This method assumes that newItems are sorted and deduplicated. - */ - private void merge(T[] newData, int newDataSize) { - final int mergedCapacity = mSize + newDataSize + CAPACITY_GROWTH; - mData = (T[]) Array.newInstance(mTClass, mergedCapacity); - mMergedSize = 0; - - int newDataStart = 0; - while (mOldDataStart < mOldDataSize || newDataStart < newDataSize) { - if (mOldDataStart == mOldDataSize) { - // No more old items, copy the remaining new items. - int itemCount = newDataSize - newDataStart; - System.arraycopy(newData, newDataStart, mData, mMergedSize, itemCount); - mMergedSize += itemCount; - mSize += itemCount; - mCallback.onInserted(mMergedSize - itemCount, itemCount); - break; - } - - if (newDataStart == newDataSize) { - // No more new items, copy the remaining old items. - int itemCount = mOldDataSize - mOldDataStart; - System.arraycopy(mOldData, mOldDataStart, mData, mMergedSize, itemCount); - mMergedSize += itemCount; - break; - } - - T oldItem = mOldData[mOldDataStart]; - T newItem = newData[newDataStart]; - int compare = mCallback.compare(oldItem, newItem); - if (compare > 0) { - // New item is lower, output it. - mData[mMergedSize++] = newItem; - mSize++; - newDataStart++; - mCallback.onInserted(mMergedSize - 1, 1); - } else if (compare == 0 && mCallback.areItemsTheSame(oldItem, newItem)) { - // Items are the same. Output the new item, but consume both. - mData[mMergedSize++] = newItem; - newDataStart++; - mOldDataStart++; - if (!mCallback.areContentsTheSame(oldItem, newItem)) { - mCallback.onChanged(mMergedSize - 1, 1); - } - } else { - // Old item is lower than or equal to (but not the same as the new). Output it. - // New item with the same sort order will be inserted later. - mData[mMergedSize++] = oldItem; - mOldDataStart++; - } - } - } - - private void throwIfMerging() { - if (mOldData != null) { - throw new IllegalStateException("Cannot call this method from within addAll"); - } - } - - /** - * Batches adapter updates that happen between calling this method until calling - * {@link #endBatchedUpdates()}. For example, if you add multiple items in a loop - * and they are placed into consecutive indices, SortedList calls - * {@link Callback#onInserted(int, int)} only once with the proper item count. If an event - * cannot be merged with the previous event, the previous event is dispatched - * to the callback instantly. - *

- * After running your data updates, you must call {@link #endBatchedUpdates()} - * which will dispatch any deferred data change event to the current callback. - *

- * A sample implementation may look like this: - *

-     *     mSortedList.beginBatchedUpdates();
-     *     try {
-     *         mSortedList.add(item1)
-     *         mSortedList.add(item2)
-     *         mSortedList.remove(item3)
-     *         ...
-     *     } finally {
-     *         mSortedList.endBatchedUpdates();
-     *     }
-     * 
- *

- * Instead of using this method to batch calls, you can use a Callback that extends - * {@link BatchedCallback}. In that case, you must make sure that you are manually calling - * {@link BatchedCallback#dispatchLastEvent()} right after you complete your data changes. - * Failing to do so may create data inconsistencies with the Callback. - *

- * If the current Callback in an instance of {@link BatchedCallback}, calling this method - * has no effect. - */ - public void beginBatchedUpdates() { - throwIfMerging(); - if (mCallback instanceof BatchedCallback) { - return; - } - if (mBatchedCallback == null) { - mBatchedCallback = new BatchedCallback(mCallback); - } - mCallback = mBatchedCallback; - } - - /** - * Ends the update transaction and dispatches any remaining event to the callback. - */ - public void endBatchedUpdates() { - throwIfMerging(); - if (mCallback instanceof BatchedCallback) { - ((BatchedCallback) mCallback).dispatchLastEvent(); - } - if (mCallback == mBatchedCallback) { - mCallback = mBatchedCallback.mWrappedCallback; - } - } - - private int add(T item, boolean notify) { - int index = findIndexOf(item, mData, 0, mSize, INSERTION); - if (index == INVALID_POSITION) { - index = 0; - } else if (index < mSize) { - T existing = mData[index]; - if (mCallback.areItemsTheSame(existing, item)) { - if (mCallback.areContentsTheSame(existing, item)) { - //no change but still replace the item - mData[index] = item; - return index; - } else { - mData[index] = item; - mCallback.onChanged(index, 1); - return index; - } - } - } - addToData(index, item); - if (notify) { - mCallback.onInserted(index, 1); - } - return index; - } - - /** - * Removes the provided item from the list and calls {@link Callback#onRemoved(int, int)}. - * - * @param item The item to be removed from the list. - * - * @return True if item is removed, false if item cannot be found in the list. - */ - public boolean remove(T item) { - throwIfMerging(); - return remove(item, true); - } - - /** - * Removes the item at the given index and calls {@link Callback#onRemoved(int, int)}. - * - * @param index The index of the item to be removed. - * - * @return The removed item. - */ - public T removeItemAt(int index) { - throwIfMerging(); - T item = get(index); - removeItemAtIndex(index, true); - return item; - } - - private boolean remove(T item, boolean notify) { - int index = findIndexOf(item, mData, 0, mSize, DELETION); - if (index == INVALID_POSITION) { - return false; - } - removeItemAtIndex(index, notify); - return true; - } - - private void removeItemAtIndex(int index, boolean notify) { - System.arraycopy(mData, index + 1, mData, index, mSize - index - 1); - mSize--; - mData[mSize] = null; - if (notify) { - mCallback.onRemoved(index, 1); - } - } - - /** - * Updates the item at the given index and calls {@link Callback#onChanged(int, int)} and/or - * {@link Callback#onMoved(int, int)} if necessary. - *

- * You can use this method if you need to change an existing Item such that its position in the - * list may change. - *

- * If the new object is a different object (get(index) != item) and - * {@link Callback#areContentsTheSame(Object, Object)} returns true, SortedList - * avoids calling {@link Callback#onChanged(int, int)} otherwise it calls - * {@link Callback#onChanged(int, int)}. - *

- * If the new position of the item is different than the provided index, - * SortedList - * calls {@link Callback#onMoved(int, int)}. - * - * @param index The index of the item to replace - * @param item The item to replace the item at the given Index. - * @see #add(Object) - */ - public void updateItemAt(int index, T item) { - throwIfMerging(); - final T existing = get(index); - // assume changed if the same object is given back - boolean contentsChanged = existing == item || !mCallback.areContentsTheSame(existing, item); - if (existing != item) { - // different items, we can use comparison and may avoid lookup - final int cmp = mCallback.compare(existing, item); - if (cmp == 0) { - mData[index] = item; - if (contentsChanged) { - mCallback.onChanged(index, 1); - } - return; - } - } - if (contentsChanged) { - mCallback.onChanged(index, 1); - } - // TODO this done in 1 pass to avoid shifting twice. - removeItemAtIndex(index, false); - int newIndex = add(item, false); - if (index != newIndex) { - mCallback.onMoved(index, newIndex); - } - } - - /** - * This method can be used to recalculate the position of the item at the given index, without - * triggering an {@link Callback#onChanged(int, int)} callback. - *

- * If you are editing objects in the list such that their position in the list may change but - * you don't want to trigger an onChange animation, you can use this method to re-position it. - * If the item changes position, SortedList will call {@link Callback#onMoved(int, int)} - * without - * calling {@link Callback#onChanged(int, int)}. - *

- * A sample usage may look like: - * - *

-     *     final int position = mSortedList.indexOf(item);
-     *     item.incrementPriority(); // assume items are sorted by priority
-     *     mSortedList.recalculatePositionOfItemAt(position);
-     * 
- * In the example above, because the sorting criteria of the item has been changed, - * mSortedList.indexOf(item) will not be able to find the item. This is why the code above - * first - * gets the position before editing the item, edits it and informs the SortedList that item - * should be repositioned. - * - * @param index The current index of the Item whose position should be re-calculated. - * @see #updateItemAt(int, Object) - * @see #add(Object) - */ - public void recalculatePositionOfItemAt(int index) { - throwIfMerging(); - // TODO can be improved - final T item = get(index); - removeItemAtIndex(index, false); - int newIndex = add(item, false); - if (index != newIndex) { - mCallback.onMoved(index, newIndex); - } - } - - /** - * Returns the item at the given index. - * - * @param index The index of the item to retrieve. - * - * @return The item at the given index. - * @throws java.lang.IndexOutOfBoundsException if provided index is negative or larger than the - * size of the list. - */ - public T get(int index) throws IndexOutOfBoundsException { - if (index >= mSize || index < 0) { - throw new IndexOutOfBoundsException("Asked to get item at " + index + " but size is " - + mSize); - } - if (mOldData != null) { - // The call is made from a callback during addAll execution. The data is split - // between mData and mOldData. - if (index >= mMergedSize) { - return mOldData[index - mMergedSize + mOldDataStart]; - } - } - return mData[index]; - } - - /** - * Returns the position of the provided item. - * - * @param item The item to query for position. - * - * @return The position of the provided item or {@link #INVALID_POSITION} if item is not in the - * list. - */ - public int indexOf(T item) { - if (mOldData != null) { - int index = findIndexOf(item, mData, 0, mMergedSize, LOOKUP); - if (index != INVALID_POSITION) { - return index; - } - index = findIndexOf(item, mOldData, mOldDataStart, mOldDataSize, LOOKUP); - if (index != INVALID_POSITION) { - return index - mOldDataStart + mMergedSize; - } - return INVALID_POSITION; - } - return findIndexOf(item, mData, 0, mSize, LOOKUP); - } - - private int findIndexOf(T item, T[] mData, int left, int right, int reason) { - while (left < right) { - final int middle = (left + right) / 2; - T myItem = mData[middle]; - final int cmp = mCallback.compare(myItem, item); - if (cmp < 0) { - left = middle + 1; - } else if (cmp == 0) { - if (mCallback.areItemsTheSame(myItem, item)) { - return middle; - } else { - int exact = linearEqualitySearch(item, middle, left, right); - if (reason == INSERTION) { - return exact == INVALID_POSITION ? middle : exact; - } else { - return exact; - } - } - } else { - right = middle; - } - } - return reason == INSERTION ? left : INVALID_POSITION; - } - - private int linearEqualitySearch(T item, int middle, int left, int right) { - // go left - for (int next = middle - 1; next >= left; next--) { - T nextItem = mData[next]; - int cmp = mCallback.compare(nextItem, item); - if (cmp != 0) { - break; - } - if (mCallback.areItemsTheSame(nextItem, item)) { - return next; - } - } - for (int next = middle + 1; next < right; next++) { - T nextItem = mData[next]; - int cmp = mCallback.compare(nextItem, item); - if (cmp != 0) { - break; - } - if (mCallback.areItemsTheSame(nextItem, item)) { - return next; - } - } - return INVALID_POSITION; - } - - private void addToData(int index, T item) { - if (index > mSize) { - throw new IndexOutOfBoundsException( - "cannot add item to " + index + " because size is " + mSize); - } - if (mSize == mData.length) { - // we are at the limit enlarge - T[] newData = (T[]) Array.newInstance(mTClass, mData.length + CAPACITY_GROWTH); - System.arraycopy(mData, 0, newData, 0, index); - newData[index] = item; - System.arraycopy(mData, index, newData, index + 1, mSize - index); - mData = newData; - } else { - // just shift, we fit - System.arraycopy(mData, index, mData, index + 1, mSize - index); - mData[index] = item; - } - mSize++; - } - - /** - * Removes all items from the SortedList. - */ - public void clear() { - throwIfMerging(); - if (mSize == 0) { - return; - } - final int prevSize = mSize; - Arrays.fill(mData, 0, prevSize, null); - mSize = 0; - mCallback.onRemoved(0, prevSize); - } - - /** - * The class that controls the behavior of the {@link SortedList}. - *

- * It defines how items should be sorted and how duplicates should be handled. - *

- * SortedList calls the callback methods on this class to notify changes about the underlying - * data. - */ - public static abstract class Callback implements Comparator, ListUpdateCallback { - - /** - * Similar to {@link java.util.Comparator#compare(Object, Object)}, should compare two and - * return how they should be ordered. - * - * @param o1 The first object to compare. - * @param o2 The second object to compare. - * - * @return a negative integer, zero, or a positive integer as the - * first argument is less than, equal to, or greater than the - * second. - */ - @Override - abstract public int compare(T2 o1, T2 o2); - - /** - * Called by the SortedList when the item at the given position is updated. - * - * @param position The position of the item which has been updated. - * @param count The number of items which has changed. - */ - abstract public void onChanged(int position, int count); - - @Override - public void onChanged(int position, int count, Object payload) { - onChanged(position, count); - } - - /** - * Called by the SortedList when it wants to check whether two items have the same data - * or not. SortedList uses this information to decide whether it should call - * {@link #onChanged(int, int)} or not. - *

- * SortedList uses this method to check equality instead of {@link Object#equals(Object)} - * so - * that you can change its behavior depending on your UI. - *

- * For example, if you are using SortedList with a {@link android.support.v7.widget.RecyclerView.Adapter - * RecyclerView.Adapter}, you should - * return whether the items' visual representations are the same or not. - * - * @param oldItem The previous representation of the object. - * @param newItem The new object that replaces the previous one. - * - * @return True if the contents of the items are the same or false if they are different. - */ - abstract public boolean areContentsTheSame(T2 oldItem, T2 newItem); - - /** - * Called by the SortedList to decide whether two object represent the same Item or not. - *

- * For example, if your items have unique ids, this method should check their equality. - * - * @param item1 The first item to check. - * @param item2 The second item to check. - * - * @return True if the two items represent the same object or false if they are different. - */ - abstract public boolean areItemsTheSame(T2 item1, T2 item2); - } - - /** - * A callback implementation that can batch notify events dispatched by the SortedList. - *

- * This class can be useful if you want to do multiple operations on a SortedList but don't - * want to dispatch each event one by one, which may result in a performance issue. - *

- * For example, if you are going to add multiple items to a SortedList, BatchedCallback call - * convert individual onInserted(index, 1) calls into one - * onInserted(index, N) if items are added into consecutive indices. This change - * can help RecyclerView resolve changes much more easily. - *

- * If consecutive changes in the SortedList are not suitable for batching, BatchingCallback - * dispatches them as soon as such case is detected. After your edits on the SortedList is - * complete, you must always call {@link BatchedCallback#dispatchLastEvent()} to flush - * all changes to the Callback. - */ - public static class BatchedCallback extends Callback { - - private final Callback mWrappedCallback; - private final BatchingListUpdateCallback mBatchingListUpdateCallback; - /** - * Creates a new BatchedCallback that wraps the provided Callback. - * - * @param wrappedCallback The Callback which should received the data change callbacks. - * Other method calls (e.g. {@link #compare(Object, Object)} from - * the SortedList are directly forwarded to this Callback. - */ - public BatchedCallback(Callback wrappedCallback) { - mWrappedCallback = wrappedCallback; - mBatchingListUpdateCallback = new BatchingListUpdateCallback(mWrappedCallback); - } - - @Override - public int compare(T2 o1, T2 o2) { - return mWrappedCallback.compare(o1, o2); - } - - @Override - public void onInserted(int position, int count) { - mBatchingListUpdateCallback.onInserted(position, count); - } - - @Override - public void onRemoved(int position, int count) { - mBatchingListUpdateCallback.onRemoved(position, count); - } - - @Override - public void onMoved(int fromPosition, int toPosition) { - mBatchingListUpdateCallback.onInserted(fromPosition, toPosition); - } - - @Override - public void onChanged(int position, int count) { - mBatchingListUpdateCallback.onChanged(position, count, null); - } - - @Override - public boolean areContentsTheSame(T2 oldItem, T2 newItem) { - return mWrappedCallback.areContentsTheSame(oldItem, newItem); - } - - @Override - public boolean areItemsTheSame(T2 item1, T2 item2) { - return mWrappedCallback.areItemsTheSame(item1, item2); - } - - /** - * This method dispatches any pending event notifications to the wrapped Callback. - * You must always call this method after you are done with editing the SortedList. - */ - public void dispatchLastEvent() { - mBatchingListUpdateCallback.dispatchLastEvent(); - } - } -} diff --git a/app/src/main/java/android/support/v7/util/ThreadUtil.java b/app/src/main/java/android/support/v7/util/ThreadUtil.java deleted file mode 100644 index 05db034259..0000000000 --- a/app/src/main/java/android/support/v7/util/ThreadUtil.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -interface ThreadUtil { - - interface MainThreadCallback { - - void updateItemCount(int generation, int itemCount); - - void addTile(int generation, TileList.Tile tile); - - void removeTile(int generation, int position); - } - - interface BackgroundCallback { - - void refresh(int generation); - - void updateRange(int rangeStart, int rangeEnd, int extRangeStart, int extRangeEnd, - int scrollHint); - - void loadTile(int position, int scrollHint); - - void recycleTile(TileList.Tile tile); - } - - MainThreadCallback getMainThreadProxy(MainThreadCallback callback); - - BackgroundCallback getBackgroundProxy(BackgroundCallback callback); -} diff --git a/app/src/main/java/android/support/v7/util/TileList.java b/app/src/main/java/android/support/v7/util/TileList.java deleted file mode 100644 index f686a31c24..0000000000 --- a/app/src/main/java/android/support/v7/util/TileList.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import android.util.SparseArray; - -import java.lang.reflect.Array; - -/** - * A sparse collection of tiles sorted for efficient access. - */ -class TileList { - - final int mTileSize; - - // Keyed by start position. - private final SparseArray> mTiles = new SparseArray>(10); - - Tile mLastAccessedTile; - - public TileList(int tileSize) { - mTileSize = tileSize; - } - - public T getItemAt(int pos) { - if (mLastAccessedTile == null || !mLastAccessedTile.containsPosition(pos)) { - final int startPosition = pos - (pos % mTileSize); - final int index = mTiles.indexOfKey(startPosition); - if (index < 0) { - return null; - } - mLastAccessedTile = mTiles.valueAt(index); - } - return mLastAccessedTile.getByPosition(pos); - } - - public int size() { - return mTiles.size(); - } - - public void clear() { - mTiles.clear(); - } - - public Tile getAtIndex(int index) { - return mTiles.valueAt(index); - } - - public Tile addOrReplace(Tile newTile) { - final int index = mTiles.indexOfKey(newTile.mStartPosition); - if (index < 0) { - mTiles.put(newTile.mStartPosition, newTile); - return null; - } - Tile oldTile = mTiles.valueAt(index); - mTiles.setValueAt(index, newTile); - if (mLastAccessedTile == oldTile) { - mLastAccessedTile = newTile; - } - return oldTile; - } - - public Tile removeAtPos(int startPosition) { - Tile tile = mTiles.get(startPosition); - if (mLastAccessedTile == tile) { - mLastAccessedTile = null; - } - mTiles.delete(startPosition); - return tile; - } - - public static class Tile { - public final T[] mItems; - public int mStartPosition; - public int mItemCount; - Tile mNext; // Used only for pooling recycled tiles. - - public Tile(Class klass, int size) { - //noinspection unchecked - mItems = (T[]) Array.newInstance(klass, size); - } - - boolean containsPosition(int pos) { - return mStartPosition <= pos && pos < mStartPosition + mItemCount; - } - - T getByPosition(int pos) { - return mItems[pos - mStartPosition]; - } - } -} From 63b8e9551c8d70f8aec332697d12f10338fdbe13 Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Tue, 25 Oct 2016 11:29:50 +0800 Subject: [PATCH 09/11] ... --- .../support/v7/util/AsyncListUtil.java | 592 ++++++++++++ .../v7/util/BatchingListUpdateCallback.java | 123 +++ .../android/support/v7/util/DiffUtil.java | 856 ++++++++++++++++++ .../support/v7/util/ListUpdateCallback.java | 55 ++ .../support/v7/util/MessageThreadUtil.java | 283 ++++++ .../android/support/v7/util/SortedList.java | 821 +++++++++++++++++ .../android/support/v7/util/ThreadUtil.java | 45 + .../android/support/v7/util/TileList.java | 105 +++ 8 files changed, 2880 insertions(+) create mode 100644 app/src/main/java/android/support/v7/util/AsyncListUtil.java create mode 100644 app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java create mode 100644 app/src/main/java/android/support/v7/util/DiffUtil.java create mode 100644 app/src/main/java/android/support/v7/util/ListUpdateCallback.java create mode 100644 app/src/main/java/android/support/v7/util/MessageThreadUtil.java create mode 100644 app/src/main/java/android/support/v7/util/SortedList.java create mode 100644 app/src/main/java/android/support/v7/util/ThreadUtil.java create mode 100644 app/src/main/java/android/support/v7/util/TileList.java diff --git a/app/src/main/java/android/support/v7/util/AsyncListUtil.java b/app/src/main/java/android/support/v7/util/AsyncListUtil.java new file mode 100644 index 0000000000..c2a66b4fe8 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/AsyncListUtil.java @@ -0,0 +1,592 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import android.support.annotation.UiThread; +import android.support.annotation.WorkerThread; +import android.util.Log; +import android.util.SparseBooleanArray; +import android.util.SparseIntArray; + +/** + * A utility class that supports asynchronous content loading. + *

+ * It can be used to load Cursor data in chunks without querying the Cursor on the UI Thread while + * keeping UI and cache synchronous for better user experience. + *

+ * It loads the data on a background thread and keeps only a limited number of fixed sized + * chunks in memory at all times. + *

+ * {@link AsyncListUtil} queries the currently visible range through {@link ViewCallback}, + * loads the required data items in the background through {@link DataCallback}, and notifies a + * {@link ViewCallback} when the data is loaded. It may load some extra items for smoother + * scrolling. + *

+ * Note that this class uses a single thread to load the data, so it suitable to load data from + * secondary storage such as disk, but not from network. + *

+ * This class is designed to work with {@link android.support.v7.widget.RecyclerView}, but it does + * not depend on it and can be used with other list views. + * + */ +public class AsyncListUtil { + private static final String TAG = "AsyncListUtil"; + + private static final boolean DEBUG = false; + + final Class mTClass; + final int mTileSize; + final DataCallback mDataCallback; + final ViewCallback mViewCallback; + + final TileList mTileList; + + final ThreadUtil.MainThreadCallback mMainThreadProxy; + final ThreadUtil.BackgroundCallback mBackgroundProxy; + + final int[] mTmpRange = new int[2]; + final int[] mPrevRange = new int[2]; + final int[] mTmpRangeExtended = new int[2]; + + private boolean mAllowScrollHints; + private int mScrollHint = ViewCallback.HINT_SCROLL_NONE; + + private int mItemCount = 0; + + int mDisplayedGeneration = 0; + int mRequestedGeneration = mDisplayedGeneration; + + final private SparseIntArray mMissingPositions = new SparseIntArray(); + + private void log(String s, Object... args) { + Log.d(TAG, "[MAIN] " + String.format(s, args)); + } + + /** + * Creates an AsyncListUtil. + * + * @param klass Class of the data item. + * @param tileSize Number of item per chunk loaded at once. + * @param dataCallback Data access callback. + * @param viewCallback Callback for querying visible item range and update notifications. + */ + public AsyncListUtil(Class klass, int tileSize, DataCallback dataCallback, + ViewCallback viewCallback) { + mTClass = klass; + mTileSize = tileSize; + mDataCallback = dataCallback; + mViewCallback = viewCallback; + + mTileList = new TileList(mTileSize); + + ThreadUtil threadUtil = new MessageThreadUtil(); + mMainThreadProxy = threadUtil.getMainThreadProxy(mMainThreadCallback); + mBackgroundProxy = threadUtil.getBackgroundProxy(mBackgroundCallback); + + refresh(); + } + + private boolean isRefreshPending() { + return mRequestedGeneration != mDisplayedGeneration; + } + + /** + * Updates the currently visible item range. + * + *

+ * Identifies the data items that have not been loaded yet and initiates loading them in the + * background. Should be called from the view's scroll listener (such as + * {@link android.support.v7.widget.RecyclerView.OnScrollListener#onScrolled}). + */ + public void onRangeChanged() { + if (isRefreshPending()) { + return; // Will update range will the refresh result arrives. + } + updateRange(); + mAllowScrollHints = true; + } + + /** + * Forces reloading the data. + *

+ * Discards all the cached data and reloads all required data items for the currently visible + * range. To be called when the data item count and/or contents has changed. + */ + public void refresh() { + mMissingPositions.clear(); + mBackgroundProxy.refresh(++mRequestedGeneration); + } + + /** + * Returns the data item at the given position or null if it has not been loaded + * yet. + * + *

+ * If this method has been called for a specific position and returned null, then + * {@link ViewCallback#onItemLoaded(int)} will be called when it finally loads. Note that if + * this position stays outside of the cached item range (as defined by + * {@link ViewCallback#extendRangeInto} method), then the callback will never be called for + * this position. + * + * @param position Item position. + * + * @return The data item at the given position or null if it has not been loaded + * yet. + */ + public T getItem(int position) { + if (position < 0 || position >= mItemCount) { + throw new IndexOutOfBoundsException(position + " is not within 0 and " + mItemCount); + } + T item = mTileList.getItemAt(position); + if (item == null && !isRefreshPending()) { + mMissingPositions.put(position, 0); + } + return item; + } + + /** + * Returns the number of items in the data set. + * + *

+ * This is the number returned by a recent call to + * {@link DataCallback#refreshData()}. + * + * @return Number of items. + */ + public int getItemCount() { + return mItemCount; + } + + private void updateRange() { + mViewCallback.getItemRangeInto(mTmpRange); + if (mTmpRange[0] > mTmpRange[1] || mTmpRange[0] < 0) { + return; + } + if (mTmpRange[1] >= mItemCount) { + // Invalid range may arrive soon after the refresh. + return; + } + + if (!mAllowScrollHints) { + mScrollHint = ViewCallback.HINT_SCROLL_NONE; + } else if (mTmpRange[0] > mPrevRange[1] || mPrevRange[0] > mTmpRange[1]) { + // Ranges do not intersect, long leap not a scroll. + mScrollHint = ViewCallback.HINT_SCROLL_NONE; + } else if (mTmpRange[0] < mPrevRange[0]) { + mScrollHint = ViewCallback.HINT_SCROLL_DESC; + } else if (mTmpRange[0] > mPrevRange[0]) { + mScrollHint = ViewCallback.HINT_SCROLL_ASC; + } + + mPrevRange[0] = mTmpRange[0]; + mPrevRange[1] = mTmpRange[1]; + + mViewCallback.extendRangeInto(mTmpRange, mTmpRangeExtended, mScrollHint); + mTmpRangeExtended[0] = Math.min(mTmpRange[0], Math.max(mTmpRangeExtended[0], 0)); + mTmpRangeExtended[1] = + Math.max(mTmpRange[1], Math.min(mTmpRangeExtended[1], mItemCount - 1)); + + mBackgroundProxy.updateRange(mTmpRange[0], mTmpRange[1], + mTmpRangeExtended[0], mTmpRangeExtended[1], mScrollHint); + } + + private final ThreadUtil.MainThreadCallback + mMainThreadCallback = new ThreadUtil.MainThreadCallback() { + @Override + public void updateItemCount(int generation, int itemCount) { + if (DEBUG) { + log("updateItemCount: size=%d, gen #%d", itemCount, generation); + } + if (!isRequestedGeneration(generation)) { + return; + } + mItemCount = itemCount; + mViewCallback.onDataRefresh(); + mDisplayedGeneration = mRequestedGeneration; + recycleAllTiles(); + + mAllowScrollHints = false; // Will be set to true after a first real scroll. + // There will be no scroll event if the size change does not affect the current range. + updateRange(); + } + + @Override + public void addTile(int generation, TileList.Tile tile) { + if (!isRequestedGeneration(generation)) { + if (DEBUG) { + log("recycling an older generation tile @%d", tile.mStartPosition); + } + mBackgroundProxy.recycleTile(tile); + return; + } + TileList.Tile duplicate = mTileList.addOrReplace(tile); + if (duplicate != null) { + Log.e(TAG, "duplicate tile @" + duplicate.mStartPosition); + mBackgroundProxy.recycleTile(duplicate); + } + if (DEBUG) { + log("gen #%d, added tile @%d, total tiles: %d", + generation, tile.mStartPosition, mTileList.size()); + } + int endPosition = tile.mStartPosition + tile.mItemCount; + int index = 0; + while (index < mMissingPositions.size()) { + final int position = mMissingPositions.keyAt(index); + if (tile.mStartPosition <= position && position < endPosition) { + mMissingPositions.removeAt(index); + mViewCallback.onItemLoaded(position); + } else { + index++; + } + } + } + + @Override + public void removeTile(int generation, int position) { + if (!isRequestedGeneration(generation)) { + return; + } + TileList.Tile tile = mTileList.removeAtPos(position); + if (tile == null) { + Log.e(TAG, "tile not found @" + position); + return; + } + if (DEBUG) { + log("recycling tile @%d, total tiles: %d", tile.mStartPosition, mTileList.size()); + } + mBackgroundProxy.recycleTile(tile); + } + + private void recycleAllTiles() { + if (DEBUG) { + log("recycling all %d tiles", mTileList.size()); + } + for (int i = 0; i < mTileList.size(); i++) { + mBackgroundProxy.recycleTile(mTileList.getAtIndex(i)); + } + mTileList.clear(); + } + + private boolean isRequestedGeneration(int generation) { + return generation == mRequestedGeneration; + } + }; + + private final ThreadUtil.BackgroundCallback + mBackgroundCallback = new ThreadUtil.BackgroundCallback() { + + private TileList.Tile mRecycledRoot; + + final SparseBooleanArray mLoadedTiles = new SparseBooleanArray(); + + private int mGeneration; + private int mItemCount; + + private int mFirstRequiredTileStart; + private int mLastRequiredTileStart; + + @Override + public void refresh(int generation) { + mGeneration = generation; + mLoadedTiles.clear(); + mItemCount = mDataCallback.refreshData(); + mMainThreadProxy.updateItemCount(mGeneration, mItemCount); + } + + @Override + public void updateRange(int rangeStart, int rangeEnd, int extRangeStart, int extRangeEnd, + int scrollHint) { + if (DEBUG) { + log("updateRange: %d..%d extended to %d..%d, scroll hint: %d", + rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint); + } + + if (rangeStart > rangeEnd) { + return; + } + + final int firstVisibleTileStart = getTileStart(rangeStart); + final int lastVisibleTileStart = getTileStart(rangeEnd); + + mFirstRequiredTileStart = getTileStart(extRangeStart); + mLastRequiredTileStart = getTileStart(extRangeEnd); + if (DEBUG) { + log("requesting tile range: %d..%d", + mFirstRequiredTileStart, mLastRequiredTileStart); + } + + // All pending tile requests are removed by ThreadUtil at this point. + // Re-request all required tiles in the most optimal order. + if (scrollHint == ViewCallback.HINT_SCROLL_DESC) { + requestTiles(mFirstRequiredTileStart, lastVisibleTileStart, scrollHint, true); + requestTiles(lastVisibleTileStart + mTileSize, mLastRequiredTileStart, scrollHint, + false); + } else { + requestTiles(firstVisibleTileStart, mLastRequiredTileStart, scrollHint, false); + requestTiles(mFirstRequiredTileStart, firstVisibleTileStart - mTileSize, scrollHint, + true); + } + } + + private int getTileStart(int position) { + return position - position % mTileSize; + } + + private void requestTiles(int firstTileStart, int lastTileStart, int scrollHint, + boolean backwards) { + for (int i = firstTileStart; i <= lastTileStart; i += mTileSize) { + int tileStart = backwards ? (lastTileStart + firstTileStart - i) : i; + if (DEBUG) { + log("requesting tile @%d", tileStart); + } + mBackgroundProxy.loadTile(tileStart, scrollHint); + } + } + + @Override + public void loadTile(int position, int scrollHint) { + if (isTileLoaded(position)) { + if (DEBUG) { + log("already loaded tile @%d", position); + } + return; + } + TileList.Tile tile = acquireTile(); + tile.mStartPosition = position; + tile.mItemCount = Math.min(mTileSize, mItemCount - tile.mStartPosition); + mDataCallback.fillData(tile.mItems, tile.mStartPosition, tile.mItemCount); + flushTileCache(scrollHint); + addTile(tile); + } + + @Override + public void recycleTile(TileList.Tile tile) { + if (DEBUG) { + log("recycling tile @%d", tile.mStartPosition); + } + mDataCallback.recycleData(tile.mItems, tile.mItemCount); + + tile.mNext = mRecycledRoot; + mRecycledRoot = tile; + } + + private TileList.Tile acquireTile() { + if (mRecycledRoot != null) { + TileList.Tile result = mRecycledRoot; + mRecycledRoot = mRecycledRoot.mNext; + return result; + } + return new TileList.Tile(mTClass, mTileSize); + } + + private boolean isTileLoaded(int position) { + return mLoadedTiles.get(position); + } + + private void addTile(TileList.Tile tile) { + mLoadedTiles.put(tile.mStartPosition, true); + mMainThreadProxy.addTile(mGeneration, tile); + if (DEBUG) { + log("loaded tile @%d, total tiles: %d", tile.mStartPosition, mLoadedTiles.size()); + } + } + + private void removeTile(int position) { + mLoadedTiles.delete(position); + mMainThreadProxy.removeTile(mGeneration, position); + if (DEBUG) { + log("flushed tile @%d, total tiles: %s", position, mLoadedTiles.size()); + } + } + + private void flushTileCache(int scrollHint) { + final int cacheSizeLimit = mDataCallback.getMaxCachedTiles(); + while (mLoadedTiles.size() >= cacheSizeLimit) { + int firstLoadedTileStart = mLoadedTiles.keyAt(0); + int lastLoadedTileStart = mLoadedTiles.keyAt(mLoadedTiles.size() - 1); + int startMargin = mFirstRequiredTileStart - firstLoadedTileStart; + int endMargin = lastLoadedTileStart - mLastRequiredTileStart; + if (startMargin > 0 && (startMargin >= endMargin || + (scrollHint == ViewCallback.HINT_SCROLL_ASC))) { + removeTile(firstLoadedTileStart); + } else if (endMargin > 0 && (startMargin < endMargin || + (scrollHint == ViewCallback.HINT_SCROLL_DESC))){ + removeTile(lastLoadedTileStart); + } else { + // Could not flush on either side, bail out. + return; + } + } + } + + private void log(String s, Object... args) { + Log.d(TAG, "[BKGR] " + String.format(s, args)); + } + }; + + /** + * The callback that provides data access for {@link AsyncListUtil}. + * + *

+ * All methods are called on the background thread. + */ + public static abstract class DataCallback { + + /** + * Refresh the data set and return the new data item count. + * + *

+ * If the data is being accessed through {@link android.database.Cursor} this is where + * the new cursor should be created. + * + * @return Data item count. + */ + @WorkerThread + public abstract int refreshData(); + + /** + * Fill the given tile. + * + *

+ * The provided tile might be a recycled tile, in which case it will already have objects. + * It is suggested to re-use these objects if possible in your use case. + * + * @param startPosition The start position in the list. + * @param itemCount The data item count. + * @param data The data item array to fill into. Should not be accessed beyond + * itemCount. + */ + @WorkerThread + public abstract void fillData(T[] data, int startPosition, int itemCount); + + /** + * Recycle the objects created in {@link #fillData} if necessary. + * + * + * @param data Array of data items. Should not be accessed beyond itemCount. + * @param itemCount The data item count. + */ + @WorkerThread + public void recycleData(T[] data, int itemCount) { + } + + /** + * Returns tile cache size limit (in tiles). + * + *

+ * The actual number of cached tiles will be the maximum of this value and the number of + * tiles that is required to cover the range returned by + * {@link ViewCallback#extendRangeInto(int[], int[], int)}. + *

+ * For example, if this method returns 10, and the most + * recent call to {@link ViewCallback#extendRangeInto(int[], int[], int)} returned + * {100, 179}, and the tile size is 5, then the maximum number of cached tiles will be 16. + *

+ * However, if the tile size is 20, then the maximum number of cached tiles will be 10. + *

+ * The default implementation returns 10. + * + * @return Maximum cache size. + */ + @WorkerThread + public int getMaxCachedTiles() { + return 10; + } + } + + /** + * The callback that links {@link AsyncListUtil} with the list view. + * + *

+ * All methods are called on the main thread. + */ + public static abstract class ViewCallback { + + /** + * No scroll direction hint available. + */ + public static final int HINT_SCROLL_NONE = 0; + + /** + * Scrolling in descending order (from higher to lower positions in the order of the backing + * storage). + */ + public static final int HINT_SCROLL_DESC = 1; + + /** + * Scrolling in ascending order (from lower to higher positions in the order of the backing + * storage). + */ + public static final int HINT_SCROLL_ASC = 2; + + /** + * Compute the range of visible item positions. + *

+ * outRange[0] is the position of the first visible item (in the order of the backing + * storage). + *

+ * outRange[1] is the position of the last visible item (in the order of the backing + * storage). + *

+ * Negative positions and positions greater or equal to {@link #getItemCount} are invalid. + * If the returned range contains invalid positions it is ignored (no item will be loaded). + * + * @param outRange The visible item range. + */ + @UiThread + public abstract void getItemRangeInto(int[] outRange); + + /** + * Compute a wider range of items that will be loaded for smoother scrolling. + * + *

+ * If there is no scroll hint, the default implementation extends the visible range by half + * its length in both directions. If there is a scroll hint, the range is extended by + * its full length in the scroll direction, and by half in the other direction. + *

+ * For example, if range is {100, 200} and scrollHint + * is {@link #HINT_SCROLL_ASC}, then outRange will be {50, 300}. + *

+ * However, if scrollHint is {@link #HINT_SCROLL_NONE}, then + * outRange will be {50, 250} + * + * @param range Visible item range. + * @param outRange Extended range. + * @param scrollHint The scroll direction hint. + */ + @UiThread + public void extendRangeInto(int[] range, int[] outRange, int scrollHint) { + final int fullRange = range[1] - range[0] + 1; + final int halfRange = fullRange / 2; + outRange[0] = range[0] - (scrollHint == HINT_SCROLL_DESC ? fullRange : halfRange); + outRange[1] = range[1] + (scrollHint == HINT_SCROLL_ASC ? fullRange : halfRange); + } + + /** + * Called when the entire data set has changed. + */ + @UiThread + public abstract void onDataRefresh(); + + /** + * Called when an item at the given position is loaded. + * @param position Item position. + */ + @UiThread + public abstract void onItemLoaded(int position); + } +} diff --git a/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java b/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java new file mode 100644 index 0000000000..c8bc1a4b8b --- /dev/null +++ b/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.support.v7.util; + +/** + * Wraps a {@link ListUpdateCallback} callback and batches operations that can be merged. + *

+ * For instance, when 2 add operations comes that adds 2 consecutive elements, + * BatchingListUpdateCallback merges them and calls the wrapped callback only once. + *

+ * This is a general purpose class and is also used by + * {@link android.support.v7.util.DiffUtil.DiffResult DiffResult} and + * {@link SortedList} to minimize the number of updates that are dispatched. + *

+ * If you use this class to batch updates, you must call {@link #dispatchLastEvent()} when the + * stream of update events drain. + */ +public class BatchingListUpdateCallback implements ListUpdateCallback { + private static final int TYPE_NONE = 0; + private static final int TYPE_ADD = 1; + private static final int TYPE_REMOVE = 2; + private static final int TYPE_CHANGE = 3; + + final ListUpdateCallback mWrapped; + + int mLastEventType = TYPE_NONE; + int mLastEventPosition = -1; + int mLastEventCount = -1; + Object mLastEventPayload = null; + + public BatchingListUpdateCallback(ListUpdateCallback callback) { + mWrapped = callback; + } + + /** + * BatchingListUpdateCallback holds onto the last event to see if it can be merged with the + * next one. When stream of events finish, you should call this method to dispatch the last + * event. + */ + public void dispatchLastEvent() { + if (mLastEventType == TYPE_NONE) { + return; + } + switch (mLastEventType) { + case TYPE_ADD: + mWrapped.onInserted(mLastEventPosition, mLastEventCount); + break; + case TYPE_REMOVE: + mWrapped.onRemoved(mLastEventPosition, mLastEventCount); + break; + case TYPE_CHANGE: + mWrapped.onChanged(mLastEventPosition, mLastEventCount, mLastEventPayload); + break; + } + mLastEventPayload = null; + mLastEventType = TYPE_NONE; + } + + @Override + public void onInserted(int position, int count) { + if (mLastEventType == TYPE_ADD && position >= mLastEventPosition + && position <= mLastEventPosition + mLastEventCount) { + mLastEventCount += count; + mLastEventPosition = Math.min(position, mLastEventPosition); + return; + } + dispatchLastEvent(); + mLastEventPosition = position; + mLastEventCount = count; + mLastEventType = TYPE_ADD; + } + + @Override + public void onRemoved(int position, int count) { + if (mLastEventType == TYPE_REMOVE && mLastEventPosition >= position && + mLastEventPosition <= position + count) { + mLastEventCount += count; + mLastEventPosition = position; + return; + } + dispatchLastEvent(); + mLastEventPosition = position; + mLastEventCount = count; + mLastEventType = TYPE_REMOVE; + } + + @Override + public void onMoved(int fromPosition, int toPosition) { + dispatchLastEvent(); // moves are not merged + mWrapped.onMoved(fromPosition, toPosition); + } + + @Override + public void onChanged(int position, int count, Object payload) { + if (mLastEventType == TYPE_CHANGE && + !(position > mLastEventPosition + mLastEventCount + || position + count < mLastEventPosition || mLastEventPayload != payload)) { + // take potential overlap into account + int previousEnd = mLastEventPosition + mLastEventCount; + mLastEventPosition = Math.min(position, mLastEventPosition); + mLastEventCount = Math.max(previousEnd, position + count) - mLastEventPosition; + return; + } + dispatchLastEvent(); + mLastEventPosition = position; + mLastEventCount = count; + mLastEventPayload = payload; + mLastEventType = TYPE_CHANGE; + } +} diff --git a/app/src/main/java/android/support/v7/util/DiffUtil.java b/app/src/main/java/android/support/v7/util/DiffUtil.java new file mode 100644 index 0000000000..6f0a078fa7 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/DiffUtil.java @@ -0,0 +1,856 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; +import android.support.v7.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * DiffUtil is a utility class that can calculate the difference between two lists and output a + * list of update operations that converts the first list into the second one. + *

+ * It can be used to calculate updates for a RecyclerView Adapter. + *

+ * DiffUtil uses Eugene W. Myers's difference algorithm to calculate the minimal number of updates + * to convert one list into another. Myers's algorithm does not handle items that are moved so + * DiffUtil runs a second pass on the result to detect items that were moved. + *

+ * If the lists are large, this operation may take significant time so you are advised to run this + * on a background thread, get the {@link DiffResult} then apply it on the RecyclerView on the main + * thread. + *

+ * This algorithm is optimized for space and uses O(N) space to find the minimal + * number of addition and removal operations between the two lists. It has O(N + D^2) expected time + * performance where D is the length of the edit script. + *

+ * If move detection is enabled, it takes an additional O(N^2) time where N is the total number of + * added and removed items. If your lists are already sorted by the same constraint (e.g. a created + * timestamp for a list of posts), you can disable move detection to improve performance. + *

+ * The actual runtime of the algorithm significantly depends on the number of changes in the list + * and the cost of your comparison methods. Below are some average run times for reference: + * (The test list is composed of random UUID Strings and the tests are run on Nexus 5X with M) + *

    + *
  • 100 items and 10 modifications: avg: 0.39 ms, median: 0.35 ms + *
  • 100 items and 100 modifications: 3.82 ms, median: 3.75 ms + *
  • 100 items and 100 modifications without moves: 2.09 ms, median: 2.06 ms + *
  • 1000 items and 50 modifications: avg: 4.67 ms, median: 4.59 ms + *
  • 1000 items and 50 modifications without moves: avg: 3.59 ms, median: 3.50 ms + *
  • 1000 items and 200 modifications: 27.07 ms, median: 26.92 ms + *
  • 1000 items and 200 modifications without moves: 13.54 ms, median: 13.36 ms + *
+ *

+ * Due to implementation constraints, the max size of the list can be 2^26. + */ +public class DiffUtil { + + private DiffUtil() { + // utility class, no instance. + } + + private static final Comparator SNAKE_COMPARATOR = new Comparator() { + @Override + public int compare(Snake o1, Snake o2) { + int cmpX = o1.x - o2.x; + return cmpX == 0 ? o1.y - o2.y : cmpX; + } + }; + + // Myers' algorithm uses two lists as axis labels. In DiffUtil's implementation, `x` axis is + // used for old list and `y` axis is used for new list. + + /** + * Calculates the list of update operations that can covert one list into the other one. + * + * @param cb The callback that acts as a gateway to the backing list data + * + * @return A DiffResult that contains the information about the edit sequence to convert the + * old list into the new list. + */ + public static DiffResult calculateDiff(Callback cb) { + return calculateDiff(cb, true); + } + + /** + * Calculates the list of update operations that can covert one list into the other one. + *

+ * If your old and new lists are sorted by the same constraint and items never move (swap + * positions), you can disable move detection which takes O(N^2) time where + * N is the number of added, moved, removed items. + * + * @param cb The callback that acts as a gateway to the backing list data + * @param detectMoves True if DiffUtil should try to detect moved items, false otherwise. + * + * @return A DiffResult that contains the information about the edit sequence to convert the + * old list into the new list. + */ + public static DiffResult calculateDiff(Callback cb, boolean detectMoves) { + final int oldSize = cb.getOldListSize(); + final int newSize = cb.getNewListSize(); + + final List snakes = new ArrayList<>(); + + // instead of a recursive implementation, we keep our own stack to avoid potential stack + // overflow exceptions + final List stack = new ArrayList<>(); + + stack.add(new Range(0, oldSize, 0, newSize)); + + final int max = oldSize + newSize + Math.abs(oldSize - newSize); + // allocate forward and backward k-lines. K lines are diagonal lines in the matrix. (see the + // paper for details) + // These arrays lines keep the max reachable position for each k-line. + final int[] forward = new int[max * 2]; + final int[] backward = new int[max * 2]; + + // We pool the ranges to avoid allocations for each recursive call. + final List rangePool = new ArrayList<>(); + while (!stack.isEmpty()) { + final Range range = stack.remove(stack.size() - 1); + final Snake snake = diffPartial(cb, range.oldListStart, range.oldListEnd, + range.newListStart, range.newListEnd, forward, backward, max); + if (snake != null) { + if (snake.size > 0) { + snakes.add(snake); + } + // offset the snake to convert its coordinates from the Range's area to global + snake.x += range.oldListStart; + snake.y += range.newListStart; + + // add new ranges for left and right + final Range left = rangePool.isEmpty() ? new Range() : rangePool.remove( + rangePool.size() - 1); + left.oldListStart = range.oldListStart; + left.newListStart = range.newListStart; + if (snake.reverse) { + left.oldListEnd = snake.x; + left.newListEnd = snake.y; + } else { + if (snake.removal) { + left.oldListEnd = snake.x - 1; + left.newListEnd = snake.y; + } else { + left.oldListEnd = snake.x; + left.newListEnd = snake.y - 1; + } + } + stack.add(left); + + // re-use range for right + //noinspection UnnecessaryLocalVariable + final Range right = range; + if (snake.reverse) { + if (snake.removal) { + right.oldListStart = snake.x + snake.size + 1; + right.newListStart = snake.y + snake.size; + } else { + right.oldListStart = snake.x + snake.size; + right.newListStart = snake.y + snake.size + 1; + } + } else { + right.oldListStart = snake.x + snake.size; + right.newListStart = snake.y + snake.size; + } + stack.add(right); + } else { + rangePool.add(range); + } + + } + // sort snakes + Collections.sort(snakes, SNAKE_COMPARATOR); + + return new DiffResult(cb, snakes, forward, backward, detectMoves); + + } + + private static Snake diffPartial(Callback cb, int startOld, int endOld, + int startNew, int endNew, int[] forward, int[] backward, int kOffset) { + final int oldSize = endOld - startOld; + final int newSize = endNew - startNew; + + if (endOld - startOld < 1 || endNew - startNew < 1) { + return null; + } + + final int delta = oldSize - newSize; + final int dLimit = (oldSize + newSize + 1) / 2; + Arrays.fill(forward, kOffset - dLimit - 1, kOffset + dLimit + 1, 0); + Arrays.fill(backward, kOffset - dLimit - 1 + delta, kOffset + dLimit + 1 + delta, oldSize); + final boolean checkInFwd = delta % 2 != 0; + for (int d = 0; d <= dLimit; d++) { + for (int k = -d; k <= d; k += 2) { + // find forward path + // we can reach k from k - 1 or k + 1. Check which one is further in the graph + int x; + final boolean removal; + if (k == -d || k != d && forward[kOffset + k - 1] < forward[kOffset + k + 1]) { + x = forward[kOffset + k + 1]; + removal = false; + } else { + x = forward[kOffset + k - 1] + 1; + removal = true; + } + // set y based on x + int y = x - k; + // move diagonal as long as items match + while (x < oldSize && y < newSize + && cb.areItemsTheSame(startOld + x, startNew + y)) { + x++; + y++; + } + forward[kOffset + k] = x; + if (checkInFwd && k >= delta - d + 1 && k <= delta + d - 1) { + if (forward[kOffset + k] >= backward[kOffset + k]) { + Snake outSnake = new Snake(); + outSnake.x = backward[kOffset + k]; + outSnake.y = outSnake.x - k; + outSnake.size = forward[kOffset + k] - backward[kOffset + k]; + outSnake.removal = removal; + outSnake.reverse = false; + return outSnake; + } + } + } + for (int k = -d; k <= d; k += 2) { + // find reverse path at k + delta, in reverse + final int backwardK = k + delta; + int x; + final boolean removal; + if (backwardK == d + delta || backwardK != -d + delta + && backward[kOffset + backwardK - 1] < backward[kOffset + backwardK + 1]) { + x = backward[kOffset + backwardK - 1]; + removal = false; + } else { + x = backward[kOffset + backwardK + 1] - 1; + removal = true; + } + + // set y based on x + int y = x - backwardK; + // move diagonal as long as items match + while (x > 0 && y > 0 + && cb.areItemsTheSame(startOld + x - 1, startNew + y - 1)) { + x--; + y--; + } + backward[kOffset + backwardK] = x; + if (!checkInFwd && k + delta >= -d && k + delta <= d) { + if (forward[kOffset + backwardK] >= backward[kOffset + backwardK]) { + Snake outSnake = new Snake(); + outSnake.x = backward[kOffset + backwardK]; + outSnake.y = outSnake.x - backwardK; + outSnake.size = + forward[kOffset + backwardK] - backward[kOffset + backwardK]; + outSnake.removal = removal; + outSnake.reverse = true; + return outSnake; + } + } + } + } + throw new IllegalStateException("DiffUtil hit an unexpected case while trying to calculate" + + " the optimal path. Please make sure your data is not changing during the" + + " diff calculation."); + } + + /** + * A Callback class used by DiffUtil while calculating the diff between two lists. + */ + public abstract static class Callback { + /** + * Returns the size of the old list. + * + * @return The size of the old list. + */ + public abstract int getOldListSize(); + + /** + * Returns the size of the new list. + * + * @return The size of the new list. + */ + public abstract int getNewListSize(); + + /** + * Called by the DiffUtil to decide whether two object represent the same Item. + *

+ * For example, if your items have unique ids, this method should check their id equality. + * + * @param oldItemPosition The position of the item in the old list + * @param newItemPosition The position of the item in the new list + * @return True if the two items represent the same object or false if they are different. + */ + public abstract boolean areItemsTheSame(int oldItemPosition, int newItemPosition); + + /** + * Called by the DiffUtil when it wants to check whether two items have the same data. + * DiffUtil uses this information to detect if the contents of an item has changed. + *

+ * DiffUtil uses this method to check equality instead of {@link Object#equals(Object)} + * so that you can change its behavior depending on your UI. + * For example, if you are using DiffUtil with a + * {@link android.support.v7.widget.RecyclerView.Adapter RecyclerView.Adapter}, you should + * return whether the items' visual representations are the same. + *

+ * This method is called only if {@link #areItemsTheSame(int, int)} returns + * {@code true} for these items. + * + * @param oldItemPosition The position of the item in the old list + * @param newItemPosition The position of the item in the new list which replaces the + * oldItem + * @return True if the contents of the items are the same or false if they are different. + */ + public abstract boolean areContentsTheSame(int oldItemPosition, int newItemPosition); + + /** + * When {@link #areItemsTheSame(int, int)} returns {@code true} for two items and + * {@link #areContentsTheSame(int, int)} returns false for them, DiffUtil + * calls this method to get a payload about the change. + *

+ * For example, if you are using DiffUtil with {@link RecyclerView}, you can return the + * particular field that changed in the item and your + * {@link android.support.v7.widget.RecyclerView.ItemAnimator ItemAnimator} can use that + * information to run the correct animation. + *

+ * Default implementation returns {@code null}. + * + * @param oldItemPosition The position of the item in the old list + * @param newItemPosition The position of the item in the new list + * + * @return A payload object that represents the change between the two items. + */ + @Nullable + public Object getChangePayload(int oldItemPosition, int newItemPosition) { + return null; + } + } + + /** + * Snakes represent a match between two lists. It is optionally prefixed or postfixed with an + * add or remove operation. See the Myers' paper for details. + */ + static class Snake { + /** + * Position in the old list + */ + int x; + + /** + * Position in the new list + */ + int y; + + /** + * Number of matches. Might be 0. + */ + int size; + + /** + * If true, this is a removal from the original list followed by {@code size} matches. + * If false, this is an addition from the new list followed by {@code size} matches. + */ + boolean removal; + + /** + * If true, the addition or removal is at the end of the snake. + * If false, the addition or removal is at the beginning of the snake. + */ + boolean reverse; + } + + /** + * Represents a range in two lists that needs to be solved. + *

+ * This internal class is used when running Myers' algorithm without recursion. + */ + static class Range { + + int oldListStart, oldListEnd; + + int newListStart, newListEnd; + + public Range() { + } + + public Range(int oldListStart, int oldListEnd, int newListStart, int newListEnd) { + this.oldListStart = oldListStart; + this.oldListEnd = oldListEnd; + this.newListStart = newListStart; + this.newListEnd = newListEnd; + } + } + + /** + * This class holds the information about the result of a + * {@link DiffUtil#calculateDiff(Callback, boolean)} call. + *

+ * You can consume the updates in a DiffResult via + * {@link #dispatchUpdatesTo(ListUpdateCallback)} or directly stream the results into a + * {@link RecyclerView.Adapter} via {@link #dispatchUpdatesTo(RecyclerView.Adapter)}. + */ + public static class DiffResult { + /** + * While reading the flags below, keep in mind that when multiple items move in a list, + * Myers's may pick any of them as the anchor item and consider that one NOT_CHANGED while + * picking others as additions and removals. This is completely fine as we later detect + * all moves. + *

+ * Below, when an item is mentioned to stay in the same "location", it means we won't + * dispatch a move/add/remove for it, it DOES NOT mean the item is still in the same + * position. + */ + // item stayed the same. + private static final int FLAG_NOT_CHANGED = 1; + // item stayed in the same location but changed. + private static final int FLAG_CHANGED = FLAG_NOT_CHANGED << 1; + // Item has moved and also changed. + private static final int FLAG_MOVED_CHANGED = FLAG_CHANGED << 1; + // Item has moved but did not change. + private static final int FLAG_MOVED_NOT_CHANGED = FLAG_MOVED_CHANGED << 1; + // Ignore this update. + // If this is an addition from the new list, it means the item is actually removed from an + // earlier position and its move will be dispatched when we process the matching removal + // from the old list. + // If this is a removal from the old list, it means the item is actually added back to an + // earlier index in the new list and we'll dispatch its move when we are processing that + // addition. + private static final int FLAG_IGNORE = FLAG_MOVED_NOT_CHANGED << 1; + + // since we are re-using the int arrays that were created in the Myers' step, we mask + // change flags + private static final int FLAG_OFFSET = 5; + + private static final int FLAG_MASK = (1 << FLAG_OFFSET) - 1; + + // The Myers' snakes. At this point, we only care about their diagonal sections. + private final List mSnakes; + + // The list to keep oldItemStatuses. As we traverse old items, we assign flags to them + // which also includes whether they were a real removal or a move (and its new index). + private final int[] mOldItemStatuses; + // The list to keep newItemStatuses. As we traverse new items, we assign flags to them + // which also includes whether they were a real addition or a move(and its old index). + private final int[] mNewItemStatuses; + // The callback that was given to calcualte diff method. + private final Callback mCallback; + + private final int mOldListSize; + + private final int mNewListSize; + + private final boolean mDetectMoves; + + /** + * @param callback The callback that was used to calculate the diff + * @param snakes The list of Myers' snakes + * @param oldItemStatuses An int[] that can be re-purposed to keep metadata + * @param newItemStatuses An int[] that can be re-purposed to keep metadata + * @param detectMoves True if this DiffResult will try to detect moved items + */ + DiffResult(Callback callback, List snakes, int[] oldItemStatuses, + int[] newItemStatuses, boolean detectMoves) { + mSnakes = snakes; + mOldItemStatuses = oldItemStatuses; + mNewItemStatuses = newItemStatuses; + Arrays.fill(mOldItemStatuses, 0); + Arrays.fill(mNewItemStatuses, 0); + mCallback = callback; + mOldListSize = callback.getOldListSize(); + mNewListSize = callback.getNewListSize(); + mDetectMoves = detectMoves; + addRootSnake(); + findMatchingItems(); + } + + /** + * We always add a Snake to 0/0 so that we can run loops from end to beginning and be done + * when we run out of snakes. + */ + private void addRootSnake() { + Snake firstSnake = mSnakes.isEmpty() ? null : mSnakes.get(0); + if (firstSnake == null || firstSnake.x != 0 || firstSnake.y != 0) { + Snake root = new Snake(); + root.x = 0; + root.y = 0; + root.removal = false; + root.size = 0; + root.reverse = false; + mSnakes.add(0, root); + } + } + + /** + * This method traverses each addition / removal and tries to match it to a previous + * removal / addition. This is how we detect move operations. + *

+ * This class also flags whether an item has been changed or not. + *

+ * DiffUtil does this pre-processing so that if it is running on a big list, it can be moved + * to background thread where most of the expensive stuff will be calculated and kept in + * the statuses maps. DiffResult uses this pre-calculated information while dispatching + * the updates (which is probably being called on the main thread). + */ + private void findMatchingItems() { + int posOld = mOldListSize; + int posNew = mNewListSize; + // traverse the matrix from right bottom to 0,0. + for (int i = mSnakes.size() - 1; i >= 0; i--) { + final Snake snake = mSnakes.get(i); + final int endX = snake.x + snake.size; + final int endY = snake.y + snake.size; + if (mDetectMoves) { + while (posOld > endX) { + // this is a removal. Check remaining snakes to see if this was added before + findAddition(posOld, posNew, i); + posOld--; + } + while (posNew > endY) { + // this is an addition. Check remaining snakes to see if this was removed + // before + findRemoval(posOld, posNew, i); + posNew--; + } + } + for (int j = 0; j < snake.size; j++) { + // matching items. Check if it is changed or not + final int oldItemPos = snake.x + j; + final int newItemPos = snake.y + j; + final boolean theSame = mCallback + .areContentsTheSame(oldItemPos, newItemPos); + final int changeFlag = theSame ? FLAG_NOT_CHANGED : FLAG_CHANGED; + mOldItemStatuses[oldItemPos] = (newItemPos << FLAG_OFFSET) | changeFlag; + mNewItemStatuses[newItemPos] = (oldItemPos << FLAG_OFFSET) | changeFlag; + } + posOld = snake.x; + posNew = snake.y; + } + } + + private void findAddition(int x, int y, int snakeIndex) { + if (mOldItemStatuses[x - 1] != 0) { + return; // already set by a latter item + } + findMatchingItem(x, y, snakeIndex, false); + } + + private void findRemoval(int x, int y, int snakeIndex) { + if (mNewItemStatuses[y - 1] != 0) { + return; // already set by a latter item + } + findMatchingItem(x, y, snakeIndex, true); + } + + /** + * Finds a matching item that is before the given coordinates in the matrix + * (before : left and above). + * + * @param x The x position in the matrix (position in the old list) + * @param y The y position in the matrix (position in the new list) + * @param snakeIndex The current snake index + * @param removal True if we are looking for a removal, false otherwise + * + * @return True if such item is found. + */ + private boolean findMatchingItem(final int x, final int y, final int snakeIndex, + final boolean removal) { + final int myItemPos; + int curX; + int curY; + if (removal) { + myItemPos = y - 1; + curX = x; + curY = y - 1; + } else { + myItemPos = x - 1; + curX = x - 1; + curY = y; + } + for (int i = snakeIndex; i >= 0; i--) { + final Snake snake = mSnakes.get(i); + final int endX = snake.x + snake.size; + final int endY = snake.y + snake.size; + if (removal) { + // check removals for a match + for (int pos = curX - 1; pos >= endX; pos--) { + if (mCallback.areItemsTheSame(pos, myItemPos)) { + // found! + final boolean theSame = mCallback.areContentsTheSame(pos, myItemPos); + final int changeFlag = theSame ? FLAG_MOVED_NOT_CHANGED + : FLAG_MOVED_CHANGED; + mNewItemStatuses[myItemPos] = (pos << FLAG_OFFSET) | FLAG_IGNORE; + mOldItemStatuses[pos] = (myItemPos << FLAG_OFFSET) | changeFlag; + return true; + } + } + } else { + // check for additions for a match + for (int pos = curY - 1; pos >= endY; pos--) { + if (mCallback.areItemsTheSame(myItemPos, pos)) { + // found + final boolean theSame = mCallback.areContentsTheSame(myItemPos, pos); + final int changeFlag = theSame ? FLAG_MOVED_NOT_CHANGED + : FLAG_MOVED_CHANGED; + mOldItemStatuses[x - 1] = (pos << FLAG_OFFSET) | FLAG_IGNORE; + mNewItemStatuses[pos] = ((x - 1) << FLAG_OFFSET) | changeFlag; + return true; + } + } + } + curX = snake.x; + curY = snake.y; + } + return false; + } + + /** + * Dispatches the update events to the given adapter. + *

+ * For example, if you have an {@link android.support.v7.widget.RecyclerView.Adapter Adapter} + * that is backed by a {@link List}, you can swap the list with the new one then call this + * method to dispatch all updates to the RecyclerView. + *

+         *     List oldList = mAdapter.getData();
+         *     DiffResult result = DiffUtil.calculateDiff(new MyCallback(oldList, newList));
+         *     mAdapter.setData(newList);
+         *     result.dispatchUpdatesTo(mAdapter);
+         * 
+ *

+ * Note that the RecyclerView requires you to dispatch adapter updates immediately when you + * change the data (you cannot defer {@code notify*} calls). The usage above adheres to this + * rule because updates are sent to the adapter right after the backing data is changed, + * before RecyclerView tries to read it. + *

+ * On the other hand, if you have another + * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver AdapterDataObserver} + * that tries to process events synchronously, this may confuse that observer because the + * list is instantly moved to its final state while the adapter updates are dispatched later + * on, one by one. If you have such an + * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver AdapterDataObserver}, + * you can use + * {@link #dispatchUpdatesTo(ListUpdateCallback)} to handle each modification + * manually. + * + * @param adapter A RecyclerView adapter which was displaying the old list and will start + * displaying the new list. + */ + public void dispatchUpdatesTo(final RecyclerView.Adapter adapter) { + dispatchUpdatesTo(new ListUpdateCallback() { + @Override + public void onInserted(int position, int count) { + adapter.notifyItemRangeInserted(position, count); + } + + @Override + public void onRemoved(int position, int count) { + adapter.notifyItemRangeRemoved(position, count); + } + + @Override + public void onMoved(int fromPosition, int toPosition) { + adapter.notifyItemMoved(fromPosition, toPosition); + } + + @Override + public void onChanged(int position, int count, Object payload) { + adapter.notifyItemRangeChanged(position, count, payload); + } + }); + } + + /** + * Dispatches update operations to the given Callback. + *

+ * These updates are atomic such that the first update call effects every update call that + * comes after it (the same as RecyclerView). + * + * @param updateCallback The callback to receive the update operations. + * @see #dispatchUpdatesTo(RecyclerView.Adapter) + */ + public void dispatchUpdatesTo(ListUpdateCallback updateCallback) { + final BatchingListUpdateCallback batchingCallback; + if (updateCallback instanceof BatchingListUpdateCallback) { + batchingCallback = (BatchingListUpdateCallback) updateCallback; + } else { + batchingCallback = new BatchingListUpdateCallback(updateCallback); + // replace updateCallback with a batching callback and override references to + // updateCallback so that we don't call it directly by mistake + //noinspection UnusedAssignment + updateCallback = batchingCallback; + } + // These are add/remove ops that are converted to moves. We track their positions until + // their respective update operations are processed. + final List postponedUpdates = new ArrayList<>(); + int posOld = mOldListSize; + int posNew = mNewListSize; + for (int snakeIndex = mSnakes.size() - 1; snakeIndex >= 0; snakeIndex--) { + final Snake snake = mSnakes.get(snakeIndex); + final int snakeSize = snake.size; + final int endX = snake.x + snakeSize; + final int endY = snake.y + snakeSize; + if (endX < posOld) { + dispatchRemovals(postponedUpdates, batchingCallback, endX, posOld - endX, endX); + } + + if (endY < posNew) { + dispatchAdditions(postponedUpdates, batchingCallback, endX, posNew - endY, + endY); + } + for (int i = snakeSize - 1; i >= 0; i--) { + if ((mOldItemStatuses[snake.x + i] & FLAG_MASK) == FLAG_CHANGED) { + batchingCallback.onChanged(snake.x + i, 1, + mCallback.getChangePayload(snake.x + i, snake.y + i)); + } + } + posOld = snake.x; + posNew = snake.y; + } + batchingCallback.dispatchLastEvent(); + } + + private static PostponedUpdate removePostponedUpdate(List updates, + int pos, boolean removal) { + for (int i = updates.size() - 1; i >= 0; i--) { + final PostponedUpdate update = updates.get(i); + if (update.posInOwnerList == pos && update.removal == removal) { + updates.remove(i); + for (int j = i; j < updates.size(); j++) { + // offset other ops since they swapped positions + updates.get(j).currentPos += removal ? 1 : -1; + } + return update; + } + } + return null; + } + + private void dispatchAdditions(List postponedUpdates, + ListUpdateCallback updateCallback, int start, int count, int globalIndex) { + if (!mDetectMoves) { + updateCallback.onInserted(start, count); + return; + } + for (int i = count - 1; i >= 0; i--) { + int status = mNewItemStatuses[globalIndex + i] & FLAG_MASK; + switch (status) { + case 0: // real addition + updateCallback.onInserted(start, 1); + for (PostponedUpdate update : postponedUpdates) { + update.currentPos += 1; + } + break; + case FLAG_MOVED_CHANGED: + case FLAG_MOVED_NOT_CHANGED: + final int pos = mNewItemStatuses[globalIndex + i] >> FLAG_OFFSET; + final PostponedUpdate update = removePostponedUpdate(postponedUpdates, pos, + true); + // the item was moved from that position + //noinspection ConstantConditions + updateCallback.onMoved(update.currentPos, start); + if (status == FLAG_MOVED_CHANGED) { + // also dispatch a change + updateCallback.onChanged(start, 1, + mCallback.getChangePayload(pos, globalIndex + i)); + } + break; + case FLAG_IGNORE: // ignoring this + postponedUpdates.add(new PostponedUpdate(globalIndex + i, start, false)); + break; + default: + throw new IllegalStateException( + "unknown flag for pos " + (globalIndex + i) + " " + Long + .toBinaryString(status)); + } + } + } + + private void dispatchRemovals(List postponedUpdates, + ListUpdateCallback updateCallback, int start, int count, int globalIndex) { + if (!mDetectMoves) { + updateCallback.onRemoved(start, count); + return; + } + for (int i = count - 1; i >= 0; i--) { + final int status = mOldItemStatuses[globalIndex + i] & FLAG_MASK; + switch (status) { + case 0: // real removal + updateCallback.onRemoved(start + i, 1); + for (PostponedUpdate update : postponedUpdates) { + update.currentPos -= 1; + } + break; + case FLAG_MOVED_CHANGED: + case FLAG_MOVED_NOT_CHANGED: + final int pos = mOldItemStatuses[globalIndex + i] >> FLAG_OFFSET; + final PostponedUpdate update = removePostponedUpdate(postponedUpdates, pos, + false); + // the item was moved to that position. we do -1 because this is a move not + // add and removing current item offsets the target move by 1 + //noinspection ConstantConditions + updateCallback.onMoved(start + i, update.currentPos - 1); + if (status == FLAG_MOVED_CHANGED) { + // also dispatch a change + updateCallback.onChanged(update.currentPos - 1, 1, + mCallback.getChangePayload(globalIndex + i, pos)); + } + break; + case FLAG_IGNORE: // ignoring this + postponedUpdates.add(new PostponedUpdate(globalIndex + i, start + i, true)); + break; + default: + throw new IllegalStateException( + "unknown flag for pos " + (globalIndex + i) + " " + Long + .toBinaryString(status)); + } + } + } + + @VisibleForTesting + List getSnakes() { + return mSnakes; + } + } + + /** + * Represents an update that we skipped because it was a move. + *

+ * When an update is skipped, it is tracked as other updates are dispatched until the matching + * add/remove operation is found at which point the tracked position is used to dispatch the + * update. + */ + private static class PostponedUpdate { + + int posInOwnerList; + + int currentPos; + + boolean removal; + + public PostponedUpdate(int posInOwnerList, int currentPos, boolean removal) { + this.posInOwnerList = posInOwnerList; + this.currentPos = currentPos; + this.removal = removal; + } + } +} diff --git a/app/src/main/java/android/support/v7/util/ListUpdateCallback.java b/app/src/main/java/android/support/v7/util/ListUpdateCallback.java new file mode 100644 index 0000000000..2136202958 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/ListUpdateCallback.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.support.v7.util; + +/** + * An interface that can receive Update operations that are applied to a list. + *

+ * This class can be used together with DiffUtil to detect changes between two lists. + */ +public interface ListUpdateCallback { + /** + * Called when {@code count} number of items are inserted at the given position. + * + * @param position The position of the new item. + * @param count The number of items that have been added. + */ + void onInserted(int position, int count); + + /** + * Called when {@code count} number of items are removed from the given position. + * + * @param position The position of the item which has been removed. + * @param count The number of items which have been removed. + */ + void onRemoved(int position, int count); + + /** + * Called when an item changes its position in the list. + * + * @param fromPosition The previous position of the item before the move. + * @param toPosition The new position of the item. + */ + void onMoved(int fromPosition, int toPosition); + + /** + * Called when {@code count} number of items are updated at the given position. + * + * @param position The position of the item which has been updated. + * @param count The number of items which has changed. + */ + void onChanged(int position, int count, Object payload); +} diff --git a/app/src/main/java/android/support/v7/util/MessageThreadUtil.java b/app/src/main/java/android/support/v7/util/MessageThreadUtil.java new file mode 100644 index 0000000000..8aa9eda764 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/MessageThreadUtil.java @@ -0,0 +1,283 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import android.os.Handler; +import android.os.Looper; +import android.support.v4.content.ParallelExecutorCompat; +import android.util.Log; + +import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; + +class MessageThreadUtil implements ThreadUtil { + + @Override + public MainThreadCallback getMainThreadProxy(final MainThreadCallback callback) { + return new MainThreadCallback() { + final private MessageQueue mQueue = new MessageQueue(); + final private Handler mMainThreadHandler = new Handler(Looper.getMainLooper()); + + private static final int UPDATE_ITEM_COUNT = 1; + private static final int ADD_TILE = 2; + private static final int REMOVE_TILE = 3; + + @Override + public void updateItemCount(int generation, int itemCount) { + sendMessage(SyncQueueItem.obtainMessage(UPDATE_ITEM_COUNT, generation, itemCount)); + } + + @Override + public void addTile(int generation, TileList.Tile tile) { + sendMessage(SyncQueueItem.obtainMessage(ADD_TILE, generation, tile)); + } + + @Override + public void removeTile(int generation, int position) { + sendMessage(SyncQueueItem.obtainMessage(REMOVE_TILE, generation, position)); + } + + private void sendMessage(SyncQueueItem msg) { + mQueue.sendMessage(msg); + mMainThreadHandler.post(mMainThreadRunnable); + } + + private Runnable mMainThreadRunnable = new Runnable() { + @Override + public void run() { + SyncQueueItem msg = mQueue.next(); + while (msg != null) { + switch (msg.what) { + case UPDATE_ITEM_COUNT: + callback.updateItemCount(msg.arg1, msg.arg2); + break; + case ADD_TILE: + //noinspection unchecked + callback.addTile(msg.arg1, (TileList.Tile) msg.data); + break; + case REMOVE_TILE: + callback.removeTile(msg.arg1, msg.arg2); + break; + default: + Log.e("ThreadUtil", "Unsupported message, what=" + msg.what); + } + msg = mQueue.next(); + } + } + }; + }; + } + + @Override + public BackgroundCallback getBackgroundProxy(final BackgroundCallback callback) { + return new BackgroundCallback() { + final private MessageQueue mQueue = new MessageQueue(); + final private Executor mExecutor = ParallelExecutorCompat.getParallelExecutor(); + AtomicBoolean mBackgroundRunning = new AtomicBoolean(false); + + private static final int REFRESH = 1; + private static final int UPDATE_RANGE = 2; + private static final int LOAD_TILE = 3; + private static final int RECYCLE_TILE = 4; + + @Override + public void refresh(int generation) { + sendMessageAtFrontOfQueue(SyncQueueItem.obtainMessage(REFRESH, generation, null)); + } + + @Override + public void updateRange(int rangeStart, int rangeEnd, + int extRangeStart, int extRangeEnd, int scrollHint) { + sendMessageAtFrontOfQueue(SyncQueueItem.obtainMessage(UPDATE_RANGE, + rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint, null)); + } + + @Override + public void loadTile(int position, int scrollHint) { + sendMessage(SyncQueueItem.obtainMessage(LOAD_TILE, position, scrollHint)); + } + + @Override + public void recycleTile(TileList.Tile tile) { + sendMessage(SyncQueueItem.obtainMessage(RECYCLE_TILE, 0, tile)); + } + + private void sendMessage(SyncQueueItem msg) { + mQueue.sendMessage(msg); + maybeExecuteBackgroundRunnable(); + } + + private void sendMessageAtFrontOfQueue(SyncQueueItem msg) { + mQueue.sendMessageAtFrontOfQueue(msg); + maybeExecuteBackgroundRunnable(); + } + + private void maybeExecuteBackgroundRunnable() { + if (mBackgroundRunning.compareAndSet(false, true)) { + mExecutor.execute(mBackgroundRunnable); + } + } + + private Runnable mBackgroundRunnable = new Runnable() { + @Override + public void run() { + while (true) { + SyncQueueItem msg = mQueue.next(); + if (msg == null) { + break; + } + switch (msg.what) { + case REFRESH: + mQueue.removeMessages(REFRESH); + callback.refresh(msg.arg1); + break; + case UPDATE_RANGE: + mQueue.removeMessages(UPDATE_RANGE); + mQueue.removeMessages(LOAD_TILE); + callback.updateRange( + msg.arg1, msg.arg2, msg.arg3, msg.arg4, msg.arg5); + break; + case LOAD_TILE: + callback.loadTile(msg.arg1, msg.arg2); + break; + case RECYCLE_TILE: + //noinspection unchecked + callback.recycleTile((TileList.Tile) msg.data); + break; + default: + Log.e("ThreadUtil", "Unsupported message, what=" + msg.what); + } + } + mBackgroundRunning.set(false); + } + }; + }; + } + + /** + * Replica of android.os.Message. Unfortunately, cannot use it without a Handler and don't want + * to create a thread just for this component. + */ + static class SyncQueueItem { + + private static SyncQueueItem sPool; + private static final Object sPoolLock = new Object(); + private SyncQueueItem next; + public int what; + public int arg1; + public int arg2; + public int arg3; + public int arg4; + public int arg5; + public Object data; + + void recycle() { + next = null; + what = arg1 = arg2 = arg3 = arg4 = arg5 = 0; + data = null; + synchronized (sPoolLock) { + if (sPool != null) { + next = sPool; + } + sPool = this; + } + } + + static SyncQueueItem obtainMessage(int what, int arg1, int arg2, int arg3, int arg4, + int arg5, Object data) { + synchronized (sPoolLock) { + final SyncQueueItem item; + if (sPool == null) { + item = new SyncQueueItem(); + } else { + item = sPool; + sPool = sPool.next; + item.next = null; + } + item.what = what; + item.arg1 = arg1; + item.arg2 = arg2; + item.arg3 = arg3; + item.arg4 = arg4; + item.arg5 = arg5; + item.data = data; + return item; + } + } + + static SyncQueueItem obtainMessage(int what, int arg1, int arg2) { + return obtainMessage(what, arg1, arg2, 0, 0, 0, null); + } + + static SyncQueueItem obtainMessage(int what, int arg1, Object data) { + return obtainMessage(what, arg1, 0, 0, 0, 0, data); + } + } + + static class MessageQueue { + + private SyncQueueItem mRoot; + + synchronized SyncQueueItem next() { + if (mRoot == null) { + return null; + } + final SyncQueueItem next = mRoot; + mRoot = mRoot.next; + return next; + } + + synchronized void sendMessageAtFrontOfQueue(SyncQueueItem item) { + item.next = mRoot; + mRoot = item; + } + + synchronized void sendMessage(SyncQueueItem item) { + if (mRoot == null) { + mRoot = item; + return; + } + SyncQueueItem last = mRoot; + while (last.next != null) { + last = last.next; + } + last.next = item; + } + + synchronized void removeMessages(int what) { + while (mRoot != null && mRoot.what == what) { + SyncQueueItem item = mRoot; + mRoot = mRoot.next; + item.recycle(); + } + if (mRoot != null) { + SyncQueueItem prev = mRoot; + SyncQueueItem item = prev.next; + while (item != null) { + SyncQueueItem next = item.next; + if (item.what == what) { + prev.next = next; + item.recycle(); + } else { + prev = item; + } + item = next; + } + } + } + } +} diff --git a/app/src/main/java/android/support/v7/util/SortedList.java b/app/src/main/java/android/support/v7/util/SortedList.java new file mode 100644 index 0000000000..1d48468191 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/SortedList.java @@ -0,0 +1,821 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; + +/** + * A Sorted list implementation that can keep items in order and also notify for changes in the + * list + * such that it can be bound to a {@link android.support.v7.widget.RecyclerView.Adapter + * RecyclerView.Adapter}. + *

+ * It keeps items ordered using the {@link Callback#compare(Object, Object)} method and uses + * binary search to retrieve items. If the sorting criteria of your items may change, make sure you + * call appropriate methods while editing them to avoid data inconsistencies. + *

+ * You can control the order of items and change notifications via the {@link Callback} parameter. + */ +@SuppressWarnings("unchecked") +public class SortedList { + + /** + * Used by {@link #indexOf(Object)} when he item cannot be found in the list. + */ + public static final int INVALID_POSITION = -1; + + private static final int MIN_CAPACITY = 10; + private static final int CAPACITY_GROWTH = MIN_CAPACITY; + private static final int INSERTION = 1; + private static final int DELETION = 1 << 1; + private static final int LOOKUP = 1 << 2; + T[] mData; + + /** + * A copy of the previous list contents used during the merge phase of addAll. + */ + private T[] mOldData; + private int mOldDataStart; + private int mOldDataSize; + + /** + * The size of the valid portion of mData during the merge phase of addAll. + */ + private int mMergedSize; + + + /** + * The callback instance that controls the behavior of the SortedList and get notified when + * changes happen. + */ + private Callback mCallback; + + private BatchedCallback mBatchedCallback; + + private int mSize; + private final Class mTClass; + + /** + * Creates a new SortedList of type T. + * + * @param klass The class of the contents of the SortedList. + * @param callback The callback that controls the behavior of SortedList. + */ + public SortedList(Class klass, Callback callback) { + this(klass, callback, MIN_CAPACITY); + } + + /** + * Creates a new SortedList of type T. + * + * @param klass The class of the contents of the SortedList. + * @param callback The callback that controls the behavior of SortedList. + * @param initialCapacity The initial capacity to hold items. + */ + public SortedList(Class klass, Callback callback, int initialCapacity) { + mTClass = klass; + mData = (T[]) Array.newInstance(klass, initialCapacity); + mCallback = callback; + mSize = 0; + } + + /** + * The number of items in the list. + * + * @return The number of items in the list. + */ + public int size() { + return mSize; + } + + /** + * Adds the given item to the list. If this is a new item, SortedList calls + * {@link Callback#onInserted(int, int)}. + *

+ * If the item already exists in the list and its sorting criteria is not changed, it is + * replaced with the existing Item. SortedList uses + * {@link Callback#areItemsTheSame(Object, Object)} to check if two items are the same item + * and uses {@link Callback#areContentsTheSame(Object, Object)} to decide whether it should + * call {@link Callback#onChanged(int, int)} or not. In both cases, it always removes the + * reference to the old item and puts the new item into the backing array even if + * {@link Callback#areContentsTheSame(Object, Object)} returns false. + *

+ * If the sorting criteria of the item is changed, SortedList won't be able to find + * its duplicate in the list which will result in having a duplicate of the Item in the list. + * If you need to update sorting criteria of an item that already exists in the list, + * use {@link #updateItemAt(int, Object)}. You can find the index of the item using + * {@link #indexOf(Object)} before you update the object. + * + * @param item The item to be added into the list. + * + * @return The index of the newly added item. + * @see {@link Callback#compare(Object, Object)} + * @see {@link Callback#areItemsTheSame(Object, Object)} + * @see {@link Callback#areContentsTheSame(Object, Object)}} + */ + public int add(T item) { + throwIfMerging(); + return add(item, true); + } + + /** + * Adds the given items to the list. Equivalent to calling {@link SortedList#add} in a loop, + * except the callback events may be in a different order/granularity since addAll can batch + * them for better performance. + *

+ * If allowed, may modify the input array and even take the ownership over it in order + * to avoid extra memory allocation during sorting and deduplication. + *

+ * @param items Array of items to be added into the list. + * @param mayModifyInput If true, SortedList is allowed to modify the input. + * @see {@link SortedList#addAll(Object[] items)}. + */ + public void addAll(T[] items, boolean mayModifyInput) { + throwIfMerging(); + if (items.length == 0) { + return; + } + if (mayModifyInput) { + addAllInternal(items); + } else { + T[] copy = (T[]) Array.newInstance(mTClass, items.length); + System.arraycopy(items, 0, copy, 0, items.length); + addAllInternal(copy); + } + + } + + /** + * Adds the given items to the list. Does not modify the input. + * + * @see {@link SortedList#addAll(T[] items, boolean mayModifyInput)} + * + * @param items Array of items to be added into the list. + */ + public void addAll(T... items) { + addAll(items, false); + } + + /** + * Adds the given items to the list. Does not modify the input. + * + * @see {@link SortedList#addAll(T[] items, boolean mayModifyInput)} + * + * @param items Collection of items to be added into the list. + */ + public void addAll(Collection items) { + T[] copy = (T[]) Array.newInstance(mTClass, items.size()); + addAll(items.toArray(copy), true); + } + + private void addAllInternal(T[] newItems) { + final boolean forceBatchedUpdates = !(mCallback instanceof BatchedCallback); + if (forceBatchedUpdates) { + beginBatchedUpdates(); + } + + mOldData = mData; + mOldDataStart = 0; + mOldDataSize = mSize; + + Arrays.sort(newItems, mCallback); // Arrays.sort is stable. + + final int newSize = deduplicate(newItems); + if (mSize == 0) { + mData = newItems; + mSize = newSize; + mMergedSize = newSize; + mCallback.onInserted(0, newSize); + } else { + merge(newItems, newSize); + } + + mOldData = null; + + if (forceBatchedUpdates) { + endBatchedUpdates(); + } + } + + /** + * Remove duplicate items, leaving only the last item from each group of "same" items. + * Move the remaining items to the beginning of the array. + * + * @return Number of deduplicated items at the beginning of the array. + */ + private int deduplicate(T[] items) { + if (items.length == 0) { + throw new IllegalArgumentException("Input array must be non-empty"); + } + + // Keep track of the range of equal items at the end of the output. + // Start with the range containing just the first item. + int rangeStart = 0; + int rangeEnd = 1; + + for (int i = 1; i < items.length; ++i) { + T currentItem = items[i]; + + int compare = mCallback.compare(items[rangeStart], currentItem); + if (compare > 0) { + throw new IllegalArgumentException("Input must be sorted in ascending order."); + } + + if (compare == 0) { + // The range of equal items continues, update it. + final int sameItemPos = findSameItem(currentItem, items, rangeStart, rangeEnd); + if (sameItemPos != INVALID_POSITION) { + // Replace the duplicate item. + items[sameItemPos] = currentItem; + } else { + // Expand the range. + if (rangeEnd != i) { // Avoid redundant copy. + items[rangeEnd] = currentItem; + } + rangeEnd++; + } + } else { + // The range has ended. Reset it to contain just the current item. + if (rangeEnd != i) { // Avoid redundant copy. + items[rangeEnd] = currentItem; + } + rangeStart = rangeEnd++; + } + } + return rangeEnd; + } + + + private int findSameItem(T item, T[] items, int from, int to) { + for (int pos = from; pos < to; pos++) { + if (mCallback.areItemsTheSame(items[pos], item)) { + return pos; + } + } + return INVALID_POSITION; + } + + /** + * This method assumes that newItems are sorted and deduplicated. + */ + private void merge(T[] newData, int newDataSize) { + final int mergedCapacity = mSize + newDataSize + CAPACITY_GROWTH; + mData = (T[]) Array.newInstance(mTClass, mergedCapacity); + mMergedSize = 0; + + int newDataStart = 0; + while (mOldDataStart < mOldDataSize || newDataStart < newDataSize) { + if (mOldDataStart == mOldDataSize) { + // No more old items, copy the remaining new items. + int itemCount = newDataSize - newDataStart; + System.arraycopy(newData, newDataStart, mData, mMergedSize, itemCount); + mMergedSize += itemCount; + mSize += itemCount; + mCallback.onInserted(mMergedSize - itemCount, itemCount); + break; + } + + if (newDataStart == newDataSize) { + // No more new items, copy the remaining old items. + int itemCount = mOldDataSize - mOldDataStart; + System.arraycopy(mOldData, mOldDataStart, mData, mMergedSize, itemCount); + mMergedSize += itemCount; + break; + } + + T oldItem = mOldData[mOldDataStart]; + T newItem = newData[newDataStart]; + int compare = mCallback.compare(oldItem, newItem); + if (compare > 0) { + // New item is lower, output it. + mData[mMergedSize++] = newItem; + mSize++; + newDataStart++; + mCallback.onInserted(mMergedSize - 1, 1); + } else if (compare == 0 && mCallback.areItemsTheSame(oldItem, newItem)) { + // Items are the same. Output the new item, but consume both. + mData[mMergedSize++] = newItem; + newDataStart++; + mOldDataStart++; + if (!mCallback.areContentsTheSame(oldItem, newItem)) { + mCallback.onChanged(mMergedSize - 1, 1); + } + } else { + // Old item is lower than or equal to (but not the same as the new). Output it. + // New item with the same sort order will be inserted later. + mData[mMergedSize++] = oldItem; + mOldDataStart++; + } + } + } + + private void throwIfMerging() { + if (mOldData != null) { + throw new IllegalStateException("Cannot call this method from within addAll"); + } + } + + /** + * Batches adapter updates that happen between calling this method until calling + * {@link #endBatchedUpdates()}. For example, if you add multiple items in a loop + * and they are placed into consecutive indices, SortedList calls + * {@link Callback#onInserted(int, int)} only once with the proper item count. If an event + * cannot be merged with the previous event, the previous event is dispatched + * to the callback instantly. + *

+ * After running your data updates, you must call {@link #endBatchedUpdates()} + * which will dispatch any deferred data change event to the current callback. + *

+ * A sample implementation may look like this: + *

+     *     mSortedList.beginBatchedUpdates();
+     *     try {
+     *         mSortedList.add(item1)
+     *         mSortedList.add(item2)
+     *         mSortedList.remove(item3)
+     *         ...
+     *     } finally {
+     *         mSortedList.endBatchedUpdates();
+     *     }
+     * 
+ *

+ * Instead of using this method to batch calls, you can use a Callback that extends + * {@link BatchedCallback}. In that case, you must make sure that you are manually calling + * {@link BatchedCallback#dispatchLastEvent()} right after you complete your data changes. + * Failing to do so may create data inconsistencies with the Callback. + *

+ * If the current Callback in an instance of {@link BatchedCallback}, calling this method + * has no effect. + */ + public void beginBatchedUpdates() { + throwIfMerging(); + if (mCallback instanceof BatchedCallback) { + return; + } + if (mBatchedCallback == null) { + mBatchedCallback = new BatchedCallback(mCallback); + } + mCallback = mBatchedCallback; + } + + /** + * Ends the update transaction and dispatches any remaining event to the callback. + */ + public void endBatchedUpdates() { + throwIfMerging(); + if (mCallback instanceof BatchedCallback) { + ((BatchedCallback) mCallback).dispatchLastEvent(); + } + if (mCallback == mBatchedCallback) { + mCallback = mBatchedCallback.mWrappedCallback; + } + } + + private int add(T item, boolean notify) { + int index = findIndexOf(item, mData, 0, mSize, INSERTION); + if (index == INVALID_POSITION) { + index = 0; + } else if (index < mSize) { + T existing = mData[index]; + if (mCallback.areItemsTheSame(existing, item)) { + if (mCallback.areContentsTheSame(existing, item)) { + //no change but still replace the item + mData[index] = item; + return index; + } else { + mData[index] = item; + mCallback.onChanged(index, 1); + return index; + } + } + } + addToData(index, item); + if (notify) { + mCallback.onInserted(index, 1); + } + return index; + } + + /** + * Removes the provided item from the list and calls {@link Callback#onRemoved(int, int)}. + * + * @param item The item to be removed from the list. + * + * @return True if item is removed, false if item cannot be found in the list. + */ + public boolean remove(T item) { + throwIfMerging(); + return remove(item, true); + } + + /** + * Removes the item at the given index and calls {@link Callback#onRemoved(int, int)}. + * + * @param index The index of the item to be removed. + * + * @return The removed item. + */ + public T removeItemAt(int index) { + throwIfMerging(); + T item = get(index); + removeItemAtIndex(index, true); + return item; + } + + private boolean remove(T item, boolean notify) { + int index = findIndexOf(item, mData, 0, mSize, DELETION); + if (index == INVALID_POSITION) { + return false; + } + removeItemAtIndex(index, notify); + return true; + } + + private void removeItemAtIndex(int index, boolean notify) { + System.arraycopy(mData, index + 1, mData, index, mSize - index - 1); + mSize--; + mData[mSize] = null; + if (notify) { + mCallback.onRemoved(index, 1); + } + } + + /** + * Updates the item at the given index and calls {@link Callback#onChanged(int, int)} and/or + * {@link Callback#onMoved(int, int)} if necessary. + *

+ * You can use this method if you need to change an existing Item such that its position in the + * list may change. + *

+ * If the new object is a different object (get(index) != item) and + * {@link Callback#areContentsTheSame(Object, Object)} returns true, SortedList + * avoids calling {@link Callback#onChanged(int, int)} otherwise it calls + * {@link Callback#onChanged(int, int)}. + *

+ * If the new position of the item is different than the provided index, + * SortedList + * calls {@link Callback#onMoved(int, int)}. + * + * @param index The index of the item to replace + * @param item The item to replace the item at the given Index. + * @see #add(Object) + */ + public void updateItemAt(int index, T item) { + throwIfMerging(); + final T existing = get(index); + // assume changed if the same object is given back + boolean contentsChanged = existing == item || !mCallback.areContentsTheSame(existing, item); + if (existing != item) { + // different items, we can use comparison and may avoid lookup + final int cmp = mCallback.compare(existing, item); + if (cmp == 0) { + mData[index] = item; + if (contentsChanged) { + mCallback.onChanged(index, 1); + } + return; + } + } + if (contentsChanged) { + mCallback.onChanged(index, 1); + } + // TODO this done in 1 pass to avoid shifting twice. + removeItemAtIndex(index, false); + int newIndex = add(item, false); + if (index != newIndex) { + mCallback.onMoved(index, newIndex); + } + } + + /** + * This method can be used to recalculate the position of the item at the given index, without + * triggering an {@link Callback#onChanged(int, int)} callback. + *

+ * If you are editing objects in the list such that their position in the list may change but + * you don't want to trigger an onChange animation, you can use this method to re-position it. + * If the item changes position, SortedList will call {@link Callback#onMoved(int, int)} + * without + * calling {@link Callback#onChanged(int, int)}. + *

+ * A sample usage may look like: + * + *

+     *     final int position = mSortedList.indexOf(item);
+     *     item.incrementPriority(); // assume items are sorted by priority
+     *     mSortedList.recalculatePositionOfItemAt(position);
+     * 
+ * In the example above, because the sorting criteria of the item has been changed, + * mSortedList.indexOf(item) will not be able to find the item. This is why the code above + * first + * gets the position before editing the item, edits it and informs the SortedList that item + * should be repositioned. + * + * @param index The current index of the Item whose position should be re-calculated. + * @see #updateItemAt(int, Object) + * @see #add(Object) + */ + public void recalculatePositionOfItemAt(int index) { + throwIfMerging(); + // TODO can be improved + final T item = get(index); + removeItemAtIndex(index, false); + int newIndex = add(item, false); + if (index != newIndex) { + mCallback.onMoved(index, newIndex); + } + } + + /** + * Returns the item at the given index. + * + * @param index The index of the item to retrieve. + * + * @return The item at the given index. + * @throws java.lang.IndexOutOfBoundsException if provided index is negative or larger than the + * size of the list. + */ + public T get(int index) throws IndexOutOfBoundsException { + if (index >= mSize || index < 0) { + throw new IndexOutOfBoundsException("Asked to get item at " + index + " but size is " + + mSize); + } + if (mOldData != null) { + // The call is made from a callback during addAll execution. The data is split + // between mData and mOldData. + if (index >= mMergedSize) { + return mOldData[index - mMergedSize + mOldDataStart]; + } + } + return mData[index]; + } + + /** + * Returns the position of the provided item. + * + * @param item The item to query for position. + * + * @return The position of the provided item or {@link #INVALID_POSITION} if item is not in the + * list. + */ + public int indexOf(T item) { + if (mOldData != null) { + int index = findIndexOf(item, mData, 0, mMergedSize, LOOKUP); + if (index != INVALID_POSITION) { + return index; + } + index = findIndexOf(item, mOldData, mOldDataStart, mOldDataSize, LOOKUP); + if (index != INVALID_POSITION) { + return index - mOldDataStart + mMergedSize; + } + return INVALID_POSITION; + } + return findIndexOf(item, mData, 0, mSize, LOOKUP); + } + + private int findIndexOf(T item, T[] mData, int left, int right, int reason) { + while (left < right) { + final int middle = (left + right) / 2; + T myItem = mData[middle]; + final int cmp = mCallback.compare(myItem, item); + if (cmp < 0) { + left = middle + 1; + } else if (cmp == 0) { + if (mCallback.areItemsTheSame(myItem, item)) { + return middle; + } else { + int exact = linearEqualitySearch(item, middle, left, right); + if (reason == INSERTION) { + return exact == INVALID_POSITION ? middle : exact; + } else { + return exact; + } + } + } else { + right = middle; + } + } + return reason == INSERTION ? left : INVALID_POSITION; + } + + private int linearEqualitySearch(T item, int middle, int left, int right) { + // go left + for (int next = middle - 1; next >= left; next--) { + T nextItem = mData[next]; + int cmp = mCallback.compare(nextItem, item); + if (cmp != 0) { + break; + } + if (mCallback.areItemsTheSame(nextItem, item)) { + return next; + } + } + for (int next = middle + 1; next < right; next++) { + T nextItem = mData[next]; + int cmp = mCallback.compare(nextItem, item); + if (cmp != 0) { + break; + } + if (mCallback.areItemsTheSame(nextItem, item)) { + return next; + } + } + return INVALID_POSITION; + } + + private void addToData(int index, T item) { + if (index > mSize) { + throw new IndexOutOfBoundsException( + "cannot add item to " + index + " because size is " + mSize); + } + if (mSize == mData.length) { + // we are at the limit enlarge + T[] newData = (T[]) Array.newInstance(mTClass, mData.length + CAPACITY_GROWTH); + System.arraycopy(mData, 0, newData, 0, index); + newData[index] = item; + System.arraycopy(mData, index, newData, index + 1, mSize - index); + mData = newData; + } else { + // just shift, we fit + System.arraycopy(mData, index, mData, index + 1, mSize - index); + mData[index] = item; + } + mSize++; + } + + /** + * Removes all items from the SortedList. + */ + public void clear() { + throwIfMerging(); + if (mSize == 0) { + return; + } + final int prevSize = mSize; + Arrays.fill(mData, 0, prevSize, null); + mSize = 0; + mCallback.onRemoved(0, prevSize); + } + + /** + * The class that controls the behavior of the {@link SortedList}. + *

+ * It defines how items should be sorted and how duplicates should be handled. + *

+ * SortedList calls the callback methods on this class to notify changes about the underlying + * data. + */ + public static abstract class Callback implements Comparator, ListUpdateCallback { + + /** + * Similar to {@link java.util.Comparator#compare(Object, Object)}, should compare two and + * return how they should be ordered. + * + * @param o1 The first object to compare. + * @param o2 The second object to compare. + * + * @return a negative integer, zero, or a positive integer as the + * first argument is less than, equal to, or greater than the + * second. + */ + @Override + abstract public int compare(T2 o1, T2 o2); + + /** + * Called by the SortedList when the item at the given position is updated. + * + * @param position The position of the item which has been updated. + * @param count The number of items which has changed. + */ + abstract public void onChanged(int position, int count); + + @Override + public void onChanged(int position, int count, Object payload) { + onChanged(position, count); + } + + /** + * Called by the SortedList when it wants to check whether two items have the same data + * or not. SortedList uses this information to decide whether it should call + * {@link #onChanged(int, int)} or not. + *

+ * SortedList uses this method to check equality instead of {@link Object#equals(Object)} + * so + * that you can change its behavior depending on your UI. + *

+ * For example, if you are using SortedList with a {@link android.support.v7.widget.RecyclerView.Adapter + * RecyclerView.Adapter}, you should + * return whether the items' visual representations are the same or not. + * + * @param oldItem The previous representation of the object. + * @param newItem The new object that replaces the previous one. + * + * @return True if the contents of the items are the same or false if they are different. + */ + abstract public boolean areContentsTheSame(T2 oldItem, T2 newItem); + + /** + * Called by the SortedList to decide whether two object represent the same Item or not. + *

+ * For example, if your items have unique ids, this method should check their equality. + * + * @param item1 The first item to check. + * @param item2 The second item to check. + * + * @return True if the two items represent the same object or false if they are different. + */ + abstract public boolean areItemsTheSame(T2 item1, T2 item2); + } + + /** + * A callback implementation that can batch notify events dispatched by the SortedList. + *

+ * This class can be useful if you want to do multiple operations on a SortedList but don't + * want to dispatch each event one by one, which may result in a performance issue. + *

+ * For example, if you are going to add multiple items to a SortedList, BatchedCallback call + * convert individual onInserted(index, 1) calls into one + * onInserted(index, N) if items are added into consecutive indices. This change + * can help RecyclerView resolve changes much more easily. + *

+ * If consecutive changes in the SortedList are not suitable for batching, BatchingCallback + * dispatches them as soon as such case is detected. After your edits on the SortedList is + * complete, you must always call {@link BatchedCallback#dispatchLastEvent()} to flush + * all changes to the Callback. + */ + public static class BatchedCallback extends Callback { + + private final Callback mWrappedCallback; + private final BatchingListUpdateCallback mBatchingListUpdateCallback; + /** + * Creates a new BatchedCallback that wraps the provided Callback. + * + * @param wrappedCallback The Callback which should received the data change callbacks. + * Other method calls (e.g. {@link #compare(Object, Object)} from + * the SortedList are directly forwarded to this Callback. + */ + public BatchedCallback(Callback wrappedCallback) { + mWrappedCallback = wrappedCallback; + mBatchingListUpdateCallback = new BatchingListUpdateCallback(mWrappedCallback); + } + + @Override + public int compare(T2 o1, T2 o2) { + return mWrappedCallback.compare(o1, o2); + } + + @Override + public void onInserted(int position, int count) { + mBatchingListUpdateCallback.onInserted(position, count); + } + + @Override + public void onRemoved(int position, int count) { + mBatchingListUpdateCallback.onRemoved(position, count); + } + + @Override + public void onMoved(int fromPosition, int toPosition) { + mBatchingListUpdateCallback.onInserted(fromPosition, toPosition); + } + + @Override + public void onChanged(int position, int count) { + mBatchingListUpdateCallback.onChanged(position, count, null); + } + + @Override + public boolean areContentsTheSame(T2 oldItem, T2 newItem) { + return mWrappedCallback.areContentsTheSame(oldItem, newItem); + } + + @Override + public boolean areItemsTheSame(T2 item1, T2 item2) { + return mWrappedCallback.areItemsTheSame(item1, item2); + } + + /** + * This method dispatches any pending event notifications to the wrapped Callback. + * You must always call this method after you are done with editing the SortedList. + */ + public void dispatchLastEvent() { + mBatchingListUpdateCallback.dispatchLastEvent(); + } + } +} diff --git a/app/src/main/java/android/support/v7/util/ThreadUtil.java b/app/src/main/java/android/support/v7/util/ThreadUtil.java new file mode 100644 index 0000000000..05db034259 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/ThreadUtil.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +interface ThreadUtil { + + interface MainThreadCallback { + + void updateItemCount(int generation, int itemCount); + + void addTile(int generation, TileList.Tile tile); + + void removeTile(int generation, int position); + } + + interface BackgroundCallback { + + void refresh(int generation); + + void updateRange(int rangeStart, int rangeEnd, int extRangeStart, int extRangeEnd, + int scrollHint); + + void loadTile(int position, int scrollHint); + + void recycleTile(TileList.Tile tile); + } + + MainThreadCallback getMainThreadProxy(MainThreadCallback callback); + + BackgroundCallback getBackgroundProxy(BackgroundCallback callback); +} diff --git a/app/src/main/java/android/support/v7/util/TileList.java b/app/src/main/java/android/support/v7/util/TileList.java new file mode 100644 index 0000000000..f686a31c24 --- /dev/null +++ b/app/src/main/java/android/support/v7/util/TileList.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.util; + +import android.util.SparseArray; + +import java.lang.reflect.Array; + +/** + * A sparse collection of tiles sorted for efficient access. + */ +class TileList { + + final int mTileSize; + + // Keyed by start position. + private final SparseArray> mTiles = new SparseArray>(10); + + Tile mLastAccessedTile; + + public TileList(int tileSize) { + mTileSize = tileSize; + } + + public T getItemAt(int pos) { + if (mLastAccessedTile == null || !mLastAccessedTile.containsPosition(pos)) { + final int startPosition = pos - (pos % mTileSize); + final int index = mTiles.indexOfKey(startPosition); + if (index < 0) { + return null; + } + mLastAccessedTile = mTiles.valueAt(index); + } + return mLastAccessedTile.getByPosition(pos); + } + + public int size() { + return mTiles.size(); + } + + public void clear() { + mTiles.clear(); + } + + public Tile getAtIndex(int index) { + return mTiles.valueAt(index); + } + + public Tile addOrReplace(Tile newTile) { + final int index = mTiles.indexOfKey(newTile.mStartPosition); + if (index < 0) { + mTiles.put(newTile.mStartPosition, newTile); + return null; + } + Tile oldTile = mTiles.valueAt(index); + mTiles.setValueAt(index, newTile); + if (mLastAccessedTile == oldTile) { + mLastAccessedTile = newTile; + } + return oldTile; + } + + public Tile removeAtPos(int startPosition) { + Tile tile = mTiles.get(startPosition); + if (mLastAccessedTile == tile) { + mLastAccessedTile = null; + } + mTiles.delete(startPosition); + return tile; + } + + public static class Tile { + public final T[] mItems; + public int mStartPosition; + public int mItemCount; + Tile mNext; // Used only for pooling recycled tiles. + + public Tile(Class klass, int size) { + //noinspection unchecked + mItems = (T[]) Array.newInstance(klass, size); + } + + boolean containsPosition(int pos) { + return mStartPosition <= pos && pos < mStartPosition + mItemCount; + } + + T getByPosition(int pos) { + return mItems[pos - mStartPosition]; + } + } +} From b1fdeeef6598c3ce0ed5a0219365238f254f9e49 Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Tue, 25 Oct 2016 11:31:42 +0800 Subject: [PATCH 10/11] ... --- .../support/v7/widget/AdapterHelper.java | 136 +- .../support/v7/widget/ChildHelper.java | 87 +- .../v7/widget/DefaultItemAnimator.java | 147 +- .../support/v7/widget/GridLayoutManager.java | 488 +- .../support/v7/widget/LayoutState.java | 43 +- .../v7/widget/LinearLayoutManager.java | 622 +- .../v7/widget/LinearSmoothScroller.java | 65 +- .../support/v7/widget/LinearSnapHelper.java | 286 + .../support/v7/widget/OpReorderer.java | 12 +- .../support/v7/widget/OrientationHelper.java | 105 +- .../support/v7/widget/RecyclerView.java | 5974 +++++++++++++---- .../RecyclerViewAccessibilityDelegate.java | 21 +- .../support/v7/widget/ScrollbarHelper.java | 14 +- .../support/v7/widget/SimpleItemAnimator.java | 442 ++ .../android/support/v7/widget/SnapHelper.java | 274 + .../v7/widget/StaggeredGridLayoutManager.java | 1054 ++- .../support/v7/widget/ViewInfoStore.java | 331 + .../v7/widget/helper/ItemTouchHelper.java | 2408 +++++++ .../v7/widget/helper/ItemTouchUIUtil.java | 64 + .../v7/widget/helper/ItemTouchUIUtilImpl.java | 138 + .../util/SortedListAdapterCallback.java | 59 + 21 files changed, 10745 insertions(+), 2025 deletions(-) create mode 100644 app/src/main/java/android/support/v7/widget/LinearSnapHelper.java create mode 100644 app/src/main/java/android/support/v7/widget/SimpleItemAnimator.java create mode 100644 app/src/main/java/android/support/v7/widget/SnapHelper.java create mode 100644 app/src/main/java/android/support/v7/widget/ViewInfoStore.java create mode 100644 app/src/main/java/android/support/v7/widget/helper/ItemTouchHelper.java create mode 100644 app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtil.java create mode 100644 app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtilImpl.java create mode 100644 app/src/main/java/android/support/v7/widget/util/SortedListAdapterCallback.java diff --git a/app/src/main/java/android/support/v7/widget/AdapterHelper.java b/app/src/main/java/android/support/v7/widget/AdapterHelper.java index ecf0c294d4..47e972225e 100644 --- a/app/src/main/java/android/support/v7/widget/AdapterHelper.java +++ b/app/src/main/java/android/support/v7/widget/AdapterHelper.java @@ -23,7 +23,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import static android.support.v7.widget.RecyclerView.ViewHolder; +import static android.support.v7.widget.RecyclerView.*; /** * Helper class that can enqueue and process adapter update operations. @@ -67,6 +67,8 @@ class AdapterHelper implements OpReorderer.Callback { final OpReorderer mOpReorderer; + private int mExistingUpdateTypes = 0; + AdapterHelper(Callback callback) { this(callback, false); } @@ -85,6 +87,7 @@ class AdapterHelper implements OpReorderer.Callback { void reset() { recycleUpdateOpsAndClearList(mPendingUpdates); recycleUpdateOpsAndClearList(mPostponedList); + mExistingUpdateTypes = 0; } void preProcess() { @@ -119,6 +122,7 @@ class AdapterHelper implements OpReorderer.Callback { mCallback.onDispatchSecondPass(mPostponedList.get(i)); } recycleUpdateOpsAndClearList(mPostponedList); + mExistingUpdateTypes = 0; } private void applyMove(UpdateOp op) { @@ -145,7 +149,7 @@ class AdapterHelper implements OpReorderer.Callback { if (type == POSITION_TYPE_INVISIBLE) { // Looks like we have other updates that we cannot merge with this one. // Create an UpdateOp and dispatch it to LayoutManager. - UpdateOp newOp = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount); + UpdateOp newOp = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount, null); dispatchAndUpdateViewHolders(newOp); typeChanged = true; } @@ -156,7 +160,7 @@ class AdapterHelper implements OpReorderer.Callback { if (type == POSITION_TYPE_NEW_OR_LAID_OUT) { // Looks like we have other updates that we cannot merge with this one. // Create UpdateOp op and dispatch it to LayoutManager. - UpdateOp newOp = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount); + UpdateOp newOp = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount, null); postponeAndUpdateViewHolders(newOp); typeChanged = true; } @@ -172,7 +176,7 @@ class AdapterHelper implements OpReorderer.Callback { } if (tmpCount != op.itemCount) { // all 1 effect recycleUpdateOp(op); - op = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount); + op = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount, null); } if (type == POSITION_TYPE_INVISIBLE) { dispatchAndUpdateViewHolders(op); @@ -190,7 +194,8 @@ class AdapterHelper implements OpReorderer.Callback { ViewHolder vh = mCallback.findViewHolder(position); if (vh != null || canFindInPreLayout(position)) { // deferred if (type == POSITION_TYPE_INVISIBLE) { - UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount); + UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount, + op.payload); dispatchAndUpdateViewHolders(newOp); tmpCount = 0; tmpStart = position; @@ -198,7 +203,8 @@ class AdapterHelper implements OpReorderer.Callback { type = POSITION_TYPE_NEW_OR_LAID_OUT; } else { // applied if (type == POSITION_TYPE_NEW_OR_LAID_OUT) { - UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount); + UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount, + op.payload); postponeAndUpdateViewHolders(newOp); tmpCount = 0; tmpStart = position; @@ -208,8 +214,9 @@ class AdapterHelper implements OpReorderer.Callback { tmpCount++; } if (tmpCount != op.itemCount) { // all 1 effect + Object payload = op.payload; recycleUpdateOp(op); - op = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount); + op = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount, payload); } if (type == POSITION_TYPE_INVISIBLE) { dispatchAndUpdateViewHolders(op); @@ -272,7 +279,7 @@ class AdapterHelper implements OpReorderer.Callback { tmpCnt++; } else { // need to dispatch this separately - UpdateOp tmp = obtainUpdateOp(op.cmd, tmpStart, tmpCnt); + UpdateOp tmp = obtainUpdateOp(op.cmd, tmpStart, tmpCnt, op.payload); if (DEBUG) { Log.d(TAG, "need to dispatch separately " + tmp); } @@ -285,9 +292,10 @@ class AdapterHelper implements OpReorderer.Callback { tmpCnt = 1; } } + Object payload = op.payload; recycleUpdateOp(op); if (tmpCnt > 0) { - UpdateOp tmp = obtainUpdateOp(op.cmd, tmpStart, tmpCnt); + UpdateOp tmp = obtainUpdateOp(op.cmd, tmpStart, tmpCnt, payload); if (DEBUG) { Log.d(TAG, "dispatching:" + tmp); } @@ -311,7 +319,7 @@ class AdapterHelper implements OpReorderer.Callback { mCallback.offsetPositionsForRemovingInvisible(offsetStart, op.itemCount); break; case UpdateOp.UPDATE: - mCallback.markViewHoldersUpdated(offsetStart, op.itemCount); + mCallback.markViewHoldersUpdated(offsetStart, op.itemCount, op.payload); break; default: throw new IllegalArgumentException("only remove and update ops can be dispatched" @@ -429,9 +437,7 @@ class AdapterHelper implements OpReorderer.Callback { if (DEBUG) { Log.d(TAG, "postponing " + op); } -// Utils.log("add UpdateOp to PostponedList"); mPostponedList.add(op); -// Utils.log("op" + op.positionStart + "=" + op.itemCount); switch (op.cmd) { case UpdateOp.ADD: mCallback.offsetPositionsForAdd(op.positionStart, op.itemCount); @@ -444,7 +450,7 @@ class AdapterHelper implements OpReorderer.Callback { op.itemCount); break; case UpdateOp.UPDATE: - mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount); + mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount, op.payload); break; default: throw new IllegalArgumentException("Unknown update op type for " + op); @@ -455,6 +461,10 @@ class AdapterHelper implements OpReorderer.Callback { return mPendingUpdates.size() > 0; } + boolean hasAnyUpdateTypes(int updateTypes) { + return (mExistingUpdateTypes & updateTypes) != 0; + } + int findPositionOffset(int position) { return findPositionOffset(position, 0); } @@ -491,8 +501,12 @@ class AdapterHelper implements OpReorderer.Callback { /** * @return True if updates should be processed. */ - boolean onItemRangeChanged(int positionStart, int itemCount) { - mPendingUpdates.add(obtainUpdateOp(UpdateOp.UPDATE, positionStart, itemCount)); + boolean onItemRangeChanged(int positionStart, int itemCount, Object payload) { + if (itemCount < 1) { + return false; + } + mPendingUpdates.add(obtainUpdateOp(UpdateOp.UPDATE, positionStart, itemCount, payload)); + mExistingUpdateTypes |= UpdateOp.UPDATE; return mPendingUpdates.size() == 1; } @@ -500,7 +514,11 @@ class AdapterHelper implements OpReorderer.Callback { * @return True if updates should be processed. */ boolean onItemRangeInserted(int positionStart, int itemCount) { - mPendingUpdates.add(obtainUpdateOp(UpdateOp.ADD, positionStart, itemCount)); + if (itemCount < 1) { + return false; + } + mPendingUpdates.add(obtainUpdateOp(UpdateOp.ADD, positionStart, itemCount, null)); + mExistingUpdateTypes |= UpdateOp.ADD; return mPendingUpdates.size() == 1; } @@ -508,7 +526,11 @@ class AdapterHelper implements OpReorderer.Callback { * @return True if updates should be processed. */ boolean onItemRangeRemoved(int positionStart, int itemCount) { - mPendingUpdates.add(obtainUpdateOp(UpdateOp.REMOVE, positionStart, itemCount)); + if (itemCount < 1) { + return false; + } + mPendingUpdates.add(obtainUpdateOp(UpdateOp.REMOVE, positionStart, itemCount, null)); + mExistingUpdateTypes |= UpdateOp.REMOVE; return mPendingUpdates.size() == 1; } @@ -517,12 +539,13 @@ class AdapterHelper implements OpReorderer.Callback { */ boolean onItemRangeMoved(int from, int to, int itemCount) { if (from == to) { - return false;//no-op + return false; // no-op } if (itemCount != 1) { throw new IllegalArgumentException("Moving more than 1 item is not supported yet"); } - mPendingUpdates.add(obtainUpdateOp(UpdateOp.MOVE, from, to)); + mPendingUpdates.add(obtainUpdateOp(UpdateOp.MOVE, from, to, null)); + mExistingUpdateTypes |= UpdateOp.MOVE; return mPendingUpdates.size() == 1; } @@ -547,7 +570,7 @@ class AdapterHelper implements OpReorderer.Callback { break; case UpdateOp.UPDATE: mCallback.onDispatchSecondPass(op); - mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount); + mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount, op.payload); break; case UpdateOp.MOVE: mCallback.onDispatchSecondPass(op); @@ -559,6 +582,47 @@ class AdapterHelper implements OpReorderer.Callback { } } recycleUpdateOpsAndClearList(mPendingUpdates); + mExistingUpdateTypes = 0; + } + + public int applyPendingUpdatesToPosition(int position) { + final int size = mPendingUpdates.size(); + for (int i = 0; i < size; i ++) { + UpdateOp op = mPendingUpdates.get(i); + switch (op.cmd) { + case UpdateOp.ADD: + if (op.positionStart <= position) { + position += op.itemCount; + } + break; + case UpdateOp.REMOVE: + if (op.positionStart <= position) { + final int end = op.positionStart + op.itemCount; + if (end > position) { + return RecyclerView.NO_POSITION; + } + position -= op.itemCount; + } + break; + case UpdateOp.MOVE: + if (op.positionStart == position) { + position = op.itemCount;//position end + } else { + if (op.positionStart < position) { + position -= 1; + } + if (op.itemCount <= position) { + position += 1; + } + } + break; + } + } + return position; + } + + boolean hasUpdates() { + return !mPostponedList.isEmpty() && !mPendingUpdates.isEmpty(); } /** @@ -566,13 +630,13 @@ class AdapterHelper implements OpReorderer.Callback { */ static class UpdateOp { - static final int ADD = 0; + static final int ADD = 1; - static final int REMOVE = 1; + static final int REMOVE = 1 << 1; - static final int UPDATE = 2; + static final int UPDATE = 1 << 2; - static final int MOVE = 3; + static final int MOVE = 1 << 3; static final int POOL_SIZE = 30; @@ -580,13 +644,16 @@ class AdapterHelper implements OpReorderer.Callback { int positionStart; + Object payload; + // holds the target position if this is a MOVE int itemCount; - UpdateOp(int cmd, int positionStart, int itemCount) { + UpdateOp(int cmd, int positionStart, int itemCount, Object payload) { this.cmd = cmd; this.positionStart = positionStart; this.itemCount = itemCount; + this.payload = payload; } String cmdToString() { @@ -605,7 +672,9 @@ class AdapterHelper implements OpReorderer.Callback { @Override public String toString() { - return "[" + cmdToString() + ",s:" + positionStart + "c:" + itemCount + "]"; + return Integer.toHexString(System.identityHashCode(this)) + + "[" + cmdToString() + ",s:" + positionStart + "c:" + itemCount + +",p:"+payload + "]"; } @Override @@ -634,6 +703,13 @@ class AdapterHelper implements OpReorderer.Callback { if (positionStart != op.positionStart) { return false; } + if (payload != null) { + if (!payload.equals(op.payload)) { + return false; + } + } else if (op.payload != null) { + return false; + } return true; } @@ -648,14 +724,15 @@ class AdapterHelper implements OpReorderer.Callback { } @Override - public UpdateOp obtainUpdateOp(int cmd, int positionStart, int itemCount) { + public UpdateOp obtainUpdateOp(int cmd, int positionStart, int itemCount, Object payload) { UpdateOp op = mUpdateOpPool.acquire(); if (op == null) { - op = new UpdateOp(cmd, positionStart, itemCount); + op = new UpdateOp(cmd, positionStart, itemCount, payload); } else { op.cmd = cmd; op.positionStart = positionStart; op.itemCount = itemCount; + op.payload = payload; } return op; } @@ -663,6 +740,7 @@ class AdapterHelper implements OpReorderer.Callback { @Override public void recycleUpdateOp(UpdateOp op) { if (!mDisableRecycler) { + op.payload = null; mUpdateOpPool.release(op); } } @@ -686,7 +764,7 @@ class AdapterHelper implements OpReorderer.Callback { void offsetPositionsForRemovingLaidOutOrNewView(int positionStart, int itemCount); - void markViewHoldersUpdated(int positionStart, int itemCount); + void markViewHoldersUpdated(int positionStart, int itemCount, Object payloads); void onDispatchFirstPass(UpdateOp updateOp); diff --git a/app/src/main/java/android/support/v7/widget/ChildHelper.java b/app/src/main/java/android/support/v7/widget/ChildHelper.java index e5556796f1..0afa4056ff 100644 --- a/app/src/main/java/android/support/v7/widget/ChildHelper.java +++ b/app/src/main/java/android/support/v7/widget/ChildHelper.java @@ -51,6 +51,30 @@ class ChildHelper { mHiddenViews = new ArrayList(); } + /** + * Marks a child view as hidden + * + * @param child View to hide. + */ + private void hideViewInternal(View child) { + mHiddenViews.add(child); + mCallback.onEnteredHiddenState(child); + } + + /** + * Unmarks a child view as hidden. + * + * @param child View to hide. + */ + private boolean unhideViewInternal(View child) { + if (mHiddenViews.remove(child)) { + mCallback.onLeftHiddenState(child); + return true; + } else { + return false; + } + } + /** * Adds a view to the ViewGroup * @@ -76,11 +100,11 @@ class ChildHelper { } else { offset = getOffset(index); } - mCallback.addView(child, offset); mBucket.insert(offset, hidden); if (hidden) { - mHiddenViews.add(child); + hideViewInternal(child); } + mCallback.addView(child, offset); if (DEBUG) { Log.d(TAG, "addViewAt " + index + ",h:" + hidden + ", " + this); } @@ -117,10 +141,10 @@ class ChildHelper { if (index < 0) { return; } - mCallback.removeViewAt(index); if (mBucket.remove(index)) { - mHiddenViews.remove(view); + unhideViewInternal(view); } + mCallback.removeViewAt(index); if (DEBUG) { Log.d(TAG, "remove View off:" + index + "," + this); } @@ -138,10 +162,10 @@ class ChildHelper { if (view == null) { return; } - mCallback.removeViewAt(offset); if (mBucket.remove(offset)) { - mHiddenViews.remove(view); + unhideViewInternal(view); } + mCallback.removeViewAt(offset); if (DEBUG) { Log.d(TAG, "removeViewAt " + index + ", off:" + offset + ", " + this); } @@ -161,9 +185,12 @@ class ChildHelper { * Removes all views from the ViewGroup including the hidden ones. */ void removeAllViewsUnfiltered() { - mCallback.removeAllViews(); mBucket.reset(); - mHiddenViews.clear(); + for (int i = mHiddenViews.size() - 1; i >= 0; i--) { + mCallback.onLeftHiddenState(mHiddenViews.get(i)); + mHiddenViews.remove(i); + } + mCallback.removeAllViews(); if (DEBUG) { Log.d(TAG, "removeAllViewsUnfiltered"); } @@ -181,8 +208,8 @@ class ChildHelper { for (int i = 0; i < count; i++) { final View view = mHiddenViews.get(i); RecyclerView.ViewHolder holder = mCallback.getChildViewHolder(view); - if (holder.getPosition() == position && !holder.isInvalid() && - (type == RecyclerView.INVALID_TYPE || holder.getItemViewType() == type)) { + if (holder.getLayoutPosition() == position && !holder.isInvalid() && !holder.isRemoved() + && (type == RecyclerView.INVALID_TYPE || holder.getItemViewType() == type)) { return view; } } @@ -205,8 +232,11 @@ class ChildHelper { } else { offset = getOffset(index); } - mCallback.attachViewToParent(child, offset, layoutParams); mBucket.insert(offset, hidden); + if (hidden) { + hideViewInternal(child); + } + mCallback.attachViewToParent(child, offset, layoutParams); if (DEBUG) { Log.d(TAG, "attach view to parent index:" + index + ",off:" + offset + "," + "h:" + hidden + ", " + this); @@ -250,8 +280,8 @@ class ChildHelper { */ void detachViewFromParent(int index) { final int offset = getOffset(index); - mCallback.detachViewFromParent(offset); mBucket.remove(offset); + mCallback.detachViewFromParent(offset); if (DEBUG) { Log.d(TAG, "detach view from parent " + index + ", off:" + offset); } @@ -303,15 +333,34 @@ class ChildHelper { throw new RuntimeException("trying to hide same view twice, how come ? " + view); } mBucket.set(offset); - mHiddenViews.add(view); + hideViewInternal(view); if (DEBUG) { Log.d(TAG, "hiding child " + view + " at offset " + offset+ ", " + this); } } + /** + * Moves a child view from hidden list to regular list. + * Calling this method should probably be followed by a detach, otherwise, it will suddenly + * show up in LayoutManager's children list. + * + * @param view The hidden View to unhide + */ + void unhide(View view) { + final int offset = mCallback.indexOfChild(view); + if (offset < 0) { + throw new IllegalArgumentException("view is not a child, cannot hide " + view); + } + if (!mBucket.get(offset)) { + throw new RuntimeException("trying to unhide a view that was not hidden" + view); + } + mBucket.clear(offset); + unhideViewInternal(view); + } + @Override public String toString() { - return mBucket.toString(); + return mBucket.toString() + ", hidden list:" + mHiddenViews.size(); } /** @@ -323,18 +372,18 @@ class ChildHelper { boolean removeViewIfHidden(View view) { final int index = mCallback.indexOfChild(view); if (index == -1) { - if (mHiddenViews.remove(view) && DEBUG) { + if (unhideViewInternal(view) && DEBUG) { throw new IllegalStateException("view is in hidden list but not in view group"); } return true; } if (mBucket.get(index)) { mBucket.remove(index); - mCallback.removeViewAt(index); - if (!mHiddenViews.remove(view) && DEBUG) { + if (!unhideViewInternal(view) && DEBUG) { throw new IllegalStateException( "removed a hidden view but it is not in hidden views list"); } + mCallback.removeViewAt(index); return true; } return false; @@ -480,5 +529,9 @@ class ChildHelper { void attachViewToParent(View child, int index, ViewGroup.LayoutParams layoutParams); void detachViewFromParent(int offset); + + void onEnteredHiddenState(View child); + + void onLeftHiddenState(View child); } } diff --git a/app/src/main/java/android/support/v7/widget/DefaultItemAnimator.java b/app/src/main/java/android/support/v7/widget/DefaultItemAnimator.java index 2a27d65ab8..d64621ade9 100644 --- a/app/src/main/java/android/support/v7/widget/DefaultItemAnimator.java +++ b/app/src/main/java/android/support/v7/widget/DefaultItemAnimator.java @@ -15,6 +15,8 @@ */ package android.support.v7.widget; +import android.support.annotation.NonNull; +import android.support.v4.animation.AnimatorCompatHelper; import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewPropertyAnimatorCompat; import android.support.v4.view.ViewPropertyAnimatorListener; @@ -31,23 +33,22 @@ import java.util.List; * * @see RecyclerView#setItemAnimator(RecyclerView.ItemAnimator) */ -public class DefaultItemAnimator extends RecyclerView.ItemAnimator { +public class DefaultItemAnimator extends SimpleItemAnimator { private static final boolean DEBUG = false; - private ArrayList mPendingRemovals = new ArrayList(); - private ArrayList mPendingAdditions = new ArrayList(); - private ArrayList mPendingMoves = new ArrayList(); - private ArrayList mPendingChanges = new ArrayList(); + private ArrayList mPendingRemovals = new ArrayList<>(); + private ArrayList mPendingAdditions = new ArrayList<>(); + private ArrayList mPendingMoves = new ArrayList<>(); + private ArrayList mPendingChanges = new ArrayList<>(); - private ArrayList> mAdditionsList = - new ArrayList>(); - private ArrayList> mMovesList = new ArrayList>(); - private ArrayList> mChangesList = new ArrayList>(); + private ArrayList> mAdditionsList = new ArrayList<>(); + private ArrayList> mMovesList = new ArrayList<>(); + private ArrayList> mChangesList = new ArrayList<>(); - private ArrayList mAddAnimations = new ArrayList(); - private ArrayList mMoveAnimations = new ArrayList(); - private ArrayList mRemoveAnimations = new ArrayList(); - private ArrayList mChangeAnimations = new ArrayList(); + private ArrayList mAddAnimations = new ArrayList<>(); + private ArrayList mMoveAnimations = new ArrayList<>(); + private ArrayList mRemoveAnimations = new ArrayList<>(); + private ArrayList mChangeAnimations = new ArrayList<>(); private static class MoveInfo { public ViewHolder holder; @@ -109,7 +110,7 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { mPendingRemovals.clear(); // Next, move stuff if (movesPending) { - final ArrayList moves = new ArrayList(); + final ArrayList moves = new ArrayList<>(); moves.addAll(mPendingMoves); mMovesList.add(moves); mPendingMoves.clear(); @@ -133,7 +134,7 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { } // Next, change stuff, to run in parallel with move animations if (changesPending) { - final ArrayList changes = new ArrayList(); + final ArrayList changes = new ArrayList<>(); changes.addAll(mPendingChanges); mChangesList.add(changes); mPendingChanges.clear(); @@ -156,11 +157,12 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { } // Next, add stuff if (additionsPending) { - final ArrayList additions = new ArrayList(); + final ArrayList additions = new ArrayList<>(); additions.addAll(mPendingAdditions); mAdditionsList.add(additions); mPendingAdditions.clear(); Runnable adder = new Runnable() { + @Override public void run() { for (ViewHolder holder : additions) { animateAddImpl(holder); @@ -184,7 +186,7 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { @Override public boolean animateRemove(final ViewHolder holder) { - endAnimation(holder); + resetAnimation(holder); mPendingRemovals.add(holder); return true; } @@ -192,12 +194,14 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { private void animateRemoveImpl(final ViewHolder holder) { final View view = holder.itemView; final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view); + mRemoveAnimations.add(holder); animation.setDuration(getRemoveDuration()) .alpha(0).setListener(new VpaListenerAdapter() { @Override public void onAnimationStart(View view) { dispatchRemoveStarting(holder); } + @Override public void onAnimationEnd(View view) { animation.setListener(null); @@ -207,12 +211,11 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { dispatchFinishedWhenDone(); } }).start(); - mRemoveAnimations.add(holder); } @Override public boolean animateAdd(final ViewHolder holder) { - endAnimation(holder); + resetAnimation(holder); ViewCompat.setAlpha(holder.itemView, 0); mPendingAdditions.add(holder); return true; @@ -220,8 +223,8 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { private void animateAddImpl(final ViewHolder holder) { final View view = holder.itemView; - mAddAnimations.add(holder); final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view); + mAddAnimations.add(holder); animation.alpha(1).setDuration(getAddDuration()). setListener(new VpaListenerAdapter() { @Override @@ -249,7 +252,7 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { final View view = holder.itemView; fromX += ViewCompat.getTranslationX(holder.itemView); fromY += ViewCompat.getTranslationY(holder.itemView); - endAnimation(holder); + resetAnimation(holder); int deltaX = toX - fromX; int deltaY = toY - fromY; if (deltaX == 0 && deltaY == 0) { @@ -279,8 +282,8 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { // TODO: make EndActions end listeners instead, since end actions aren't called when // vpas are canceled (and can't end them. why?) // need listener functionality in VPACompat for this. Ick. - mMoveAnimations.add(holder); final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view); + mMoveAnimations.add(holder); animation.setDuration(getMoveDuration()).setListener(new VpaListenerAdapter() { @Override public void onAnimationStart(View view) { @@ -308,19 +311,24 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { @Override public boolean animateChange(ViewHolder oldHolder, ViewHolder newHolder, int fromX, int fromY, int toX, int toY) { + if (oldHolder == newHolder) { + // Don't know how to run change animations when the same view holder is re-used. + // run a move animation to handle position changes. + return animateMove(oldHolder, fromX, fromY, toX, toY); + } final float prevTranslationX = ViewCompat.getTranslationX(oldHolder.itemView); final float prevTranslationY = ViewCompat.getTranslationY(oldHolder.itemView); final float prevAlpha = ViewCompat.getAlpha(oldHolder.itemView); - endAnimation(oldHolder); + resetAnimation(oldHolder); int deltaX = (int) (toX - fromX - prevTranslationX); int deltaY = (int) (toY - fromY - prevTranslationY); // recover prev translation state after ending animation ViewCompat.setTranslationX(oldHolder.itemView, prevTranslationX); ViewCompat.setTranslationY(oldHolder.itemView, prevTranslationY); ViewCompat.setAlpha(oldHolder.itemView, prevAlpha); - if (newHolder != null && newHolder.itemView != null) { + if (newHolder != null) { // carry over translation values - endAnimation(newHolder); + resetAnimation(newHolder); ViewCompat.setTranslationX(newHolder.itemView, -deltaX); ViewCompat.setTranslationY(newHolder.itemView, -deltaY); ViewCompat.setAlpha(newHolder.itemView, 0); @@ -331,34 +339,36 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { private void animateChangeImpl(final ChangeInfo changeInfo) { final ViewHolder holder = changeInfo.oldHolder; - final View view = holder.itemView; + final View view = holder == null ? null : holder.itemView; final ViewHolder newHolder = changeInfo.newHolder; final View newView = newHolder != null ? newHolder.itemView : null; - mChangeAnimations.add(changeInfo.oldHolder); + if (view != null) { + final ViewPropertyAnimatorCompat oldViewAnim = ViewCompat.animate(view).setDuration( + getChangeDuration()); + mChangeAnimations.add(changeInfo.oldHolder); + oldViewAnim.translationX(changeInfo.toX - changeInfo.fromX); + oldViewAnim.translationY(changeInfo.toY - changeInfo.fromY); + oldViewAnim.alpha(0).setListener(new VpaListenerAdapter() { + @Override + public void onAnimationStart(View view) { + dispatchChangeStarting(changeInfo.oldHolder, true); + } - final ViewPropertyAnimatorCompat oldViewAnim = ViewCompat.animate(view).setDuration( - getChangeDuration()); - oldViewAnim.translationX(changeInfo.toX - changeInfo.fromX); - oldViewAnim.translationY(changeInfo.toY - changeInfo.fromY); - oldViewAnim.alpha(0).setListener(new VpaListenerAdapter() { - @Override - public void onAnimationStart(View view) { - dispatchChangeStarting(changeInfo.oldHolder, true); - } - @Override - public void onAnimationEnd(View view) { - oldViewAnim.setListener(null); - ViewCompat.setAlpha(view, 1); - ViewCompat.setTranslationX(view, 0); - ViewCompat.setTranslationY(view, 0); - dispatchChangeFinished(changeInfo.oldHolder, true); - mChangeAnimations.remove(changeInfo.oldHolder); - dispatchFinishedWhenDone(); - } - }).start(); + @Override + public void onAnimationEnd(View view) { + oldViewAnim.setListener(null); + ViewCompat.setAlpha(view, 1); + ViewCompat.setTranslationX(view, 0); + ViewCompat.setTranslationY(view, 0); + dispatchChangeFinished(changeInfo.oldHolder, true); + mChangeAnimations.remove(changeInfo.oldHolder); + dispatchFinishedWhenDone(); + } + }).start(); + } if (newView != null) { - mChangeAnimations.add(changeInfo.newHolder); final ViewPropertyAnimatorCompat newViewAnimation = ViewCompat.animate(newView); + mChangeAnimations.add(changeInfo.newHolder); newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()). alpha(1).setListener(new VpaListenerAdapter() { @Override @@ -427,7 +437,7 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { ViewCompat.setTranslationY(view, 0); ViewCompat.setTranslationX(view, 0); dispatchMoveFinished(item); - mPendingMoves.remove(item); + mPendingMoves.remove(i); } } endChangeAnimation(mPendingChanges, item); @@ -444,7 +454,7 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { ArrayList changes = mChangesList.get(i); endChangeAnimation(changes, item); if (changes.isEmpty()) { - mChangesList.remove(changes); + mChangesList.remove(i); } } for (int i = mMovesList.size() - 1; i >= 0; i--) { @@ -457,7 +467,7 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { dispatchMoveFinished(item); moves.remove(j); if (moves.isEmpty()) { - mMovesList.remove(moves); + mMovesList.remove(i); } break; } @@ -469,27 +479,31 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { ViewCompat.setAlpha(view, 1); dispatchAddFinished(item); if (additions.isEmpty()) { - mAdditionsList.remove(additions); + mAdditionsList.remove(i); } } } // animations should be ended by the cancel above. + //noinspection PointlessBooleanExpression,ConstantConditions if (mRemoveAnimations.remove(item) && DEBUG) { throw new IllegalStateException("after animation is cancelled, item should not be in " + "mRemoveAnimations list"); } + //noinspection PointlessBooleanExpression,ConstantConditions if (mAddAnimations.remove(item) && DEBUG) { throw new IllegalStateException("after animation is cancelled, item should not be in " + "mAddAnimations list"); } + //noinspection PointlessBooleanExpression,ConstantConditions if (mChangeAnimations.remove(item) && DEBUG) { throw new IllegalStateException("after animation is cancelled, item should not be in " + "mChangeAnimations list"); } + //noinspection PointlessBooleanExpression,ConstantConditions if (mMoveAnimations.remove(item) && DEBUG) { throw new IllegalStateException("after animation is cancelled, item should not be in " + "mMoveAnimations list"); @@ -497,6 +511,11 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { dispatchFinishedWhenDone(); } + private void resetAnimation(ViewHolder holder) { + AnimatorCompatHelper.clearInterpolator(holder.itemView); + endAnimation(holder); + } + @Override public boolean isRunning() { return (!mPendingAdditions.isEmpty() || @@ -615,6 +634,28 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { } } + /** + * {@inheritDoc} + *

+ * If the payload list is not empty, DefaultItemAnimator returns true. + * When this is the case: + *

    + *
  • If you override {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}, both + * ViewHolder arguments will be the same instance. + *
  • + *
  • + * If you are not overriding {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}, + * then DefaultItemAnimator will call {@link #animateMove(ViewHolder, int, int, int, int)} and + * run a move animation instead. + *
  • + *
+ */ + @Override + public boolean canReuseUpdatedViewHolder(@NonNull ViewHolder viewHolder, + @NonNull List payloads) { + return !payloads.isEmpty() || super.canReuseUpdatedViewHolder(viewHolder, payloads); + } + private static class VpaListenerAdapter implements ViewPropertyAnimatorListener { @Override public void onAnimationStart(View view) {} @@ -624,5 +665,5 @@ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { @Override public void onAnimationCancel(View view) {} - }; + } } diff --git a/app/src/main/java/android/support/v7/widget/GridLayoutManager.java b/app/src/main/java/android/support/v7/widget/GridLayoutManager.java index a05ac473e4..4d19163614 100644 --- a/app/src/main/java/android/support/v7/widget/GridLayoutManager.java +++ b/app/src/main/java/android/support/v7/widget/GridLayoutManager.java @@ -10,7 +10,7 @@ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific languag`e governing permissions and + * See the License for the specific language governing permissions and * limitations under the License. */ package android.support.v7.widget; @@ -38,16 +38,16 @@ public class GridLayoutManager extends LinearLayoutManager { private static final String TAG = "GridLayoutManager"; public static final int DEFAULT_SPAN_COUNT = -1; /** - * The measure spec for the scroll direction. + * Span size have been changed but we've not done a new layout calculation. */ - static final int MAIN_DIR_SPEC = - View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); - + boolean mPendingSpanCountChange = false; int mSpanCount = DEFAULT_SPAN_COUNT; /** - * The size of each span + * Right borders for each span. + *

For i-th item start is {@link #mCachedBorders}[i-1] + 1 + * and end is {@link #mCachedBorders}[i]. */ - int mSizePerSpan; + int [] mCachedBorders; /** * Temporary array to keep views in layoutChunk method */ @@ -58,6 +58,21 @@ public class GridLayoutManager extends LinearLayoutManager { // re-used variable to acquire decor insets from RecyclerView final Rect mDecorInsets = new Rect(); + + /** + * Constructor used when layout manager is set in XML by RecyclerView attribute + * "layoutManager". If spanCount is not specified in the XML, it defaults to a + * single column. + * + * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_spanCount + */ + public GridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, + int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + Properties properties = getProperties(context, attrs, defStyleAttr, defStyleRes); + setSpanCount(properties.spanCount); + } + /** * Creates a vertical GridLayoutManager * @@ -105,7 +120,9 @@ public class GridLayoutManager extends LinearLayoutManager { if (state.getItemCount() < 1) { return 0; } - return getSpanGroupIndex(recycler, state, state.getItemCount() - 1); + + // Row count is one more than the last item's row index. + return getSpanGroupIndex(recycler, state, state.getItemCount() - 1) + 1; } @Override @@ -117,7 +134,9 @@ public class GridLayoutManager extends LinearLayoutManager { if (state.getItemCount() < 1) { return 0; } - return getSpanGroupIndex(recycler, state, state.getItemCount() - 1); + + // Column count is one more than the last item's column index. + return getSpanGroupIndex(recycler, state, state.getItemCount() - 1) + 1; } @Override @@ -129,7 +148,7 @@ public class GridLayoutManager extends LinearLayoutManager { return; } LayoutParams glp = (LayoutParams) lp; - int spanGroupIndex = getSpanGroupIndex(recycler, state, glp.getViewPosition()); + int spanGroupIndex = getSpanGroupIndex(recycler, state, glp.getViewLayoutPosition()); if (mOrientation == HORIZONTAL) { info.setCollectionItemInfo(AccessibilityNodeInfoCompat.CollectionItemInfoCompat.obtain( glp.getSpanIndex(), glp.getSpanSize(), @@ -155,6 +174,12 @@ public class GridLayoutManager extends LinearLayoutManager { clearPreLayoutSpanMappingCache(); } + @Override + public void onLayoutCompleted(RecyclerView.State state) { + super.onLayoutCompleted(state); + mPendingSpanCountChange = false; + } + private void clearPreLayoutSpanMappingCache() { mPreLayoutSpanSizeCache.clear(); mPreLayoutSpanIndexCache.clear(); @@ -164,7 +189,7 @@ public class GridLayoutManager extends LinearLayoutManager { final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { final LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams(); - final int viewPosition = lp.getViewPosition(); + final int viewPosition = lp.getViewLayoutPosition(); mPreLayoutSpanSizeCache.put(viewPosition, lp.getSpanSize()); mPreLayoutSpanIndexCache.put(viewPosition, lp.getSpanIndex()); } @@ -186,7 +211,8 @@ public class GridLayoutManager extends LinearLayoutManager { } @Override - public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount) { + public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount, + Object payload) { mSpanSizeLookup.invalidateSpanIndexCache(); } @@ -197,8 +223,13 @@ public class GridLayoutManager extends LinearLayoutManager { @Override public RecyclerView.LayoutParams generateDefaultLayoutParams() { - return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, - ViewGroup.LayoutParams.WRAP_CONTENT); + if (mOrientation == HORIZONTAL) { + return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.MATCH_PARENT); + } else { + return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + } } @Override @@ -246,29 +277,174 @@ public class GridLayoutManager extends LinearLayoutManager { } else { totalSpace = getHeight() - getPaddingBottom() - getPaddingTop(); } - mSizePerSpan = totalSpace / mSpanCount; + calculateItemBorders(totalSpace); } @Override - void onAnchorReady(RecyclerView.State state, AnchorInfo anchorInfo) { - super.onAnchorReady(state, anchorInfo); + public void setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec) { + if (mCachedBorders == null) { + super.setMeasuredDimension(childrenBounds, wSpec, hSpec); + } + final int width, height; + final int horizontalPadding = getPaddingLeft() + getPaddingRight(); + final int verticalPadding = getPaddingTop() + getPaddingBottom(); + if (mOrientation == VERTICAL) { + final int usedHeight = childrenBounds.height() + verticalPadding; + height = chooseSize(hSpec, usedHeight, getMinimumHeight()); + width = chooseSize(wSpec, mCachedBorders[mCachedBorders.length - 1] + horizontalPadding, + getMinimumWidth()); + } else { + final int usedWidth = childrenBounds.width() + horizontalPadding; + width = chooseSize(wSpec, usedWidth, getMinimumWidth()); + height = chooseSize(hSpec, mCachedBorders[mCachedBorders.length - 1] + verticalPadding, + getMinimumHeight()); + } + setMeasuredDimension(width, height); + } + + /** + * @param totalSpace Total available space after padding is removed + */ + private void calculateItemBorders(int totalSpace) { + mCachedBorders = calculateItemBorders(mCachedBorders, mSpanCount, totalSpace); + } + + /** + * @param cachedBorders The out array + * @param spanCount number of spans + * @param totalSpace total available space after padding is removed + * @return The updated array. Might be the same instance as the provided array if its size + * has not changed. + */ + static int[] calculateItemBorders(int[] cachedBorders, int spanCount, int totalSpace) { + if (cachedBorders == null || cachedBorders.length != spanCount + 1 + || cachedBorders[cachedBorders.length - 1] != totalSpace) { + cachedBorders = new int[spanCount + 1]; + } + cachedBorders[0] = 0; + int sizePerSpan = totalSpace / spanCount; + int sizePerSpanRemainder = totalSpace % spanCount; + int consumedPixels = 0; + int additionalSize = 0; + for (int i = 1; i <= spanCount; i++) { + int itemSize = sizePerSpan; + additionalSize += sizePerSpanRemainder; + if (additionalSize > 0 && (spanCount - additionalSize) < sizePerSpanRemainder) { + itemSize += 1; + additionalSize -= spanCount; + } + consumedPixels += itemSize; + cachedBorders[i] = consumedPixels; + } + return cachedBorders; + } + + int getSpaceForSpanRange(int startSpan, int spanSize) { + if (mOrientation == VERTICAL && isLayoutRTL()) { + return mCachedBorders[mSpanCount - startSpan] + - mCachedBorders[mSpanCount - startSpan - spanSize]; + } else { + return mCachedBorders[startSpan + spanSize] - mCachedBorders[startSpan]; + } + } + + @Override + void onAnchorReady(RecyclerView.Recycler recycler, RecyclerView.State state, + AnchorInfo anchorInfo, int itemDirection) { + super.onAnchorReady(recycler, state, anchorInfo, itemDirection); updateMeasurements(); if (state.getItemCount() > 0 && !state.isPreLayout()) { - ensureAnchorIsInFirstSpan(anchorInfo); + ensureAnchorIsInCorrectSpan(recycler, state, anchorInfo, itemDirection); } + ensureViewSet(); + } + + private void ensureViewSet() { if (mSet == null || mSet.length != mSpanCount) { mSet = new View[mSpanCount]; } } - private void ensureAnchorIsInFirstSpan(AnchorInfo anchorInfo) { - int span = mSpanSizeLookup.getCachedSpanIndex(anchorInfo.mPosition, mSpanCount); - while (span > 0 && anchorInfo.mPosition > 0) { - anchorInfo.mPosition--; - span = mSpanSizeLookup.getCachedSpanIndex(anchorInfo.mPosition, mSpanCount); + @Override + public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, + RecyclerView.State state) { + updateMeasurements(); + ensureViewSet(); + return super.scrollHorizontallyBy(dx, recycler, state); + } + + @Override + public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, + RecyclerView.State state) { + updateMeasurements(); + ensureViewSet(); + return super.scrollVerticallyBy(dy, recycler, state); + } + + private void ensureAnchorIsInCorrectSpan(RecyclerView.Recycler recycler, + RecyclerView.State state, AnchorInfo anchorInfo, int itemDirection) { + final boolean layingOutInPrimaryDirection = + itemDirection == LayoutState.ITEM_DIRECTION_TAIL; + int span = getSpanIndex(recycler, state, anchorInfo.mPosition); + if (layingOutInPrimaryDirection) { + // choose span 0 + while (span > 0 && anchorInfo.mPosition > 0) { + anchorInfo.mPosition--; + span = getSpanIndex(recycler, state, anchorInfo.mPosition); + } + } else { + // choose the max span we can get. hopefully last one + final int indexLimit = state.getItemCount() - 1; + int pos = anchorInfo.mPosition; + int bestSpan = span; + while (pos < indexLimit) { + int next = getSpanIndex(recycler, state, pos + 1); + if (next > bestSpan) { + pos += 1; + bestSpan = next; + } else { + break; + } + } + anchorInfo.mPosition = pos; } } + @Override + View findReferenceChild(RecyclerView.Recycler recycler, RecyclerView.State state, + int start, int end, int itemCount) { + ensureLayoutState(); + View invalidMatch = null; + View outOfBoundsMatch = null; + final int boundsStart = mOrientationHelper.getStartAfterPadding(); + final int boundsEnd = mOrientationHelper.getEndAfterPadding(); + final int diff = end > start ? 1 : -1; + + for (int i = start; i != end; i += diff) { + final View view = getChildAt(i); + final int position = getPosition(view); + if (position >= 0 && position < itemCount) { + final int span = getSpanIndex(recycler, state, position); + if (span != 0) { + continue; + } + if (((RecyclerView.LayoutParams) view.getLayoutParams()).isItemRemoved()) { + if (invalidMatch == null) { + invalidMatch = view; // removed item, least preferred + } + } else if (mOrientationHelper.getDecoratedStart(view) >= boundsEnd || + mOrientationHelper.getDecoratedEnd(view) < boundsStart) { + if (outOfBoundsMatch == null) { + outOfBoundsMatch = view; // item is not visible, less preferred + } + } else { + return view; + } + } + } + return outOfBoundsMatch != null ? outOfBoundsMatch : invalidMatch; + } + private int getSpanGroupIndex(RecyclerView.Recycler recycler, RecyclerView.State state, int viewPosition) { if (!state.isPreLayout()) { @@ -331,6 +507,15 @@ public class GridLayoutManager extends LinearLayoutManager { @Override void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State state, LayoutState layoutState, LayoutChunkResult result) { + final int otherDirSpecMode = mOrientationHelper.getModeInOther(); + final boolean flexibleInOtherDir = otherDirSpecMode != View.MeasureSpec.EXACTLY; + final int currentOtherDirSize = getChildCount() > 0 ? mCachedBorders[mSpanCount] : 0; + // if grid layout's dimensions are not specified, let the new row change the measurements + // This is not perfect since we not covering all rows but still solves an important case + // where they may have a header row which should be laid out according to children. + if (flexibleInOtherDir) { + updateMeasurements(); // reset measurements + } final boolean layingOutInPrimaryDirection = layoutState.mItemDirection == LayoutState.ITEM_DIRECTION_TAIL; int count = 0; @@ -368,6 +553,7 @@ public class GridLayoutManager extends LinearLayoutManager { } int maxSize = 0; + float maxSizeInOther = 0; // use a float to get size per span // we should assign spans before item decor offsets are calculated assignSpans(recycler, state, count, consumedSpanCount, layingOutInPrimaryDirection); @@ -386,35 +572,61 @@ public class GridLayoutManager extends LinearLayoutManager { addDisappearingView(view, 0); } } + calculateItemDecorationsForChild(view, mDecorInsets); - int spanSize = getSpanSize(recycler, state, getPosition(view)); - final int spec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan * spanSize, - View.MeasureSpec.EXACTLY); - final LayoutParams lp = (LayoutParams) view.getLayoutParams(); - if (mOrientation == VERTICAL) { - measureChildWithDecorationsAndMargin(view, spec, getMainDirSpec(lp.height)); - } else { - measureChildWithDecorationsAndMargin(view, getMainDirSpec(lp.width), spec); - } + measureChild(view, otherDirSpecMode, false); final int size = mOrientationHelper.getDecoratedMeasurement(view); if (size > maxSize) { maxSize = size; } + final LayoutParams lp = (LayoutParams) view.getLayoutParams(); + final float otherSize = 1f * mOrientationHelper.getDecoratedMeasurementInOther(view) / + lp.mSpanSize; + if (otherSize > maxSizeInOther) { + maxSizeInOther = otherSize; + } + } + if (flexibleInOtherDir) { + // re-distribute columns + guessMeasurement(maxSizeInOther, currentOtherDirSize); + // now we should re-measure any item that was match parent. + maxSize = 0; + for (int i = 0; i < count; i++) { + View view = mSet[i]; + measureChild(view, View.MeasureSpec.EXACTLY, true); + final int size = mOrientationHelper.getDecoratedMeasurement(view); + if (size > maxSize) { + maxSize = size; + } + } } - // views that did not measure the maxSize has to be re-measured - final int maxMeasureSpec = getMainDirSpec(maxSize); + // Views that did not measure the maxSize has to be re-measured + // We will stop doing this once we introduce Gravity in the GLM layout params for (int i = 0; i < count; i ++) { final View view = mSet[i]; if (mOrientationHelper.getDecoratedMeasurement(view) != maxSize) { - int spanSize = getSpanSize(recycler, state, getPosition(view)); - final int spec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan * spanSize, - View.MeasureSpec.EXACTLY); + final LayoutParams lp = (LayoutParams) view.getLayoutParams(); + final Rect decorInsets = lp.mDecorInsets; + final int verticalInsets = decorInsets.top + decorInsets.bottom + + lp.topMargin + lp.bottomMargin; + final int horizontalInsets = decorInsets.left + decorInsets.right + + lp.leftMargin + lp.rightMargin; + final int totalSpaceInOther = getSpaceForSpanRange(lp.mSpanIndex, lp.mSpanSize); + final int wSpec; + final int hSpec; if (mOrientation == VERTICAL) { - measureChildWithDecorationsAndMargin(view, spec, maxMeasureSpec); + wSpec = getChildMeasureSpec(totalSpaceInOther, View.MeasureSpec.EXACTLY, + horizontalInsets, lp.width, false); + hSpec = View.MeasureSpec.makeMeasureSpec(maxSize - verticalInsets, + View.MeasureSpec.EXACTLY); } else { - measureChildWithDecorationsAndMargin(view, maxMeasureSpec, spec); + wSpec = View.MeasureSpec.makeMeasureSpec(maxSize - horizontalInsets, + View.MeasureSpec.EXACTLY); + hSpec = getChildMeasureSpec(totalSpaceInOther, View.MeasureSpec.EXACTLY, + verticalInsets, lp.height, false); } + measureChildWithDecorationsAndMargin(view, wSpec, hSpec, true); } } @@ -442,16 +654,20 @@ public class GridLayoutManager extends LinearLayoutManager { View view = mSet[i]; LayoutParams params = (LayoutParams) view.getLayoutParams(); if (mOrientation == VERTICAL) { - left = getPaddingLeft() + mSizePerSpan * params.mSpanIndex; - right = left + mOrientationHelper.getDecoratedMeasurementInOther(view); + if (isLayoutRTL()) { + right = getPaddingLeft() + mCachedBorders[mSpanCount - params.mSpanIndex]; + left = right - mOrientationHelper.getDecoratedMeasurementInOther(view); + } else { + left = getPaddingLeft() + mCachedBorders[params.mSpanIndex]; + right = left + mOrientationHelper.getDecoratedMeasurementInOther(view); + } } else { - top = getPaddingTop() + mSizePerSpan * params.mSpanIndex; + top = getPaddingTop() + mCachedBorders[params.mSpanIndex]; bottom = top + mOrientationHelper.getDecoratedMeasurementInOther(view); } // We calculate everything with View's bounding box (which includes decor and margins) // To calculate correct layout position, we subtract margins. - layoutDecorated(view, left + params.leftMargin, top + params.topMargin, - right - params.rightMargin, bottom - params.bottomMargin); + layoutDecoratedWithMargins(view, left, top, right, bottom); if (DEBUG) { Log.d(TAG, "laid out child at position " + getPosition(view) + ", with l:" + (left + params.leftMargin) + ", t:" + (top + params.topMargin) + ", r:" @@ -467,39 +683,74 @@ public class GridLayoutManager extends LinearLayoutManager { Arrays.fill(mSet, null); } - private int getMainDirSpec(int dim) { - if (dim < 0) { - return MAIN_DIR_SPEC; + /** + * Measures a child with currently known information. This is not necessarily the child's final + * measurement. (see fillChunk for details). + * + * @param view The child view to be measured + * @param otherDirParentSpecMode The RV measure spec that should be used in the secondary + * orientation + * @param alreadyMeasured True if we've already measured this view once + */ + private void measureChild(View view, int otherDirParentSpecMode, boolean alreadyMeasured) { + final LayoutParams lp = (LayoutParams) view.getLayoutParams(); + final Rect decorInsets = lp.mDecorInsets; + final int verticalInsets = decorInsets.top + decorInsets.bottom + + lp.topMargin + lp.bottomMargin; + final int horizontalInsets = decorInsets.left + decorInsets.right + + lp.leftMargin + lp.rightMargin; + final int availableSpaceInOther = getSpaceForSpanRange(lp.mSpanIndex, lp.mSpanSize); + final int wSpec; + final int hSpec; + if (mOrientation == VERTICAL) { + wSpec = getChildMeasureSpec(availableSpaceInOther, otherDirParentSpecMode, + horizontalInsets, lp.width, false); + hSpec = getChildMeasureSpec(mOrientationHelper.getTotalSpace(), getHeightMode(), + verticalInsets, lp.height, true); } else { - return View.MeasureSpec.makeMeasureSpec(dim, View.MeasureSpec.EXACTLY); + hSpec = getChildMeasureSpec(availableSpaceInOther, otherDirParentSpecMode, + verticalInsets, lp.height, false); + wSpec = getChildMeasureSpec(mOrientationHelper.getTotalSpace(), getWidthMode(), + horizontalInsets, lp.width, true); } + measureChildWithDecorationsAndMargin(view, wSpec, hSpec, alreadyMeasured); } - private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int heightSpec) { - calculateItemDecorationsForChild(child, mDecorInsets); + /** + * This is called after laying out a row (if vertical) or a column (if horizontal) when the + * RecyclerView does not have exact measurement specs. + *

+ * Here we try to assign a best guess width or height and re-do the layout to update other + * views that wanted to MATCH_PARENT in the non-scroll orientation. + * + * @param maxSizeInOther The maximum size per span ratio from the measurement of the children. + * @param currentOtherDirSize The size before this layout chunk. There is no reason to go below. + */ + private void guessMeasurement(float maxSizeInOther, int currentOtherDirSize) { + final int contentSize = Math.round(maxSizeInOther * mSpanCount); + // always re-calculate because borders were stretched during the fill + calculateItemBorders(Math.max(contentSize, currentOtherDirSize)); + } + + private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int heightSpec, + boolean alreadyMeasured) { RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams(); - widthSpec = updateSpecWithExtra(widthSpec, lp.leftMargin + mDecorInsets.left, - lp.rightMargin + mDecorInsets.right); - heightSpec = updateSpecWithExtra(heightSpec, lp.topMargin + mDecorInsets.top, - lp.bottomMargin + mDecorInsets.bottom); - child.measure(widthSpec, heightSpec); - } - - private int updateSpecWithExtra(int spec, int startInset, int endInset) { - if (startInset == 0 && endInset == 0) { - return spec; + final boolean measure; + if (alreadyMeasured) { + measure = shouldReMeasureChild(child, widthSpec, heightSpec, lp); + } else { + measure = shouldMeasureChild(child, widthSpec, heightSpec, lp); } - final int mode = View.MeasureSpec.getMode(spec); - if (mode == View.MeasureSpec.AT_MOST || mode == View.MeasureSpec.EXACTLY) { - return View.MeasureSpec.makeMeasureSpec( - View.MeasureSpec.getSize(spec) - startInset - endInset, mode); + if (measure) { + child.measure(widthSpec, heightSpec); } - return spec; } private void assignSpans(RecyclerView.Recycler recycler, RecyclerView.State state, int count, int consumedSpanCount, boolean layingOutInPrimaryDirection) { - int span, spanDiff, start, end, diff; + // spans are always assigned from 0 to N no matter if it is RTL or not. + // RTL is used only when positioning the view. + int span, start, end, diff; // make sure we traverse from min position to max position if (layingOutInPrimaryDirection) { start = 0; @@ -510,23 +761,13 @@ public class GridLayoutManager extends LinearLayoutManager { end = -1; diff = -1; } - if (mOrientation == VERTICAL && isLayoutRTL()) { // start from last span - span = consumedSpanCount - 1; - spanDiff = -1; - } else { - span = 0; - spanDiff = 1; - } + span = 0; for (int i = start; i != end; i += diff) { View view = mSet[i]; LayoutParams params = (LayoutParams) view.getLayoutParams(); params.mSpanSize = getSpanSize(recycler, state, getPosition(view)); - if (spanDiff == -1 && params.mSpanSize > 1) { - params.mSpanIndex = span - (params.mSpanSize - 1); - } else { - params.mSpanIndex = span; - } - span += spanDiff * params.mSpanSize; + params.mSpanIndex = span; + span += params.mSpanSize; } } @@ -553,12 +794,14 @@ public class GridLayoutManager extends LinearLayoutManager { if (spanCount == mSpanCount) { return; } + mPendingSpanCountChange = true; if (spanCount < 1) { throw new IllegalArgumentException("Span count should be at least 1. Provided " + spanCount); } mSpanCount = spanCount; mSpanSizeLookup.invalidateSpanIndexCache(); + requestLayout(); } /** @@ -730,9 +973,81 @@ public class GridLayoutManager extends LinearLayoutManager { } } + @Override + public View onFocusSearchFailed(View focused, int focusDirection, + RecyclerView.Recycler recycler, RecyclerView.State state) { + View prevFocusedChild = findContainingItemView(focused); + if (prevFocusedChild == null) { + return null; + } + LayoutParams lp = (LayoutParams) prevFocusedChild.getLayoutParams(); + final int prevSpanStart = lp.mSpanIndex; + final int prevSpanEnd = lp.mSpanIndex + lp.mSpanSize; + View view = super.onFocusSearchFailed(focused, focusDirection, recycler, state); + if (view == null) { + return null; + } + // LinearLayoutManager finds the last child. What we want is the child which has the same + // spanIndex. + final int layoutDir = convertFocusDirectionToLayoutDirection(focusDirection); + final boolean ascend = (layoutDir == LayoutState.LAYOUT_END) != mShouldReverseLayout; + final int start, inc, limit; + if (ascend) { + start = getChildCount() - 1; + inc = -1; + limit = -1; + } else { + start = 0; + inc = 1; + limit = getChildCount(); + } + final boolean preferLastSpan = mOrientation == VERTICAL && isLayoutRTL(); + View weakCandidate = null; // somewhat matches but not strong + int weakCandidateSpanIndex = -1; + int weakCandidateOverlap = 0; // how many spans overlap + + for (int i = start; i != limit; i += inc) { + View candidate = getChildAt(i); + if (candidate == prevFocusedChild) { + break; + } + if (!candidate.isFocusable()) { + continue; + } + final LayoutParams candidateLp = (LayoutParams) candidate.getLayoutParams(); + final int candidateStart = candidateLp.mSpanIndex; + final int candidateEnd = candidateLp.mSpanIndex + candidateLp.mSpanSize; + if (candidateStart == prevSpanStart && candidateEnd == prevSpanEnd) { + return candidate; // perfect match + } + boolean assignAsWeek = false; + if (weakCandidate == null) { + assignAsWeek = true; + } else { + int maxStart = Math.max(candidateStart, prevSpanStart); + int minEnd = Math.min(candidateEnd, prevSpanEnd); + int overlap = minEnd - maxStart; + if (overlap > weakCandidateOverlap) { + assignAsWeek = true; + } else if (overlap == weakCandidateOverlap && + preferLastSpan == (candidateStart > weakCandidateSpanIndex)) { + assignAsWeek = true; + } + } + + if (assignAsWeek) { + weakCandidate = candidate; + weakCandidateSpanIndex = candidateLp.mSpanIndex; + weakCandidateOverlap = Math.min(candidateEnd, prevSpanEnd) - + Math.max(candidateStart, prevSpanStart); + } + } + return weakCandidate; + } + @Override public boolean supportsPredictiveItemAnimations() { - return mPendingSavedState == null; + return mPendingSavedState == null && !mPendingSpanCountChange; } /** @@ -753,6 +1068,10 @@ public class GridLayoutManager extends LinearLayoutManager { /** * LayoutParams used by GridLayoutManager. + *

+ * Note that if the orientation is {@link #VERTICAL}, the width parameter is ignored and if the + * orientation is {@link #HORIZONTAL} the height parameter is ignored because child view is + * expected to fill all of the space given to it. */ public static class LayoutParams extends RecyclerView.LayoutParams { @@ -789,10 +1108,11 @@ public class GridLayoutManager extends LinearLayoutManager { * Returns the current span index of this View. If the View is not laid out yet, the return * value is undefined. *

- * Note that span index may change by whether the RecyclerView is RTL or not. For - * example, if the number of spans is 3 and layout is RTL, the rightmost item will have - * span index of 2. If the layout changes back to LTR, span index for this view will be 0. - * If the item was occupying 2 spans, span indices would be 1 and 0 respectively. + * Starting with RecyclerView 24.2.0, span indices are always indexed from position 0 + * even if the layout is RTL. In a vertical GridLayoutManager, leftmost span is span + * 0 if the layout is LTR and rightmost span is span 0 if the layout is + * RTL. Prior to 24.2.0, it was the opposite which was conflicting with + * {@link SpanSizeLookup#getSpanIndex(int, int)}. *

* If the View occupies multiple spans, span with the minimum index is returned. * diff --git a/app/src/main/java/android/support/v7/widget/LayoutState.java b/app/src/main/java/android/support/v7/widget/LayoutState.java index e62a80a289..23d8ee8940 100644 --- a/app/src/main/java/android/support/v7/widget/LayoutState.java +++ b/app/src/main/java/android/support/v7/widget/LayoutState.java @@ -10,11 +10,12 @@ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific languag`e governing permissions and + * See the License for the specific language governing permissions and * limitations under the License. */ package android.support.v7.widget; + import android.view.View; /** @@ -35,8 +36,11 @@ class LayoutState { final static int ITEM_DIRECTION_TAIL = 1; - final static int SCOLLING_OFFSET_NaN = Integer.MIN_VALUE; - + /** + * We may not want to recycle children in some cases (e.g. layout) + */ + boolean mRecycle = true; + /** * Number of pixels that we should fill, in the layout direction. */ @@ -60,11 +64,24 @@ class LayoutState { int mLayoutDirection; /** - * Used if you want to pre-layout items that are not yet visible. - * The difference with {@link #mAvailable} is that, when recycling, distance rendered for - * {@link #mExtra} is not considered not to recycle visible children. + * This is the target pixel closest to the start of the layout that we are trying to fill */ - int mExtra = 0; + int mStartLine = 0; + + /** + * This is the target pixel closest to the end of the layout that we are trying to fill + */ + int mEndLine = 0; + + /** + * If true, layout should stop if a focusable view is added + */ + boolean mStopInFocusable; + + /** + * If the content is not wrapped with any value + */ + boolean mInfinite; /** * @return true if there are more items in the data adapter @@ -84,4 +101,16 @@ class LayoutState { mCurrentPosition += mItemDirection; return view; } + + @Override + public String toString() { + return "LayoutState{" + + "mAvailable=" + mAvailable + + ", mCurrentPosition=" + mCurrentPosition + + ", mItemDirection=" + mItemDirection + + ", mLayoutDirection=" + mLayoutDirection + + ", mStartLine=" + mStartLine + + ", mEndLine=" + mEndLine + + '}'; + } } diff --git a/app/src/main/java/android/support/v7/widget/LinearLayoutManager.java b/app/src/main/java/android/support/v7/widget/LinearLayoutManager.java index 230db34523..5ee2537ffb 100644 --- a/app/src/main/java/android/support/v7/widget/LinearLayoutManager.java +++ b/app/src/main/java/android/support/v7/widget/LinearLayoutManager.java @@ -10,12 +10,14 @@ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific languag`e governing permissions and + * See the License for the specific language governing permissions and * limitations under the License. */ package android.support.v7.widget; +import static android.support.v7.widget.RecyclerView.NO_POSITION; + import android.content.Context; import android.graphics.PointF; import android.os.Parcel; @@ -23,6 +25,9 @@ import android.os.Parcelable; import android.support.v4.view.ViewCompat; import android.support.v4.view.accessibility.AccessibilityEventCompat; import android.support.v4.view.accessibility.AccessibilityRecordCompat; +import android.support.v7.widget.RecyclerView.LayoutParams; +import android.support.v7.widget.helper.ItemTouchHelper; +import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; @@ -30,13 +35,12 @@ import android.view.accessibility.AccessibilityEvent; import java.util.List; -import static android.support.v7.widget.RecyclerView.NO_POSITION; - /** - * A {@link RecyclerView.LayoutManager} implementation which provides + * A {@link android.support.v7.widget.RecyclerView.LayoutManager} implementation which provides * similar functionality to {@link android.widget.ListView}. */ -public class LinearLayoutManager extends RecyclerView.LayoutManager { +public class LinearLayoutManager extends RecyclerView.LayoutManager implements + ItemTouchHelper.ViewDropHandler, RecyclerView.SmoothScroller.ScrollVectorProvider { private static final String TAG = "LinearLayoutManager"; @@ -54,7 +58,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * than this factor times the total space of the list. If layout is vertical, total space is the * height minus padding, if layout is horizontal, total space is the width minus padding. */ - private static final float MAX_SCROLL_FACTOR = 0.33f; + private static final float MAX_SCROLL_FACTOR = 1 / 3f; /** @@ -130,7 +134,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * Re-used variable to keep anchor information on re-layout. * Anchor position and coordinate defines the reference point for LLM while doing a layout. * */ - final AnchorInfo mAnchorInfo; + final AnchorInfo mAnchorInfo = new AnchorInfo(); + + /** + * Stashed to avoid allocation, currently only used in #fill() + */ + private final LayoutChunkResult mLayoutChunkResult = new LayoutChunkResult(); /** * Creates a vertical LinearLayoutManager @@ -148,17 +157,34 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * @param reverseLayout When set to true, layouts from end to start. */ public LinearLayoutManager(Context context, int orientation, boolean reverseLayout) { - mAnchorInfo = new AnchorInfo(); setOrientation(orientation); setReverseLayout(reverseLayout); + setAutoMeasureEnabled(true); + } + + /** + * Constructor used when layout manager is set in XML by RecyclerView attribute + * "layoutManager". Defaults to vertical orientation. + * + * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_android_orientation + * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_reverseLayout + * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_stackFromEnd + */ + public LinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, + int defStyleRes) { + Properties properties = getProperties(context, attrs, defStyleAttr, defStyleRes); + setOrientation(properties.orientation); + setReverseLayout(properties.reverseLayout); + setStackFromEnd(properties.stackFromEnd); + setAutoMeasureEnabled(true); } /** * {@inheritDoc} */ @Override - public RecyclerView.LayoutParams generateDefaultLayoutParams() { - return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + public LayoutParams generateDefaultLayoutParams() { + return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); } @@ -178,7 +204,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * RecyclerView. *

* If you are using a {@link RecyclerView.RecycledViewPool}, it might be a good idea to set - * this flag to true so that views will be avilable to other RecyclerViews + * this flag to true so that views will be available to other RecyclerViews * immediately. *

* Note that, setting this flag will result in a performance drop if RecyclerView @@ -217,6 +243,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } SavedState state = new SavedState(); if (getChildCount() > 0) { + ensureLayoutState(); boolean didLayoutFromEnd = mLastStackFromEnd ^ mShouldReverseLayout; state.mAnchorLayoutFromEnd = didLayoutFromEnd; if (didLayoutFromEnd) { @@ -282,10 +309,9 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } /** - * Returns the current orientaion of the layout. + * Returns the current orientation of the layout. * - * @return Current orientation. - * @see #mOrientation + * @return Current orientation, either {@link #HORIZONTAL} or {@link #VERTICAL} * @see #setOrientation(int) */ public int getOrientation() { @@ -293,7 +319,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } /** - * Sets the orientation of the layout. {@link LinearLayoutManager} + * Sets the orientation of the layout. {@link android.support.v7.widget.LinearLayoutManager} * will do its best to keep scroll position. * * @param orientation {@link #HORIZONTAL} or {@link #VERTICAL} @@ -329,7 +355,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * Returns if views are laid out from the opposite direction of the layout. * * @return If layout is reversed or not. - * @see {@link #setReverseLayout(boolean)} + * @see #setReverseLayout(boolean) */ public boolean getReverseLayout() { return mReverseLayout; @@ -341,8 +367,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * laid out at the end of the UI, second item is laid out before it etc. * * For horizontal layouts, it depends on the layout direction. - * When set to true, If {@link RecyclerView} is LTR, than it will - * layout from RTL, if {@link RecyclerView}} is RTL, it will layout + * When set to true, If {@link android.support.v7.widget.RecyclerView} is LTR, than it will + * layout from RTL, if {@link android.support.v7.widget.RecyclerView}} is RTL, it will layout * from LTR. * * If you are looking for the exact same behavior of @@ -370,14 +396,18 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { final int firstChild = getPosition(getChildAt(0)); final int viewPosition = position - firstChild; if (viewPosition >= 0 && viewPosition < childCount) { - return getChildAt(viewPosition); + final View child = getChildAt(viewPosition); + if (getPosition(child) == position) { + return child; // in pre-layout, this may not match + } } - return null; + // fallback to traversal. This might be necessary in pre-layout. + return super.findViewByPosition(position); } /** *

Returns the amount of extra space that should be laid out by LayoutManager. - * By default, {@link LinearLayoutManager} lays out 1 extra page of + * By default, {@link android.support.v7.widget.LinearLayoutManager} lays out 1 extra page of * items while smooth scrolling and 0 otherwise. You can override this method to implement your * custom layout pre-cache logic.

*

Laying out invisible elements will eventually come with performance cost. On the other @@ -399,17 +429,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { LinearSmoothScroller linearSmoothScroller = - new LinearSmoothScroller(recyclerView.getContext()) { - @Override - public PointF computeScrollVectorForPosition(int targetPosition) { - return LinearLayoutManager.this - .computeScrollVectorForPosition(targetPosition); - } - }; + new LinearSmoothScroller(recyclerView.getContext()); linearSmoothScroller.setTargetPosition(position); startSmoothScroll(linearSmoothScroller); } + @Override public PointF computeScrollVectorForPosition(int targetPosition) { if (getChildCount() == 0) { return null; @@ -438,6 +463,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { if (DEBUG) { Log.d(TAG, "is pre layout:" + state.isPreLayout()); } + if (mPendingSavedState != null || mPendingScrollPosition != NO_POSITION) { + if (state.getItemCount() == 0) { + removeAndRecycleAllViews(recycler); + return; + } + } if (mPendingSavedState != null && mPendingSavedState.hasValidAnchor()) { mPendingScrollPosition = mPendingSavedState.mAnchorPosition; } @@ -447,10 +478,14 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { // resolve layout direction resolveShouldLayoutReverse(); - mAnchorInfo.reset(); - mAnchorInfo.mLayoutFromEnd = mShouldReverseLayout ^ mStackFromEnd; - // calculate anchor position and coordinate - updateAnchorInfoForLayout(state, mAnchorInfo); + if (!mAnchorInfo.mValid || mPendingScrollPosition != NO_POSITION || + mPendingSavedState != null) { + mAnchorInfo.reset(); + mAnchorInfo.mLayoutFromEnd = mShouldReverseLayout ^ mStackFromEnd; + // calculate anchor position and coordinate + updateAnchorInfoForLayout(recycler, state, mAnchorInfo); + mAnchorInfo.mValid = true; + } if (DEBUG) { Log.d(TAG, "Anchor info:" + mAnchorInfo); } @@ -460,8 +495,9 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { int extraForStart; int extraForEnd; final int extra = getExtraLayoutSpace(state); - boolean before = state.getTargetScrollPosition() < mAnchorInfo.mPosition; - if (before == mShouldReverseLayout) { + // If the previous scroll delta was less than zero, the extra space should be laid out + // at the start. Otherwise, it should be at the end. + if (mLayoutState.mLastScrollDelta >= 0) { extraForEnd = extra; extraForStart = 0; } else { @@ -497,8 +533,18 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } int startOffset; int endOffset; - onAnchorReady(state, mAnchorInfo); + final int firstLayoutDirection; + if (mAnchorInfo.mLayoutFromEnd) { + firstLayoutDirection = mShouldReverseLayout ? LayoutState.ITEM_DIRECTION_TAIL : + LayoutState.ITEM_DIRECTION_HEAD; + } else { + firstLayoutDirection = mShouldReverseLayout ? LayoutState.ITEM_DIRECTION_HEAD : + LayoutState.ITEM_DIRECTION_TAIL; + } + + onAnchorReady(recycler, state, mAnchorInfo, firstLayoutDirection); detachAndScrapAttachedViews(recycler); + mLayoutState.mInfinite = resolveIsInfinite(); mLayoutState.mIsPreLayout = state.isPreLayout(); if (mAnchorInfo.mLayoutFromEnd) { // fill towards start @@ -506,6 +552,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { mLayoutState.mExtra = extraForStart; fill(recycler, mLayoutState, state, false); startOffset = mLayoutState.mOffset; + final int firstElement = mLayoutState.mCurrentPosition; if (mLayoutState.mAvailable > 0) { extraForEnd += mLayoutState.mAvailable; } @@ -515,12 +562,22 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { mLayoutState.mCurrentPosition += mLayoutState.mItemDirection; fill(recycler, mLayoutState, state, false); endOffset = mLayoutState.mOffset; + + if (mLayoutState.mAvailable > 0) { + // end could not consume all. add more items towards start + extraForStart = mLayoutState.mAvailable; + updateLayoutStateToFillStart(firstElement, startOffset); + mLayoutState.mExtra = extraForStart; + fill(recycler, mLayoutState, state, false); + startOffset = mLayoutState.mOffset; + } } else { // fill towards end updateLayoutStateToFillEnd(mAnchorInfo); mLayoutState.mExtra = extraForEnd; fill(recycler, mLayoutState, state, false); endOffset = mLayoutState.mOffset; + final int lastElement = mLayoutState.mCurrentPosition; if (mLayoutState.mAvailable > 0) { extraForStart += mLayoutState.mAvailable; } @@ -530,6 +587,15 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { mLayoutState.mCurrentPosition += mLayoutState.mItemDirection; fill(recycler, mLayoutState, state, false); startOffset = mLayoutState.mOffset; + + if (mLayoutState.mAvailable > 0) { + extraForEnd = mLayoutState.mAvailable; + // start could not consume all it should. add more items towards end + updateLayoutStateToFillEnd(lastElement, endOffset); + mLayoutState.mExtra = extraForEnd; + fill(recycler, mLayoutState, state, false); + endOffset = mLayoutState.mOffset; + } } // changes may cause gaps on the UI, try to fix them. @@ -557,25 +623,36 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } layoutForPredictiveAnimations(recycler, state, startOffset, endOffset); if (!state.isPreLayout()) { - mPendingScrollPosition = NO_POSITION; - mPendingScrollPositionOffset = INVALID_OFFSET; mOrientationHelper.onLayoutComplete(); + } else { + mAnchorInfo.reset(); } mLastStackFromEnd = mStackFromEnd; - mPendingSavedState = null; // we don't need this anymore if (DEBUG) { validateChildOrder(); } } + @Override + public void onLayoutCompleted(RecyclerView.State state) { + super.onLayoutCompleted(state); + mPendingSavedState = null; // we don't need this anymore + mPendingScrollPosition = NO_POSITION; + mPendingScrollPositionOffset = INVALID_OFFSET; + mAnchorInfo.reset(); + } + /** * Method called when Anchor position is decided. Extending class can setup accordingly or * even update anchor info if necessary. - * - * @param state - * @param anchorInfo Simple data structure to keep anchor point information for the next layout + * @param recycler The recycler for the layout + * @param state The layout state + * @param anchorInfo The mutable POJO that keeps the position and offset. + * @param firstLayoutItemDirection The direction of the first layout filling in terms of adapter + * indices. */ - void onAnchorReady(RecyclerView.State state, AnchorInfo anchorInfo) { + void onAnchorReady(RecyclerView.Recycler recycler, RecyclerView.State state, + AnchorInfo anchorInfo, int firstLayoutItemDirection) { } /** @@ -591,7 +668,6 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { || !supportsPredictiveItemAnimations()) { return; } - // to make the logic simpler, we calculate the size of children and call fill. int scrapExtraStart = 0, scrapExtraEnd = 0; final List scrapList = recycler.getScrapList(); @@ -599,7 +675,10 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { final int firstChildPos = getPosition(getChildAt(0)); for (int i = 0; i < scrapSize; i++) { RecyclerView.ViewHolder scrap = scrapList.get(i); - final int position = scrap.getPosition(); + if (scrap.isRemoved()) { + continue; + } + final int position = scrap.getLayoutPosition(); final int direction = position < firstChildPos != mShouldReverseLayout ? LayoutState.LAYOUT_START : LayoutState.LAYOUT_END; if (direction == LayoutState.LAYOUT_START) { @@ -619,7 +698,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { updateLayoutStateToFillStart(getPosition(anchor), startOffset); mLayoutState.mExtra = scrapExtraStart; mLayoutState.mAvailable = 0; - mLayoutState.mCurrentPosition += mShouldReverseLayout ? 1 : -1; + mLayoutState.assignPositionFromScrapList(); fill(recycler, mLayoutState, state, false); } @@ -628,13 +707,14 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { updateLayoutStateToFillEnd(getPosition(anchor), endOffset); mLayoutState.mExtra = scrapExtraEnd; mLayoutState.mAvailable = 0; - mLayoutState.mCurrentPosition += mShouldReverseLayout ? -1 : 1; + mLayoutState.assignPositionFromScrapList(); fill(recycler, mLayoutState, state, false); } mLayoutState.mScrapList = null; } - private void updateAnchorInfoForLayout(RecyclerView.State state, AnchorInfo anchorInfo) { + private void updateAnchorInfoForLayout(RecyclerView.Recycler recycler, RecyclerView.State state, + AnchorInfo anchorInfo) { if (updateAnchorFromPendingData(state, anchorInfo)) { if (DEBUG) { Log.d(TAG, "updated anchor info from pending information"); @@ -642,7 +722,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { return; } - if (updateAnchorFromChildren(state, anchorInfo)) { + if (updateAnchorFromChildren(recycler, state, anchorInfo)) { if (DEBUG) { Log.d(TAG, "updated anchor info from existing children"); } @@ -661,24 +741,22 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { *

* If a child has focus, it is given priority. */ - private boolean updateAnchorFromChildren(RecyclerView.State state, AnchorInfo anchorInfo) { + private boolean updateAnchorFromChildren(RecyclerView.Recycler recycler, + RecyclerView.State state, AnchorInfo anchorInfo) { if (getChildCount() == 0) { return false; } - View focused = getFocusedChild(); - if (focused != null && anchorInfo.assignFromViewIfValid(focused, state)) { - if (DEBUG) { - Log.d(TAG, "decided anchor child from focused view"); - } + final View focused = getFocusedChild(); + if (focused != null && anchorInfo.isViewValidAsAnchor(focused, state)) { + anchorInfo.assignFromViewAndKeepVisibleRect(focused); return true; } - if (mLastStackFromEnd != mStackFromEnd) { return false; } - - View referenceChild = anchorInfo.mLayoutFromEnd ? findReferenceChildClosestToEnd(state) - : findReferenceChildClosestToStart(state); + View referenceChild = anchorInfo.mLayoutFromEnd + ? findReferenceChildClosestToEnd(recycler, state) + : findReferenceChildClosestToStart(recycler, state); if (referenceChild != null) { anchorInfo.assignFromView(referenceChild); // If all visible views are removed in 1 pass, reference child might be out of bounds. @@ -776,6 +854,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } // override layout from end values for consistency anchorInfo.mLayoutFromEnd = mShouldReverseLayout; + // if this changes, we should update prepareForDrop as well if (mShouldReverseLayout) { anchorInfo.mCoordinate = mOrientationHelper.getEndAfterPadding() - mPendingScrollPositionOffset; @@ -847,7 +926,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { mLayoutState.mCurrentPosition = itemPosition; mLayoutState.mLayoutDirection = LayoutState.LAYOUT_END; mLayoutState.mOffset = offset; - mLayoutState.mScrollingOffset = LayoutState.SCOLLING_OFFSET_NaN; + mLayoutState.mScrollingOffset = LayoutState.SCROLLING_OFFSET_NaN; } private void updateLayoutStateToFillStart(AnchorInfo anchorInfo) { @@ -861,7 +940,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { LayoutState.ITEM_DIRECTION_HEAD; mLayoutState.mLayoutDirection = LayoutState.LAYOUT_START; mLayoutState.mOffset = offset; - mLayoutState.mScrollingOffset = LayoutState.SCOLLING_OFFSET_NaN; + mLayoutState.mScrollingOffset = LayoutState.SCROLLING_OFFSET_NaN; } @@ -871,13 +950,22 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { void ensureLayoutState() { if (mLayoutState == null) { - mLayoutState = new LayoutState(); + mLayoutState = createLayoutState(); } if (mOrientationHelper == null) { mOrientationHelper = OrientationHelper.createOrientationHelper(this, mOrientation); } } + /** + * Test overrides this to plug some tracking and verification. + * + * @return A new LayoutState + */ + LayoutState createLayoutState() { + return new LayoutState(); + } + /** *

Scroll the RecyclerView to make the position visible.

* @@ -905,14 +993,13 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { /** * Scroll to the specified adapter position with the given offset from resolved layout * start. Resolved layout start depends on {@link #getReverseLayout()}, - * {@link ViewCompat#getLayoutDirection(View)} and {@link #getStackFromEnd()}. + * {@link ViewCompat#getLayoutDirection(android.view.View)} and {@link #getStackFromEnd()}. *

* For example, if layout is {@link #VERTICAL} and {@link #getStackFromEnd()} is true, calling * scrollToPositionWithOffset(10, 20) will layout such that * item[10]'s bottom is 20 pixels above the RecyclerView's bottom. *

* Note that scroll position change will not be reflected until the next layout call. - * *

* If you are just trying to make a position visible, use {@link #scrollToPosition(int)}. * @@ -990,27 +1077,33 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { if (getChildCount() == 0) { return 0; } + ensureLayoutState(); return ScrollbarHelper.computeScrollOffset(state, mOrientationHelper, - getChildClosestToStart(), getChildClosestToEnd(), this, - mSmoothScrollbarEnabled, mShouldReverseLayout); + findFirstVisibleChildClosestToStart(!mSmoothScrollbarEnabled, true), + findFirstVisibleChildClosestToEnd(!mSmoothScrollbarEnabled, true), + this, mSmoothScrollbarEnabled, mShouldReverseLayout); } private int computeScrollExtent(RecyclerView.State state) { if (getChildCount() == 0) { return 0; } + ensureLayoutState(); return ScrollbarHelper.computeScrollExtent(state, mOrientationHelper, - getChildClosestToStart(), getChildClosestToEnd(), this, - mSmoothScrollbarEnabled); + findFirstVisibleChildClosestToStart(!mSmoothScrollbarEnabled, true), + findFirstVisibleChildClosestToEnd(!mSmoothScrollbarEnabled, true), + this, mSmoothScrollbarEnabled); } private int computeScrollRange(RecyclerView.State state) { if (getChildCount() == 0) { return 0; } + ensureLayoutState(); return ScrollbarHelper.computeScrollRange(state, mOrientationHelper, - getChildClosestToStart(), getChildClosestToEnd(), this, - mSmoothScrollbarEnabled); + findFirstVisibleChildClosestToStart(!mSmoothScrollbarEnabled, true), + findFirstVisibleChildClosestToEnd(!mSmoothScrollbarEnabled, true), + this, mSmoothScrollbarEnabled); } /** @@ -1047,9 +1140,11 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { private void updateLayoutState(int layoutDirection, int requiredSpace, boolean canUseExistingSpace, RecyclerView.State state) { + // If parent provides a hint, don't measure unlimited. + mLayoutState.mInfinite = resolveIsInfinite(); mLayoutState.mExtra = getExtraLayoutSpace(state); mLayoutState.mLayoutDirection = layoutDirection; - int fastScrollSpace; + int scrollingOffset; if (layoutDirection == LayoutState.LAYOUT_END) { mLayoutState.mExtra += mOrientationHelper.getEndPadding(); // get the first child in the direction we are going @@ -1060,7 +1155,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { mLayoutState.mCurrentPosition = getPosition(child) + mLayoutState.mItemDirection; mLayoutState.mOffset = mOrientationHelper.getDecoratedEnd(child); // calculate how much we can scroll without adding new children (independent of layout) - fastScrollSpace = mOrientationHelper.getDecoratedEnd(child) + scrollingOffset = mOrientationHelper.getDecoratedEnd(child) - mOrientationHelper.getEndAfterPadding(); } else { @@ -1070,14 +1165,19 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { : LayoutState.ITEM_DIRECTION_HEAD; mLayoutState.mCurrentPosition = getPosition(child) + mLayoutState.mItemDirection; mLayoutState.mOffset = mOrientationHelper.getDecoratedStart(child); - fastScrollSpace = -mOrientationHelper.getDecoratedStart(child) + scrollingOffset = -mOrientationHelper.getDecoratedStart(child) + mOrientationHelper.getStartAfterPadding(); } mLayoutState.mAvailable = requiredSpace; if (canUseExistingSpace) { - mLayoutState.mAvailable -= fastScrollSpace; + mLayoutState.mAvailable -= scrollingOffset; } - mLayoutState.mScrollingOffset = fastScrollSpace; + mLayoutState.mScrollingOffset = scrollingOffset; + } + + boolean resolveIsInfinite() { + return mOrientationHelper.getMode() == View.MeasureSpec.UNSPECIFIED + && mOrientationHelper.getEnd() == 0; } int scrollBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { @@ -1089,8 +1189,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { final int layoutDirection = dy > 0 ? LayoutState.LAYOUT_END : LayoutState.LAYOUT_START; final int absDy = Math.abs(dy); updateLayoutState(layoutDirection, absDy, true, state); - final int freeScroll = mLayoutState.mScrollingOffset; - final int consumed = freeScroll + fill(recycler, mLayoutState, state, false); + final int consumed = mLayoutState.mScrollingOffset + + fill(recycler, mLayoutState, state, false); if (consumed < 0) { if (DEBUG) { Log.d(TAG, "Don't have any more elements to scroll"); @@ -1102,6 +1202,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { if (DEBUG) { Log.d(TAG, "scroll req: " + dy + " scrolled: " + scrolled); } + mLayoutState.mLastScrollDelta = scrolled; return scrolled; } @@ -1138,12 +1239,13 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { /** * Recycles views that went out of bounds after scrolling towards the end of the layout. + *

+ * Checks both layout position and visible position to guarantee that the view is not visible. * - * @param recycler Recycler instance of {@link RecyclerView} + * @param recycler Recycler instance of {@link android.support.v7.widget.RecyclerView} * @param dt This can be used to add additional padding to the visible area. This is used - * to - * detect children that will go out of bounds after scrolling, without actually - * moving them. + * to detect children that will go out of bounds after scrolling, without + * actually moving them. */ private void recycleViewsFromStart(RecyclerView.Recycler recycler, int dt) { if (dt < 0) { @@ -1159,7 +1261,9 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { if (mShouldReverseLayout) { for (int i = childCount - 1; i >= 0; i--) { View child = getChildAt(i); - if (mOrientationHelper.getDecoratedEnd(child) > limit) {// stop here + if (mOrientationHelper.getDecoratedEnd(child) > limit + || mOrientationHelper.getTransformedEndWithDecoration(child) > limit) { + // stop here recycleChildren(recycler, childCount - 1, i); return; } @@ -1167,7 +1271,9 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } else { for (int i = 0; i < childCount; i++) { View child = getChildAt(i); - if (mOrientationHelper.getDecoratedEnd(child) > limit) {// stop here + if (mOrientationHelper.getDecoratedEnd(child) > limit + || mOrientationHelper.getTransformedEndWithDecoration(child) > limit) { + // stop here recycleChildren(recycler, 0, i); return; } @@ -1178,8 +1284,10 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { /** * Recycles views that went out of bounds after scrolling towards the start of the layout. + *

+ * Checks both layout position and visible position to guarantee that the view is not visible. * - * @param recycler Recycler instance of {@link RecyclerView} + * @param recycler Recycler instance of {@link android.support.v7.widget.RecyclerView} * @param dt This can be used to add additional padding to the visible area. This is used * to detect children that will go out of bounds after scrolling, without * actually moving them. @@ -1197,7 +1305,9 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { if (mShouldReverseLayout) { for (int i = 0; i < childCount; i++) { View child = getChildAt(i); - if (mOrientationHelper.getDecoratedStart(child) < limit) {// stop here + if (mOrientationHelper.getDecoratedStart(child) < limit + || mOrientationHelper.getTransformedStartWithDecoration(child) < limit) { + // stop here recycleChildren(recycler, 0, i); return; } @@ -1205,7 +1315,9 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } else { for (int i = childCount - 1; i >= 0; i--) { View child = getChildAt(i); - if (mOrientationHelper.getDecoratedStart(child) < limit) {// stop here + if (mOrientationHelper.getDecoratedStart(child) < limit + || mOrientationHelper.getTransformedStartWithDecoration(child) < limit) { + // stop here recycleChildren(recycler, childCount - 1, i); return; } @@ -1221,12 +1333,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * @param layoutState Current layout state. Right now, this object does not change but * we may consider moving it out of this view so passing around as a * parameter for now, rather than accessing {@link #mLayoutState} - * @see #recycleViewsFromStart(RecyclerView.Recycler, int) - * @see #recycleViewsFromEnd(RecyclerView.Recycler, int) - * @see LayoutState#mLayoutDirection + * @see #recycleViewsFromStart(android.support.v7.widget.RecyclerView.Recycler, int) + * @see #recycleViewsFromEnd(android.support.v7.widget.RecyclerView.Recycler, int) + * @see android.support.v7.widget.LinearLayoutManager.LayoutState#mLayoutDirection */ private void recycleByLayoutState(RecyclerView.Recycler recycler, LayoutState layoutState) { - if (!layoutState.mRecycle) { + if (!layoutState.mRecycle || layoutState.mInfinite) { return; } if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) { @@ -1238,20 +1350,20 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { /** * The magic functions :). Fills the given layout, defined by the layoutState. This is fairly - * independent from the rest of the {@link LinearLayoutManager} + * independent from the rest of the {@link android.support.v7.widget.LinearLayoutManager} * and with little change, can be made publicly available as a helper class. * * @param recycler Current recycler that is attached to RecyclerView * @param layoutState Configuration on how we should fill out the available space. * @param state Context passed by the RecyclerView to control scroll steps. * @param stopOnFocusable If true, filling stops in the first focusable new child - * @return Number of pixels that it added. Useful for scoll functions. + * @return Number of pixels that it added. Useful for scroll functions. */ int fill(RecyclerView.Recycler recycler, LayoutState layoutState, RecyclerView.State state, boolean stopOnFocusable) { // max offset we should set is mFastScroll + available final int start = layoutState.mAvailable; - if (layoutState.mScrollingOffset != LayoutState.SCOLLING_OFFSET_NaN) { + if (layoutState.mScrollingOffset != LayoutState.SCROLLING_OFFSET_NaN) { // TODO ugly bug fix. should not happen if (layoutState.mAvailable < 0) { layoutState.mScrollingOffset += layoutState.mAvailable; @@ -1259,8 +1371,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { recycleByLayoutState(recycler, layoutState); } int remainingSpace = layoutState.mAvailable + layoutState.mExtra; - LayoutChunkResult layoutChunkResult = new LayoutChunkResult(); - while (remainingSpace > 0 && layoutState.hasMore(state)) { + LayoutChunkResult layoutChunkResult = mLayoutChunkResult; + while ((layoutState.mInfinite || remainingSpace > 0) && layoutState.hasMore(state)) { layoutChunkResult.resetInternal(); layoutChunk(recycler, state, layoutState, layoutChunkResult); if (layoutChunkResult.mFinished) { @@ -1280,7 +1392,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { remainingSpace -= layoutChunkResult.mConsumed; } - if (layoutState.mScrollingOffset != LayoutState.SCOLLING_OFFSET_NaN) { + if (layoutState.mScrollingOffset != LayoutState.SCROLLING_OFFSET_NaN) { layoutState.mScrollingOffset += layoutChunkResult.mConsumed; if (layoutState.mAvailable < 0) { layoutState.mScrollingOffset += layoutState.mAvailable; @@ -1309,7 +1421,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { result.mFinished = true; return; } - RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams(); + LayoutParams params = (LayoutParams) view.getLayoutParams(); if (layoutState.mScrapList == null) { if (mShouldReverseLayout == (layoutState.mLayoutDirection == LayoutState.LAYOUT_START)) { @@ -1357,8 +1469,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } // We calculate everything with View's bounding box (which includes decor and margins) // To calculate correct layout position, we subtract margins. - layoutDecorated(view, left + params.leftMargin, top + params.topMargin, - right - params.rightMargin, bottom - params.bottomMargin); + layoutDecoratedWithMargins(view, left, top, right, bottom); if (DEBUG) { Log.d(TAG, "laid out child at position " + getPosition(view) + ", with l:" + (left + params.leftMargin) + ", t:" + (top + params.topMargin) + ", r:" @@ -1371,6 +1482,13 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { result.mFocusable = view.isFocusable(); } + @Override + boolean shouldMeasureTwice() { + return getHeightMode() != View.MeasureSpec.EXACTLY + && getWidthMode() != View.MeasureSpec.EXACTLY + && hasFlexibleChildInBothOrientations(); + } + /** * Converts a focusDirection to orientation. * @@ -1381,12 +1499,24 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * @return {@link LayoutState#LAYOUT_START} or {@link LayoutState#LAYOUT_END} if focus direction * is applicable to current state, {@link LayoutState#INVALID_LAYOUT} otherwise. */ - private int convertFocusDirectionToLayoutDirection(int focusDirection) { + int convertFocusDirectionToLayoutDirection(int focusDirection) { switch (focusDirection) { case View.FOCUS_BACKWARD: - return LayoutState.LAYOUT_START; + if (mOrientation == VERTICAL) { + return LayoutState.LAYOUT_START; + } else if (isLayoutRTL()) { + return LayoutState.LAYOUT_END; + } else { + return LayoutState.LAYOUT_START; + } case View.FOCUS_FORWARD: - return LayoutState.LAYOUT_END; + if (mOrientation == VERTICAL) { + return LayoutState.LAYOUT_END; + } else if (isLayoutRTL()) { + return LayoutState.LAYOUT_START; + } else { + return LayoutState.LAYOUT_END; + } case View.FOCUS_UP: return mOrientation == VERTICAL ? LayoutState.LAYOUT_START : LayoutState.INVALID_LAYOUT; @@ -1428,6 +1558,42 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { return getChildAt(mShouldReverseLayout ? 0 : getChildCount() - 1); } + /** + * Convenience method to find the visible child closes to start. Caller should check if it has + * enough children. + * + * @param completelyVisible Whether child should be completely visible or not + * @return The first visible child closest to start of the layout from user's perspective. + */ + private View findFirstVisibleChildClosestToStart(boolean completelyVisible, + boolean acceptPartiallyVisible) { + if (mShouldReverseLayout) { + return findOneVisibleChild(getChildCount() - 1, -1, completelyVisible, + acceptPartiallyVisible); + } else { + return findOneVisibleChild(0, getChildCount(), completelyVisible, + acceptPartiallyVisible); + } + } + + /** + * Convenience method to find the visible child closes to end. Caller should check if it has + * enough children. + * + * @param completelyVisible Whether child should be completely visible or not + * @return The first visible child closest to end of the layout from user's perspective. + */ + private View findFirstVisibleChildClosestToEnd(boolean completelyVisible, + boolean acceptPartiallyVisible) { + if (mShouldReverseLayout) { + return findOneVisibleChild(0, getChildCount(), completelyVisible, + acceptPartiallyVisible); + } else { + return findOneVisibleChild(getChildCount() - 1, -1, completelyVisible, + acceptPartiallyVisible); + } + } + /** * Among the children that are suitable to be considered as an anchor child, returns the one @@ -1439,9 +1605,10 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * It also prioritizes children that are within the visible bounds. * @return A View that can be used an an anchor View. */ - private View findReferenceChildClosestToEnd(RecyclerView.State state) { - return mShouldReverseLayout ? findFirstReferenceChild(state.getItemCount()) : - findLastReferenceChild(state.getItemCount()); + private View findReferenceChildClosestToEnd(RecyclerView.Recycler recycler, + RecyclerView.State state) { + return mShouldReverseLayout ? findFirstReferenceChild(recycler, state) : + findLastReferenceChild(recycler, state); } /** @@ -1455,20 +1622,24 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * * @return A View that can be used an an anchor View. */ - private View findReferenceChildClosestToStart(RecyclerView.State state) { - return mShouldReverseLayout ? findLastReferenceChild(state.getItemCount()) : - findFirstReferenceChild(state.getItemCount()); + private View findReferenceChildClosestToStart(RecyclerView.Recycler recycler, + RecyclerView.State state) { + return mShouldReverseLayout ? findLastReferenceChild(recycler, state) : + findFirstReferenceChild(recycler, state); } - private View findFirstReferenceChild(int itemCount) { - return findReferenceChild(0, getChildCount(), itemCount); + private View findFirstReferenceChild(RecyclerView.Recycler recycler, RecyclerView.State state) { + return findReferenceChild(recycler, state, 0, getChildCount(), state.getItemCount()); } - private View findLastReferenceChild(int itemCount) { - return findReferenceChild(getChildCount() - 1, -1, itemCount); + private View findLastReferenceChild(RecyclerView.Recycler recycler, RecyclerView.State state) { + return findReferenceChild(recycler, state, getChildCount() - 1, -1, state.getItemCount()); } - private View findReferenceChild(int start, int end, int itemCount) { + // overridden by GridLayoutManager + View findReferenceChild(RecyclerView.Recycler recycler, RecyclerView.State state, + int start, int end, int itemCount) { + ensureLayoutState(); View invalidMatch = null; View outOfBoundsMatch = null; final int boundsStart = mOrientationHelper.getStartAfterPadding(); @@ -1478,7 +1649,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { final View view = getChildAt(i); final int position = getPosition(view); if (position >= 0 && position < itemCount) { - if (((RecyclerView.LayoutParams) view.getLayoutParams()).isItemRemoved()) { + if (((LayoutParams) view.getLayoutParams()).isItemRemoved()) { if (invalidMatch == null) { invalidMatch = view; // removed item, least preferred } @@ -1496,7 +1667,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } /** - * Returns the adapter position of the first visible view. + * Returns the adapter position of the first visible view. This position does not include + * adapter changes that were dispatched after the last layout pass. *

* Note that, this value is not affected by layout orientation or item order traversal. * ({@link #setReverseLayout(boolean)}). Views are sorted by their positions in the adapter, @@ -1513,12 +1685,13 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * @see #findLastVisibleItemPosition() */ public int findFirstVisibleItemPosition() { - final View child = findOneVisibleChild(0, getChildCount(), false); + final View child = findOneVisibleChild(0, getChildCount(), false, true); return child == null ? NO_POSITION : getPosition(child); } /** - * Returns the adapter position of the first fully visible view. + * Returns the adapter position of the first fully visible view. This position does not include + * adapter changes that were dispatched after the last layout pass. *

* Note that bounds check is only performed in the current orientation. That means, if * LayoutManager is horizontal, it will only check the view's left and right edges. @@ -1529,12 +1702,13 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * @see #findLastCompletelyVisibleItemPosition() */ public int findFirstCompletelyVisibleItemPosition() { - final View child = findOneVisibleChild(0, getChildCount(), true); + final View child = findOneVisibleChild(0, getChildCount(), true, false); return child == null ? NO_POSITION : getPosition(child); } /** - * Returns the adapter position of the last visible view. + * Returns the adapter position of the last visible view. This position does not include + * adapter changes that were dispatched after the last layout pass. *

* Note that, this value is not affected by layout orientation or item order traversal. * ({@link #setReverseLayout(boolean)}). Views are sorted by their positions in the adapter, @@ -1551,12 +1725,13 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * @see #findFirstVisibleItemPosition() */ public int findLastVisibleItemPosition() { - final View child = findOneVisibleChild(getChildCount() - 1, -1, false); + final View child = findOneVisibleChild(getChildCount() - 1, -1, false, true); return child == null ? NO_POSITION : getPosition(child); } /** - * Returns the adapter position of the last fully visible view. + * Returns the adapter position of the last fully visible view. This position does not include + * adapter changes that were dispatched after the last layout pass. *

* Note that bounds check is only performed in the current orientation. That means, if * LayoutManager is horizontal, it will only check the view's left and right edges. @@ -1567,14 +1742,17 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { * @see #findFirstCompletelyVisibleItemPosition() */ public int findLastCompletelyVisibleItemPosition() { - final View child = findOneVisibleChild(getChildCount() - 1, -1, true); + final View child = findOneVisibleChild(getChildCount() - 1, -1, true, false); return child == null ? NO_POSITION : getPosition(child); } - View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible) { + View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible, + boolean acceptPartiallyVisible) { + ensureLayoutState(); final int start = mOrientationHelper.getStartAfterPadding(); final int end = mOrientationHelper.getEndAfterPadding(); final int next = toIndex > fromIndex ? 1 : -1; + View partiallyVisible = null; for (int i = fromIndex; i != toIndex; i+=next) { final View child = getChildAt(i); final int childStart = mOrientationHelper.getDecoratedStart(child); @@ -1583,13 +1761,15 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { if (completelyVisible) { if (childStart >= start && childEnd <= end) { return child; + } else if (acceptPartiallyVisible && partiallyVisible == null) { + partiallyVisible = child; } } else { return child; } } } - return null; + return partiallyVisible; } @Override @@ -1604,11 +1784,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { if (layoutDir == LayoutState.INVALID_LAYOUT) { return null; } + ensureLayoutState(); final View referenceChild; if (layoutDir == LayoutState.LAYOUT_START) { - referenceChild = findReferenceChildClosestToStart(state); + referenceChild = findReferenceChildClosestToStart(recycler, state); } else { - referenceChild = findReferenceChildClosestToEnd(state); + referenceChild = findReferenceChildClosestToEnd(recycler, state); } if (referenceChild == null) { if (DEBUG) { @@ -1620,7 +1801,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { ensureLayoutState(); final int maxScroll = (int) (MAX_SCROLL_FACTOR * mOrientationHelper.getTotalSpace()); updateLayoutState(layoutDir, maxScroll, false, state); - mLayoutState.mScrollingOffset = LayoutState.SCOLLING_OFFSET_NaN; + mLayoutState.mScrollingOffset = LayoutState.SCROLLING_OFFSET_NaN; mLayoutState.mRecycle = false; fill(recycler, mLayoutState, state, true); final View nextFocus; @@ -1704,13 +1885,47 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { return mPendingSavedState == null && mLastStackFromEnd == mStackFromEnd; } + /** + * @hide This method should be called by ItemTouchHelper only. + */ + @Override + public void prepareForDrop(View view, View target, int x, int y) { + assertNotInLayoutOrScroll("Cannot drop a view during a scroll or layout calculation"); + ensureLayoutState(); + resolveShouldLayoutReverse(); + final int myPos = getPosition(view); + final int targetPos = getPosition(target); + final int dropDirection = myPos < targetPos ? LayoutState.ITEM_DIRECTION_TAIL : + LayoutState.ITEM_DIRECTION_HEAD; + if (mShouldReverseLayout) { + if (dropDirection == LayoutState.ITEM_DIRECTION_TAIL) { + scrollToPositionWithOffset(targetPos, + mOrientationHelper.getEndAfterPadding() - + (mOrientationHelper.getDecoratedStart(target) + + mOrientationHelper.getDecoratedMeasurement(view))); + } else { + scrollToPositionWithOffset(targetPos, + mOrientationHelper.getEndAfterPadding() - + mOrientationHelper.getDecoratedEnd(target)); + } + } else { + if (dropDirection == LayoutState.ITEM_DIRECTION_HEAD) { + scrollToPositionWithOffset(targetPos, mOrientationHelper.getDecoratedStart(target)); + } else { + scrollToPositionWithOffset(targetPos, + mOrientationHelper.getDecoratedEnd(target) - + mOrientationHelper.getDecoratedMeasurement(view)); + } + } + } + /** * Helper class that keeps temporary state while {LayoutManager} is filling out the empty * space. */ static class LayoutState { - final static String TAG = "LinearLayoutManager#LayoutState"; + final static String TAG = "LLM#LayoutState"; final static int LAYOUT_START = -1; @@ -1722,7 +1937,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { final static int ITEM_DIRECTION_TAIL = 1; - final static int SCOLLING_OFFSET_NaN = Integer.MIN_VALUE; + final static int SCROLLING_OFFSET_NaN = Integer.MIN_VALUE; /** * We may not want to recycle children in some cases (e.g. layout) @@ -1777,12 +1992,23 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { */ boolean mIsPreLayout = false; + /** + * The most recent {@link #scrollBy(int, RecyclerView.Recycler, RecyclerView.State)} + * amount. + */ + int mLastScrollDelta; + /** * When LLM needs to layout particular views, it sets this list in which case, LayoutState * will only return views from this list and return null if it cannot find an item. */ List mScrapList = null; + /** + * Used when there is no limit in how many views can be laid out. + */ + boolean mInfinite; + /** * @return true if there are more items in the data adapter */ @@ -1798,7 +2024,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { */ View next(RecyclerView.Recycler recycler) { if (mScrapList != null) { - return nextFromLimitedList(); + return nextViewFromScrapList(); } final View view = recycler.getViewForPosition(mCurrentPosition); mCurrentPosition += mItemDirection; @@ -1806,41 +2032,69 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } /** - * Returns next item from limited list. + * Returns the next item from the scrap list. *

* Upon finding a valid VH, sets current item position to VH.itemPosition + mItemDirection * * @return View if an item in the current position or direction exists if not null. */ - private View nextFromLimitedList() { - int size = mScrapList.size(); - RecyclerView.ViewHolder closest = null; - int closestDistance = Integer.MAX_VALUE; + private View nextViewFromScrapList() { + final int size = mScrapList.size(); for (int i = 0; i < size; i++) { - RecyclerView.ViewHolder viewHolder = mScrapList.get(i); - if (!mIsPreLayout && viewHolder.isRemoved()) { + final View view = mScrapList.get(i).itemView; + final LayoutParams lp = (LayoutParams) view.getLayoutParams(); + if (lp.isItemRemoved()) { continue; } - final int distance = (viewHolder.getPosition() - mCurrentPosition) * mItemDirection; + if (mCurrentPosition == lp.getViewLayoutPosition()) { + assignPositionFromScrapList(view); + return view; + } + } + return null; + } + + public void assignPositionFromScrapList() { + assignPositionFromScrapList(null); + } + + public void assignPositionFromScrapList(View ignore) { + final View closest = nextViewInLimitedList(ignore); + if (closest == null) { + mCurrentPosition = NO_POSITION; + } else { + mCurrentPosition = ((LayoutParams) closest.getLayoutParams()) + .getViewLayoutPosition(); + } + } + + public View nextViewInLimitedList(View ignore) { + int size = mScrapList.size(); + View closest = null; + int closestDistance = Integer.MAX_VALUE; + if (DEBUG && mIsPreLayout) { + throw new IllegalStateException("Scrap list cannot be used in pre layout"); + } + for (int i = 0; i < size; i++) { + View view = mScrapList.get(i).itemView; + final LayoutParams lp = (LayoutParams) view.getLayoutParams(); + if (view == ignore || lp.isItemRemoved()) { + continue; + } + final int distance = (lp.getViewLayoutPosition() - mCurrentPosition) * + mItemDirection; if (distance < 0) { continue; // item is not in current direction } if (distance < closestDistance) { - closest = viewHolder; + closest = view; closestDistance = distance; if (distance == 0) { break; } } } - if (DEBUG) { - Log.d(TAG, "layout from scrap. found view:?" + (closest != null)); - } - if (closest != null) { - mCurrentPosition = closest.getPosition() + mItemDirection; - return closest.itemView; - } - return null; + return closest; } void log() { @@ -1849,7 +2103,10 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { } } - static class SavedState implements Parcelable { + /** + * @hide + */ + public static class SavedState implements Parcelable { int mAnchorPosition; @@ -1893,8 +2150,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { dest.writeInt(mAnchorLayoutFromEnd ? 1 : 0); } - public static final Creator CREATOR - = new Creator() { + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { @Override public SavedState createFromParcel(Parcel in) { return new SavedState(in); @@ -1914,10 +2171,17 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { int mPosition; int mCoordinate; boolean mLayoutFromEnd; + boolean mValid; + + AnchorInfo() { + reset(); + } + void reset() { mPosition = NO_POSITION; mCoordinate = INVALID_OFFSET; mLayoutFromEnd = false; + mValid = false; } /** @@ -1936,21 +2200,61 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager { "mPosition=" + mPosition + ", mCoordinate=" + mCoordinate + ", mLayoutFromEnd=" + mLayoutFromEnd + + ", mValid=" + mValid + '}'; } - /** - * Assign anchor position information from the provided view if it is valid as a reference - * child. - */ - public boolean assignFromViewIfValid(View child, RecyclerView.State state) { - RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams(); - if (!lp.isItemRemoved() && lp.getViewPosition() >= 0 - && lp.getViewPosition() < state.getItemCount()) { + private boolean isViewValidAsAnchor(View child, RecyclerView.State state) { + LayoutParams lp = (LayoutParams) child.getLayoutParams(); + return !lp.isItemRemoved() && lp.getViewLayoutPosition() >= 0 + && lp.getViewLayoutPosition() < state.getItemCount(); + } + + public void assignFromViewAndKeepVisibleRect(View child) { + final int spaceChange = mOrientationHelper.getTotalSpaceChange(); + if (spaceChange >= 0) { assignFromView(child); - return true; + return; + } + mPosition = getPosition(child); + if (mLayoutFromEnd) { + final int prevLayoutEnd = mOrientationHelper.getEndAfterPadding() - spaceChange; + final int childEnd = mOrientationHelper.getDecoratedEnd(child); + final int previousEndMargin = prevLayoutEnd - childEnd; + mCoordinate = mOrientationHelper.getEndAfterPadding() - previousEndMargin; + // ensure we did not push child's top out of bounds because of this + if (previousEndMargin > 0) {// we have room to shift bottom if necessary + final int childSize = mOrientationHelper.getDecoratedMeasurement(child); + final int estimatedChildStart = mCoordinate - childSize; + final int layoutStart = mOrientationHelper.getStartAfterPadding(); + final int previousStartMargin = mOrientationHelper.getDecoratedStart(child) - + layoutStart; + final int startReference = layoutStart + Math.min(previousStartMargin, 0); + final int startMargin = estimatedChildStart - startReference; + if (startMargin < 0) { + // offset to make top visible but not too much + mCoordinate += Math.min(previousEndMargin, -startMargin); + } + } + } else { + final int childStart = mOrientationHelper.getDecoratedStart(child); + final int startMargin = childStart - mOrientationHelper.getStartAfterPadding(); + mCoordinate = childStart; + if (startMargin > 0) { // we have room to fix end as well + final int estimatedEnd = childStart + + mOrientationHelper.getDecoratedMeasurement(child); + final int previousLayoutEnd = mOrientationHelper.getEndAfterPadding() - + spaceChange; + final int previousEndMargin = previousLayoutEnd - + mOrientationHelper.getDecoratedEnd(child); + final int endReference = mOrientationHelper.getEndAfterPadding() - + Math.min(0, previousEndMargin); + final int endMargin = endReference - estimatedEnd; + if (endMargin < 0) { + mCoordinate -= Math.min(startMargin, -endMargin); + } + } } - return false; } public void assignFromView(View child) { diff --git a/app/src/main/java/android/support/v7/widget/LinearSmoothScroller.java b/app/src/main/java/android/support/v7/widget/LinearSmoothScroller.java index bdf2803d73..78250c1496 100644 --- a/app/src/main/java/android/support/v7/widget/LinearSmoothScroller.java +++ b/app/src/main/java/android/support/v7/widget/LinearSmoothScroller.java @@ -18,6 +18,7 @@ package android.support.v7.widget; import android.content.Context; import android.graphics.PointF; +import android.support.annotation.Nullable; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; @@ -25,12 +26,16 @@ import android.view.animation.DecelerateInterpolator; import android.view.animation.LinearInterpolator; /** - * {@link RecyclerView.SmoothScroller} implementation which uses - * {@link LinearInterpolator} until the target position becames a child of - * the RecyclerView and then uses + * {@link RecyclerView.SmoothScroller} implementation which uses a {@link LinearInterpolator} until + * the target position becomes a child of the RecyclerView and then uses a * {@link DecelerateInterpolator} to slowly approach to target position. + *

+ * If the {@link RecyclerView.LayoutManager} you are using does not implement the + * {@link RecyclerView.SmoothScroller.ScrollVectorProvider} interface, then you must override the + * {@link #computeScrollVectorForPosition(int)} method. All the LayoutManagers bundled with + * the support library implement this interface. */ -abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { +public class LinearSmoothScroller extends RecyclerView.SmoothScroller { private static final String TAG = "LinearSmoothScroller"; @@ -44,8 +49,8 @@ abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { * Align child view's left or top with parent view's left or top * * @see #calculateDtToFit(int, int, int, int, int) - * @see #calculateDxToMakeVisible(View, int) - * @see #calculateDyToMakeVisible(View, int) + * @see #calculateDxToMakeVisible(android.view.View, int) + * @see #calculateDyToMakeVisible(android.view.View, int) */ public static final int SNAP_TO_START = -1; @@ -53,8 +58,8 @@ abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { * Align child view's right or bottom with parent view's right or bottom * * @see #calculateDtToFit(int, int, int, int, int) - * @see #calculateDxToMakeVisible(View, int) - * @see #calculateDyToMakeVisible(View, int) + * @see #calculateDxToMakeVisible(android.view.View, int) + * @see #calculateDyToMakeVisible(android.view.View, int) */ public static final int SNAP_TO_END = 1; @@ -65,8 +70,8 @@ abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { * {@code SNAP_TO_ANY} is the same as using {@code SNAP_TO_START}

* * @see #calculateDtToFit(int, int, int, int, int) - * @see #calculateDxToMakeVisible(View, int) - * @see #calculateDyToMakeVisible(View, int) + * @see #calculateDxToMakeVisible(android.view.View, int) + * @see #calculateDyToMakeVisible(android.view.View, int) */ public static final int SNAP_TO_ANY = 0; @@ -122,6 +127,7 @@ abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { stop(); return; } + //noinspection PointlessBooleanExpression if (DEBUG && mTargetVector != null && ((mTargetVector.x * dx < 0 || mTargetVector.y * dy < 0))) { throw new IllegalStateException("Scroll happened in the opposite direction" @@ -178,7 +184,7 @@ abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { * * @param dx Distance in pixels that we want to scroll * @return Time in milliseconds - * @see #calculateSpeedPerPixel(DisplayMetrics) + * @see #calculateSpeedPerPixel(android.util.DisplayMetrics) */ protected int calculateTimeForScrolling(int dx) { // In a case where dx is very small, rounding may return 0 although dx > 0. @@ -225,12 +231,9 @@ abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { // find an interim target position PointF scrollVector = computeScrollVectorForPosition(getTargetPosition()); if (scrollVector == null || (scrollVector.x == 0 && scrollVector.y == 0)) { - Log.e(TAG, "To support smooth scrolling, you should override \n" - + "LayoutManager#computeScrollVectorForPosition.\n" - + "Falling back to instant scroll"); final int target = getTargetPosition(); + action.jumpTo(target); stop(); - instantScrollToPosition(target); return; } normalize(scrollVector); @@ -257,8 +260,8 @@ abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { } /** - * Helper method for {@link #calculateDxToMakeVisible(View, int)} and - * {@link #calculateDyToMakeVisible(View, int)} + * Helper method for {@link #calculateDxToMakeVisible(android.view.View, int)} and + * {@link #calculateDyToMakeVisible(android.view.View, int)} */ public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) { @@ -291,13 +294,13 @@ abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { * @param view The view which we want to make fully visible * @param snapPreference The edge which the view should snap to when entering the visible * area. One of {@link #SNAP_TO_START}, {@link #SNAP_TO_END} or - * {@link #SNAP_TO_END}. + * {@link #SNAP_TO_ANY}. * @return The vertical scroll amount necessary to make the view visible with the given * snap preference. */ public int calculateDyToMakeVisible(View view, int snapPreference) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); - if (!layoutManager.canScrollVertically()) { + if (layoutManager == null || !layoutManager.canScrollVertically()) { return 0; } final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) @@ -322,7 +325,7 @@ abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { */ public int calculateDxToMakeVisible(View view, int snapPreference) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); - if (!layoutManager.canScrollHorizontally()) { + if (layoutManager == null || !layoutManager.canScrollHorizontally()) { return 0; } final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) @@ -334,5 +337,25 @@ abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { return calculateDtToFit(left, right, start, end, snapPreference); } - abstract public PointF computeScrollVectorForPosition(int targetPosition); + /** + * Compute the scroll vector for a given target position. + *

+ * This method can return null if the layout manager cannot calculate a scroll vector + * for the given position (e.g. it has no current scroll position). + * + * @param targetPosition the position to which the scroller is scrolling + * + * @return the scroll vector for a given target position + */ + @Nullable + public PointF computeScrollVectorForPosition(int targetPosition) { + RecyclerView.LayoutManager layoutManager = getLayoutManager(); + if (layoutManager instanceof ScrollVectorProvider) { + return ((ScrollVectorProvider) layoutManager) + .computeScrollVectorForPosition(targetPosition); + } + Log.w(TAG, "You should override computeScrollVectorForPosition when the LayoutManager" + + " does not implement " + ScrollVectorProvider.class.getCanonicalName()); + return null; + } } diff --git a/app/src/main/java/android/support/v7/widget/LinearSnapHelper.java b/app/src/main/java/android/support/v7/widget/LinearSnapHelper.java new file mode 100644 index 0000000000..4b37c685bb --- /dev/null +++ b/app/src/main/java/android/support/v7/widget/LinearSnapHelper.java @@ -0,0 +1,286 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific languag`e governing permissions and + * limitations under the License. + */ + +package android.support.v7.widget; + +import android.graphics.PointF; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.View; + +/** + * Implementation of the {@link SnapHelper} supporting snapping in either vertical or horizontal + * orientation. + *

+ * The implementation will snap the center of the target child view to the center of + * the attached {@link RecyclerView}. If you intend to change this behavior then override + * {@link SnapHelper#calculateDistanceToFinalSnap}. + */ +public class LinearSnapHelper extends SnapHelper { + + private static final float INVALID_DISTANCE = 1f; + + // Orientation helpers are lazily created per LayoutManager. + @Nullable + private OrientationHelper mVerticalHelper; + @Nullable + private OrientationHelper mHorizontalHelper; + + @Override + public int[] calculateDistanceToFinalSnap( + @NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) { + int[] out = new int[2]; + if (layoutManager.canScrollHorizontally()) { + out[0] = distanceToCenter(layoutManager, targetView, + getHorizontalHelper(layoutManager)); + } else { + out[0] = 0; + } + + if (layoutManager.canScrollVertically()) { + out[1] = distanceToCenter(layoutManager, targetView, + getVerticalHelper(layoutManager)); + } else { + out[1] = 0; + } + return out; + } + + @Override + public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, + int velocityY) { + if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) { + return RecyclerView.NO_POSITION; + } + + final int itemCount = layoutManager.getItemCount(); + if (itemCount == 0) { + return RecyclerView.NO_POSITION; + } + + final View currentView = findSnapView(layoutManager); + if (currentView == null) { + return RecyclerView.NO_POSITION; + } + + final int currentPosition = layoutManager.getPosition(currentView); + if (currentPosition == RecyclerView.NO_POSITION) { + return RecyclerView.NO_POSITION; + } + + RecyclerView.SmoothScroller.ScrollVectorProvider vectorProvider = + (RecyclerView.SmoothScroller.ScrollVectorProvider) layoutManager; + // deltaJumps sign comes from the velocity which may not match the order of children in + // the LayoutManager. To overcome this, we ask for a vector from the LayoutManager to + // get the direction. + PointF vectorForEnd = vectorProvider.computeScrollVectorForPosition(itemCount - 1); + if (vectorForEnd == null) { + // cannot get a vector for the given position. + return RecyclerView.NO_POSITION; + } + + int vDeltaJump, hDeltaJump; + if (layoutManager.canScrollHorizontally()) { + hDeltaJump = estimateNextPositionDiffForFling(layoutManager, + getHorizontalHelper(layoutManager), velocityX, 0); + if (vectorForEnd.x < 0) { + hDeltaJump = -hDeltaJump; + } + } else { + hDeltaJump = 0; + } + if (layoutManager.canScrollVertically()) { + vDeltaJump = estimateNextPositionDiffForFling(layoutManager, + getVerticalHelper(layoutManager), 0, velocityY); + if (vectorForEnd.y < 0) { + vDeltaJump = -vDeltaJump; + } + } else { + vDeltaJump = 0; + } + + int deltaJump = layoutManager.canScrollVertically() ? vDeltaJump : hDeltaJump; + if (deltaJump == 0) { + return RecyclerView.NO_POSITION; + } + + int targetPos = currentPosition + deltaJump; + if (targetPos < 0) { + targetPos = 0; + } + if (targetPos >= itemCount) { + targetPos = itemCount - 1; + } + return targetPos; + } + + @Override + public View findSnapView(RecyclerView.LayoutManager layoutManager) { + if (layoutManager.canScrollVertically()) { + return findCenterView(layoutManager, getVerticalHelper(layoutManager)); + } else if (layoutManager.canScrollHorizontally()) { + return findCenterView(layoutManager, getHorizontalHelper(layoutManager)); + } + return null; + } + + private int distanceToCenter(@NonNull RecyclerView.LayoutManager layoutManager, + @NonNull View targetView, OrientationHelper helper) { + final int childCenter = helper.getDecoratedStart(targetView) + + (helper.getDecoratedMeasurement(targetView) / 2); + final int containerCenter; + if (layoutManager.getClipToPadding()) { + containerCenter = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; + } else { + containerCenter = helper.getEnd() / 2; + } + return childCenter - containerCenter; + } + + /** + * Estimates a position to which SnapHelper will try to scroll to in response to a fling. + * + * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached + * {@link RecyclerView}. + * @param helper The {@link OrientationHelper} that is created from the LayoutManager. + * @param velocityX The velocity on the x axis. + * @param velocityY The velocity on the y axis. + * + * @return The diff between the target scroll position and the current position. + */ + private int estimateNextPositionDiffForFling(RecyclerView.LayoutManager layoutManager, + OrientationHelper helper, int velocityX, int velocityY) { + int[] distances = calculateScrollDistance(velocityX, velocityY); + float distancePerChild = computeDistancePerChild(layoutManager, helper); + if (distancePerChild <= 0) { + return 0; + } + int distance = + Math.abs(distances[0]) > Math.abs(distances[1]) ? distances[0] : distances[1]; + return (int) Math.floor(distance / distancePerChild); + } + + /** + * Return the child view that is currently closest to the center of this parent. + * + * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached + * {@link RecyclerView}. + * @param helper The relevant {@link OrientationHelper} for the attached {@link RecyclerView}. + * + * @return the child view that is currently closest to the center of this parent. + */ + @Nullable + private View findCenterView(RecyclerView.LayoutManager layoutManager, + OrientationHelper helper) { + int childCount = layoutManager.getChildCount(); + if (childCount == 0) { + return null; + } + + View closestChild = null; + final int center; + if (layoutManager.getClipToPadding()) { + center = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; + } else { + center = helper.getEnd() / 2; + } + int absClosest = Integer.MAX_VALUE; + + for (int i = 0; i < childCount; i++) { + final View child = layoutManager.getChildAt(i); + int childCenter = helper.getDecoratedStart(child) + + (helper.getDecoratedMeasurement(child) / 2); + int absDistance = Math.abs(childCenter - center); + + /** if child center is closer than previous closest, set it as closest **/ + if (absDistance < absClosest) { + absClosest = absDistance; + closestChild = child; + } + } + return closestChild; + } + + /** + * Computes an average pixel value to pass a single child. + *

+ * Returns a negative value if it cannot be calculated. + * + * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached + * {@link RecyclerView}. + * @param helper The relevant {@link OrientationHelper} for the attached + * {@link RecyclerView.LayoutManager}. + * + * @return A float value that is the average number of pixels needed to scroll by one view in + * the relevant direction. + */ + private float computeDistancePerChild(RecyclerView.LayoutManager layoutManager, + OrientationHelper helper) { + View minPosView = null; + View maxPosView = null; + int minPos = Integer.MAX_VALUE; + int maxPos = Integer.MIN_VALUE; + int childCount = layoutManager.getChildCount(); + if (childCount == 0) { + return INVALID_DISTANCE; + } + + for (int i = 0; i < childCount; i++) { + View child = layoutManager.getChildAt(i); + final int pos = layoutManager.getPosition(child); + if (pos == RecyclerView.NO_POSITION) { + continue; + } + if (pos < minPos) { + minPos = pos; + minPosView = child; + } + if (pos > maxPos) { + maxPos = pos; + maxPosView = child; + } + } + if (minPosView == null || maxPosView == null) { + return INVALID_DISTANCE; + } + int start = Math.min(helper.getDecoratedStart(minPosView), + helper.getDecoratedStart(maxPosView)); + int end = Math.max(helper.getDecoratedEnd(minPosView), + helper.getDecoratedEnd(maxPosView)); + int distance = end - start; + if (distance == 0) { + return INVALID_DISTANCE; + } + return 1f * distance / ((maxPos - minPos) + 1); + } + + @NonNull + private OrientationHelper getVerticalHelper(@NonNull RecyclerView.LayoutManager layoutManager) { + if (mVerticalHelper == null || mVerticalHelper.mLayoutManager != layoutManager) { + mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager); + } + return mVerticalHelper; + } + + @NonNull + private OrientationHelper getHorizontalHelper( + @NonNull RecyclerView.LayoutManager layoutManager) { + if (mHorizontalHelper == null || mHorizontalHelper.mLayoutManager != layoutManager) { + mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager); + } + return mHorizontalHelper; + } +} diff --git a/app/src/main/java/android/support/v7/widget/OpReorderer.java b/app/src/main/java/android/support/v7/widget/OpReorderer.java index 1a34cba3e2..db01a0cffc 100644 --- a/app/src/main/java/android/support/v7/widget/OpReorderer.java +++ b/app/src/main/java/android/support/v7/widget/OpReorderer.java @@ -16,10 +16,9 @@ package android.support.v7.widget; -import android.support.v7.widget.AdapterHelper.UpdateOp; - import java.util.List; +import android.support.v7.widget.AdapterHelper.UpdateOp; import static android.support.v7.widget.AdapterHelper.UpdateOp.ADD; import static android.support.v7.widget.AdapterHelper.UpdateOp.MOVE; import static android.support.v7.widget.AdapterHelper.UpdateOp.REMOVE; @@ -101,7 +100,7 @@ class OpReorderer { } else if (moveOp.positionStart < removeOp.positionStart + removeOp.itemCount) { final int remaining = removeOp.positionStart + removeOp.itemCount - moveOp.positionStart; - extraRm = mCallback.obtainUpdateOp(REMOVE, moveOp.positionStart + 1, remaining); + extraRm = mCallback.obtainUpdateOp(REMOVE, moveOp.positionStart + 1, remaining, null); removeOp.itemCount = moveOp.positionStart - removeOp.positionStart; } @@ -188,7 +187,7 @@ class OpReorderer { } else if (moveOp.itemCount < updateOp.positionStart + updateOp.itemCount) { // moved item is updated. add an update for it updateOp.itemCount--; - extraUp1 = mCallback.obtainUpdateOp(UPDATE, moveOp.positionStart, 1); + extraUp1 = mCallback.obtainUpdateOp(UPDATE, moveOp.positionStart, 1, updateOp.payload); } // now affect of add is consumed. now apply effect of first remove if (moveOp.positionStart <= updateOp.positionStart) { @@ -196,7 +195,8 @@ class OpReorderer { } else if (moveOp.positionStart < updateOp.positionStart + updateOp.itemCount) { final int remaining = updateOp.positionStart + updateOp.itemCount - moveOp.positionStart; - extraUp2 = mCallback.obtainUpdateOp(UPDATE, moveOp.positionStart + 1, remaining); + extraUp2 = mCallback.obtainUpdateOp(UPDATE, moveOp.positionStart + 1, remaining, + updateOp.payload); updateOp.itemCount -= remaining; } list.set(update, moveOp); @@ -231,7 +231,7 @@ class OpReorderer { static interface Callback { - UpdateOp obtainUpdateOp(int cmd, int startPosition, int itemCount); + UpdateOp obtainUpdateOp(int cmd, int startPosition, int itemCount, Object payload); void recycleUpdateOp(UpdateOp op); } diff --git a/app/src/main/java/android/support/v7/widget/OrientationHelper.java b/app/src/main/java/android/support/v7/widget/OrientationHelper.java index a678d86de0..8987b9cc8b 100644 --- a/app/src/main/java/android/support/v7/widget/OrientationHelper.java +++ b/app/src/main/java/android/support/v7/widget/OrientationHelper.java @@ -16,6 +16,7 @@ package android.support.v7.widget; +import android.graphics.Rect; import android.view.View; import android.widget.LinearLayout; @@ -41,6 +42,8 @@ public abstract class OrientationHelper { private int mLastTotalSpace = INVALID_SIZE; + final Rect mTmpRect = new Rect(); + private OrientationHelper(RecyclerView.LayoutManager layoutManager) { mLayoutManager = layoutManager; } @@ -76,7 +79,7 @@ public abstract class OrientationHelper { * * @param view The view element to check * @return The first pixel of the element - * @see #getDecoratedEnd(View) + * @see #getDecoratedEnd(android.view.View) */ public abstract int getDecoratedStart(View view); @@ -88,10 +91,42 @@ public abstract class OrientationHelper { * * @param view The view element to check * @return The last pixel of the element - * @see #getDecoratedStart(View) + * @see #getDecoratedStart(android.view.View) */ public abstract int getDecoratedEnd(View view); + /** + * Returns the end of the View after its matrix transformations are applied to its layout + * position. + *

+ * This method is useful when trying to detect the visible edge of a View. + *

+ * It includes the decorations but does not include the margins. + * + * @param view The view whose transformed end will be returned + * @return The end of the View after its decor insets and transformation matrix is applied to + * its position + * + * @see RecyclerView.LayoutManager#getTransformedBoundingBox(View, boolean, Rect) + */ + public abstract int getTransformedEndWithDecoration(View view); + + /** + * Returns the start of the View after its matrix transformations are applied to its layout + * position. + *

+ * This method is useful when trying to detect the visible edge of a View. + *

+ * It includes the decorations but does not include the margins. + * + * @param view The view whose transformed start will be returned + * @return The start of the View after its decor insets and transformation matrix is applied to + * its position + * + * @see RecyclerView.LayoutManager#getTransformedBoundingBox(View, boolean, Rect) + */ + public abstract int getTransformedStartWithDecoration(View view); + /** * Returns the space occupied by this View in the current orientation including decorations and * margins. @@ -165,6 +200,28 @@ public abstract class OrientationHelper { */ public abstract int getEndPadding(); + /** + * Returns the MeasureSpec mode for the current orientation from the LayoutManager. + * + * @return The current measure spec mode. + * + * @see View.MeasureSpec + * @see RecyclerView.LayoutManager#getWidthMode() + * @see RecyclerView.LayoutManager#getHeightMode() + */ + public abstract int getMode(); + + /** + * Returns the MeasureSpec mode for the perpendicular orientation from the LayoutManager. + * + * @return The current measure spec mode. + * + * @see View.MeasureSpec + * @see RecyclerView.LayoutManager#getWidthMode() + * @see RecyclerView.LayoutManager#getHeightMode() + */ + public abstract int getModeInOther(); + /** * Creates an OrientationHelper for the given LayoutManager and orientation. * @@ -242,6 +299,18 @@ public abstract class OrientationHelper { return mLayoutManager.getDecoratedLeft(view) - params.leftMargin; } + @Override + public int getTransformedEndWithDecoration(View view) { + mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect); + return mTmpRect.right; + } + + @Override + public int getTransformedStartWithDecoration(View view) { + mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect); + return mTmpRect.left; + } + @Override public int getTotalSpace() { return mLayoutManager.getWidth() - mLayoutManager.getPaddingLeft() @@ -257,6 +326,16 @@ public abstract class OrientationHelper { public int getEndPadding() { return mLayoutManager.getPaddingRight(); } + + @Override + public int getMode() { + return mLayoutManager.getWidthMode(); + } + + @Override + public int getModeInOther() { + return mLayoutManager.getHeightMode(); + } }; } @@ -318,6 +397,18 @@ public abstract class OrientationHelper { return mLayoutManager.getDecoratedTop(view) - params.topMargin; } + @Override + public int getTransformedEndWithDecoration(View view) { + mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect); + return mTmpRect.bottom; + } + + @Override + public int getTransformedStartWithDecoration(View view) { + mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect); + return mTmpRect.top; + } + @Override public int getTotalSpace() { return mLayoutManager.getHeight() - mLayoutManager.getPaddingTop() @@ -333,6 +424,16 @@ public abstract class OrientationHelper { public int getEndPadding() { return mLayoutManager.getPaddingBottom(); } + + @Override + public int getMode() { + return mLayoutManager.getHeightMode(); + } + + @Override + public int getModeInOther() { + return mLayoutManager.getWidthMode(); + } }; } } \ No newline at end of file diff --git a/app/src/main/java/android/support/v7/widget/RecyclerView.java b/app/src/main/java/android/support/v7/widget/RecyclerView.java index 2bfa3cee6d..87ebb16489 100644 --- a/app/src/main/java/android/support/v7/widget/RecyclerView.java +++ b/app/src/main/java/android/support/v7/widget/RecyclerView.java @@ -18,17 +18,32 @@ package android.support.v7.widget; import android.content.Context; +import android.content.res.TypedArray; import android.database.Observable; import android.graphics.Canvas; +import android.graphics.Matrix; import android.graphics.PointF; import android.graphics.Rect; +import android.graphics.RectF; import android.os.Build; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; +import android.os.SystemClock; +import android.support.annotation.CallSuper; +import android.support.annotation.IntDef; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.v4.util.ArrayMap; +import android.support.annotation.VisibleForTesting; +import android.support.v4.os.ParcelableCompat; +import android.support.v4.os.ParcelableCompatCreatorCallbacks; +import android.support.v4.os.TraceCompat; +import android.support.v4.view.AbsSavedState; +import android.support.v4.view.InputDeviceCompat; import android.support.v4.view.MotionEventCompat; +import android.support.v4.view.NestedScrollingChild; +import android.support.v4.view.NestedScrollingChildHelper; +import android.support.v4.view.ScrollingView; import android.support.v4.view.VelocityTrackerCompat; import android.support.v4.view.ViewCompat; import android.support.v4.view.accessibility.AccessibilityEventCompat; @@ -36,10 +51,13 @@ import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; import android.support.v4.view.accessibility.AccessibilityRecordCompat; import android.support.v4.widget.EdgeEffectCompat; import android.support.v4.widget.ScrollerCompat; +import android.support.v7.recyclerview.R; +import android.support.v7.widget.RecyclerView.ItemAnimator.ItemHolderInfo; import android.util.AttributeSet; import android.util.Log; import android.util.SparseArray; import android.util.SparseIntArray; +import android.util.TypedValue; import android.view.FocusFinder; import android.view.MotionEvent; import android.view.VelocityTracker; @@ -51,6 +69,10 @@ import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityManager; import android.view.animation.Interpolator; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -82,23 +104,77 @@ import static android.support.v7.widget.AdapterHelper.UpdateOp; *

  • Dirty (view): A child view that must be rebound by the adapter before * being displayed.
  • * + * + *

    Positions in RecyclerView:

    + *

    + * RecyclerView introduces an additional level of abstraction between the {@link Adapter} and + * {@link LayoutManager} to be able to detect data set changes in batches during a layout + * calculation. This saves LayoutManager from tracking adapter changes to calculate animations. + * It also helps with performance because all view bindings happen at the same time and unnecessary + * bindings are avoided. + *

    + * For this reason, there are two types of position related methods in RecyclerView: + *

      + *
    • layout position: Position of an item in the latest layout calculation. This is the + * position from the LayoutManager's perspective.
    • + *
    • adapter position: Position of an item in the adapter. This is the position from + * the Adapter's perspective.
    • + *
    + *

    + * These two positions are the same except the time between dispatching adapter.notify* + * events and calculating the updated layout. + *

    + * Methods that return or receive *LayoutPosition* use position as of the latest + * layout calculation (e.g. {@link ViewHolder#getLayoutPosition()}, + * {@link #findViewHolderForLayoutPosition(int)}). These positions include all changes until the + * last layout calculation. You can rely on these positions to be consistent with what user is + * currently seeing on the screen. For example, if you have a list of items on the screen and user + * asks for the 5th element, you should use these methods as they'll match what user + * is seeing. + *

    + * The other set of position related methods are in the form of + * *AdapterPosition*. (e.g. {@link ViewHolder#getAdapterPosition()}, + * {@link #findViewHolderForAdapterPosition(int)}) You should use these methods when you need to + * work with up-to-date adapter positions even if they may not have been reflected to layout yet. + * For example, if you want to access the item in the adapter on a ViewHolder click, you should use + * {@link ViewHolder#getAdapterPosition()}. Beware that these methods may not be able to calculate + * adapter positions if {@link Adapter#notifyDataSetChanged()} has been called and new layout has + * not yet been calculated. For this reasons, you should carefully handle {@link #NO_POSITION} or + * null results from these methods. + *

    + * When writing a {@link LayoutManager} you almost always want to use layout positions whereas when + * writing an {@link Adapter}, you probably want to use adapter positions. + * + * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_layoutManager */ -public class RecyclerView extends ViewGroup { +public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild { + private static final String TAG = "RecyclerView"; private static final boolean DEBUG = false; - /** - * On Kitkat, there is a bug which prevents DisplayList from being invalidated if a View is two - * levels deep(wrt to ViewHolder.itemView). DisplayList can be invalidated by setting - * View's visibility to INVISIBLE when View is detached. On Kitkat, Recycler recursively - * traverses itemView and invalidates display list for each ViewGroup that matches this - * criteria. - */ - private static final boolean FORCE_INVALIDATE_DISPLAY_LIST = Build.VERSION.SDK_INT == 19 || - Build.VERSION.SDK_INT == 20; + private static final int[] NESTED_SCROLLING_ATTRS + = {16843830 /* android.R.attr.nestedScrollingEnabled */}; - private static final boolean DISPATCH_TEMP_DETACH = false; + private static final int[] CLIP_TO_PADDING_ATTR = {android.R.attr.clipToPadding}; + + /** + * On Kitkat and JB MR2, there is a bug which prevents DisplayList from being invalidated if + * a View is two levels deep(wrt to ViewHolder.itemView). DisplayList can be invalidated by + * setting View's visibility to INVISIBLE when View is detached. On Kitkat and JB MR2, Recycler + * recursively traverses itemView and invalidates display list for each ViewGroup that matches + * this criteria. + */ + private static final boolean FORCE_INVALIDATE_DISPLAY_LIST = Build.VERSION.SDK_INT == 18 + || Build.VERSION.SDK_INT == 19 || Build.VERSION.SDK_INT == 20; + /** + * On M+, an unspecified measure spec may include a hint which we can use. On older platforms, + * this value might be garbage. To save LayoutManagers from it, RecyclerView sets the size to + * 0 when mode is unspecified. + */ + static final boolean ALLOW_SIZE_IN_UNSPECIFIED_SPEC = Build.VERSION.SDK_INT >= 23; + + static final boolean DISPATCH_TEMP_DETACH = false; public static final int HORIZONTAL = 0; public static final int VERTICAL = 1; @@ -106,20 +182,97 @@ public class RecyclerView extends ViewGroup { public static final long NO_ID = -1; public static final int INVALID_TYPE = -1; + /** + * Constant for use with {@link #setScrollingTouchSlop(int)}. Indicates + * that the RecyclerView should use the standard touch slop for smooth, + * continuous scrolling. + */ + public static final int TOUCH_SLOP_DEFAULT = 0; + + /** + * Constant for use with {@link #setScrollingTouchSlop(int)}. Indicates + * that the RecyclerView should use the standard touch slop for scrolling + * widgets that snap to a page or other coarse-grained barrier. + */ + public static final int TOUCH_SLOP_PAGING = 1; + private static final int MAX_SCROLL_DURATION = 2000; + /** + * RecyclerView is calculating a scroll. + * If there are too many of these in Systrace, some Views inside RecyclerView might be causing + * it. Try to avoid using EditText, focusable views or handle them with care. + */ + private static final String TRACE_SCROLL_TAG = "RV Scroll"; + + /** + * OnLayout has been called by the View system. + * If this shows up too many times in Systrace, make sure the children of RecyclerView do not + * update themselves directly. This will cause a full re-layout but when it happens via the + * Adapter notifyItemChanged, RecyclerView can avoid full layout calculation. + */ + private static final String TRACE_ON_LAYOUT_TAG = "RV OnLayout"; + + /** + * NotifyDataSetChanged or equal has been called. + * If this is taking a long time, try sending granular notify adapter changes instead of just + * calling notifyDataSetChanged or setAdapter / swapAdapter. Adding stable ids to your adapter + * might help. + */ + private static final String TRACE_ON_DATA_SET_CHANGE_LAYOUT_TAG = "RV FullInvalidate"; + + /** + * RecyclerView is doing a layout for partial adapter updates (we know what has changed) + * If this is taking a long time, you may have dispatched too many Adapter updates causing too + * many Views being rebind. Make sure all are necessary and also prefer using notify*Range + * methods. + */ + private static final String TRACE_HANDLE_ADAPTER_UPDATES_TAG = "RV PartialInvalidate"; + + /** + * RecyclerView is rebinding a View. + * If this is taking a lot of time, consider optimizing your layout or make sure you are not + * doing extra operations in onBindViewHolder call. + */ + private static final String TRACE_BIND_VIEW_TAG = "RV OnBindView"; + + /** + * RecyclerView is creating a new View. + * If too many of these present in Systrace: + * - There might be a problem in Recycling (e.g. custom Animations that set transient state and + * prevent recycling or ItemAnimator not implementing the contract properly. ({@link + * > Adapter#onFailedToRecycleView(ViewHolder)}) + * + * - There might be too many item view types. + * > Try merging them + * + * - There might be too many itemChange animations and not enough space in RecyclerPool. + * >Try increasing your pool size and item cache size. + */ + private static final String TRACE_CREATE_VIEW_TAG = "RV CreateView"; + private static final Class[] LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE = + new Class[]{Context.class, AttributeSet.class, int.class, int.class}; + private final RecyclerViewDataObserver mObserver = new RecyclerViewDataObserver(); final Recycler mRecycler = new Recycler(); private SavedState mPendingSavedState; + /** + * Handles adapter updates + */ AdapterHelper mAdapterHelper; + /** + * Handles abstraction between LayoutManager children and RecyclerView children + */ ChildHelper mChildHelper; - // we use this like a set - final List mDisappearingViewsInLayoutPass = new ArrayList(); + /** + * Keeps data about views to be used for animations + */ + final ViewInfoStore mViewInfoStore = new ViewInfoStore(); /** * Prior to L, there is no way to query this variable which is why we override the setter and @@ -134,45 +287,52 @@ public class RecyclerView extends ViewGroup { * 3) We're attached */ private final Runnable mUpdateChildViewsRunnable = new Runnable() { + @Override public void run() { - if (!mAdapterHelper.hasPendingUpdates()) { - return; - } - if (!mFirstLayoutComplete) { + if (!mFirstLayoutComplete || isLayoutRequested()) { // a layout request will happen, we should not do layout here. return; } - if (mDataSetHasChangedAfterLayout) { - dispatchLayout(); - } else { - eatRequestLayout(); - mAdapterHelper.preProcess(); - if (!mLayoutRequestEaten) { - // We run this after pre-processing is complete so that ViewHolders have their - // final adapter positions. No need to run it if a layout is already requested. - rebindUpdatedViewHolders(); - } - resumeRequestLayout(true); + if (!mIsAttached) { + requestLayout(); + // if we are not attached yet, mark us as requiring layout and skip + return; } + if (mLayoutFrozen) { + mLayoutRequestEaten = true; + return; //we'll process updates when ice age ends. + } + consumePendingUpdateOperations(); } }; private final Rect mTempRect = new Rect(); + private final Rect mTempRect2 = new Rect(); + private final RectF mTempRectF = new RectF(); private Adapter mAdapter; - private LayoutManager mLayout; + @VisibleForTesting LayoutManager mLayout; private RecyclerListener mRecyclerListener; - private final ArrayList mItemDecorations = new ArrayList(); + private final ArrayList mItemDecorations = new ArrayList<>(); private final ArrayList mOnItemTouchListeners = - new ArrayList(); + new ArrayList<>(); private OnItemTouchListener mActiveOnItemTouchListener; private boolean mIsAttached; private boolean mHasFixedSize; - private boolean mFirstLayoutComplete; - private boolean mEatRequestLayout; + @VisibleForTesting boolean mFirstLayoutComplete; + + // Counting lock to control whether we should ignore requestLayout calls from children or not. + private int mEatRequestLayout = 0; + private boolean mLayoutRequestEaten; + private boolean mLayoutFrozen; + private boolean mIgnoreMotionEventTillDown; + + // binary OR of change events that were eaten during a layout or scroll. + private int mEatenAccessibilityChangeFlags; private boolean mAdapterUpdateDuringMeasure; private final boolean mPostUpdatesOnAnimation; private final AccessibilityManager mAccessibilityManager; + private List mOnChildAttachStateListeners; /** * Set to true when an adapter data set changed notification is received. @@ -181,14 +341,23 @@ public class RecyclerView extends ViewGroup { private boolean mDataSetHasChangedAfterLayout = false; /** - * This variable is set to true during a dispatchLayout and/or scroll. + * This variable is incremented during a dispatchLayout and/or scroll. * Some methods should not be called during these periods (e.g. adapter data change). * Doing so will create hard to find bugs so we better check it and throw an exception. * * @see #assertInLayoutOrScroll(String) * @see #assertNotInLayoutOrScroll(String) */ - private boolean mRunningLayoutOrScroll = false; + private int mLayoutOrScrollCounter = 0; + + /** + * Similar to mLayoutOrScrollCounter but logs a warning instead of throwing an exception + * (for API compatibility). + *

    + * It is a bad practice for a developer to update the data in a scroll callback since it is + * potentially called during a layout. + */ + private int mDispatchScrollCounter = 0; private EdgeEffectCompat mLeftGlow, mTopGlow, mRightGlow, mBottomGlow; @@ -224,15 +393,20 @@ public class RecyclerView extends ViewGroup { private int mInitialTouchY; private int mLastTouchX; private int mLastTouchY; - private final int mTouchSlop; + private int mTouchSlop; + private OnFlingListener mOnFlingListener; private final int mMinFlingVelocity; private final int mMaxFlingVelocity; + // This value is used when handling generic motion events. + private float mScrollFactor = Float.MIN_VALUE; + private boolean mPreserveFocusAfterLayout = true; private final ViewFlinger mViewFlinger = new ViewFlinger(); final State mState = new State(); private OnScrollListener mScrollListener; + private List mScrollListeners; // For use in item animations boolean mItemsAddedOrRemoved = false; @@ -241,6 +415,17 @@ public class RecyclerView extends ViewGroup { new ItemAnimatorRestoreListener(); private boolean mPostedAnimatorRunner = false; private RecyclerViewAccessibilityDelegate mAccessibilityDelegate; + private ChildDrawingOrderCallback mChildDrawingOrderCallback; + + // simple array to keep min and max child position during a layout calculation + // preserved not to create a new one in each layout pass + private final int[] mMinMaxLayoutPositions = new int[2]; + + private NestedScrollingChildHelper mScrollingChildHelper; + private final int[] mScrollOffset = new int[2]; + private final int[] mScrollConsumed = new int[2]; + private final int[] mNestedOffsets = new int[2]; + private Runnable mItemAnimatorRunner = new Runnable() { @Override public void run() { @@ -252,22 +437,69 @@ public class RecyclerView extends ViewGroup { }; private static final Interpolator sQuinticInterpolator = new Interpolator() { + @Override public float getInterpolation(float t) { t -= 1.0f; return t * t * t * t * t + 1.0f; } }; + /** + * The callback to convert view info diffs into animations. + */ + private final ViewInfoStore.ProcessCallback mViewInfoProcessCallback = + new ViewInfoStore.ProcessCallback() { + @Override + public void processDisappeared(ViewHolder viewHolder, @NonNull ItemHolderInfo info, + @Nullable ItemHolderInfo postInfo) { + mRecycler.unscrapView(viewHolder); + animateDisappearance(viewHolder, info, postInfo); + } + @Override + public void processAppeared(ViewHolder viewHolder, + ItemHolderInfo preInfo, ItemHolderInfo info) { + animateAppearance(viewHolder, preInfo, info); + } + + @Override + public void processPersistent(ViewHolder viewHolder, + @NonNull ItemHolderInfo preInfo, @NonNull ItemHolderInfo postInfo) { + viewHolder.setIsRecyclable(false); + if (mDataSetHasChangedAfterLayout) { + // since it was rebound, use change instead as we'll be mapping them from + // stable ids. If stable ids were false, we would not be running any + // animations + if (mItemAnimator.animateChange(viewHolder, viewHolder, preInfo, postInfo)) { + postAnimationRunner(); + } + } else if (mItemAnimator.animatePersistence(viewHolder, preInfo, postInfo)) { + postAnimationRunner(); + } + } + @Override + public void unused(ViewHolder viewHolder) { + mLayout.removeAndRecycleView(viewHolder.itemView, mRecycler); + } + }; + public RecyclerView(Context context) { this(context, null); } - public RecyclerView(Context context, AttributeSet attrs) { + public RecyclerView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } - public RecyclerView(Context context, AttributeSet attrs, int defStyle) { + public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); + if (attrs != null) { + TypedArray a = context.obtainStyledAttributes(attrs, CLIP_TO_PADDING_ATTR, defStyle, 0); + mClipToPadding = a.getBoolean(0, true); + a.recycle(); + } else { + mClipToPadding = true; + } + setScrollContainer(true); setFocusableInTouchMode(true); final int version = Build.VERSION.SDK_INT; mPostUpdatesOnAnimation = version >= 16; @@ -276,7 +508,7 @@ public class RecyclerView extends ViewGroup { mTouchSlop = vc.getScaledTouchSlop(); mMinFlingVelocity = vc.getScaledMinimumFlingVelocity(); mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity(); - setWillNotDraw(ViewCompat.getOverScrollMode(this) == ViewCompat.OVER_SCROLL_NEVER); + setWillNotDraw(getOverScrollMode() == View.OVER_SCROLL_NEVER); mItemAnimator.setListener(mItemAnimatorListener); initAdapterManager(); @@ -290,6 +522,35 @@ public class RecyclerView extends ViewGroup { mAccessibilityManager = (AccessibilityManager) getContext() .getSystemService(Context.ACCESSIBILITY_SERVICE); setAccessibilityDelegateCompat(new RecyclerViewAccessibilityDelegate(this)); + // Create the layoutManager if specified. + + boolean nestedScrollingEnabled = true; + + if (attrs != null) { + int defStyleRes = 0; + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecyclerView, + defStyle, defStyleRes); + String layoutManagerName = a.getString(R.styleable.RecyclerView_layoutManager); + int descendantFocusability = a.getInt( + R.styleable.RecyclerView_android_descendantFocusability, -1); + if (descendantFocusability == -1) { + setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); + } + a.recycle(); + createLayoutManager(context, layoutManagerName, attrs, defStyle, defStyleRes); + + if (Build.VERSION.SDK_INT >= 21) { + a = context.obtainStyledAttributes(attrs, NESTED_SCROLLING_ATTRS, + defStyle, defStyleRes); + nestedScrollingEnabled = a.getBoolean(0, true); + a.recycle(); + } + } else { + setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); + } + + // Re-set whether nested scrolling is enabled so that it is set on all API levels + setNestedScrollingEnabled(nestedScrollingEnabled); } /** @@ -310,6 +571,72 @@ public class RecyclerView extends ViewGroup { ViewCompat.setAccessibilityDelegate(this, mAccessibilityDelegate); } + /** + * Instantiate and set a LayoutManager, if specified in the attributes. + */ + private void createLayoutManager(Context context, String className, AttributeSet attrs, + int defStyleAttr, int defStyleRes) { + if (className != null) { + className = className.trim(); + if (className.length() != 0) { // Can't use isEmpty since it was added in API 9. + className = getFullClassName(context, className); + try { + ClassLoader classLoader; + if (isInEditMode()) { + // Stupid layoutlib cannot handle simple class loaders. + classLoader = this.getClass().getClassLoader(); + } else { + classLoader = context.getClassLoader(); + } + Class layoutManagerClass = + classLoader.loadClass(className).asSubclass(LayoutManager.class); + Constructor constructor; + Object[] constructorArgs = null; + try { + constructor = layoutManagerClass + .getConstructor(LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE); + constructorArgs = new Object[]{context, attrs, defStyleAttr, defStyleRes}; + } catch (NoSuchMethodException e) { + try { + constructor = layoutManagerClass.getConstructor(); + } catch (NoSuchMethodException e1) { + e1.initCause(e); + throw new IllegalStateException(attrs.getPositionDescription() + + ": Error creating LayoutManager " + className, e1); + } + } + constructor.setAccessible(true); + setLayoutManager(constructor.newInstance(constructorArgs)); + } catch (ClassNotFoundException e) { + throw new IllegalStateException(attrs.getPositionDescription() + + ": Unable to find LayoutManager " + className, e); + } catch (InvocationTargetException e) { + throw new IllegalStateException(attrs.getPositionDescription() + + ": Could not instantiate the LayoutManager: " + className, e); + } catch (InstantiationException e) { + throw new IllegalStateException(attrs.getPositionDescription() + + ": Could not instantiate the LayoutManager: " + className, e); + } catch (IllegalAccessException e) { + throw new IllegalStateException(attrs.getPositionDescription() + + ": Cannot access non-public constructor " + className, e); + } catch (ClassCastException e) { + throw new IllegalStateException(attrs.getPositionDescription() + + ": Class is not a LayoutManager " + className, e); + } + } + } + } + + private String getFullClassName(Context context, String className) { + if (className.charAt(0) == '.') { + return context.getPackageName() + className; + } + if (className.contains(".")) { + return className; + } + return RecyclerView.class.getPackage().getName() + '.' + className; + } + private void initChildrenHelper() { mChildHelper = new ChildHelper(new ChildHelper.Callback() { @Override @@ -359,13 +686,54 @@ public class RecyclerView extends ViewGroup { @Override public void attachViewToParent(View child, int index, ViewGroup.LayoutParams layoutParams) { + final ViewHolder vh = getChildViewHolderInt(child); + if (vh != null) { + if (!vh.isTmpDetached() && !vh.shouldIgnore()) { + throw new IllegalArgumentException("Called attach on a child which is not" + + " detached: " + vh); + } + if (DEBUG) { + Log.d(TAG, "reAttach " + vh); + } + vh.clearTmpDetachFlag(); + } RecyclerView.this.attachViewToParent(child, index, layoutParams); } @Override public void detachViewFromParent(int offset) { + final View view = getChildAt(offset); + if (view != null) { + final ViewHolder vh = getChildViewHolderInt(view); + if (vh != null) { + if (vh.isTmpDetached() && !vh.shouldIgnore()) { + throw new IllegalArgumentException("called detach on an already" + + " detached child " + vh); + } + if (DEBUG) { + Log.d(TAG, "tmpDetach " + vh); + } + vh.addFlags(ViewHolder.FLAG_TMP_DETACHED); + } + } RecyclerView.this.detachViewFromParent(offset); } + + @Override + public void onEnteredHiddenState(View child) { + final ViewHolder vh = getChildViewHolderInt(child); + if (vh != null) { + vh.onEnteredHiddenState(); + } + } + + @Override + public void onLeftHiddenState(View child) { + final ViewHolder vh = getChildViewHolderInt(child); + if (vh != null) { + vh.onLeftHiddenState(); + } + } }); } @@ -373,7 +741,19 @@ public class RecyclerView extends ViewGroup { mAdapterHelper = new AdapterHelper(new Callback() { @Override public ViewHolder findViewHolder(int position) { - return findViewHolderForPosition(position, true); + final ViewHolder vh = findViewHolderForPosition(position, true); + if (vh == null) { + return null; + } + // ensure it is not hidden because for adapter helper, the only thing matter is that + // LM thinks view is a child. + if (mChildHelper.isHidden(vh.itemView)) { + if (DEBUG) { + Log.d(TAG, "assuming view holder cannot be find because it is hidden"); + } + return null; + } + return vh; } @Override @@ -390,8 +770,8 @@ public class RecyclerView extends ViewGroup { } @Override - public void markViewHoldersUpdated(int positionStart, int itemCount) { - viewRangeUpdate(positionStart, itemCount); + public void markViewHoldersUpdated(int positionStart, int itemCount, Object payload) { + viewRangeUpdate(positionStart, itemCount, payload); mItemsChanged = true; } @@ -409,7 +789,8 @@ public class RecyclerView extends ViewGroup { mLayout.onItemsRemoved(RecyclerView.this, op.positionStart, op.itemCount); break; case UpdateOp.UPDATE: - mLayout.onItemsUpdated(RecyclerView.this, op.positionStart, op.itemCount); + mLayout.onItemsUpdated(RecyclerView.this, op.positionStart, op.itemCount, + op.payload); break; case UpdateOp.MOVE: mLayout.onItemsMoved(RecyclerView.this, op.positionStart, op.itemCount, 1); @@ -438,9 +819,13 @@ public class RecyclerView extends ViewGroup { } /** - * RecyclerView can perform several optimizations if it can know in advance that changes in - * adapter content cannot change the size of the RecyclerView itself. - * If your use of RecyclerView falls into this category, set this to true. + * RecyclerView can perform several optimizations if it can know in advance that RecyclerView's + * size is not affected by the adapter contents. RecyclerView can still change its size based + * on other factors (e.g. its parent's size) but this size calculation cannot depend on the + * size of its children or contents of its adapter (except the number of items in the adapter). + *

    + * If your use of RecyclerView falls into this category, set this to {@code true}. It will allow + * RecyclerView to avoid invalidating the whole layout when its adapter contents change. * * @param hasFixedSize true if adapter changes cannot affect the size of the RecyclerView. */ @@ -468,6 +853,32 @@ public class RecyclerView extends ViewGroup { } } + /** + * Configure the scrolling touch slop for a specific use case. + * + * Set up the RecyclerView's scrolling motion threshold based on common usages. + * Valid arguments are {@link #TOUCH_SLOP_DEFAULT} and {@link #TOUCH_SLOP_PAGING}. + * + * @param slopConstant One of the TOUCH_SLOP_ constants representing + * the intended usage of this RecyclerView + */ + public void setScrollingTouchSlop(int slopConstant) { + final ViewConfiguration vc = ViewConfiguration.get(getContext()); + switch (slopConstant) { + default: + Log.w(TAG, "setScrollingTouchSlop(): bad argument constant " + + slopConstant + "; using default value"); + // fall-through + case TOUCH_SLOP_DEFAULT: + mTouchSlop = vc.getScaledTouchSlop(); + break; + + case TOUCH_SLOP_PAGING: + mTouchSlop = vc.getScaledPagingTouchSlop(); + break; + } + } + /** * Swaps the current adapter with the provided one. It is similar to * {@link #setAdapter(Adapter)} but assumes existing adapter and the new adapter uses the same @@ -483,8 +894,10 @@ public class RecyclerView extends ViewGroup { * @see #setAdapter(Adapter) */ public void swapAdapter(Adapter adapter, boolean removeAndRecycleExistingViews) { + // bail out if layout is frozen + setLayoutFrozen(false); setAdapterInternal(adapter, true, removeAndRecycleExistingViews); - mDataSetHasChangedAfterLayout = true; + setDataSetChangedAfterLayout(); requestLayout(); } /** @@ -497,6 +910,8 @@ public class RecyclerView extends ViewGroup { * @see #swapAdapter(Adapter, boolean) */ public void setAdapter(Adapter adapter) { + // bail out if layout is frozen + setLayoutFrozen(false); setAdapterInternal(adapter, false, true); requestLayout(); } @@ -514,6 +929,7 @@ public class RecyclerView extends ViewGroup { boolean removeAndRecycleViews) { if (mAdapter != null) { mAdapter.unregisterAdapterDataObserver(mObserver); + mAdapter.onDetachedFromRecyclerView(this); } if (!compatibleWithPrevious || removeAndRecycleViews) { // end all running animations @@ -526,14 +942,17 @@ public class RecyclerView extends ViewGroup { // count. if (mLayout != null) { mLayout.removeAndRecycleAllViews(mRecycler); - mLayout.removeAndRecycleScrapInt(mRecycler, true); + mLayout.removeAndRecycleScrapInt(mRecycler); } + // we should clear it here before adapters are swapped to ensure correct callbacks. + mRecycler.clear(); } mAdapterHelper.reset(); final Adapter oldAdapter = mAdapter; mAdapter = adapter; if (adapter != null) { adapter.registerAdapterDataObserver(mObserver); + adapter.onAttachedToRecyclerView(this); } if (mLayout != null) { mLayout.onAdapterChanged(oldAdapter, mAdapter); @@ -567,6 +986,63 @@ public class RecyclerView extends ViewGroup { mRecyclerListener = listener; } + /** + *

    Return the offset of the RecyclerView's text baseline from the its top + * boundary. If the LayoutManager of this RecyclerView does not support baseline alignment, + * this method returns -1.

    + * + * @return the offset of the baseline within the RecyclerView's bounds or -1 + * if baseline alignment is not supported + */ + @Override + public int getBaseline() { + if (mLayout != null) { + return mLayout.getBaseline(); + } else { + return super.getBaseline(); + } + } + + /** + * Register a listener that will be notified whenever a child view is attached to or detached + * from RecyclerView. + * + *

    This listener will be called when a LayoutManager or the RecyclerView decides + * that a child view is no longer needed. If an application associates expensive + * or heavyweight data with item views, this may be a good place to release + * or free those resources.

    + * + * @param listener Listener to register + */ + public void addOnChildAttachStateChangeListener(OnChildAttachStateChangeListener listener) { + if (mOnChildAttachStateListeners == null) { + mOnChildAttachStateListeners = new ArrayList<>(); + } + mOnChildAttachStateListeners.add(listener); + } + + /** + * Removes the provided listener from child attached state listeners list. + * + * @param listener Listener to unregister + */ + public void removeOnChildAttachStateChangeListener(OnChildAttachStateChangeListener listener) { + if (mOnChildAttachStateListeners == null) { + return; + } + mOnChildAttachStateListeners.remove(listener); + } + + /** + * Removes all listeners that were added via + * {@link #addOnChildAttachStateChangeListener(OnChildAttachStateChangeListener)}. + */ + public void clearOnChildAttachStateChangeListeners() { + if (mOnChildAttachStateListeners != null) { + mOnChildAttachStateListeners.clear(); + } + } + /** * Set the {@link LayoutManager} that this RecyclerView will use. * @@ -583,15 +1059,27 @@ public class RecyclerView extends ViewGroup { if (layout == mLayout) { return; } - // TODO We should do this switch a dispachLayout pass and animate children. There is a good + stopScroll(); + // TODO We should do this switch a dispatchLayout pass and animate children. There is a good // chance that LayoutManagers will re-use views. if (mLayout != null) { + // end all running animations + if (mItemAnimator != null) { + mItemAnimator.endAnimations(); + } + mLayout.removeAndRecycleAllViews(mRecycler); + mLayout.removeAndRecycleScrapInt(mRecycler); + mRecycler.clear(); + if (mIsAttached) { - mLayout.onDetachedFromWindow(this, mRecycler); + mLayout.dispatchDetachedFromWindow(this, mRecycler); } mLayout.setRecyclerView(null); + mLayout = null; + } else { + mRecycler.clear(); } - mRecycler.clear(); + // this is just a defensive measure for faulty item animators. mChildHelper.removeAllViewsUnfiltered(); mLayout = layout; if (layout != null) { @@ -601,12 +1089,34 @@ public class RecyclerView extends ViewGroup { } mLayout.setRecyclerView(this); if (mIsAttached) { - mLayout.onAttachedToWindow(this); + mLayout.dispatchAttachedToWindow(this); } } requestLayout(); } + /** + * Set a {@link OnFlingListener} for this {@link RecyclerView}. + *

    + * If the {@link OnFlingListener} is set then it will receive + * calls to {@link #fling(int,int)} and will be able to intercept them. + * + * @param onFlingListener The {@link OnFlingListener} instance. + */ + public void setOnFlingListener(@Nullable OnFlingListener onFlingListener) { + mOnFlingListener = onFlingListener; + } + + /** + * Get the current {@link OnFlingListener} from this {@link RecyclerView}. + * + * @return The {@link OnFlingListener} instance currently set (can be null). + */ + @Nullable + public OnFlingListener getOnFlingListener() { + return mOnFlingListener; + } + @Override protected Parcelable onSaveInstanceState() { SavedState state = new SavedState(super.onSaveInstanceState()); @@ -623,6 +1133,11 @@ public class RecyclerView extends ViewGroup { @Override protected void onRestoreInstanceState(Parcelable state) { + if (!(state instanceof SavedState)) { + super.onRestoreInstanceState(state); + return; + } + mPendingSavedState = (SavedState) state; super.onRestoreInstanceState(mPendingSavedState.getSuperState()); if (mLayout != null && mPendingSavedState.mLayoutState != null) { @@ -630,18 +1145,38 @@ public class RecyclerView extends ViewGroup { } } + /** + * Override to prevent freezing of any views created by the adapter. + */ + @Override + protected void dispatchSaveInstanceState(SparseArray container) { + dispatchFreezeSelfOnly(container); + } + + /** + * Override to prevent thawing of any views created by the adapter. + */ + @Override + protected void dispatchRestoreInstanceState(SparseArray container) { + dispatchThawSelfOnly(container); + } + /** * Adds a view to the animatingViews list. * mAnimatingViews holds the child views that are currently being kept around * purely for the purpose of being animated out of view. They are drawn as a regular * part of the child list of the RecyclerView, but they are invisible to the LayoutManager * as they are managed separately from the regular child views. - * @param view The view to be removed + * @param viewHolder The ViewHolder to be removed */ - private void addAnimatingView(View view) { + private void addAnimatingView(ViewHolder viewHolder) { + final View view = viewHolder.itemView; final boolean alreadyParented = view.getParent() == this; mRecycler.unscrapView(getChildViewHolder(view)); - if (!alreadyParented) { + if (viewHolder.isTmpDetached()) { + // re-attach + mChildHelper.attachViewToParent(view, -1, view.getLayoutParams(), true); + } else if(!alreadyParented) { mChildHelper.addView(view, true); } else { mChildHelper.hide(view); @@ -651,11 +1186,13 @@ public class RecyclerView extends ViewGroup { /** * Removes a view from the animatingViews list. * @param view The view to be removed - * @see #addAnimatingView(View) + * @see #addAnimatingView(RecyclerView.ViewHolder) + * @return true if an animating view is removed */ - private void removeAnimatingView(View view) { + private boolean removeAnimatingView(View view) { eatRequestLayout(); - if (mChildHelper.removeViewIfHidden(view)) { + final boolean removed = mChildHelper.removeViewIfHidden(view); + if (removed) { final ViewHolder viewHolder = getChildViewHolderInt(view); mRecycler.unscrapView(viewHolder); mRecycler.recycleViewHolderInternal(viewHolder); @@ -663,7 +1200,9 @@ public class RecyclerView extends ViewGroup { Log.d(TAG, "after removing animated view: " + view + ", " + this); } } - resumeRequestLayout(false); + // only clear request eaten flag if we removed the view. + resumeRequestLayout(!removed); + return removed; } /** @@ -741,16 +1280,14 @@ public class RecyclerView extends ViewGroup { return; } if (DEBUG) { - Log.d(TAG, "setting scroll state to " + state + " from " + mScrollState, new Exception()); + Log.d(TAG, "setting scroll state to " + state + " from " + mScrollState, + new Exception()); } mScrollState = state; if (state != SCROLL_STATE_SETTLING) { stopScrollersInternal(); } - if (mScrollListener != null) { - mScrollListener.onScrollStateChanged(this, state); - } - mLayout.onScrollStateChanged(state); + dispatchOnScrollStateChanged(state); } /** @@ -816,31 +1353,107 @@ public class RecyclerView extends ViewGroup { } mItemDecorations.remove(decor); if (mItemDecorations.isEmpty()) { - setWillNotDraw(ViewCompat.getOverScrollMode(this) == ViewCompat.OVER_SCROLL_NEVER); + setWillNotDraw(getOverScrollMode() == View.OVER_SCROLL_NEVER); } markItemDecorInsetsDirty(); requestLayout(); } + /** + * Sets the {@link ChildDrawingOrderCallback} to be used for drawing children. + *

    + * See {@link ViewGroup#getChildDrawingOrder(int, int)} for details. Calling this method will + * always call {@link ViewGroup#setChildrenDrawingOrderEnabled(boolean)}. The parameter will be + * true if childDrawingOrderCallback is not null, false otherwise. + *

    + * Note that child drawing order may be overridden by View's elevation. + * + * @param childDrawingOrderCallback The ChildDrawingOrderCallback to be used by the drawing + * system. + */ + public void setChildDrawingOrderCallback(ChildDrawingOrderCallback childDrawingOrderCallback) { + if (childDrawingOrderCallback == mChildDrawingOrderCallback) { + return; + } + mChildDrawingOrderCallback = childDrawingOrderCallback; + setChildrenDrawingOrderEnabled(mChildDrawingOrderCallback != null); + } + /** * Set a listener that will be notified of any changes in scroll state or position. * * @param listener Listener to set or null to clear + * + * @deprecated Use {@link #addOnScrollListener(OnScrollListener)} and + * {@link #removeOnScrollListener(OnScrollListener)} */ + @Deprecated public void setOnScrollListener(OnScrollListener listener) { mScrollListener = listener; } + /** + * Add a listener that will be notified of any changes in scroll state or position. + * + *

    Components that add a listener should take care to remove it when finished. + * Other components that take ownership of a view may call {@link #clearOnScrollListeners()} + * to remove all attached listeners.

    + * + * @param listener listener to set or null to clear + */ + public void addOnScrollListener(OnScrollListener listener) { + if (mScrollListeners == null) { + mScrollListeners = new ArrayList<>(); + } + mScrollListeners.add(listener); + } + + /** + * Remove a listener that was notified of any changes in scroll state or position. + * + * @param listener listener to set or null to clear + */ + public void removeOnScrollListener(OnScrollListener listener) { + if (mScrollListeners != null) { + mScrollListeners.remove(listener); + } + } + + /** + * Remove all secondary listener that were notified of any changes in scroll state or position. + */ + public void clearOnScrollListeners() { + if (mScrollListeners != null) { + mScrollListeners.clear(); + } + } + /** * Convenience method to scroll to a certain position. * * RecyclerView does not implement scrolling logic, rather forwards the call to - * {@link LayoutManager#scrollToPosition(int)} + * {@link android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition(int)} * @param position Scroll to this adapter position - * @see LayoutManager#scrollToPosition(int) + * @see android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition(int) */ public void scrollToPosition(int position) { + if (mLayoutFrozen) { + return; + } stopScroll(); + if (mLayout == null) { + Log.e(TAG, "Cannot scroll to position a LayoutManager set. " + + "Call setLayoutManager with a non-null argument."); + return; + } + mLayout.scrollToPosition(position); + awakenScrollBars(); + } + + private void jumpToPositionForSmoothScroller(int position) { + if (mLayout == null) { + return; + } mLayout.scrollToPosition(position); awakenScrollBars(); } @@ -861,25 +1474,37 @@ public class RecyclerView extends ViewGroup { * @see LayoutManager#smoothScrollToPosition(RecyclerView, State, int) */ public void smoothScrollToPosition(int position) { + if (mLayoutFrozen) { + return; + } + if (mLayout == null) { + Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. " + + "Call setLayoutManager with a non-null argument."); + return; + } mLayout.smoothScrollToPosition(this, mState, position); } @Override public void scrollTo(int x, int y) { - throw new UnsupportedOperationException( - "RecyclerView does not support scrolling to an absolute position."); + Log.w(TAG, "RecyclerView does not support scrolling to an absolute position. " + + "Use scrollToPosition instead"); } @Override public void scrollBy(int x, int y) { if (mLayout == null) { - throw new IllegalStateException("Cannot scroll without a LayoutManager set. " + + Log.e(TAG, "Cannot scroll without a LayoutManager set. " + "Call setLayoutManager with a non-null argument."); + return; + } + if (mLayoutFrozen) { + return; } final boolean canScrollHorizontal = mLayout.canScrollHorizontally(); final boolean canScrollVertical = mLayout.canScrollVertically(); if (canScrollHorizontal || canScrollVertical) { - scrollByInternal(canScrollHorizontal ? x : 0, canScrollVertical ? y : 0); + scrollByInternal(canScrollHorizontal ? x : 0, canScrollVertical ? y : 0, null); } } @@ -892,69 +1517,116 @@ public class RecyclerView extends ViewGroup { * This method consumes all deferred changes to avoid that case. */ private void consumePendingUpdateOperations() { - if (mAdapterHelper.hasPendingUpdates()) { - mUpdateChildViewsRunnable.run(); + if (!mFirstLayoutComplete || mDataSetHasChangedAfterLayout) { + TraceCompat.beginSection(TRACE_ON_DATA_SET_CHANGE_LAYOUT_TAG); + dispatchLayout(); + TraceCompat.endSection(); + return; + } + if (!mAdapterHelper.hasPendingUpdates()) { + return; + } + + // if it is only an item change (no add-remove-notifyDataSetChanged) we can check if any + // of the visible items is affected and if not, just ignore the change. + if (mAdapterHelper.hasAnyUpdateTypes(UpdateOp.UPDATE) && !mAdapterHelper + .hasAnyUpdateTypes(UpdateOp.ADD | UpdateOp.REMOVE | UpdateOp.MOVE)) { + TraceCompat.beginSection(TRACE_HANDLE_ADAPTER_UPDATES_TAG); + eatRequestLayout(); + mAdapterHelper.preProcess(); + if (!mLayoutRequestEaten) { + if (hasUpdatedView()) { + dispatchLayout(); + } else { + // no need to layout, clean state + mAdapterHelper.consumePostponedUpdates(); + } + } + resumeRequestLayout(true); + TraceCompat.endSection(); + } else if (mAdapterHelper.hasPendingUpdates()) { + TraceCompat.beginSection(TRACE_ON_DATA_SET_CHANGE_LAYOUT_TAG); + dispatchLayout(); + TraceCompat.endSection(); } } /** - * Does not perform bounds checking. Used by internal methods that have already validated input. + * @return True if an existing view holder needs to be updated */ - void scrollByInternal(int x, int y) { - int overscrollX = 0, overscrollY = 0; - int hresult = 0, vresult = 0; + private boolean hasUpdatedView() { + final int childCount = mChildHelper.getChildCount(); + for (int i = 0; i < childCount; i++) { + final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); + if (holder == null || holder.shouldIgnore()) { + continue; + } + if (holder.isUpdated()) { + return true; + } + } + return false; + } + + /** + * Does not perform bounds checking. Used by internal methods that have already validated input. + *

    + * It also reports any unused scroll request to the related EdgeEffect. + * + * @param x The amount of horizontal scroll request + * @param y The amount of vertical scroll request + * @param ev The originating MotionEvent, or null if not from a touch event. + * + * @return Whether any scroll was consumed in either direction. + */ + boolean scrollByInternal(int x, int y, MotionEvent ev) { + int unconsumedX = 0, unconsumedY = 0; + int consumedX = 0, consumedY = 0; + consumePendingUpdateOperations(); if (mAdapter != null) { eatRequestLayout(); - mRunningLayoutOrScroll = true; + onEnterLayoutOrScroll(); + TraceCompat.beginSection(TRACE_SCROLL_TAG); if (x != 0) { - hresult = mLayout.scrollHorizontallyBy(x, mRecycler, mState); - overscrollX = x - hresult; + consumedX = mLayout.scrollHorizontallyBy(x, mRecycler, mState); + unconsumedX = x - consumedX; } if (y != 0) { - vresult = mLayout.scrollVerticallyBy(y, mRecycler, mState); - overscrollY = y - vresult; + consumedY = mLayout.scrollVerticallyBy(y, mRecycler, mState); + unconsumedY = y - consumedY; } - if (supportsChangeAnimations()) { - // Fix up shadow views used by changing animations - int count = mChildHelper.getChildCount(); - for (int i = 0; i < count; i++) { - View view = mChildHelper.getChildAt(i); - ViewHolder holder = getChildViewHolder(view); - if (holder != null && holder.mShadowingHolder != null) { - ViewHolder shadowingHolder = holder.mShadowingHolder; - View shadowingView = shadowingHolder != null ? shadowingHolder.itemView : null; - if (shadowingView != null) { - int left = view.getLeft(); - int top = view.getTop(); - if (left != shadowingView.getLeft() || top != shadowingView.getTop()) { - shadowingView.layout(left, top, - left + shadowingView.getWidth(), - top + shadowingView.getHeight()); - } - } - } - } - } - mRunningLayoutOrScroll = false; + TraceCompat.endSection(); + repositionShadowingViews(); + onExitLayoutOrScroll(); resumeRequestLayout(false); } if (!mItemDecorations.isEmpty()) { invalidate(); } - if (ViewCompat.getOverScrollMode(this) != ViewCompat.OVER_SCROLL_NEVER) { - considerReleasingGlowsOnScroll(x, y); - pullGlows(overscrollX, overscrollY); - } - if (hresult != 0 || vresult != 0) { - onScrollChanged(0, 0, 0, 0); // dummy values, View's implementation does not use these. - if (mScrollListener != null) { - mScrollListener.onScrolled(this, hresult, vresult); + + if (dispatchNestedScroll(consumedX, consumedY, unconsumedX, unconsumedY, mScrollOffset)) { + // Update the last touch co-ords, taking any scroll offset into account + mLastTouchX -= mScrollOffset[0]; + mLastTouchY -= mScrollOffset[1]; + if (ev != null) { + ev.offsetLocation(mScrollOffset[0], mScrollOffset[1]); } + mNestedOffsets[0] += mScrollOffset[0]; + mNestedOffsets[1] += mScrollOffset[1]; + } else if (getOverScrollMode() != View.OVER_SCROLL_NEVER) { + if (ev != null) { + pullGlows(ev.getX(), unconsumedX, ev.getY(), unconsumedY); + } + considerReleasingGlowsOnScroll(x, y); + } + if (consumedX != 0 || consumedY != 0) { + dispatchOnScrolled(consumedX, consumedY); } if (!awakenScrollBars()) { invalidate(); } + return consumedX != 0 || consumedY != 0; } /** @@ -968,17 +1640,19 @@ public class RecyclerView extends ViewGroup { *

    Default implementation returns 0.

    * *

    If you want to support scroll bars, override - * {@link LayoutManager#computeHorizontalScrollOffset(State)} in your + * {@link RecyclerView.LayoutManager#computeHorizontalScrollOffset(RecyclerView.State)} in your * LayoutManager.

    * * @return The horizontal offset of the scrollbar's thumb - * @see LayoutManager#computeHorizontalScrollOffset - * (RecyclerView.Adapter) + * @see android.support.v7.widget.RecyclerView.LayoutManager#computeHorizontalScrollOffset + * (RecyclerView.State) */ @Override - protected int computeHorizontalScrollOffset() { - return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollOffset(mState) - : 0; + public int computeHorizontalScrollOffset() { + if (mLayout == null) { + return 0; + } + return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollOffset(mState) : 0; } /** @@ -992,14 +1666,17 @@ public class RecyclerView extends ViewGroup { *

    Default implementation returns 0.

    * *

    If you want to support scroll bars, override - * {@link LayoutManager#computeHorizontalScrollExtent(State)} in your + * {@link RecyclerView.LayoutManager#computeHorizontalScrollExtent(RecyclerView.State)} in your * LayoutManager.

    * * @return The horizontal extent of the scrollbar's thumb - * @see LayoutManager#computeHorizontalScrollExtent(State) + * @see RecyclerView.LayoutManager#computeHorizontalScrollExtent(RecyclerView.State) */ @Override - protected int computeHorizontalScrollExtent() { + public int computeHorizontalScrollExtent() { + if (mLayout == null) { + return 0; + } return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollExtent(mState) : 0; } @@ -1012,14 +1689,17 @@ public class RecyclerView extends ViewGroup { *

    Default implementation returns 0.

    * *

    If you want to support scroll bars, override - * {@link LayoutManager#computeHorizontalScrollRange(State)} in your + * {@link RecyclerView.LayoutManager#computeHorizontalScrollRange(RecyclerView.State)} in your * LayoutManager.

    * * @return The total horizontal range represented by the vertical scrollbar - * @see LayoutManager#computeHorizontalScrollRange(State) + * @see RecyclerView.LayoutManager#computeHorizontalScrollRange(RecyclerView.State) */ @Override - protected int computeHorizontalScrollRange() { + public int computeHorizontalScrollRange() { + if (mLayout == null) { + return 0; + } return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollRange(mState) : 0; } @@ -1033,15 +1713,18 @@ public class RecyclerView extends ViewGroup { *

    Default implementation returns 0.

    * *

    If you want to support scroll bars, override - * {@link LayoutManager#computeVerticalScrollOffset(State)} in your + * {@link RecyclerView.LayoutManager#computeVerticalScrollOffset(RecyclerView.State)} in your * LayoutManager.

    * * @return The vertical offset of the scrollbar's thumb - * @see LayoutManager#computeVerticalScrollOffset - * (RecyclerView.Adapter) + * @see android.support.v7.widget.RecyclerView.LayoutManager#computeVerticalScrollOffset + * (RecyclerView.State) */ @Override - protected int computeVerticalScrollOffset() { + public int computeVerticalScrollOffset() { + if (mLayout == null) { + return 0; + } return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollOffset(mState) : 0; } @@ -1055,14 +1738,17 @@ public class RecyclerView extends ViewGroup { *

    Default implementation returns 0.

    * *

    If you want to support scroll bars, override - * {@link LayoutManager#computeVerticalScrollExtent(State)} in your + * {@link RecyclerView.LayoutManager#computeVerticalScrollExtent(RecyclerView.State)} in your * LayoutManager.

    * * @return The vertical extent of the scrollbar's thumb - * @see LayoutManager#computeVerticalScrollExtent(State) + * @see RecyclerView.LayoutManager#computeVerticalScrollExtent(RecyclerView.State) */ @Override - protected int computeVerticalScrollExtent() { + public int computeVerticalScrollExtent() { + if (mLayout == null) { + return 0; + } return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollExtent(mState) : 0; } @@ -1075,34 +1761,111 @@ public class RecyclerView extends ViewGroup { *

    Default implementation returns 0.

    * *

    If you want to support scroll bars, override - * {@link LayoutManager#computeVerticalScrollRange(State)} in your + * {@link RecyclerView.LayoutManager#computeVerticalScrollRange(RecyclerView.State)} in your * LayoutManager.

    * * @return The total vertical range represented by the vertical scrollbar - * @see LayoutManager#computeVerticalScrollRange(State) + * @see RecyclerView.LayoutManager#computeVerticalScrollRange(RecyclerView.State) */ @Override - protected int computeVerticalScrollRange() { + public int computeVerticalScrollRange() { + if (mLayout == null) { + return 0; + } return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollRange(mState) : 0; } void eatRequestLayout() { - if (!mEatRequestLayout) { - mEatRequestLayout = true; + mEatRequestLayout++; + if (mEatRequestLayout == 1 && !mLayoutFrozen) { mLayoutRequestEaten = false; } } void resumeRequestLayout(boolean performLayoutChildren) { - if (mEatRequestLayout) { - if (performLayoutChildren && mLayoutRequestEaten && + if (mEatRequestLayout < 1) { + //noinspection PointlessBooleanExpression + if (DEBUG) { + throw new IllegalStateException("invalid eat request layout count"); + } + mEatRequestLayout = 1; + } + if (!performLayoutChildren) { + // Reset the layout request eaten counter. + // This is necessary since eatRequest calls can be nested in which case the other + // call will override the inner one. + // for instance: + // eat layout for process adapter updates + // eat layout for dispatchLayout + // a bunch of req layout calls arrive + + mLayoutRequestEaten = false; + } + if (mEatRequestLayout == 1) { + // when layout is frozen we should delay dispatchLayout() + if (performLayoutChildren && mLayoutRequestEaten && !mLayoutFrozen && mLayout != null && mAdapter != null) { dispatchLayout(); } - mEatRequestLayout = false; - mLayoutRequestEaten = false; + if (!mLayoutFrozen) { + mLayoutRequestEaten = false; + } } + mEatRequestLayout--; + } + + /** + * Enable or disable layout and scroll. After setLayoutFrozen(true) is called, + * Layout requests will be postponed until setLayoutFrozen(false) is called; + * child views are not updated when RecyclerView is frozen, {@link #smoothScrollBy(int, int)}, + * {@link #scrollBy(int, int)}, {@link #scrollToPosition(int)} and + * {@link #smoothScrollToPosition(int)} are dropped; TouchEvents and GenericMotionEvents are + * dropped; {@link LayoutManager#onFocusSearchFailed(View, int, Recycler, State)} will not be + * called. + * + *

    + * setLayoutFrozen(true) does not prevent app from directly calling {@link + * LayoutManager#scrollToPosition(int)}, {@link LayoutManager#smoothScrollToPosition( + * RecyclerView, State, int)}. + *

    + * {@link #setAdapter(Adapter)} and {@link #swapAdapter(Adapter, boolean)} will automatically + * stop frozen. + *

    + * Note: Running ItemAnimator is not stopped automatically, it's caller's + * responsibility to call ItemAnimator.end(). + * + * @param frozen true to freeze layout and scroll, false to re-enable. + */ + public void setLayoutFrozen(boolean frozen) { + if (frozen != mLayoutFrozen) { + assertNotInLayoutOrScroll("Do not setLayoutFrozen in layout or scroll"); + if (!frozen) { + mLayoutFrozen = false; + if (mLayoutRequestEaten && mLayout != null && mAdapter != null) { + requestLayout(); + } + mLayoutRequestEaten = false; + } else { + final long now = SystemClock.uptimeMillis(); + MotionEvent cancelEvent = MotionEvent.obtain(now, now, + MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); + onTouchEvent(cancelEvent); + mLayoutFrozen = true; + mIgnoreMotionEventTillDown = true; + stopScroll(); + } + } + } + + /** + * Returns true if layout and scroll are frozen. + * + * @return true if layout and scroll are frozen + * @see #setLayoutFrozen(boolean) + */ + public boolean isLayoutFrozen() { + return mLayoutFrozen; } /** @@ -1112,6 +1875,20 @@ public class RecyclerView extends ViewGroup { * @param dy Pixels to scroll vertically */ public void smoothScrollBy(int dx, int dy) { + if (mLayout == null) { + Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. " + + "Call setLayoutManager with a non-null argument."); + return; + } + if (mLayoutFrozen) { + return; + } + if (!mLayout.canScrollHorizontally()) { + dx = 0; + } + if (!mLayout.canScrollVertically()) { + dy = 0; + } if (dx != 0 || dy != 0) { mViewFlinger.smoothScrollBy(dx, dy); } @@ -1124,20 +1901,50 @@ public class RecyclerView extends ViewGroup { * * @param velocityX Initial horizontal velocity in pixels per second * @param velocityY Initial vertical velocity in pixels per second - * @return true if the fling was started, false if the velocity was too low to fling + * @return true if the fling was started, false if the velocity was too low to fling or + * LayoutManager does not support scrolling in the axis fling is issued. + * + * @see LayoutManager#canScrollVertically() + * @see LayoutManager#canScrollHorizontally() */ public boolean fling(int velocityX, int velocityY) { - if (Math.abs(velocityX) < mMinFlingVelocity) { + if (mLayout == null) { + Log.e(TAG, "Cannot fling without a LayoutManager set. " + + "Call setLayoutManager with a non-null argument."); + return false; + } + if (mLayoutFrozen) { + return false; + } + + final boolean canScrollHorizontal = mLayout.canScrollHorizontally(); + final boolean canScrollVertical = mLayout.canScrollVertically(); + + if (!canScrollHorizontal || Math.abs(velocityX) < mMinFlingVelocity) { velocityX = 0; } - if (Math.abs(velocityY) < mMinFlingVelocity) { + if (!canScrollVertical || Math.abs(velocityY) < mMinFlingVelocity) { velocityY = 0; } - velocityX = Math.max(-mMaxFlingVelocity, Math.min(velocityX, mMaxFlingVelocity)); - velocityY = Math.max(-mMaxFlingVelocity, Math.min(velocityY, mMaxFlingVelocity)); - if (velocityX != 0 || velocityY != 0) { - mViewFlinger.fling(velocityX, velocityY); - return true; + if (velocityX == 0 && velocityY == 0) { + // If we don't have any velocity, return false + return false; + } + + if (!dispatchNestedPreFling(velocityX, velocityY)) { + final boolean canScroll = canScrollHorizontal || canScrollVertical; + dispatchNestedFling(velocityX, velocityY, canScroll); + + if (mOnFlingListener != null && mOnFlingListener.onFling(velocityX, velocityY)) { + return true; + } + + if (canScroll) { + velocityX = Math.max(-mMaxFlingVelocity, Math.min(velocityX, mMaxFlingVelocity)); + velocityY = Math.max(-mMaxFlingVelocity, Math.min(velocityY, mMaxFlingVelocity)); + mViewFlinger.fling(velocityX, velocityY); + return true; + } } return false; } @@ -1156,30 +1963,60 @@ public class RecyclerView extends ViewGroup { */ private void stopScrollersInternal() { mViewFlinger.stop(); - mLayout.stopSmoothScroller(); + if (mLayout != null) { + mLayout.stopSmoothScroller(); + } + } + + /** + * Returns the minimum velocity to start a fling. + * + * @return The minimum velocity to start a fling + */ + public int getMinFlingVelocity() { + return mMinFlingVelocity; + } + + + /** + * Returns the maximum fling velocity used by this RecyclerView. + * + * @return The maximum fling velocity used by this RecyclerView. + */ + public int getMaxFlingVelocity() { + return mMaxFlingVelocity; } /** * Apply a pull to relevant overscroll glow effects */ - private void pullGlows(int overscrollX, int overscrollY) { + private void pullGlows(float x, float overscrollX, float y, float overscrollY) { + boolean invalidate = false; if (overscrollX < 0) { ensureLeftGlow(); - mLeftGlow.onPull(-overscrollX / (float) getWidth()); + if (mLeftGlow.onPull(-overscrollX / getWidth(), 1f - y / getHeight())) { + invalidate = true; + } } else if (overscrollX > 0) { ensureRightGlow(); - mRightGlow.onPull(overscrollX / (float) getWidth()); + if (mRightGlow.onPull(overscrollX / getWidth(), y / getHeight())) { + invalidate = true; + } } if (overscrollY < 0) { ensureTopGlow(); - mTopGlow.onPull(-overscrollY / (float) getHeight()); + if (mTopGlow.onPull(-overscrollY / getHeight(), x / getWidth())) { + invalidate = true; + } } else if (overscrollY > 0) { ensureBottomGlow(); - mBottomGlow.onPull(overscrollY / (float) getHeight()); + if (mBottomGlow.onPull(overscrollY / getHeight(), 1f - x / getWidth())) { + invalidate = true; + } } - if (overscrollX != 0 || overscrollY != 0) { + if (invalidate || overscrollX != 0 || overscrollY != 0) { ViewCompat.postInvalidateOnAnimation(this); } } @@ -1293,28 +2130,178 @@ public class RecyclerView extends ViewGroup { mLeftGlow = mRightGlow = mTopGlow = mBottomGlow = null; } - // Focus handling - + /** + * Since RecyclerView is a collection ViewGroup that includes virtual children (items that are + * in the Adapter but not visible in the UI), it employs a more involved focus search strategy + * that differs from other ViewGroups. + *

    + * It first does a focus search within the RecyclerView. If this search finds a View that is in + * the focus direction with respect to the currently focused View, RecyclerView returns that + * child as the next focus target. When it cannot find such child, it calls + * {@link LayoutManager#onFocusSearchFailed(View, int, Recycler, State)} to layout more Views + * in the focus search direction. If LayoutManager adds a View that matches the + * focus search criteria, it will be returned as the focus search result. Otherwise, + * RecyclerView will call parent to handle the focus search like a regular ViewGroup. + *

    + * When the direction is {@link View#FOCUS_FORWARD} or {@link View#FOCUS_BACKWARD}, a View that + * is not in the focus direction is still valid focus target which may not be the desired + * behavior if the Adapter has more children in the focus direction. To handle this case, + * RecyclerView converts the focus direction to an absolute direction and makes a preliminary + * focus search in that direction. If there are no Views to gain focus, it will call + * {@link LayoutManager#onFocusSearchFailed(View, int, Recycler, State)} before running a + * focus search with the original (relative) direction. This allows RecyclerView to provide + * better candidates to the focus search while still allowing the view system to take focus from + * the RecyclerView and give it to a more suitable child if such child exists. + * + * @param focused The view that currently has focus + * @param direction One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN}, + * {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT}, {@link View#FOCUS_FORWARD}, + * {@link View#FOCUS_BACKWARD} or 0 for not applicable. + * + * @return A new View that can be the next focus after the focused View + */ @Override public View focusSearch(View focused, int direction) { View result = mLayout.onInterceptFocusSearch(focused, direction); if (result != null) { return result; } + final boolean canRunFocusFailure = mAdapter != null && mLayout != null + && !isComputingLayout() && !mLayoutFrozen; + final FocusFinder ff = FocusFinder.getInstance(); - result = ff.findNextFocus(this, focused, direction); - if (result == null && mAdapter != null) { - eatRequestLayout(); - result = mLayout.onFocusSearchFailed(focused, direction, mRecycler, mState); - resumeRequestLayout(false); + if (canRunFocusFailure + && (direction == View.FOCUS_FORWARD || direction == View.FOCUS_BACKWARD)) { + // convert direction to absolute direction and see if we have a view there and if not + // tell LayoutManager to add if it can. + boolean needsFocusFailureLayout = false; + if (mLayout.canScrollVertically()) { + final int absDir = + direction == View.FOCUS_FORWARD ? View.FOCUS_DOWN : View.FOCUS_UP; + final View found = ff.findNextFocus(this, focused, absDir); + needsFocusFailureLayout = found == null; + } + if (!needsFocusFailureLayout && mLayout.canScrollHorizontally()) { + boolean rtl = mLayout.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL; + final int absDir = (direction == View.FOCUS_FORWARD) ^ rtl + ? View.FOCUS_RIGHT : View.FOCUS_LEFT; + final View found = ff.findNextFocus(this, focused, absDir); + needsFocusFailureLayout = found == null; + } + if (needsFocusFailureLayout) { + consumePendingUpdateOperations(); + final View focusedItemView = findContainingItemView(focused); + if (focusedItemView == null) { + // panic, focused view is not a child anymore, cannot call super. + return null; + } + eatRequestLayout(); + mLayout.onFocusSearchFailed(focused, direction, mRecycler, mState); + resumeRequestLayout(false); + } + result = ff.findNextFocus(this, focused, direction); + } else { + result = ff.findNextFocus(this, focused, direction); + if (result == null && canRunFocusFailure) { + consumePendingUpdateOperations(); + final View focusedItemView = findContainingItemView(focused); + if (focusedItemView == null) { + // panic, focused view is not a child anymore, cannot call super. + return null; + } + eatRequestLayout(); + result = mLayout.onFocusSearchFailed(focused, direction, mRecycler, mState); + resumeRequestLayout(false); + } } - return result != null ? result : super.focusSearch(focused, direction); + return isPreferredNextFocus(focused, result, direction) + ? result : super.focusSearch(focused, direction); + } + + /** + * Checks if the new focus candidate is a good enough candidate such that RecyclerView will + * assign it as the next focus View instead of letting view hierarchy decide. + * A good candidate means a View that is aligned in the focus direction wrt the focused View + * and is not the RecyclerView itself. + * When this method returns false, RecyclerView will let the parent make the decision so the + * same View may still get the focus as a result of that search. + */ + private boolean isPreferredNextFocus(View focused, View next, int direction) { + if (next == null || next == this) { + return false; + } + if (focused == null) { + return true; + } + + if(direction == View.FOCUS_FORWARD || direction == View.FOCUS_BACKWARD) { + final boolean rtl = mLayout.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL; + final int absHorizontal = (direction == View.FOCUS_FORWARD) ^ rtl + ? View.FOCUS_RIGHT : View.FOCUS_LEFT; + if (isPreferredNextFocusAbsolute(focused, next, absHorizontal)) { + return true; + } + if (direction == View.FOCUS_FORWARD) { + return isPreferredNextFocusAbsolute(focused, next, View.FOCUS_DOWN); + } else { + return isPreferredNextFocusAbsolute(focused, next, View.FOCUS_UP); + } + } else { + return isPreferredNextFocusAbsolute(focused, next, direction); + } + + } + + /** + * Logic taken from FocusSearch#isCandidate + */ + private boolean isPreferredNextFocusAbsolute(View focused, View next, int direction) { + mTempRect.set(0, 0, focused.getWidth(), focused.getHeight()); + mTempRect2.set(0, 0, next.getWidth(), next.getHeight()); + offsetDescendantRectToMyCoords(focused, mTempRect); + offsetDescendantRectToMyCoords(next, mTempRect2); + switch (direction) { + case View.FOCUS_LEFT: + return (mTempRect.right > mTempRect2.right + || mTempRect.left >= mTempRect2.right) + && mTempRect.left > mTempRect2.left; + case View.FOCUS_RIGHT: + return (mTempRect.left < mTempRect2.left + || mTempRect.right <= mTempRect2.left) + && mTempRect.right < mTempRect2.right; + case View.FOCUS_UP: + return (mTempRect.bottom > mTempRect2.bottom + || mTempRect.top >= mTempRect2.bottom) + && mTempRect.top > mTempRect2.top; + case View.FOCUS_DOWN: + return (mTempRect.top < mTempRect2.top + || mTempRect.bottom <= mTempRect2.top) + && mTempRect.bottom < mTempRect2.bottom; + } + throw new IllegalArgumentException("direction must be absolute. received:" + direction); } @Override public void requestChildFocus(View child, View focused) { if (!mLayout.onRequestChildFocus(this, mState, child, focused) && focused != null) { mTempRect.set(0, 0, focused.getWidth(), focused.getHeight()); + + // get item decor offsets w/o refreshing. If they are invalid, there will be another + // layout pass to fix them, then it is LayoutManager's responsibility to keep focused + // View in viewport. + final ViewGroup.LayoutParams focusedLayoutParams = focused.getLayoutParams(); + if (focusedLayoutParams instanceof LayoutParams) { + // if focused child has item decors, use them. Otherwise, ignore. + final LayoutParams lp = (LayoutParams) focusedLayoutParams; + if (!lp.mInsetsDirty) { + final Rect insets = lp.mDecorInsets; + mTempRect.left -= insets.left; + mTempRect.right += insets.right; + mTempRect.top -= insets.top; + mTempRect.bottom += insets.bottom; + } + } + offsetDescendantRectToMyCoords(focused, mTempRect); offsetRectIntoDescendantCoords(child, mTempRect); requestChildRectangleOnScreen(child, mTempRect, !mFirstLayoutComplete); @@ -1329,18 +2316,29 @@ public class RecyclerView extends ViewGroup { @Override public void addFocusables(ArrayList views, int direction, int focusableMode) { - if (!mLayout.onAddFocusables(this, views, direction, focusableMode)) { + if (mLayout == null || !mLayout.onAddFocusables(this, views, direction, focusableMode)) { super.addFocusables(views, direction, focusableMode); } } + @Override + protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { + if (isComputingLayout()) { + // if we are in the middle of a layout calculation, don't let any child take focus. + // RV will handle it after layout calculation is finished. + return false; + } + return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); + } + @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); + mLayoutOrScrollCounter = 0; mIsAttached = true; - mFirstLayoutComplete = false; + mFirstLayoutComplete = mFirstLayoutComplete && !isLayoutRequested(); if (mLayout != null) { - mLayout.onAttachedToWindow(this); + mLayout.dispatchAttachedToWindow(this); } mPostedAnimatorRunner = false; } @@ -1351,14 +2349,21 @@ public class RecyclerView extends ViewGroup { if (mItemAnimator != null) { mItemAnimator.endAnimations(); } - mFirstLayoutComplete = false; - stopScroll(); mIsAttached = false; if (mLayout != null) { - mLayout.onDetachedFromWindow(this, mRecycler); + mLayout.dispatchDetachedFromWindow(this, mRecycler); } removeCallbacks(mItemAnimatorRunner); + mViewInfoStore.onDetach(); + } + + /** + * Returns true if RecyclerView is attached to window. + */ + // @override + public boolean isAttachedToWindow() { + return mIsAttached; } /** @@ -1369,7 +2374,7 @@ public class RecyclerView extends ViewGroup { * @see #assertNotInLayoutOrScroll(String) */ void assertInLayoutOrScroll(String message) { - if (!mRunningLayoutOrScroll) { + if (!isComputingLayout()) { if (message == null) { throw new IllegalStateException("Cannot call this method unless RecyclerView is " + "computing a layout or scrolling"); @@ -1387,13 +2392,20 @@ public class RecyclerView extends ViewGroup { * @see #assertInLayoutOrScroll(String) */ void assertNotInLayoutOrScroll(String message) { - if (mRunningLayoutOrScroll) { + if (isComputingLayout()) { if (message == null) { throw new IllegalStateException("Cannot call this method while RecyclerView is " + "computing a layout or scrolling"); } throw new IllegalStateException(message); } + if (mDispatchScrollCounter > 0) { + Log.w(TAG, "Cannot call this method in a scroll callback. Scroll callbacks might be run" + + " during a measure & layout pass where you cannot change the RecyclerView" + + " data. Any method call that might change the structure of the RecyclerView" + + " or the adapter contents should be postponed to the next frame.", + new IllegalStateException("")); + } } /** @@ -1407,6 +2419,7 @@ public class RecyclerView extends ViewGroup { * for each incoming MotionEvent until the end of the gesture.

    * * @param listener Listener to add + * @see SimpleOnItemTouchListener */ public void addOnItemTouchListener(OnItemTouchListener listener) { mOnItemTouchListeners.add(listener); @@ -1474,11 +2487,20 @@ public class RecyclerView extends ViewGroup { @Override public boolean onInterceptTouchEvent(MotionEvent e) { + if (mLayoutFrozen) { + // When layout is frozen, RV does not intercept the motion event. + // A child view e.g. a button may still get the click. + return false; + } if (dispatchOnItemTouchIntercept(e)) { cancelTouch(); return true; } + if (mLayout == null) { + return false; + } + final boolean canScrollHorizontally = mLayout.canScrollHorizontally(); final boolean canScrollVertically = mLayout.canScrollVertically(); @@ -1492,7 +2514,10 @@ public class RecyclerView extends ViewGroup { switch (action) { case MotionEvent.ACTION_DOWN: - mScrollPointerId = MotionEventCompat.getPointerId(e, 0); + if (mIgnoreMotionEventTillDown) { + mIgnoreMotionEventTillDown = false; + } + mScrollPointerId = e.getPointerId(0); mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f); mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f); @@ -1500,24 +2525,36 @@ public class RecyclerView extends ViewGroup { getParent().requestDisallowInterceptTouchEvent(true); setScrollState(SCROLL_STATE_DRAGGING); } + + // Clear the nested offsets + mNestedOffsets[0] = mNestedOffsets[1] = 0; + + int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE; + if (canScrollHorizontally) { + nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL; + } + if (canScrollVertically) { + nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL; + } + startNestedScroll(nestedScrollAxis); break; case MotionEventCompat.ACTION_POINTER_DOWN: - mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex); - mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f); - mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f); + mScrollPointerId = e.getPointerId(actionIndex); + mInitialTouchX = mLastTouchX = (int) (e.getX(actionIndex) + 0.5f); + mInitialTouchY = mLastTouchY = (int) (e.getY(actionIndex) + 0.5f); break; case MotionEvent.ACTION_MOVE: { - final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId); + final int index = e.findPointerIndex(mScrollPointerId); if (index < 0) { Log.e(TAG, "Error processing scroll; pointer index for id " + mScrollPointerId + " not found. Did any MotionEvents get skipped?"); return false; } - final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f); - final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f); + final int x = (int) (e.getX(index) + 0.5f); + final int y = (int) (e.getY(index) + 0.5f); if (mScrollState != SCROLL_STATE_DRAGGING) { final int dx = x - mInitialTouchX; final int dy = y - mInitialTouchY; @@ -1531,7 +2568,6 @@ public class RecyclerView extends ViewGroup { startScroll = true; } if (startScroll) { - getParent().requestDisallowInterceptTouchEvent(true); setScrollState(SCROLL_STATE_DRAGGING); } } @@ -1543,6 +2579,7 @@ public class RecyclerView extends ViewGroup { case MotionEvent.ACTION_UP: { mVelocityTracker.clear(); + stopNestedScroll(); } break; case MotionEvent.ACTION_CANCEL: { @@ -1552,72 +2589,125 @@ public class RecyclerView extends ViewGroup { return mScrollState == SCROLL_STATE_DRAGGING; } + @Override + public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { + final int listenerCount = mOnItemTouchListeners.size(); + for (int i = 0; i < listenerCount; i++) { + final OnItemTouchListener listener = mOnItemTouchListeners.get(i); + listener.onRequestDisallowInterceptTouchEvent(disallowIntercept); + } + super.requestDisallowInterceptTouchEvent(disallowIntercept); + } + @Override public boolean onTouchEvent(MotionEvent e) { + if (mLayoutFrozen || mIgnoreMotionEventTillDown) { + return false; + } if (dispatchOnItemTouch(e)) { cancelTouch(); return true; } + if (mLayout == null) { + return false; + } + final boolean canScrollHorizontally = mLayout.canScrollHorizontally(); final boolean canScrollVertically = mLayout.canScrollVertically(); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } - mVelocityTracker.addMovement(e); + boolean eventAddedToVelocityTracker = false; + final MotionEvent vtev = MotionEvent.obtain(e); final int action = MotionEventCompat.getActionMasked(e); final int actionIndex = MotionEventCompat.getActionIndex(e); + if (action == MotionEvent.ACTION_DOWN) { + mNestedOffsets[0] = mNestedOffsets[1] = 0; + } + vtev.offsetLocation(mNestedOffsets[0], mNestedOffsets[1]); + switch (action) { case MotionEvent.ACTION_DOWN: { - mScrollPointerId = MotionEventCompat.getPointerId(e, 0); + mScrollPointerId = e.getPointerId(0); mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f); mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f); + + int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE; + if (canScrollHorizontally) { + nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL; + } + if (canScrollVertically) { + nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL; + } + startNestedScroll(nestedScrollAxis); } break; case MotionEventCompat.ACTION_POINTER_DOWN: { - mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex); - mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f); - mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f); + mScrollPointerId = e.getPointerId(actionIndex); + mInitialTouchX = mLastTouchX = (int) (e.getX(actionIndex) + 0.5f); + mInitialTouchY = mLastTouchY = (int) (e.getY(actionIndex) + 0.5f); } break; case MotionEvent.ACTION_MOVE: { - final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId); + final int index = e.findPointerIndex(mScrollPointerId); if (index < 0) { Log.e(TAG, "Error processing scroll; pointer index for id " + mScrollPointerId + " not found. Did any MotionEvents get skipped?"); return false; } - final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f); - final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f); + final int x = (int) (e.getX(index) + 0.5f); + final int y = (int) (e.getY(index) + 0.5f); + int dx = mLastTouchX - x; + int dy = mLastTouchY - y; + + if (dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset)) { + dx -= mScrollConsumed[0]; + dy -= mScrollConsumed[1]; + vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]); + // Updated the nested offsets + mNestedOffsets[0] += mScrollOffset[0]; + mNestedOffsets[1] += mScrollOffset[1]; + } + if (mScrollState != SCROLL_STATE_DRAGGING) { - final int dx = x - mInitialTouchX; - final int dy = y - mInitialTouchY; boolean startScroll = false; if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) { - mLastTouchX = mInitialTouchX + mTouchSlop * (dx < 0 ? -1 : 1); + if (dx > 0) { + dx -= mTouchSlop; + } else { + dx += mTouchSlop; + } startScroll = true; } if (canScrollVertically && Math.abs(dy) > mTouchSlop) { - mLastTouchY = mInitialTouchY + mTouchSlop * (dy < 0 ? -1 : 1); + if (dy > 0) { + dy -= mTouchSlop; + } else { + dy += mTouchSlop; + } startScroll = true; } if (startScroll) { - getParent().requestDisallowInterceptTouchEvent(true); setScrollState(SCROLL_STATE_DRAGGING); } } + if (mScrollState == SCROLL_STATE_DRAGGING) { - final int dx = x - mLastTouchX; - final int dy = y - mLastTouchY; - scrollByInternal(canScrollHorizontally ? -dx : 0, - canScrollVertically ? -dy : 0); + mLastTouchX = x - mScrollOffset[0]; + mLastTouchY = y - mScrollOffset[1]; + + if (scrollByInternal( + canScrollHorizontally ? dx : 0, + canScrollVertically ? dy : 0, + vtev)) { + getParent().requestDisallowInterceptTouchEvent(true); + } } - mLastTouchX = x; - mLastTouchY = y; } break; case MotionEventCompat.ACTION_POINTER_UP: { @@ -1625,6 +2715,8 @@ public class RecyclerView extends ViewGroup { } break; case MotionEvent.ACTION_UP: { + mVelocityTracker.addMovement(vtev); + eventAddedToVelocityTracker = true; mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity); final float xvel = canScrollHorizontally ? -VelocityTrackerCompat.getXVelocity(mVelocityTracker, mScrollPointerId) : 0; @@ -1633,8 +2725,7 @@ public class RecyclerView extends ViewGroup { if (!((xvel != 0 || yvel != 0) && fling((int) xvel, (int) yvel))) { setScrollState(SCROLL_STATE_IDLE); } - mVelocityTracker.clear(); - releaseGlows(); + resetTouch(); } break; case MotionEvent.ACTION_CANCEL: { @@ -1642,57 +2733,176 @@ public class RecyclerView extends ViewGroup { } break; } + if (!eventAddedToVelocityTracker) { + mVelocityTracker.addMovement(vtev); + } + vtev.recycle(); + return true; } - private void cancelTouch() { + private void resetTouch() { if (mVelocityTracker != null) { mVelocityTracker.clear(); } + stopNestedScroll(); releaseGlows(); + } + + private void cancelTouch() { + resetTouch(); setScrollState(SCROLL_STATE_IDLE); } private void onPointerUp(MotionEvent e) { final int actionIndex = MotionEventCompat.getActionIndex(e); - if (MotionEventCompat.getPointerId(e, actionIndex) == mScrollPointerId) { + if (e.getPointerId(actionIndex) == mScrollPointerId) { // Pick a new pointer to pick up the slack. final int newIndex = actionIndex == 0 ? 1 : 0; - mScrollPointerId = MotionEventCompat.getPointerId(e, newIndex); - mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, newIndex) + 0.5f); - mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, newIndex) + 0.5f); + mScrollPointerId = e.getPointerId(newIndex); + mInitialTouchX = mLastTouchX = (int) (e.getX(newIndex) + 0.5f); + mInitialTouchY = mLastTouchY = (int) (e.getY(newIndex) + 0.5f); } } + // @Override + public boolean onGenericMotionEvent(MotionEvent event) { + if (mLayout == null) { + return false; + } + if (mLayoutFrozen) { + return false; + } + if ((event.getSource() & InputDeviceCompat.SOURCE_CLASS_POINTER) != 0) { + if (event.getAction() == MotionEventCompat.ACTION_SCROLL) { + final float vScroll, hScroll; + if (mLayout.canScrollVertically()) { + // Inverse the sign of the vertical scroll to align the scroll orientation + // with AbsListView. + vScroll = -MotionEventCompat + .getAxisValue(event, MotionEventCompat.AXIS_VSCROLL); + } else { + vScroll = 0f; + } + if (mLayout.canScrollHorizontally()) { + hScroll = MotionEventCompat + .getAxisValue(event, MotionEventCompat.AXIS_HSCROLL); + } else { + hScroll = 0f; + } + + if (vScroll != 0 || hScroll != 0) { + final float scrollFactor = getScrollFactor(); + scrollByInternal((int) (hScroll * scrollFactor), + (int) (vScroll * scrollFactor), event); + } + } + } + return false; + } + + /** + * Ported from View.getVerticalScrollFactor. + */ + private float getScrollFactor() { + if (mScrollFactor == Float.MIN_VALUE) { + TypedValue outValue = new TypedValue(); + if (getContext().getTheme().resolveAttribute( + android.R.attr.listPreferredItemHeight, outValue, true)) { + mScrollFactor = outValue.getDimension( + getContext().getResources().getDisplayMetrics()); + } else { + return 0; //listPreferredItemHeight is not defined, no generic scrolling + } + } + return mScrollFactor; + } + @Override protected void onMeasure(int widthSpec, int heightSpec) { - if (mAdapterUpdateDuringMeasure) { - eatRequestLayout(); - processAdapterUpdatesAndSetAnimationFlags(); - - if (mState.mRunPredictiveAnimations) { - // TODO: try to provide a better approach. - // When RV decides to run predictive animations, we need to measure in pre-layout - // state so that pre-layout pass results in correct layout. - // On the other hand, this will prevent the layout manager from resizing properly. - mState.mInPreLayout = true; - } else { - // consume remaining updates to provide a consistent state with the layout pass. - mAdapterHelper.consumeUpdatesInOnePass(); - mState.mInPreLayout = false; + if (mLayout == null) { + defaultOnMeasure(widthSpec, heightSpec); + return; + } + if (mLayout.mAutoMeasure) { + final int widthMode = MeasureSpec.getMode(widthSpec); + final int heightMode = MeasureSpec.getMode(heightSpec); + final boolean skipMeasure = widthMode == MeasureSpec.EXACTLY + && heightMode == MeasureSpec.EXACTLY; + mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec); + if (skipMeasure || mAdapter == null) { + return; } - mAdapterUpdateDuringMeasure = false; - resumeRequestLayout(false); - } + if (mState.mLayoutStep == State.STEP_START) { + dispatchLayoutStep1(); + } + // set dimensions in 2nd step. Pre-layout should happen with old dimensions for + // consistency + mLayout.setMeasureSpecs(widthSpec, heightSpec); + mState.mIsMeasuring = true; + dispatchLayoutStep2(); - if (mAdapter != null) { - mState.mItemCount = mAdapter.getItemCount(); + // now we can get the width and height from the children. + mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec); + + // if RecyclerView has non-exact width and height and if there is at least one child + // which also has non-exact width & height, we have to re-measure. + if (mLayout.shouldMeasureTwice()) { + mLayout.setMeasureSpecs( + MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); + mState.mIsMeasuring = true; + dispatchLayoutStep2(); + // now we can get the width and height from the children. + mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec); + } } else { - mState.mItemCount = 0; - } + if (mHasFixedSize) { + mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec); + return; + } + // custom onMeasure + if (mAdapterUpdateDuringMeasure) { + eatRequestLayout(); + processAdapterUpdatesAndSetAnimationFlags(); - mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec); - mState.mInPreLayout = false; // clear + if (mState.mRunPredictiveAnimations) { + mState.mInPreLayout = true; + } else { + // consume remaining updates to provide a consistent state with the layout pass. + mAdapterHelper.consumeUpdatesInOnePass(); + mState.mInPreLayout = false; + } + mAdapterUpdateDuringMeasure = false; + resumeRequestLayout(false); + } + + if (mAdapter != null) { + mState.mItemCount = mAdapter.getItemCount(); + } else { + mState.mItemCount = 0; + } + eatRequestLayout(); + mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec); + resumeRequestLayout(false); + mState.mInPreLayout = false; // clear + } + } + + /** + * Used when onMeasure is called before layout manager is set + */ + void defaultOnMeasure(int widthSpec, int heightSpec) { + // calling LayoutManager here is not pretty but that API is already public and it is better + // than creating another method since this is internal. + final int width = LayoutManager.chooseSize(widthSpec, + getPaddingLeft() + getPaddingRight(), + ViewCompat.getMinimumWidth(this)); + final int height = LayoutManager.chooseSize(heightSpec, + getPaddingTop() + getPaddingBottom(), + ViewCompat.getMinimumHeight(this)); + + setMeasuredDimension(width, height); } @Override @@ -1700,6 +2910,7 @@ public class RecyclerView extends ViewGroup { super.onSizeChanged(w, h, oldw, oldh); if (w != oldw || h != oldh) { invalidateGlows(); + // layout's w/h are updated during measure/layout steps. } } @@ -1725,6 +2936,91 @@ public class RecyclerView extends ViewGroup { } } + private void onEnterLayoutOrScroll() { + mLayoutOrScrollCounter ++; + } + + private void onExitLayoutOrScroll() { + mLayoutOrScrollCounter --; + if (mLayoutOrScrollCounter < 1) { + if (DEBUG && mLayoutOrScrollCounter < 0) { + throw new IllegalStateException("layout or scroll counter cannot go below zero." + + "Some calls are not matching"); + } + mLayoutOrScrollCounter = 0; + dispatchContentChangedIfNecessary(); + } + } + + boolean isAccessibilityEnabled() { + return mAccessibilityManager != null && mAccessibilityManager.isEnabled(); + } + + private void dispatchContentChangedIfNecessary() { + final int flags = mEatenAccessibilityChangeFlags; + mEatenAccessibilityChangeFlags = 0; + if (flags != 0 && isAccessibilityEnabled()) { + final AccessibilityEvent event = AccessibilityEvent.obtain(); + event.setEventType(AccessibilityEventCompat.TYPE_WINDOW_CONTENT_CHANGED); + AccessibilityEventCompat.setContentChangeTypes(event, flags); + sendAccessibilityEventUnchecked(event); + } + } + + /** + * Returns whether RecyclerView is currently computing a layout. + *

    + * If this method returns true, it means that RecyclerView is in a lockdown state and any + * attempt to update adapter contents will result in an exception because adapter contents + * cannot be changed while RecyclerView is trying to compute the layout. + *

    + * It is very unlikely that your code will be running during this state as it is + * called by the framework when a layout traversal happens or RecyclerView starts to scroll + * in response to system events (touch, accessibility etc). + *

    + * This case may happen if you have some custom logic to change adapter contents in + * response to a View callback (e.g. focus change callback) which might be triggered during a + * layout calculation. In these cases, you should just postpone the change using a Handler or a + * similar mechanism. + * + * @return true if RecyclerView is currently computing a layout, false + * otherwise + */ + public boolean isComputingLayout() { + return mLayoutOrScrollCounter > 0; + } + + /** + * Returns true if an accessibility event should not be dispatched now. This happens when an + * accessibility request arrives while RecyclerView does not have a stable state which is very + * hard to handle for a LayoutManager. Instead, this method records necessary information about + * the event and dispatches a window change event after the critical section is finished. + * + * @return True if the accessibility event should be postponed. + */ + boolean shouldDeferAccessibilityEvent(AccessibilityEvent event) { + if (isComputingLayout()) { + int type = 0; + if (event != null) { + type = AccessibilityEventCompat.getContentChangeTypes(event); + } + if (type == 0) { + type = AccessibilityEventCompat.CONTENT_CHANGE_TYPE_UNDEFINED; + } + mEatenAccessibilityChangeFlags |= type; + return true; + } + return false; + } + + @Override + public void sendAccessibilityEventUnchecked(AccessibilityEvent event) { + if (shouldDeferAccessibilityEvent(event)) { + return; + } + super.sendAccessibilityEventUnchecked(event); + } + /** * Gets the current ItemAnimator for this RecyclerView. A null return value * indicates that there is no animator and that item changes will happen without @@ -1738,10 +3034,6 @@ public class RecyclerView extends ViewGroup { return mItemAnimator; } - private boolean supportsChangeAnimations() { - return mItemAnimator != null && mItemAnimator.getSupportsChangeAnimations(); - } - /** * Post a runnable to the next frame to run pending item animations. Only the first such * request will be posted, governed by the mPostedAnimatorRunner flag. @@ -1774,13 +3066,12 @@ public class RecyclerView extends ViewGroup { // simple animations are a subset of advanced animations (which will cause a // pre-layout step) // If layout supports predictive animations, pre-process to decide if we want to run them - if (mItemAnimator != null && mLayout.supportsPredictiveItemAnimations()) { + if (predictiveItemAnimationsEnabled()) { mAdapterHelper.preProcess(); } else { mAdapterHelper.consumeUpdatesInOnePass(); } - boolean animationTypeSupported = (mItemsAddedOrRemoved && !mItemsChanged) || - (mItemsAddedOrRemoved || (mItemsChanged && supportsChangeAnimations())); + boolean animationTypeSupported = mItemsAddedOrRemoved || mItemsChanged; mState.mRunSimpleAnimations = mFirstLayoutComplete && mItemAnimator != null && (mDataSetHasChangedAfterLayout || animationTypeSupported || mLayout.mRequestedSimpleAnimations) && @@ -1806,43 +3097,159 @@ public class RecyclerView extends ViewGroup { * The overall approach figures out what items exist before/after layout and * infers one of the five above states for each of the items. Then the animations * are set up accordingly: - * PERSISTENT views are moved ({@link ItemAnimator#animateMove(ViewHolder, int, int, int, int)}) - * REMOVED views are removed ({@link ItemAnimator#animateRemove(ViewHolder)}) - * ADDED views are added ({@link ItemAnimator#animateAdd(ViewHolder)}) - * DISAPPEARING views are moved off screen - * APPEARING views are moved on screen + * PERSISTENT views are animated via + * {@link ItemAnimator#animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo)} + * DISAPPEARING views are animated via + * {@link ItemAnimator#animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo)} + * APPEARING views are animated via + * {@link ItemAnimator#animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo)} + * and changed views are animated via + * {@link ItemAnimator#animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo)}. */ void dispatchLayout() { if (mAdapter == null) { Log.e(TAG, "No adapter attached; skipping layout"); + // leave the state in START return; } - mDisappearingViewsInLayoutPass.clear(); + if (mLayout == null) { + Log.e(TAG, "No layout manager attached; skipping layout"); + // leave the state in START + return; + } + mState.mIsMeasuring = false; + if (mState.mLayoutStep == State.STEP_START) { + dispatchLayoutStep1(); + mLayout.setExactMeasureSpecsFrom(this); + dispatchLayoutStep2(); + } else if (mAdapterHelper.hasUpdates() || mLayout.getWidth() != getWidth() || + mLayout.getHeight() != getHeight()) { + // First 2 steps are done in onMeasure but looks like we have to run again due to + // changed size. + mLayout.setExactMeasureSpecsFrom(this); + dispatchLayoutStep2(); + } else { + // always make sure we sync them (to ensure mode is exact) + mLayout.setExactMeasureSpecsFrom(this); + } + dispatchLayoutStep3(); + } + + private void saveFocusInfo() { + View child = null; + if (mPreserveFocusAfterLayout && hasFocus() && mAdapter != null) { + child = getFocusedChild(); + } + + final ViewHolder focusedVh = child == null ? null : findContainingViewHolder(child); + if (focusedVh == null) { + resetFocusInfo(); + } else { + mState.mFocusedItemId = mAdapter.hasStableIds() ? focusedVh.getItemId() : NO_ID; + mState.mFocusedItemPosition = mDataSetHasChangedAfterLayout ? NO_POSITION : + focusedVh.getAdapterPosition(); + mState.mFocusedSubChildId = getDeepestFocusedViewWithId(focusedVh.itemView); + } + } + + private void resetFocusInfo() { + mState.mFocusedItemId = NO_ID; + mState.mFocusedItemPosition = NO_POSITION; + mState.mFocusedSubChildId = View.NO_ID; + } + + private void recoverFocusFromState() { + if (!mPreserveFocusAfterLayout || mAdapter == null || !hasFocus()) { + return; + } + // only recover focus if RV itself has the focus or the focused view is hidden + if (!isFocused()) { + final View focusedChild = getFocusedChild(); + if (focusedChild == null || !mChildHelper.isHidden(focusedChild)) { + return; + } + } + ViewHolder focusTarget = null; + if (mState.mFocusedItemPosition != NO_POSITION) { + focusTarget = findViewHolderForAdapterPosition(mState.mFocusedItemPosition); + } + if (focusTarget == null && mState.mFocusedItemId != NO_ID && mAdapter.hasStableIds()) { + focusTarget = findViewHolderForItemId(mState.mFocusedItemId); + } + if (focusTarget == null || focusTarget.itemView.hasFocus() || + !focusTarget.itemView.hasFocusable()) { + return; + } + // looks like the focused item has been replaced with another view that represents the + // same item in the adapter. Request focus on that. + View viewToFocus = focusTarget.itemView; + if (mState.mFocusedSubChildId != NO_ID) { + View child = focusTarget.itemView.findViewById(mState.mFocusedSubChildId); + if (child != null && child.isFocusable()) { + viewToFocus = child; + } + } + viewToFocus.requestFocus(); + } + + private int getDeepestFocusedViewWithId(View view) { + int lastKnownId = view.getId(); + while (!view.isFocused() && view instanceof ViewGroup && view.hasFocus()) { + view = ((ViewGroup) view).getFocusedChild(); + final int id = view.getId(); + if (id != View.NO_ID) { + lastKnownId = view.getId(); + } + } + return lastKnownId; + } + + /** + * The first step of a layout where we; + * - process adapter updates + * - decide which animation should run + * - save information about current views + * - If necessary, run predictive layout and save its information + */ + private void dispatchLayoutStep1() { + mState.assertLayoutStep(State.STEP_START); + mState.mIsMeasuring = false; eatRequestLayout(); - mRunningLayoutOrScroll = true; - + mViewInfoStore.clear(); + onEnterLayoutOrScroll(); + saveFocusInfo(); processAdapterUpdatesAndSetAnimationFlags(); - - mState.mOldChangedHolders = mState.mRunSimpleAnimations && mItemsChanged - && supportsChangeAnimations() ? new ArrayMap() : null; + mState.mTrackOldChangeHolders = mState.mRunSimpleAnimations && mItemsChanged; mItemsAddedOrRemoved = mItemsChanged = false; - ArrayMap appearingViewInitialBounds = null; mState.mInPreLayout = mState.mRunPredictiveAnimations; mState.mItemCount = mAdapter.getItemCount(); + findMinMaxChildLayoutPositions(mMinMaxLayoutPositions); if (mState.mRunSimpleAnimations) { // Step 0: Find out where all non-removed items are, pre-layout - mState.mPreLayoutHolderMap.clear(); - mState.mPostLayoutHolderMap.clear(); int count = mChildHelper.getChildCount(); for (int i = 0; i < count; ++i) { final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); if (holder.shouldIgnore() || (holder.isInvalid() && !mAdapter.hasStableIds())) { continue; } - final View view = holder.itemView; - mState.mPreLayoutHolderMap.put(holder, new ItemHolderInfo(holder, - view.getLeft(), view.getTop(), view.getRight(), view.getBottom())); + final ItemHolderInfo animationInfo = mItemAnimator + .recordPreLayoutInformation(mState, holder, + ItemAnimator.buildAdapterChangeFlagsForAnimations(holder), + holder.getUnmodifiedPayloads()); + mViewInfoStore.addToPreLayout(holder, animationInfo); + if (mState.mTrackOldChangeHolders && holder.isUpdated() && !holder.isRemoved() + && !holder.shouldIgnore() && !holder.isInvalid()) { + long key = getChangedHolderKey(holder); + // This is NOT the only place where a ViewHolder is added to old change holders + // list. There is another case where: + // * A VH is currently hidden but not deleted + // * The hidden item is changed in the adapter + // * Layout manager decides to layout the item in the pre-Layout pass (step1) + // When this case is detected, RV will un-hide that view and add to the old + // change holders list. + mViewInfoStore.addToOldChangeHolders(key, holder); + } } } if (mState.mRunPredictiveAnimations) { @@ -1853,63 +3260,53 @@ public class RecyclerView extends ViewGroup { // Save old positions so that LayoutManager can run its mapping logic. saveOldPositions(); - // processAdapterUpdatesAndSetAnimationFlags already run pre-layout animations. - if (mState.mOldChangedHolders != null) { - int count = mChildHelper.getChildCount(); - for (int i = 0; i < count; ++i) { - final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); - if (holder.isChanged() && !holder.isRemoved() && !holder.shouldIgnore()) { - long key = getChangedHolderKey(holder); - mState.mOldChangedHolders.put(key, holder); - mState.mPreLayoutHolderMap.remove(holder); - } - } - } - final boolean didStructureChange = mState.mStructureChanged; mState.mStructureChanged = false; // temporarily disable flag because we are asking for previous layout mLayout.onLayoutChildren(mRecycler, mState); mState.mStructureChanged = didStructureChange; - appearingViewInitialBounds = new ArrayMap(); for (int i = 0; i < mChildHelper.getChildCount(); ++i) { - boolean found = false; - View child = mChildHelper.getChildAt(i); - if (getChildViewHolderInt(child).shouldIgnore()) { + final View child = mChildHelper.getChildAt(i); + final ViewHolder viewHolder = getChildViewHolderInt(child); + if (viewHolder.shouldIgnore()) { continue; } - for (int j = 0; j < mState.mPreLayoutHolderMap.size(); ++j) { - ViewHolder holder = mState.mPreLayoutHolderMap.keyAt(j); - if (holder.itemView == child) { - found = true; - break; + if (!mViewInfoStore.isInPreLayout(viewHolder)) { + int flags = ItemAnimator.buildAdapterChangeFlagsForAnimations(viewHolder); + boolean wasHidden = viewHolder + .hasAnyOfTheFlags(ViewHolder.FLAG_BOUNCED_FROM_HIDDEN_LIST); + if (!wasHidden) { + flags |= ItemAnimator.FLAG_APPEARED_IN_PRE_LAYOUT; + } + final ItemHolderInfo animationInfo = mItemAnimator.recordPreLayoutInformation( + mState, viewHolder, flags, viewHolder.getUnmodifiedPayloads()); + if (wasHidden) { + recordAnimationInfoIfBouncedHiddenView(viewHolder, animationInfo); + } else { + mViewInfoStore.addToAppearedInPreLayoutHolders(viewHolder, animationInfo); } - } - if (!found) { - appearingViewInitialBounds.put(child, new Rect(child.getLeft(), child.getTop(), - child.getRight(), child.getBottom())); } } // we don't process disappearing list because they may re-appear in post layout pass. clearOldPositions(); - mAdapterHelper.consumePostponedUpdates(); } else { clearOldPositions(); - // in case pre layout did run but we decided not to run predictive animations. - mAdapterHelper.consumeUpdatesInOnePass(); - if (mState.mOldChangedHolders != null) { - int count = mChildHelper.getChildCount(); - for (int i = 0; i < count; ++i) { - final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); - if (holder.isChanged() && !holder.isRemoved() && !holder.shouldIgnore()) { - long key = getChangedHolderKey(holder); - mState.mOldChangedHolders.put(key, holder); - mState.mPreLayoutHolderMap.remove(holder); - } - } - } } + onExitLayoutOrScroll(); + resumeRequestLayout(false); + mState.mLayoutStep = State.STEP_LAYOUT; + } + + /** + * The second layout step where we do the actual layout of the views for the final state. + * This step might be run multiple times if necessary (e.g. measure). + */ + private void dispatchLayoutStep2() { + eatRequestLayout(); + onEnterLayoutOrScroll(); + mState.assertLayoutStep(State.STEP_LAYOUT | State.STEP_ANIMATIONS); + mAdapterHelper.consumeUpdatesInOnePass(); mState.mItemCount = mAdapter.getItemCount(); mState.mDeletedInvisibleItemCountSincePreviousLayout = 0; @@ -1922,110 +3319,199 @@ public class RecyclerView extends ViewGroup { // onLayoutChildren may have caused client code to disable item animations; re-check mState.mRunSimpleAnimations = mState.mRunSimpleAnimations && mItemAnimator != null; + mState.mLayoutStep = State.STEP_ANIMATIONS; + onExitLayoutOrScroll(); + resumeRequestLayout(false); + } + /** + * The final step of the layout where we save the information about views for animations, + * trigger animations and do any necessary cleanup. + */ + private void dispatchLayoutStep3() { + mState.assertLayoutStep(State.STEP_ANIMATIONS); + eatRequestLayout(); + onEnterLayoutOrScroll(); + mState.mLayoutStep = State.STEP_START; if (mState.mRunSimpleAnimations) { - // Step 3: Find out where things are now, post-layout - ArrayMap newChangedHolders = mState.mOldChangedHolders != null ? - new ArrayMap() : null; - int count = mChildHelper.getChildCount(); - for (int i = 0; i < count; ++i) { + // Step 3: Find out where things are now, and process change animations. + // traverse list in reverse because we may call animateChange in the loop which may + // remove the target view holder. + for (int i = mChildHelper.getChildCount() - 1; i >= 0; i--) { ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); if (holder.shouldIgnore()) { continue; } - final View view = holder.itemView; long key = getChangedHolderKey(holder); - if (newChangedHolders != null && mState.mOldChangedHolders.get(key) != null) { - newChangedHolders.put(key, holder); + final ItemHolderInfo animationInfo = mItemAnimator + .recordPostLayoutInformation(mState, holder); + ViewHolder oldChangeViewHolder = mViewInfoStore.getFromOldChangeHolders(key); + if (oldChangeViewHolder != null && !oldChangeViewHolder.shouldIgnore()) { + // run a change animation + + // If an Item is CHANGED but the updated version is disappearing, it creates + // a conflicting case. + // Since a view that is marked as disappearing is likely to be going out of + // bounds, we run a change animation. Both views will be cleaned automatically + // once their animations finish. + // On the other hand, if it is the same view holder instance, we run a + // disappearing animation instead because we are not going to rebind the updated + // VH unless it is enforced by the layout manager. + final boolean oldDisappearing = mViewInfoStore.isDisappearing( + oldChangeViewHolder); + final boolean newDisappearing = mViewInfoStore.isDisappearing(holder); + if (oldDisappearing && oldChangeViewHolder == holder) { + // run disappear animation instead of change + mViewInfoStore.addToPostLayout(holder, animationInfo); + } else { + final ItemHolderInfo preInfo = mViewInfoStore.popFromPreLayout( + oldChangeViewHolder); + // we add and remove so that any post info is merged. + mViewInfoStore.addToPostLayout(holder, animationInfo); + ItemHolderInfo postInfo = mViewInfoStore.popFromPostLayout(holder); + if (preInfo == null) { + handleMissingPreInfoForChangeError(key, holder, oldChangeViewHolder); + } else { + animateChange(oldChangeViewHolder, holder, preInfo, postInfo, + oldDisappearing, newDisappearing); + } + } } else { - mState.mPostLayoutHolderMap.put(holder, new ItemHolderInfo(holder, - view.getLeft(), view.getTop(), view.getRight(), view.getBottom())); + mViewInfoStore.addToPostLayout(holder, animationInfo); } } - processDisappearingList(appearingViewInitialBounds); - // Step 4: Animate DISAPPEARING and REMOVED items - int preLayoutCount = mState.mPreLayoutHolderMap.size(); - for (int i = preLayoutCount - 1; i >= 0; i--) { - ViewHolder itemHolder = mState.mPreLayoutHolderMap.keyAt(i); - if (!mState.mPostLayoutHolderMap.containsKey(itemHolder)) { - ItemHolderInfo disappearingItem = mState.mPreLayoutHolderMap.valueAt(i); - mState.mPreLayoutHolderMap.removeAt(i); - View disappearingItemView = disappearingItem.holder.itemView; - removeDetachedView(disappearingItemView, false); - mRecycler.unscrapView(disappearingItem.holder); - - animateDisappearance(disappearingItem); - } - } - // Step 5: Animate APPEARING and ADDED items - int postLayoutCount = mState.mPostLayoutHolderMap.size(); - if (postLayoutCount > 0) { - for (int i = postLayoutCount - 1; i >= 0; i--) { - ViewHolder itemHolder = mState.mPostLayoutHolderMap.keyAt(i); - ItemHolderInfo info = mState.mPostLayoutHolderMap.valueAt(i); - if ((mState.mPreLayoutHolderMap.isEmpty() || - !mState.mPreLayoutHolderMap.containsKey(itemHolder))) { - mState.mPostLayoutHolderMap.removeAt(i); - Rect initialBounds = (appearingViewInitialBounds != null) ? - appearingViewInitialBounds.get(itemHolder.itemView) : null; - animateAppearance(itemHolder, initialBounds, - info.left, info.top); - } - } - } - // Step 6: Animate PERSISTENT items - count = mState.mPostLayoutHolderMap.size(); - for (int i = 0; i < count; ++i) { - ViewHolder postHolder = mState.mPostLayoutHolderMap.keyAt(i); - ItemHolderInfo postInfo = mState.mPostLayoutHolderMap.valueAt(i); - ItemHolderInfo preInfo = mState.mPreLayoutHolderMap.get(postHolder); - if (preInfo != null && postInfo != null) { - if (preInfo.left != postInfo.left || preInfo.top != postInfo.top) { - postHolder.setIsRecyclable(false); - if (DEBUG) { - Log.d(TAG, "PERSISTENT: " + postHolder + - " with view " + postHolder.itemView); - } - if (mItemAnimator.animateMove(postHolder, - preInfo.left, preInfo.top, postInfo.left, postInfo.top)) { - postAnimationRunner(); - } - } - } - } - // Step 7: Animate CHANGING items - count = mState.mOldChangedHolders != null ? mState.mOldChangedHolders.size() : 0; - // traverse reverse in case view gets recycled while we are traversing the list. - for (int i = count - 1; i >= 0; i--) { - long key = mState.mOldChangedHolders.keyAt(i); - ViewHolder oldHolder = mState.mOldChangedHolders.get(key); - View oldView = oldHolder.itemView; - if (oldHolder.shouldIgnore()) { - continue; - } - // We probably don't need this check anymore since these views are removed from - // the list if they are recycled. - if (mRecycler.mChangedScrap != null && - mRecycler.mChangedScrap.contains(oldHolder)) { - animateChange(oldHolder, newChangedHolders.get(key)); - } else if (DEBUG) { - Log.e(TAG, "cannot find old changed holder in changed scrap :/" + oldHolder); - } - } + // Step 4: Process view info lists and trigger animations + mViewInfoStore.process(mViewInfoProcessCallback); } - resumeRequestLayout(false); - mLayout.removeAndRecycleScrapInt(mRecycler, !mState.mRunPredictiveAnimations); + + mLayout.removeAndRecycleScrapInt(mRecycler); mState.mPreviousLayoutItemCount = mState.mItemCount; mDataSetHasChangedAfterLayout = false; mState.mRunSimpleAnimations = false; + mState.mRunPredictiveAnimations = false; - mRunningLayoutOrScroll = false; mLayout.mRequestedSimpleAnimations = false; if (mRecycler.mChangedScrap != null) { mRecycler.mChangedScrap.clear(); } - mState.mOldChangedHolders = null; + mLayout.onLayoutCompleted(mState); + onExitLayoutOrScroll(); + resumeRequestLayout(false); + mViewInfoStore.clear(); + if (didChildRangeChange(mMinMaxLayoutPositions[0], mMinMaxLayoutPositions[1])) { + dispatchOnScrolled(0, 0); + } + recoverFocusFromState(); + resetFocusInfo(); + } + + /** + * This handles the case where there is an unexpected VH missing in the pre-layout map. + *

    + * We might be able to detect the error in the application which will help the developer to + * resolve the issue. + *

    + * If it is not an expected error, we at least print an error to notify the developer and ignore + * the animation. + * + * https://code.google.com/p/android/issues/detail?id=193958 + * + * @param key The change key + * @param holder Current ViewHolder + * @param oldChangeViewHolder Changed ViewHolder + */ + private void handleMissingPreInfoForChangeError(long key, + ViewHolder holder, ViewHolder oldChangeViewHolder) { + // check if two VH have the same key, if so, print that as an error + final int childCount = mChildHelper.getChildCount(); + for (int i = 0; i < childCount; i++) { + View view = mChildHelper.getChildAt(i); + ViewHolder other = getChildViewHolderInt(view); + if (other == holder) { + continue; + } + final long otherKey = getChangedHolderKey(other); + if (otherKey == key) { + if (mAdapter != null && mAdapter.hasStableIds()) { + throw new IllegalStateException("Two different ViewHolders have the same stable" + + " ID. Stable IDs in your adapter MUST BE unique and SHOULD NOT" + + " change.\n ViewHolder 1:" + other + " \n View Holder 2:" + holder); + } else { + throw new IllegalStateException("Two different ViewHolders have the same change" + + " ID. This might happen due to inconsistent Adapter update events or" + + " if the LayoutManager lays out the same View multiple times." + + "\n ViewHolder 1:" + other + " \n View Holder 2:" + holder); + } + } + } + // Very unlikely to happen but if it does, notify the developer. + Log.e(TAG, "Problem while matching changed view holders with the new" + + "ones. The pre-layout information for the change holder " + oldChangeViewHolder + + " cannot be found but it is necessary for " + holder); + } + + /** + * Records the animation information for a view holder that was bounced from hidden list. It + * also clears the bounce back flag. + */ + private void recordAnimationInfoIfBouncedHiddenView(ViewHolder viewHolder, + ItemHolderInfo animationInfo) { + // looks like this view bounced back from hidden list! + viewHolder.setFlags(0, ViewHolder.FLAG_BOUNCED_FROM_HIDDEN_LIST); + if (mState.mTrackOldChangeHolders && viewHolder.isUpdated() + && !viewHolder.isRemoved() && !viewHolder.shouldIgnore()) { + long key = getChangedHolderKey(viewHolder); + mViewInfoStore.addToOldChangeHolders(key, viewHolder); + } + mViewInfoStore.addToPreLayout(viewHolder, animationInfo); + } + + private void findMinMaxChildLayoutPositions(int[] into) { + final int count = mChildHelper.getChildCount(); + if (count == 0) { + into[0] = NO_POSITION; + into[1] = NO_POSITION; + return; + } + int minPositionPreLayout = Integer.MAX_VALUE; + int maxPositionPreLayout = Integer.MIN_VALUE; + for (int i = 0; i < count; ++i) { + final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); + if (holder.shouldIgnore()) { + continue; + } + final int pos = holder.getLayoutPosition(); + if (pos < minPositionPreLayout) { + minPositionPreLayout = pos; + } + if (pos > maxPositionPreLayout) { + maxPositionPreLayout = pos; + } + } + into[0] = minPositionPreLayout; + into[1] = maxPositionPreLayout; + } + + private boolean didChildRangeChange(int minPositionPreLayout, int maxPositionPreLayout) { + findMinMaxChildLayoutPositions(mMinMaxLayoutPositions); + return mMinMaxLayoutPositions[0] != minPositionPreLayout || + mMinMaxLayoutPositions[1] != maxPositionPreLayout; + } + + @Override + protected void removeDetachedView(View child, boolean animate) { + ViewHolder vh = getChildViewHolderInt(child); + if (vh != null) { + if (vh.isTmpDetached()) { + vh.clearTmpDetachFlag(); + } else if (!vh.shouldIgnore()) { + throw new IllegalArgumentException("Called removeDetachedView with a view which" + + " is not flagged as tmp detached." + vh); + } + } + dispatchChildDetached(child); + super.removeDetachedView(child, animate); } /** @@ -2036,130 +3522,57 @@ public class RecyclerView extends ViewGroup { return mAdapter.hasStableIds() ? holder.getItemId() : holder.mPosition; } - /** - * A LayoutManager may want to layout a view just to animate disappearance. - * This method handles those views and triggers remove animation on them. - */ - private void processDisappearingList(ArrayMap appearingViews) { - final int count = mDisappearingViewsInLayoutPass.size(); - for (int i = 0; i < count; i ++) { - View view = mDisappearingViewsInLayoutPass.get(i); - ViewHolder vh = getChildViewHolderInt(view); - final ItemHolderInfo info = mState.mPreLayoutHolderMap.remove(vh); - if (!mState.isPreLayout()) { - mState.mPostLayoutHolderMap.remove(vh); - } - if (appearingViews.remove(view) != null) { - mLayout.removeAndRecycleView(view, mRecycler); - continue; - } - if (info != null) { - animateDisappearance(info); - } else { - // let it disappear from the position it becomes visible - animateDisappearance(new ItemHolderInfo(vh, view.getLeft(), view.getTop(), - view.getRight(), view.getBottom())); - } - } - mDisappearingViewsInLayoutPass.clear(); - } - - private void animateAppearance(ViewHolder itemHolder, Rect beforeBounds, int afterLeft, - int afterTop) { - View newItemView = itemHolder.itemView; - if (beforeBounds != null && - (beforeBounds.left != afterLeft || beforeBounds.top != afterTop)) { - // slide items in if before/after locations differ - itemHolder.setIsRecyclable(false); - if (DEBUG) { - Log.d(TAG, "APPEARING: " + itemHolder + " with view " + newItemView); - } - if (mItemAnimator.animateMove(itemHolder, - beforeBounds.left, beforeBounds.top, - afterLeft, afterTop)) { - postAnimationRunner(); - } - } else { - if (DEBUG) { - Log.d(TAG, "ADDED: " + itemHolder + " with view " + newItemView); - } - itemHolder.setIsRecyclable(false); - if (mItemAnimator.animateAdd(itemHolder)) { - postAnimationRunner(); - } + private void animateAppearance(@NonNull ViewHolder itemHolder, + @Nullable ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) { + itemHolder.setIsRecyclable(false); + if (mItemAnimator.animateAppearance(itemHolder, preLayoutInfo, postLayoutInfo)) { + postAnimationRunner(); } } - private void animateDisappearance(ItemHolderInfo disappearingItem) { - View disappearingItemView = disappearingItem.holder.itemView; - addAnimatingView(disappearingItemView); - int oldLeft = disappearingItem.left; - int oldTop = disappearingItem.top; - int newLeft = disappearingItemView.getLeft(); - int newTop = disappearingItemView.getTop(); - if (oldLeft != newLeft || oldTop != newTop) { - disappearingItem.holder.setIsRecyclable(false); - disappearingItemView.layout(newLeft, newTop, - newLeft + disappearingItemView.getWidth(), - newTop + disappearingItemView.getHeight()); - if (DEBUG) { - Log.d(TAG, "DISAPPEARING: " + disappearingItem.holder + - " with view " + disappearingItemView); - } - if (mItemAnimator.animateMove(disappearingItem.holder, oldLeft, oldTop, - newLeft, newTop)) { - postAnimationRunner(); - } - } else { - if (DEBUG) { - Log.d(TAG, "REMOVED: " + disappearingItem.holder + - " with view " + disappearingItemView); - } - disappearingItem.holder.setIsRecyclable(false); - if (mItemAnimator.animateRemove(disappearingItem.holder)) { - postAnimationRunner(); - } + private void animateDisappearance(@NonNull ViewHolder holder, + @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo) { + addAnimatingView(holder); + holder.setIsRecyclable(false); + if (mItemAnimator.animateDisappearance(holder, preLayoutInfo, postLayoutInfo)) { + postAnimationRunner(); } } - private void animateChange(ViewHolder oldHolder, ViewHolder newHolder) { + private void animateChange(@NonNull ViewHolder oldHolder, @NonNull ViewHolder newHolder, + @NonNull ItemHolderInfo preInfo, @NonNull ItemHolderInfo postInfo, + boolean oldHolderDisappearing, boolean newHolderDisappearing) { oldHolder.setIsRecyclable(false); - removeDetachedView(oldHolder.itemView, false); - addAnimatingView(oldHolder.itemView); - oldHolder.mShadowedHolder = newHolder; - mRecycler.unscrapView(oldHolder); - if (DEBUG) { - Log.d(TAG, "CHANGED: " + oldHolder + " with view " + oldHolder.itemView); + if (oldHolderDisappearing) { + addAnimatingView(oldHolder); } - final int fromLeft = oldHolder.itemView.getLeft(); - final int fromTop = oldHolder.itemView.getTop(); - final int toLeft, toTop; - if (newHolder == null || newHolder.shouldIgnore()) { - toLeft = fromLeft; - toTop = fromTop; - } else { - toLeft = newHolder.itemView.getLeft(); - toTop = newHolder.itemView.getTop(); + if (oldHolder != newHolder) { + if (newHolderDisappearing) { + addAnimatingView(newHolder); + } + oldHolder.mShadowedHolder = newHolder; + // old holder should disappear after animation ends + addAnimatingView(oldHolder); + mRecycler.unscrapView(oldHolder); newHolder.setIsRecyclable(false); newHolder.mShadowingHolder = oldHolder; } - if(mItemAnimator.animateChange(oldHolder, newHolder, - fromLeft, fromTop, toLeft, toTop)) { + if (mItemAnimator.animateChange(oldHolder, newHolder, preInfo, postInfo)) { postAnimationRunner(); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { - eatRequestLayout(); + TraceCompat.beginSection(TRACE_ON_LAYOUT_TAG); dispatchLayout(); - resumeRequestLayout(false); + TraceCompat.endSection(); mFirstLayoutComplete = true; } @Override public void requestLayout() { - if (!mEatRequestLayout) { + if (mEatRequestLayout == 0 && !mLayoutFrozen) { super.requestLayout(); } else { mLayoutRequestEaten = true; @@ -2183,7 +3596,7 @@ public class RecyclerView extends ViewGroup { for (int i = 0; i < count; i++) { mItemDecorations.get(i).onDrawOver(c, this, mState); } - // TODO If padding is not 0 and chilChildrenToPadding is false, to draw glows properly, we + // TODO If padding is not 0 and clipChildrenToPadding is false, to draw glows properly, we // need find children closest to edges. Not sure if it is worth the effort. boolean needsInvalidate = false; if (mLeftGlow != null && !mLeftGlow.isFinished()) { @@ -2275,6 +3688,18 @@ public class RecyclerView extends ViewGroup { return mLayout.generateLayoutParams(p); } + /** + * Returns true if RecyclerView is currently running some animations. + *

    + * If you want to be notified when animations are finished, use + * {@link ItemAnimator#isRunning(ItemAnimator.ItemAnimatorFinishedListener)}. + * + * @return True if there are some item animations currently running or waiting to be started. + */ + public boolean isAnimating() { + return mItemAnimator != null && mItemAnimator.isRunning(); + } + void saveOldPositions() { final int childCount = mChildHelper.getUnfilteredChildCount(); for (int i = 0; i < childCount; i++) { @@ -2387,7 +3812,7 @@ public class RecyclerView extends ViewGroup { * @param positionStart Adapter position to start at * @param itemCount Number of views that must explicitly be rebound */ - void viewRangeUpdate(int positionStart, int itemCount) { + void viewRangeUpdate(int positionStart, int itemCount, Object payload) { final int childCount = mChildHelper.getUnfilteredChildCount(); final int positionEnd = positionStart + itemCount; @@ -2401,9 +3826,7 @@ public class RecyclerView extends ViewGroup { // We re-bind these view holders after pre-processing is complete so that // ViewHolders have their final positions assigned. holder.addFlags(ViewHolder.FLAG_UPDATE); - if (supportsChangeAnimations()) { - holder.addFlags(ViewHolder.FLAG_CHANGED); - } + holder.addChangePayload(payload); // lp cannot be null since we get ViewHolder from it. ((LayoutParams) child.getLayoutParams()).mInsetsDirty = true; } @@ -2411,36 +3834,24 @@ public class RecyclerView extends ViewGroup { mRecycler.viewRangeUpdate(positionStart, itemCount); } - void rebindUpdatedViewHolders() { - final int childCount = mChildHelper.getChildCount(); + private boolean canReuseUpdatedViewHolder(ViewHolder viewHolder) { + return mItemAnimator == null || mItemAnimator.canReuseUpdatedViewHolder(viewHolder, + viewHolder.getUnmodifiedPayloads()); + } + + private void setDataSetChangedAfterLayout() { + if (mDataSetHasChangedAfterLayout) { + return; + } + mDataSetHasChangedAfterLayout = true; + final int childCount = mChildHelper.getUnfilteredChildCount(); for (int i = 0; i < childCount; i++) { - final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); - // validate type is correct - if (holder == null || holder.shouldIgnore()) { - continue; - } - if (holder.isRemoved() || holder.isInvalid()) { - requestLayout(); - } else if (holder.needsUpdate()) { - final int type = mAdapter.getItemViewType(holder.mPosition); - if (holder.getItemViewType() == type) { - // Binding an attached view will request a layout if needed. - if (!holder.isChanged() || !supportsChangeAnimations()) { - mAdapter.bindViewHolder(holder, holder.mPosition); - } else { - // Don't rebind changed holders if change animations are enabled. - // We want the old contents for the animation and will get a new - // holder for the new contents. - requestLayout(); - } - } else { - // binding to a new view will need re-layout anyways. We can as well trigger - // it here so that it happens during layout - holder.addFlags(ViewHolder.FLAG_INVALID); - requestLayout(); - } + final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i)); + if (holder != null && !holder.shouldIgnore()) { + holder.addFlags(ViewHolder.FLAG_ADAPTER_POSITION_UNKNOWN); } } + mRecycler.setAdapterPositionsAsUnknown(); } /** @@ -2475,6 +3886,39 @@ public class RecyclerView extends ViewGroup { requestLayout(); } + /** + * Returns true if the RecyclerView should attempt to preserve currently focused Adapter Item's + * focus even if the View representing the Item is replaced during a layout calculation. + *

    + * By default, this value is {@code true}. + * + * @return True if the RecyclerView will try to preserve focused Item after a layout if it loses + * focus. + * + * @see #setPreserveFocusAfterLayout(boolean) + */ + public boolean getPreserveFocusAfterLayout() { + return mPreserveFocusAfterLayout; + } + + /** + * Set whether the RecyclerView should try to keep the same Item focused after a layout + * calculation or not. + *

    + * Usually, LayoutManagers keep focused views visible before and after layout but sometimes, + * views may lose focus during a layout calculation as their state changes or they are replaced + * with another view due to type change or animation. In these cases, RecyclerView can request + * focus on the new view automatically. + * + * @param preserveFocusAfterLayout Whether RecyclerView should preserve focused Item during a + * layout calculations. Defaults to true. + * + * @see #getPreserveFocusAfterLayout() + */ + public void setPreserveFocusAfterLayout(boolean preserveFocusAfterLayout) { + mPreserveFocusAfterLayout = preserveFocusAfterLayout; + } + /** * Retrieve the {@link ViewHolder} for the given child view. * @@ -2490,6 +3934,44 @@ public class RecyclerView extends ViewGroup { return getChildViewHolderInt(child); } + /** + * Traverses the ancestors of the given view and returns the item view that contains it and + * also a direct child of the RecyclerView. This returned view can be used to get the + * ViewHolder by calling {@link #getChildViewHolder(View)}. + * + * @param view The view that is a descendant of the RecyclerView. + * + * @return The direct child of the RecyclerView which contains the given view or null if the + * provided view is not a descendant of this RecyclerView. + * + * @see #getChildViewHolder(View) + * @see #findContainingViewHolder(View) + */ + @Nullable + public View findContainingItemView(View view) { + ViewParent parent = view.getParent(); + while (parent != null && parent != this && parent instanceof View) { + view = (View) parent; + parent = view.getParent(); + } + return parent == this ? view : null; + } + + /** + * Returns the ViewHolder that contains the given view. + * + * @param view The view that is a descendant of the RecyclerView. + * + * @return The ViewHolder that contains the given view or null if the provided view is not a + * descendant of this RecyclerView. + */ + @Nullable + public ViewHolder findContainingViewHolder(View view) { + View itemView = findContainingItemView(view); + return itemView == null ? null : getChildViewHolder(itemView); + } + + static ViewHolder getChildViewHolderInt(View child) { if (child == null) { return null; @@ -2497,15 +3979,39 @@ public class RecyclerView extends ViewGroup { return ((LayoutParams) child.getLayoutParams()).mViewHolder; } + /** + * @deprecated use {@link #getChildAdapterPosition(View)} or + * {@link #getChildLayoutPosition(View)}. + */ + @Deprecated + public int getChildPosition(View child) { + return getChildAdapterPosition(child); + } + /** * Return the adapter position that the given child view corresponds to. * * @param child Child View to query * @return Adapter position corresponding to the given view or {@link #NO_POSITION} */ - public int getChildPosition(View child) { + public int getChildAdapterPosition(View child) { final ViewHolder holder = getChildViewHolderInt(child); - return holder != null ? holder.getPosition() : NO_POSITION; + return holder != null ? holder.getAdapterPosition() : NO_POSITION; + } + + /** + * Return the adapter position of the given child view as of the latest completed layout pass. + *

    + * This position may not be equal to Item's adapter position if there are pending changes + * in the adapter which have not been reflected to the layout yet. + * + * @param child Child View to query + * @return Adapter position of the given View as of last layout pass or {@link #NO_POSITION} if + * the View is representing a removed item. + */ + public int getChildLayoutPosition(View child) { + final ViewHolder holder = getChildViewHolderInt(child); + return holder != null ? holder.getLayoutPosition() : NO_POSITION; } /** @@ -2523,25 +4029,90 @@ public class RecyclerView extends ViewGroup { } /** - * Return the ViewHolder for the item in the given position of the data set. - * - * @param position The position of the item in the data set of the adapter - * @return The ViewHolder at position + * @deprecated use {@link #findViewHolderForLayoutPosition(int)} or + * {@link #findViewHolderForAdapterPosition(int)} */ + @Deprecated public ViewHolder findViewHolderForPosition(int position) { return findViewHolderForPosition(position, false); } + /** + * Return the ViewHolder for the item in the given position of the data set as of the latest + * layout pass. + *

    + * This method checks only the children of RecyclerView. If the item at the given + * position is not laid out, it will not create a new one. + *

    + * Note that when Adapter contents change, ViewHolder positions are not updated until the + * next layout calculation. If there are pending adapter updates, the return value of this + * method may not match your adapter contents. You can use + * #{@link ViewHolder#getAdapterPosition()} to get the current adapter position of a ViewHolder. + *

    + * When the ItemAnimator is running a change animation, there might be 2 ViewHolders + * with the same layout position representing the same Item. In this case, the updated + * ViewHolder will be returned. + * + * @param position The position of the item in the data set of the adapter + * @return The ViewHolder at position or null if there is no such item + */ + public ViewHolder findViewHolderForLayoutPosition(int position) { + return findViewHolderForPosition(position, false); + } + + /** + * Return the ViewHolder for the item in the given position of the data set. Unlike + * {@link #findViewHolderForLayoutPosition(int)} this method takes into account any pending + * adapter changes that may not be reflected to the layout yet. On the other hand, if + * {@link Adapter#notifyDataSetChanged()} has been called but the new layout has not been + * calculated yet, this method will return null since the new positions of views + * are unknown until the layout is calculated. + *

    + * This method checks only the children of RecyclerView. If the item at the given + * position is not laid out, it will not create a new one. + *

    + * When the ItemAnimator is running a change animation, there might be 2 ViewHolders + * representing the same Item. In this case, the updated ViewHolder will be returned. + * + * @param position The position of the item in the data set of the adapter + * @return The ViewHolder at position or null if there is no such item + */ + public ViewHolder findViewHolderForAdapterPosition(int position) { + if (mDataSetHasChangedAfterLayout) { + return null; + } + final int childCount = mChildHelper.getUnfilteredChildCount(); + // hidden VHs are not preferred but if that is the only one we find, we rather return it + ViewHolder hidden = null; + for (int i = 0; i < childCount; i++) { + final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i)); + if (holder != null && !holder.isRemoved() && getAdapterPositionFor(holder) == position) { + if (mChildHelper.isHidden(holder.itemView)) { + hidden = holder; + } else { + return holder; + } + } + } + return hidden; + } + ViewHolder findViewHolderForPosition(int position, boolean checkNewPosition) { final int childCount = mChildHelper.getUnfilteredChildCount(); + ViewHolder hidden = null; for (int i = 0; i < childCount; i++) { final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i)); if (holder != null && !holder.isRemoved()) { if (checkNewPosition) { - if (holder.mPosition == position) { - return holder; + if (holder.mPosition != position) { + continue; } - } else if (holder.getPosition() == position) { + } else if (holder.getLayoutPosition() != position) { + continue; + } + if (mChildHelper.isHidden(holder.itemView)) { + hidden = holder; + } else { return holder; } } @@ -2549,29 +4120,40 @@ public class RecyclerView extends ViewGroup { // This method should not query cached views. It creates a problem during adapter updates // when we are dealing with already laid out views. Also, for the public method, it is more // reasonable to return null if position is not laid out. - return null; + return hidden; } /** * Return the ViewHolder for the item with the given id. The RecyclerView must * use an Adapter with {@link Adapter#setHasStableIds(boolean) stableIds} to * return a non-null value. + *

    + * This method checks only the children of RecyclerView. If the item with the given + * id is not laid out, it will not create a new one. + * + * When the ItemAnimator is running a change animation, there might be 2 ViewHolders with the + * same id. In this case, the updated ViewHolder will be returned. * * @param id The id for the requested item - * @return The ViewHolder with the given id, of null if there - * is no such item. + * @return The ViewHolder with the given id or null if there is no such item */ public ViewHolder findViewHolderForItemId(long id) { + if (mAdapter == null || !mAdapter.hasStableIds()) { + return null; + } final int childCount = mChildHelper.getUnfilteredChildCount(); + ViewHolder hidden = null; for (int i = 0; i < childCount; i++) { final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i)); - if (holder != null && holder.getItemId() == id) { - return holder; + if (holder != null && !holder.isRemoved() && holder.getItemId() == id) { + if (mChildHelper.isHidden(holder.itemView)) { + hidden = holder; + } else { + return holder; + } } } - // this method should not query cached views. They are not children so they - // should not be returned in this public method - return null; + return hidden; } /** @@ -2597,6 +4179,11 @@ public class RecyclerView extends ViewGroup { return null; } + @Override + public boolean drawChild(Canvas canvas, View child, long drawingTime) { + return super.drawChild(canvas, child, drawingTime); + } + /** * Offset the bounds of all child views by dy pixels. * Useful for implementing simple scrolling in {@link LayoutManager LayoutManagers}. @@ -2654,6 +4241,10 @@ public class RecyclerView extends ViewGroup { return lp.mDecorInsets; } + if (mState.isPreLayout() && (lp.isItemChanged() || lp.isViewInvalid())) { + // changed/invalid items should not be updated until they are rebound. + return lp.mDecorInsets; + } final Rect insets = lp.mDecorInsets; insets.set(0, 0, 0, 0); final int decorCount = mItemDecorations.size(); @@ -2669,6 +4260,108 @@ public class RecyclerView extends ViewGroup { return insets; } + /** + * Called when the scroll position of this RecyclerView changes. Subclasses should use + * this method to respond to scrolling within the adapter's data set instead of an explicit + * listener. + * + *

    This method will always be invoked before listeners. If a subclass needs to perform + * any additional upkeep or bookkeeping after scrolling but before listeners run, + * this is a good place to do so.

    + * + *

    This differs from {@link View#onScrollChanged(int, int, int, int)} in that it receives + * the distance scrolled in either direction within the adapter's data set instead of absolute + * scroll coordinates. Since RecyclerView cannot compute the absolute scroll position from + * any arbitrary point in the data set, onScrollChanged will always receive + * the current {@link View#getScrollX()} and {@link View#getScrollY()} values which + * do not correspond to the data set scroll position. However, some subclasses may choose + * to use these fields as special offsets.

    + * + * @param dx horizontal distance scrolled in pixels + * @param dy vertical distance scrolled in pixels + */ + public void onScrolled(int dx, int dy) { + // Do nothing + } + + void dispatchOnScrolled(int hresult, int vresult) { + mDispatchScrollCounter ++; + // Pass the current scrollX/scrollY values; no actual change in these properties occurred + // but some general-purpose code may choose to respond to changes this way. + final int scrollX = getScrollX(); + final int scrollY = getScrollY(); + onScrollChanged(scrollX, scrollY, scrollX, scrollY); + + // Pass the real deltas to onScrolled, the RecyclerView-specific method. + onScrolled(hresult, vresult); + + // Invoke listeners last. Subclassed view methods always handle the event first. + // All internal state is consistent by the time listeners are invoked. + if (mScrollListener != null) { + mScrollListener.onScrolled(this, hresult, vresult); + } + if (mScrollListeners != null) { + for (int i = mScrollListeners.size() - 1; i >= 0; i--) { + mScrollListeners.get(i).onScrolled(this, hresult, vresult); + } + } + mDispatchScrollCounter --; + } + + /** + * Called when the scroll state of this RecyclerView changes. Subclasses should use this + * method to respond to state changes instead of an explicit listener. + * + *

    This method will always be invoked before listeners, but after the LayoutManager + * responds to the scroll state change.

    + * + * @param state the new scroll state, one of {@link #SCROLL_STATE_IDLE}, + * {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING} + */ + public void onScrollStateChanged(int state) { + // Do nothing + } + + void dispatchOnScrollStateChanged(int state) { + // Let the LayoutManager go first; this allows it to bring any properties into + // a consistent state before the RecyclerView subclass responds. + if (mLayout != null) { + mLayout.onScrollStateChanged(state); + } + + // Let the RecyclerView subclass handle this event next; any LayoutManager property + // changes will be reflected by this time. + onScrollStateChanged(state); + + // Listeners go last. All other internal state is consistent by this point. + if (mScrollListener != null) { + mScrollListener.onScrollStateChanged(this, state); + } + if (mScrollListeners != null) { + for (int i = mScrollListeners.size() - 1; i >= 0; i--) { + mScrollListeners.get(i).onScrollStateChanged(this, state); + } + } + } + + /** + * Returns whether there are pending adapter updates which are not yet applied to the layout. + *

    + * If this method returns true, it means that what user is currently seeing may not + * reflect them adapter contents (depending on what has changed). + * You may use this information to defer or cancel some operations. + *

    + * This method returns true if RecyclerView has not yet calculated the first layout after it is + * attached to the Window or the Adapter has been replaced. + * + * @return True if there are some adapter updates which are not yet reflected to layout or false + * if layout is up to date. + */ + public boolean hasPendingAdapterUpdates() { + return !mFirstLayoutComplete || mDataSetHasChangedAfterLayout + || mAdapterHelper.hasPendingUpdates(); + } + private class ViewFlinger implements Runnable { private int mLastFlingX; private int mLastFlingY; @@ -2688,6 +4381,10 @@ public class RecyclerView extends ViewGroup { @Override public void run() { + if (mLayout == null) { + stop(); + return; // no layout, cannot scroll. + } disableRunOnAnimationRequests(); consumePendingUpdateOperations(); // keep a local reference so that if it is changed during onAnimation method, it won't @@ -2706,7 +4403,8 @@ public class RecyclerView extends ViewGroup { int overscrollX = 0, overscrollY = 0; if (mAdapter != null) { eatRequestLayout(); - mRunningLayoutOrScroll = true; + onEnterLayoutOrScroll(); + TraceCompat.beginSection(TRACE_SCROLL_TAG); if (dx != 0) { hresult = mLayout.scrollHorizontallyBy(dx, mRecycler, mState); overscrollX = dx - hresult; @@ -2715,28 +4413,11 @@ public class RecyclerView extends ViewGroup { vresult = mLayout.scrollVerticallyBy(dy, mRecycler, mState); overscrollY = dy - vresult; } - if (supportsChangeAnimations()) { - // Fix up shadow views used by changing animations - int count = mChildHelper.getChildCount(); - for (int i = 0; i < count; i++) { - View view = mChildHelper.getChildAt(i); - ViewHolder holder = getChildViewHolder(view); - if (holder != null && holder.mShadowingHolder != null) { - View shadowingView = holder.mShadowingHolder != null ? - holder.mShadowingHolder.itemView : null; - if (shadowingView != null) { - int left = view.getLeft(); - int top = view.getTop(); - if (left != shadowingView.getLeft() || - top != shadowingView.getTop()) { - shadowingView.layout(left, top, - left + shadowingView.getWidth(), - top + shadowingView.getHeight()); - } - } - } - } - } + TraceCompat.endSection(); + repositionShadowingViews(); + + onExitLayoutOrScroll(); + resumeRequestLayout(false); if (smoothScroller != null && !smoothScroller.isPendingInitialRun() && smoothScroller.isRunning()) { @@ -2750,15 +4431,11 @@ public class RecyclerView extends ViewGroup { smoothScroller.onAnimation(dx - overscrollX, dy - overscrollY); } } - mRunningLayoutOrScroll = false; - resumeRequestLayout(false); } - final boolean fullyConsumedScroll = dx == hresult && dy == vresult; if (!mItemDecorations.isEmpty()) { invalidate(); } - if (ViewCompat.getOverScrollMode(RecyclerView.this) != - ViewCompat.OVER_SCROLL_NEVER) { + if (getOverScrollMode() != View.OVER_SCROLL_NEVER) { considerReleasingGlowsOnScroll(dx, dy); } if (overscrollX != 0 || overscrollY != 0) { @@ -2774,8 +4451,7 @@ public class RecyclerView extends ViewGroup { velY = overscrollY < 0 ? -vel : overscrollY > 0 ? vel : 0; } - if (ViewCompat.getOverScrollMode(RecyclerView.this) != - ViewCompat.OVER_SCROLL_NEVER) { + if (getOverScrollMode() != View.OVER_SCROLL_NEVER) { absorbGlows(velX, velY); } if ((velX != 0 || overscrollX == x || scroller.getFinalX() == 0) && @@ -2784,26 +4460,34 @@ public class RecyclerView extends ViewGroup { } } if (hresult != 0 || vresult != 0) { - // dummy values, View's implementation does not use these. - onScrollChanged(0, 0, 0, 0); - if (mScrollListener != null) { - mScrollListener.onScrolled(RecyclerView.this, hresult, vresult); - } + dispatchOnScrolled(hresult, vresult); } if (!awakenScrollBars()) { invalidate(); } - if (scroller.isFinished() || !fullyConsumedScroll) { + final boolean fullyConsumedVertical = dy != 0 && mLayout.canScrollVertically() + && vresult == dy; + final boolean fullyConsumedHorizontal = dx != 0 && mLayout.canScrollHorizontally() + && hresult == dx; + final boolean fullyConsumedAny = (dx == 0 && dy == 0) || fullyConsumedHorizontal + || fullyConsumedVertical; + + if (scroller.isFinished() || !fullyConsumedAny) { setScrollState(SCROLL_STATE_IDLE); // setting state to idle will stop this. } else { postOnAnimation(); } } // call this after the onAnimation is complete not to have inconsistent callbacks etc. - if (smoothScroller != null && smoothScroller.isPendingInitialRun()) { - smoothScroller.onAnimation(0, 0); + if (smoothScroller != null) { + if (smoothScroller.isPendingInitialRun()) { + smoothScroller.onAnimation(0, 0); + } + if (!mReSchedulePostAnimationCallback) { + smoothScroller.stop(); //stop if it does not trigger any scroll + } } enableRunOnAnimationRequests(); } @@ -2824,6 +4508,7 @@ public class RecyclerView extends ViewGroup { if (mEatRunOnAnimationRequest) { mReSchedulePostAnimationCallback = true; } else { + removeCallbacks(this); ViewCompat.postOnAnimation(RecyclerView.this, this); } } @@ -2894,6 +4579,26 @@ public class RecyclerView extends ViewGroup { } + private void repositionShadowingViews() { + // Fix up shadow views used by change animations + int count = mChildHelper.getChildCount(); + for (int i = 0; i < count; i++) { + View view = mChildHelper.getChildAt(i); + ViewHolder holder = getChildViewHolder(view); + if (holder != null && holder.mShadowingHolder != null) { + View shadowingView = holder.mShadowingHolder.itemView; + int left = view.getLeft(); + int top = view.getTop(); + if (left != shadowingView.getLeft() || + top != shadowingView.getTop()) { + shadowingView.layout(left, top, + left + shadowingView.getWidth(), + top + shadowingView.getHeight()); + } + } + } + } + private class RecyclerViewDataObserver extends AdapterDataObserver { @Override public void onChanged() { @@ -2903,10 +4608,10 @@ public class RecyclerView extends ViewGroup { // This is more important to implement now since this callback will disable all // animations because we cannot rely on positions. mState.mStructureChanged = true; - mDataSetHasChangedAfterLayout = true; + setDataSetChangedAfterLayout(); } else { mState.mStructureChanged = true; - mDataSetHasChangedAfterLayout = true; + setDataSetChangedAfterLayout(); } if (!mAdapterHelper.hasPendingUpdates()) { requestLayout(); @@ -2914,9 +4619,9 @@ public class RecyclerView extends ViewGroup { } @Override - public void onItemRangeChanged(int positionStart, int itemCount) { + public void onItemRangeChanged(int positionStart, int itemCount, Object payload) { assertNotInLayoutOrScroll(null); - if (mAdapterHelper.onItemRangeChanged(positionStart, itemCount)) { + if (mAdapterHelper.onItemRangeChanged(positionStart, itemCount, payload)) { triggerUpdateProcessor(); } } @@ -3014,6 +4719,9 @@ public class RecyclerView extends ViewGroup { if (mMaxScrap.get(viewType) <= scrapHeap.size()) { return; } + if (DEBUG && scrapHeap.contains(scrap)) { + throw new IllegalArgumentException("this scrap item already exists"); + } scrap.resetInternal(); scrapHeap.add(scrap); } @@ -3054,7 +4762,7 @@ public class RecyclerView extends ViewGroup { private ArrayList getScrapHeapForType(int viewType) { ArrayList scrap = mScrap.get(viewType); if (scrap == null) { - scrap = new ArrayList(); + scrap = new ArrayList<>(); mScrap.put(viewType, scrap); if (mMaxScrap.indexOfKey(viewType) < 0) { mMaxScrap.put(viewType, DEFAULT_MAX_SCRAP); @@ -3074,11 +4782,11 @@ public class RecyclerView extends ViewGroup { * an adapter's data set representing the data at a given position or item ID. * If the view to be reused is considered "dirty" the adapter will be asked to rebind it. * If not, the view can be quickly reused by the LayoutManager with no further work. - * Clean views that have not {@link View#isLayoutRequested() requested layout} + * Clean views that have not {@link android.view.View#isLayoutRequested() requested layout} * may be repositioned by a LayoutManager without remeasurement.

    */ public final class Recycler { - final ArrayList mAttachedScrap = new ArrayList(); + final ArrayList mAttachedScrap = new ArrayList<>(); private ArrayList mChangedScrap = null; final ArrayList mCachedViews = new ArrayList(); @@ -3112,11 +4820,7 @@ public class RecyclerView extends ViewGroup { mViewCacheMax = viewCount; // first, try the views that can be recycled for (int i = mCachedViews.size() - 1; i >= 0 && mCachedViews.size() > viewCount; i--) { - tryToRecycleCachedViewAt(i); - } - // if we could not recycle enough of them, remove some. - while (mCachedViews.size() > viewCount) { - mCachedViews.remove(mCachedViews.size() - 1); + recycleCachedViewAt(i); } } @@ -3141,7 +4845,11 @@ public class RecyclerView extends ViewGroup { // if it is a removed holder, nothing to verify since we cannot ask adapter anymore // if it is not removed, verify the type and id. if (holder.isRemoved()) { - return true; + if (DEBUG && !mState.isPreLayout()) { + throw new IllegalStateException("should not receive a removed view unless it" + + " is pre layout"); + } + return mState.isPreLayout(); } if (holder.mPosition < 0 || holder.mPosition >= mAdapter.getItemCount()) { throw new IndexOutOfBoundsException("Inconsistency detected. Invalid view holder " @@ -3188,6 +4896,7 @@ public class RecyclerView extends ViewGroup { + "position " + position + "(offset:" + offsetPosition + ")." + "state:" + mState.getItemCount()); } + holder.mOwnerRecyclerView = RecyclerView.this; mAdapter.bindViewHolder(holder, offsetPosition); attachAccessibilityDelegate(view); if (mState.isPreLayout()) { @@ -3296,16 +5005,10 @@ public class RecyclerView extends ViewGroup { } if (holder == null) { final int offsetPosition = mAdapterHelper.findPositionOffset(position); -// final int offsetPosition = position; -// Utils.log("offsetPosition position = " + position); -// Utils.log("offsetPosition = " + offsetPosition); -// Utils.log("offsetPosition count = " + mAdapter.getItemCount()); -// Utils.log("offsetPosition count = " + mState.getItemCount()); if (offsetPosition < 0 || offsetPosition >= mAdapter.getItemCount()) { throw new IndexOutOfBoundsException("Inconsistency detected. Invalid item " + "position " + position + "(offset:" + offsetPosition + ")." - + "state:" + mState.getItemCount() - + "adpter:" + mAdapter.getClass().getName()); + + "state:" + mState.getItemCount()); } final int type = mAdapter.getItemViewType(offsetPosition); @@ -3342,8 +5045,7 @@ public class RecyclerView extends ViewGroup { Log.d(TAG, "getViewForPosition(" + position + ") fetching from shared " + "pool"); } - holder = getRecycledViewPool() - .getRecycledView(mAdapter.getItemViewType(offsetPosition)); + holder = getRecycledViewPool().getRecycledView(type); if (holder != null) { holder.resetInternal(); if (FORCE_INVALIDATE_DISPLAY_LIST) { @@ -3352,13 +5054,29 @@ public class RecyclerView extends ViewGroup { } } if (holder == null) { - holder = mAdapter.createViewHolder(RecyclerView.this, - mAdapter.getItemViewType(offsetPosition)); + holder = mAdapter.createViewHolder(RecyclerView.this, type); if (DEBUG) { Log.d(TAG, "getViewForPosition created new ViewHolder"); } } } + + // This is very ugly but the only place we can grab this information + // before the View is rebound and returned to the LayoutManager for post layout ops. + // We don't need this in pre-layout since the VH is not updated by the LM. + if (fromScrap && !mState.isPreLayout() && holder + .hasAnyOfTheFlags(ViewHolder.FLAG_BOUNCED_FROM_HIDDEN_LIST)) { + holder.setFlags(0, ViewHolder.FLAG_BOUNCED_FROM_HIDDEN_LIST); + if (mState.mRunSimpleAnimations) { + int changeFlags = ItemAnimator + .buildAdapterChangeFlagsForAnimations(holder); + changeFlags |= ItemAnimator.FLAG_APPEARED_IN_PRE_LAYOUT; + final ItemHolderInfo info = mItemAnimator.recordPreLayoutInformation(mState, + holder, changeFlags, holder.getUnmodifiedPayloads()); + recordAnimationInfoIfBouncedHiddenView(holder, info); + } + } + boolean bound = false; if (mState.isPreLayout() && holder.isBound()) { // do not update unless we absolutely have to. @@ -3369,6 +5087,7 @@ public class RecyclerView extends ViewGroup { + " come here only in pre-layout. Holder: " + holder); } final int offsetPosition = mAdapterHelper.findPositionOffset(position); + holder.mOwnerRecyclerView = RecyclerView.this; mAdapter.bindViewHolder(holder, offsetPosition); attachAccessibilityDelegate(holder.itemView); bound = true; @@ -3394,7 +5113,7 @@ public class RecyclerView extends ViewGroup { } private void attachAccessibilityDelegate(View itemView) { - if (mAccessibilityManager != null && mAccessibilityManager.isEnabled()) { + if (isAccessibilityEnabled()) { if (ViewCompat.getImportantForAccessibility(itemView) == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) { ViewCompat.setImportantForAccessibility(itemView, @@ -3438,8 +5157,8 @@ public class RecyclerView extends ViewGroup { * Recycle a detached view. The specified view will be added to a pool of views * for later rebinding and reuse. * - *

    A view must be fully detached before it may be recycled. If the View is scrapped, - * it will be removed from scrap list.

    + *

    A view must be fully detached (removed from parent) before it may be recycled. If the + * View is scrapped, it will be removed from scrap list.

    * * @param view Removed view for recycling * @see LayoutManager#removeAndRecycleView(View, Recycler) @@ -3448,6 +5167,9 @@ public class RecyclerView extends ViewGroup { // This public recycle method tries to make view recycle-able since layout manager // intended to recycle this view (e.g. even if it is in scrap or change cache) ViewHolder holder = getChildViewHolderInt(view); + if (holder.isTmpDetached()) { + removeDetachedView(view, false); + } if (holder.isScrap()) { holder.unScrap(); } else if (holder.wasReturnedFromScrap()){ @@ -3457,7 +5179,7 @@ public class RecyclerView extends ViewGroup { } /** - * Internally, use this method instead of {@link #recycleView(View)} to + * Internally, use this method instead of {@link #recycleView(android.view.View)} to * catch potential bugs. * @param view */ @@ -3468,33 +5190,32 @@ public class RecyclerView extends ViewGroup { void recycleAndClearCachedViews() { final int count = mCachedViews.size(); for (int i = count - 1; i >= 0; i--) { - tryToRecycleCachedViewAt(i); + recycleCachedViewAt(i); } mCachedViews.clear(); } /** - * Tries to recyle a cached view and removes the view from the list if and only if it - * is recycled. + * Recycles a cached view and removes the view from the list. Views are added to cache + * if and only if they are recyclable, so this method does not check it again. + *

    + * A small exception to this rule is when the view does not have an animator reference + * but transient state is true (due to animations created outside ItemAnimator). In that + * case, adapter may choose to recycle it. From RecyclerView's perspective, the view is + * still recyclable since Adapter wants to do so. * * @param cachedViewIndex The index of the view in cached views list - * @return True if item is recycled */ - boolean tryToRecycleCachedViewAt(int cachedViewIndex) { + void recycleCachedViewAt(int cachedViewIndex) { if (DEBUG) { Log.d(TAG, "Recycling cached view at index " + cachedViewIndex); } ViewHolder viewHolder = mCachedViews.get(cachedViewIndex); if (DEBUG) { - Log.d(TAG, "CachedViewHolder to be recycled(if recycleable): " + viewHolder); + Log.d(TAG, "CachedViewHolder to be recycled: " + viewHolder); } - if (viewHolder.isRecyclable()) { - getRecycledViewPool().putRecycledView(viewHolder); - dispatchViewRecycled(viewHolder); - mCachedViews.remove(cachedViewIndex); - return true; - } - return false; + addViewHolderToRecycledViewPool(viewHolder); + mCachedViews.remove(cachedViewIndex); } /** @@ -3510,38 +5231,62 @@ public class RecyclerView extends ViewGroup { + (holder.itemView.getParent() != null)); } + if (holder.isTmpDetached()) { + throw new IllegalArgumentException("Tmp detached view should be removed " + + "from RecyclerView before it can be recycled: " + holder); + } + if (holder.shouldIgnore()) { throw new IllegalArgumentException("Trying to recycle an ignored view holder. You" + " should first call stopIgnoringView(view) before calling recycle."); } - if (holder.isRecyclable()) { - boolean cached = false; - if (!holder.isInvalid() && (mState.mInPreLayout || !holder.isRemoved()) && - !holder.isChanged()) { - // Retire oldest cached views first - if (mCachedViews.size() == mViewCacheMax && !mCachedViews.isEmpty()) { - for (int i = 0; i < mCachedViews.size(); i++) { - if (tryToRecycleCachedViewAt(i)) { - break; - } - } + //noinspection unchecked + final boolean transientStatePreventsRecycling = holder + .doesTransientStatePreventRecycling(); + final boolean forceRecycle = mAdapter != null + && transientStatePreventsRecycling + && mAdapter.onFailedToRecycleView(holder); + boolean cached = false; + boolean recycled = false; + if (DEBUG && mCachedViews.contains(holder)) { + throw new IllegalArgumentException("cached view received recycle internal? " + + holder); + } + if (forceRecycle || holder.isRecyclable()) { + if (!holder.hasAnyOfTheFlags(ViewHolder.FLAG_INVALID | ViewHolder.FLAG_REMOVED + | ViewHolder.FLAG_UPDATE)) { + // Retire oldest cached view + int cachedViewSize = mCachedViews.size(); + if (cachedViewSize >= mViewCacheMax && cachedViewSize > 0) { + recycleCachedViewAt(0); + cachedViewSize --; } - if (mCachedViews.size() < mViewCacheMax) { + if (cachedViewSize < mViewCacheMax) { mCachedViews.add(holder); cached = true; } } if (!cached) { - getRecycledViewPool().putRecycledView(holder); - dispatchViewRecycled(holder); + addViewHolderToRecycledViewPool(holder); + recycled = true; } } else if (DEBUG) { Log.d(TAG, "trying to recycle a non-recycleable holder. Hopefully, it will " - + "re-visit here. We are stil removing it from animation lists"); + + "re-visit here. We are still removing it from animation lists"); } // even if the holder is not removed, we still call this method so that it is removed // from view holder lists. - mState.onViewRecycled(holder); + mViewInfoStore.removeViewHolder(holder); + if (!cached && !recycled && transientStatePreventsRecycling) { + holder.mOwnerRecyclerView = null; + } + } + + void addViewHolderToRecycledViewPool(ViewHolder holder) { + ViewCompat.setAccessibilityDelegate(holder.itemView, null); + dispatchViewRecycled(holder); + holder.mOwnerRecyclerView = null; + getRecycledViewPool().putRecycledView(holder); } /** @@ -3552,6 +5297,7 @@ public class RecyclerView extends ViewGroup { void quickRecycleScrapView(View view) { final ViewHolder holder = getChildViewHolderInt(view); holder.mScrapContainer = null; + holder.mInChangeScrap = false; holder.clearReturnedFromScrapFlag(); recycleViewHolderInternal(holder); } @@ -3567,18 +5313,20 @@ public class RecyclerView extends ViewGroup { */ void scrapView(View view) { final ViewHolder holder = getChildViewHolderInt(view); - holder.setScrapContainer(this); - if (!holder.isChanged() || !supportsChangeAnimations()) { + if (holder.hasAnyOfTheFlags(ViewHolder.FLAG_REMOVED | ViewHolder.FLAG_INVALID) + || !holder.isUpdated() || canReuseUpdatedViewHolder(holder)) { if (holder.isInvalid() && !holder.isRemoved() && !mAdapter.hasStableIds()) { throw new IllegalArgumentException("Called scrap view with an invalid view." + " Invalid views cannot be reused from scrap, they should rebound from" + " recycler pool."); } + holder.setScrapContainer(this, false); mAttachedScrap.add(holder); } else { if (mChangedScrap == null) { mChangedScrap = new ArrayList(); } + holder.setScrapContainer(this, true); mChangedScrap.add(holder); } } @@ -3590,12 +5338,13 @@ public class RecyclerView extends ViewGroup { * until it is explicitly removed and recycled.

    */ void unscrapView(ViewHolder holder) { - if (!holder.isChanged() || !supportsChangeAnimations() || mChangedScrap == null) { - mAttachedScrap.remove(holder); - } else { + if (holder.mInChangeScrap) { mChangedScrap.remove(holder); + } else { + mAttachedScrap.remove(holder); } holder.mScrapContainer = null; + holder.mInChangeScrap = false; holder.clearReturnedFromScrapFlag(); } @@ -3609,6 +5358,9 @@ public class RecyclerView extends ViewGroup { void clearScrap() { mAttachedScrap.clear(); + if (mChangedScrap != null) { + mChangedScrap.clear(); + } } ViewHolder getChangedScrapViewForPosition(int position) { @@ -3620,7 +5372,7 @@ public class RecyclerView extends ViewGroup { // find by position for (int i = 0; i < changedScrapSize; i++) { final ViewHolder holder = mChangedScrap.get(i); - if (!holder.wasReturnedFromScrap() && holder.getPosition() == position) { + if (!holder.wasReturnedFromScrap() && holder.getLayoutPosition() == position) { holder.addFlags(ViewHolder.FLAG_RETURNED_FROM_SCRAP); return holder; } @@ -3657,7 +5409,7 @@ public class RecyclerView extends ViewGroup { // Try first for an exact, non-invalid match from scrap. for (int i = 0; i < scrapCount; i++) { final ViewHolder holder = mAttachedScrap.get(i); - if (!holder.wasReturnedFromScrap() && holder.getPosition() == position + if (!holder.wasReturnedFromScrap() && holder.getLayoutPosition() == position && !holder.isInvalid() && (mState.mInPreLayout || !holder.isRemoved())) { if (type != INVALID_TYPE && holder.getItemViewType() != type) { Log.e(TAG, "Scrap view for position " + position + " isn't dirty but has" + @@ -3673,8 +5425,20 @@ public class RecyclerView extends ViewGroup { if (!dryRun) { View view = mChildHelper.findHiddenNonRemovedView(position, type); if (view != null) { - // ending the animation should cause it to get recycled before we reuse it - mItemAnimator.endAnimation(getChildViewHolder(view)); + // This View is good to be used. We just need to unhide, detach and move to the + // scrap list. + final ViewHolder vh = getChildViewHolderInt(view); + mChildHelper.unhide(view); + int layoutIndex = mChildHelper.indexOfChild(view); + if (layoutIndex == RecyclerView.NO_POSITION) { + throw new IllegalStateException("layout index should not be -1 after " + + "unhiding a view:" + vh); + } + mChildHelper.detachViewFromParent(layoutIndex); + scrapView(view); + vh.addFlags(ViewHolder.FLAG_RETURNED_FROM_SCRAP + | ViewHolder.FLAG_BOUNCED_FROM_HIDDEN_LIST); + return vh; } } @@ -3684,7 +5448,7 @@ public class RecyclerView extends ViewGroup { final ViewHolder holder = mCachedViews.get(i); // invalid view holders may be in cache if adapter has stable ids as they can be // retrieved via getScrapViewForId - if (!holder.isInvalid() && holder.getPosition() == position) { + if (!holder.isInvalid() && holder.getLayoutPosition() == position) { if (!dryRun) { mCachedViews.remove(i); } @@ -3722,6 +5486,8 @@ public class RecyclerView extends ViewGroup { } return holder; } else if (!dryRun) { + // if we are running animations, it is actually better to keep it in scrap + // but this would force layout manager to lay it out which would be bad. // Recycle this scrap. Type mismatch. mAttachedScrap.remove(i); removeDetachedView(holder.itemView, false); @@ -3741,7 +5507,7 @@ public class RecyclerView extends ViewGroup { } return holder; } else if (!dryRun) { - tryToRecycleCachedViewAt(i); + recycleCachedViewAt(i); } } } @@ -3756,7 +5522,7 @@ public class RecyclerView extends ViewGroup { mAdapter.onViewRecycled(holder); } if (mState != null) { - mState.onViewRecycled(holder); + mViewInfoStore.removeViewHolder(holder); } if (DEBUG) Log.d(TAG, "dispatchViewRecycled: " + holder); } @@ -3800,7 +5566,7 @@ public class RecyclerView extends ViewGroup { final int cachedCount = mCachedViews.size(); for (int i = 0; i < cachedCount; i++) { final ViewHolder holder = mCachedViews.get(i); - if (holder != null && holder.getPosition() >= insertedAt) { + if (holder != null && holder.mPosition >= insertedAt) { if (DEBUG) { Log.d(TAG, "offsetPositionRecordsForInsert cached " + i + " holder " + holder + " now at position " + (holder.mPosition + count)); @@ -3822,28 +5588,17 @@ public class RecyclerView extends ViewGroup { for (int i = cachedCount - 1; i >= 0; i--) { final ViewHolder holder = mCachedViews.get(i); if (holder != null) { - if (holder.getPosition() >= removedEnd) { + if (holder.mPosition >= removedEnd) { if (DEBUG) { Log.d(TAG, "offsetPositionRecordsForRemove cached " + i + " holder " + holder + " now at position " + (holder.mPosition - count)); } holder.offsetPosition(-count, applyToPreLayout); - } else if (holder.getPosition() >= removedFrom) { + } else if (holder.mPosition >= removedFrom) { // Item for this view was removed. Dump it from the cache. - if (!tryToRecycleCachedViewAt(i)) { - // if we cannot recycle it, at least invalidate so that we won't return - // it by position. - holder.addFlags(ViewHolder.FLAG_INVALID); - if (DEBUG) { - Log.d(TAG, "offsetPositionRecordsForRemove cached " + i + - " holder " + holder + " now flagged as invalid because it " - + "could not be recycled"); - } - } else if (DEBUG) { - Log.d(TAG, "offsetPositionRecordsForRemove cached " + i + - " holder " + holder + " now placed in pool"); - } + holder.addFlags(ViewHolder.FLAG_REMOVED); + recycleCachedViewAt(i); } } } @@ -3873,21 +5628,32 @@ public class RecyclerView extends ViewGroup { void viewRangeUpdate(int positionStart, int itemCount) { final int positionEnd = positionStart + itemCount; final int cachedCount = mCachedViews.size(); - for (int i = 0; i < cachedCount; i++) { + for (int i = cachedCount - 1; i >= 0; i--) { final ViewHolder holder = mCachedViews.get(i); if (holder == null) { continue; } - final int pos = holder.getPosition(); + final int pos = holder.getLayoutPosition(); if (pos >= positionStart && pos < positionEnd) { holder.addFlags(ViewHolder.FLAG_UPDATE); + recycleCachedViewAt(i); // cached views should not be flagged as changed because this will cause them // to animate when they are returned from cache. } } } + void setAdapterPositionsAsUnknown() { + final int cachedCount = mCachedViews.size(); + for (int i = 0; i < cachedCount; i++) { + final ViewHolder holder = mCachedViews.get(i); + if (holder != null) { + holder.addFlags(ViewHolder.FLAG_ADAPTER_POSITION_UNKNOWN); + } + } + } + void markKnownViewsInvalid() { if (mAdapter != null && mAdapter.hasStableIds()) { final int cachedCount = mCachedViews.size(); @@ -3895,20 +5661,13 @@ public class RecyclerView extends ViewGroup { final ViewHolder holder = mCachedViews.get(i); if (holder != null) { holder.addFlags(ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID); + holder.addChangePayload(null); } } } else { - // we cannot re-use cached views in this case. Recycle the ones we can and flag - // the remaining as invalid so that they can be recycled later on (when their - // animations end.) - for (int i = mCachedViews.size() - 1; i >= 0; i--) { - if (!tryToRecycleCachedViewAt(i)) { - final ViewHolder holder = mCachedViews.get(i); - holder.addFlags(ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID); - } - } + // we cannot re-use cached views in this case. Recycle them all + recycleAndClearCachedViews(); } - } void clearOldPositions() { @@ -3943,7 +5702,7 @@ public class RecyclerView extends ViewGroup { /** * ViewCacheExtension is a helper class to provide an additional layer of view caching that can - * ben controlled by the developer. + * be controlled by the developer. *

    * When {@link Recycler#getViewForPosition(int)} is called, Recycler checks attached scrap and * first level cache to find a matching View. If it cannot find a suitable View, Recycler will @@ -3994,9 +5753,9 @@ public class RecyclerView extends ViewGroup { * layout file. *

    * The new ViewHolder will be used to display items of the adapter using - * {@link #onBindViewHolder(ViewHolder, int)}. Since it will be re-used to display different - * items in the data set, it is a good idea to cache references to sub views of the View to - * avoid unnecessary {@link View#findViewById(int)} calls. + * {@link #onBindViewHolder(ViewHolder, int, List)}. Since it will be re-used to display + * different items in the data set, it is a good idea to cache references to sub views of + * the View to avoid unnecessary {@link View#findViewById(int)} calls. * * @param parent The ViewGroup into which the new View will be added after it is bound to * an adapter position. @@ -4008,24 +5767,60 @@ public class RecyclerView extends ViewGroup { */ public abstract VH onCreateViewHolder(ViewGroup parent, int viewType); + /** + * Called by RecyclerView to display the data at the specified position. This method should + * update the contents of the {@link ViewHolder#itemView} to reflect the item at the given + * position. + *

    + * Note that unlike {@link android.widget.ListView}, RecyclerView will not call this method + * again if the position of the item changes in the data set unless the item itself is + * invalidated or the new position cannot be determined. For this reason, you should only + * use the position parameter while acquiring the related data item inside + * this method and should not keep a copy of it. If you need the position of an item later + * on (e.g. in a click listener), use {@link ViewHolder#getAdapterPosition()} which will + * have the updated adapter position. + * + * Override {@link #onBindViewHolder(ViewHolder, int, List)} instead if Adapter can + * handle efficient partial bind. + * + * @param holder The ViewHolder which should be updated to represent the contents of the + * item at the given position in the data set. + * @param position The position of the item within the adapter's data set. + */ + public abstract void onBindViewHolder(VH holder, int position); + /** * Called by RecyclerView to display the data at the specified position. This method * should update the contents of the {@link ViewHolder#itemView} to reflect the item at * the given position. *

    - * Note that unlike {@link android.widget.ListView}, RecyclerView will not call this - * method again if the position of the item changes in the data set unless the item itself - * is invalidated or the new position cannot be determined. For this reason, you should only - * use the position parameter while acquiring the related data item inside this - * method and should not keep a copy of it. If you need the position of an item later on - * (e.g. in a click listener), use {@link ViewHolder#getPosition()} which will have the - * updated position. + * Note that unlike {@link android.widget.ListView}, RecyclerView will not call this method + * again if the position of the item changes in the data set unless the item itself is + * invalidated or the new position cannot be determined. For this reason, you should only + * use the position parameter while acquiring the related data item inside + * this method and should not keep a copy of it. If you need the position of an item later + * on (e.g. in a click listener), use {@link ViewHolder#getAdapterPosition()} which will + * have the updated adapter position. + *

    + * Partial bind vs full bind: + *

    + * The payloads parameter is a merge list from {@link #notifyItemChanged(int, Object)} or + * {@link #notifyItemRangeChanged(int, int, Object)}. If the payloads list is not empty, + * the ViewHolder is currently bound to old data and Adapter may run an efficient partial + * update using the payload info. If the payload is empty, Adapter must run a full bind. + * Adapter should not assume that the payload passed in notify methods will be received by + * onBindViewHolder(). For example when the view is not attached to the screen, the + * payload in notifyItemChange() will be simply dropped. * * @param holder The ViewHolder which should be updated to represent the contents of the * item at the given position in the data set. * @param position The position of the item within the adapter's data set. + * @param payloads A non-null list of merged payloads. Can be empty list if requires full + * update. */ - public abstract void onBindViewHolder(VH holder, int position); + public void onBindViewHolder(VH holder, int position, List payloads) { + onBindViewHolder(holder, position); + } /** * This method calls {@link #onCreateViewHolder(ViewGroup, int)} to create a new @@ -4034,8 +5829,10 @@ public class RecyclerView extends ViewGroup { * @see #onCreateViewHolder(ViewGroup, int) */ public final VH createViewHolder(ViewGroup parent, int viewType) { + TraceCompat.beginSection(TRACE_CREATE_VIEW_TAG); final VH holder = onCreateViewHolder(parent, viewType); holder.mItemViewType = viewType; + TraceCompat.endSection(); return holder; } @@ -4051,9 +5848,17 @@ public class RecyclerView extends ViewGroup { if (hasStableIds()) { holder.mItemId = getItemId(position); } - onBindViewHolder(holder, position); holder.setFlags(ViewHolder.FLAG_BOUND, - ViewHolder.FLAG_BOUND | ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID); + ViewHolder.FLAG_BOUND | ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID + | ViewHolder.FLAG_ADAPTER_POSITION_UNKNOWN); + TraceCompat.beginSection(TRACE_BIND_VIEW_TAG); + onBindViewHolder(holder, position, holder.getUnmodifiedPayloads()); + holder.clearPayload(); + final ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams(); + if (layoutParams instanceof RecyclerView.LayoutParams) { + ((LayoutParams) layoutParams).mInsetsDirty = true; + } + TraceCompat.endSection(); } /** @@ -4074,7 +5879,7 @@ public class RecyclerView extends ViewGroup { /** * Indicates whether each item in the data set can be represented with a unique identifier - * of type {@link Long}. + * of type {@link java.lang.Long}. * * @param hasStableIds Whether items in data set have unique identifiers or not. * @see #hasStableIds() @@ -4101,7 +5906,7 @@ public class RecyclerView extends ViewGroup { } /** - * Returns the total number of items in the data set hold by the adapter. + * Returns the total number of items in the data set held by the adapter. * * @return The total number of items in this adapter. */ @@ -4127,18 +5932,61 @@ public class RecyclerView extends ViewGroup { * attached to the parent RecyclerView. If an item view has large or expensive data * bound to it such as large bitmaps, this may be a good place to release those * resources.

    + *

    + * RecyclerView calls this method right before clearing ViewHolder's internal data and + * sending it to RecycledViewPool. This way, if ViewHolder was holding valid information + * before being recycled, you can call {@link ViewHolder#getAdapterPosition()} to get + * its adapter position. * * @param holder The ViewHolder for the view being recycled */ public void onViewRecycled(VH holder) { } + /** + * Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled + * due to its transient state. Upon receiving this callback, Adapter can clear the + * animation(s) that effect the View's transient state and return true so that + * the View can be recycled. Keep in mind that the View in question is already removed from + * the RecyclerView. + *

    + * In some cases, it is acceptable to recycle a View although it has transient state. Most + * of the time, this is a case where the transient state will be cleared in + * {@link #onBindViewHolder(ViewHolder, int)} call when View is rebound to a new position. + * For this reason, RecyclerView leaves the decision to the Adapter and uses the return + * value of this method to decide whether the View should be recycled or not. + *

    + * Note that when all animations are created by {@link RecyclerView.ItemAnimator}, you + * should never receive this callback because RecyclerView keeps those Views as children + * until their animations are complete. This callback is useful when children of the item + * views create animations which may not be easy to implement using an {@link ItemAnimator}. + *

    + * You should never fix this issue by calling + * holder.itemView.setHasTransientState(false); unless you've previously called + * holder.itemView.setHasTransientState(true);. Each + * View.setHasTransientState(true) call must be matched by a + * View.setHasTransientState(false) call, otherwise, the state of the View + * may become inconsistent. You should always prefer to end or cancel animations that are + * triggering the transient state instead of handling it manually. + * + * @param holder The ViewHolder containing the View that could not be recycled due to its + * transient state. + * @return True if the View should be recycled, false otherwise. Note that if this method + * returns true, RecyclerView will ignore the transient state of + * the View and recycle it regardless. If this method returns false, + * RecyclerView will check the View's transient state again before giving a final decision. + * Default implementation returns false. + */ + public boolean onFailedToRecycleView(VH holder) { + return false; + } + /** * Called when a view created by this adapter has been attached to a window. * *

    This can be used as a reasonable signal that the view is about to be seen * by the user. If the adapter previously freed any resources in - * {@link #onViewDetachedFromWindow(ViewHolder) onViewDetachedFromWindow} + * {@link #onViewDetachedFromWindow(RecyclerView.ViewHolder) onViewDetachedFromWindow} * those resources should be restored here.

    * * @param holder Holder of the view being attached @@ -4151,7 +5999,7 @@ public class RecyclerView extends ViewGroup { * *

    Becoming detached from the window is not necessarily a permanent condition; * the consumer of an Adapter's views may choose to cache views offscreen while they - * are not visible, attaching an detaching them as appropriate.

    + * are not visible, attaching and detaching them as appropriate.

    * * @param holder Holder of the view being detached */ @@ -4172,16 +6020,16 @@ public class RecyclerView extends ViewGroup { * *

    The adapter may publish a variety of events describing specific changes. * Not all adapters may support all change types and some may fall back to a generic - * {@link AdapterDataObserver#onChanged() + * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver#onChanged() * "something changed"} event if more specific data is not available.

    * *

    Components registering observers with an adapter are responsible for - * {@link #unregisterAdapterDataObserver(AdapterDataObserver) + * {@link #unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver) * unregistering} those observers when finished.

    * * @param observer Observer to register * - * @see #unregisterAdapterDataObserver(AdapterDataObserver) + * @see #unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver) */ public void registerAdapterDataObserver(AdapterDataObserver observer) { mObservable.registerObserver(observer); @@ -4195,12 +6043,32 @@ public class RecyclerView extends ViewGroup { * * @param observer Observer to unregister * - * @see #registerAdapterDataObserver(AdapterDataObserver) + * @see #registerAdapterDataObserver(RecyclerView.AdapterDataObserver) */ public void unregisterAdapterDataObserver(AdapterDataObserver observer) { mObservable.unregisterObserver(observer); } + /** + * Called by RecyclerView when it starts observing this Adapter. + *

    + * Keep in mind that same adapter may be observed by multiple RecyclerViews. + * + * @param recyclerView The RecyclerView instance which started observing this adapter. + * @see #onDetachedFromRecyclerView(RecyclerView) + */ + public void onAttachedToRecyclerView(RecyclerView recyclerView) { + } + + /** + * Called by RecyclerView when it stops observing this Adapter. + * + * @param recyclerView The RecyclerView instance which stopped observing this adapter. + * @see #onAttachedToRecyclerView(RecyclerView) + */ + public void onDetachedFromRecyclerView(RecyclerView recyclerView) { + } + /** * Notify any registered observers that the data set has changed. * @@ -4236,6 +6104,7 @@ public class RecyclerView extends ViewGroup { /** * Notify any registered observers that the item at position has changed. + * Equivalent to calling notifyItemChanged(position, null);. * *

    This is an item change event, not a structural change event. It indicates that any * reflection of the data at position is out of date and should be updated. @@ -4249,9 +6118,38 @@ public class RecyclerView extends ViewGroup { mObservable.notifyItemRangeChanged(position, 1); } + /** + * Notify any registered observers that the item at position has changed with an + * optional payload object. + * + *

    This is an item change event, not a structural change event. It indicates that any + * reflection of the data at position is out of date and should be updated. + * The item at position retains the same identity. + *

    + * + *

    + * Client can optionally pass a payload for partial change. These payloads will be merged + * and may be passed to adapter's {@link #onBindViewHolder(ViewHolder, int, List)} if the + * item is already represented by a ViewHolder and it will be rebound to the same + * ViewHolder. A notifyItemRangeChanged() with null payload will clear all existing + * payloads on that item and prevent future payload until + * {@link #onBindViewHolder(ViewHolder, int, List)} is called. Adapter should not assume + * that the payload will always be passed to onBindViewHolder(), e.g. when the view is not + * attached, the payload will be simply dropped. + * + * @param position Position of the item that has changed + * @param payload Optional parameter, use null to identify a "full" update + * + * @see #notifyItemRangeChanged(int, int) + */ + public final void notifyItemChanged(int position, Object payload) { + mObservable.notifyItemRangeChanged(position, 1, payload); + } + /** * Notify any registered observers that the itemCount items starting at * position positionStart have changed. + * Equivalent to calling notifyItemRangeChanged(position, itemCount, null);. * *

    This is an item change event, not a structural change event. It indicates that * any reflection of the data in the given position range is out of date and should @@ -4266,6 +6164,36 @@ public class RecyclerView extends ViewGroup { mObservable.notifyItemRangeChanged(positionStart, itemCount); } + /** + * Notify any registered observers that the itemCount items starting at + * position positionStart have changed. An optional payload can be + * passed to each changed item. + * + *

    This is an item change event, not a structural change event. It indicates that any + * reflection of the data in the given position range is out of date and should be updated. + * The items in the given range retain the same identity. + *

    + * + *

    + * Client can optionally pass a payload for partial change. These payloads will be merged + * and may be passed to adapter's {@link #onBindViewHolder(ViewHolder, int, List)} if the + * item is already represented by a ViewHolder and it will be rebound to the same + * ViewHolder. A notifyItemRangeChanged() with null payload will clear all existing + * payloads on that item and prevent future payload until + * {@link #onBindViewHolder(ViewHolder, int, List)} is called. Adapter should not assume + * that the payload will always be passed to onBindViewHolder(), e.g. when the view is not + * attached, the payload will be simply dropped. + * + * @param positionStart Position of the first item that has changed + * @param itemCount Number of items that have changed + * @param payload Optional parameter, use null to identify a "full" update + * + * @see #notifyItemChanged(int) + */ + public final void notifyItemRangeChanged(int positionStart, int itemCount, Object payload) { + mObservable.notifyItemRangeChanged(positionStart, itemCount, payload); + } + /** * Notify any registered observers that the item reflected at position * has been newly inserted. The item previously at position is now at @@ -4353,17 +6281,31 @@ public class RecyclerView extends ViewGroup { } private void dispatchChildDetached(View child) { - if (mAdapter != null) { - mAdapter.onViewDetachedFromWindow(getChildViewHolderInt(child)); - } + final ViewHolder viewHolder = getChildViewHolderInt(child); onChildDetachedFromWindow(child); + if (mAdapter != null && viewHolder != null) { + mAdapter.onViewDetachedFromWindow(viewHolder); + } + if (mOnChildAttachStateListeners != null) { + final int cnt = mOnChildAttachStateListeners.size(); + for (int i = cnt - 1; i >= 0; i--) { + mOnChildAttachStateListeners.get(i).onChildViewDetachedFromWindow(child); + } + } } private void dispatchChildAttached(View child) { - if (mAdapter != null) { - mAdapter.onViewAttachedToWindow(getChildViewHolderInt(child)); - } + final ViewHolder viewHolder = getChildViewHolderInt(child); onChildAttachedToWindow(child); + if (mAdapter != null && viewHolder != null) { + mAdapter.onViewAttachedToWindow(viewHolder); + } + if (mOnChildAttachStateListeners != null) { + final int cnt = mOnChildAttachStateListeners.size(); + for (int i = cnt - 1; i >= 0; i--) { + mOnChildAttachStateListeners.get(i).onChildViewAttachedToWindow(child); + } + } } /** @@ -4373,6 +6315,14 @@ public class RecyclerView extends ViewGroup { * a RecyclerView can be used to implement a standard vertically scrolling list, * a uniform grid, staggered grids, horizontally scrolling collections and more. Several stock * layout managers are provided for general use. + *

    + * If the LayoutManager specifies a default constructor or one with the signature + * ({@link Context}, {@link AttributeSet}, {@code int}, {@code int}), RecyclerView will + * instantiate and set the LayoutManager when being inflated. Most used properties can + * be then obtained from {@link #getProperties(Context, AttributeSet, int, int)}. In case + * a LayoutManager specifies both constructors, the non-default constructor will take + * precedence. + * */ public static abstract class LayoutManager { ChildHelper mChildHelper; @@ -4383,15 +6333,132 @@ public class RecyclerView extends ViewGroup { private boolean mRequestedSimpleAnimations = false; + boolean mIsAttachedToWindow = false; + + private boolean mAutoMeasure = false; + + /** + * LayoutManager has its own more strict measurement cache to avoid re-measuring a child + * if the space that will be given to it is already larger than what it has measured before. + */ + private boolean mMeasurementCacheEnabled = true; + + + /** + * These measure specs might be the measure specs that were passed into RecyclerView's + * onMeasure method OR fake measure specs created by the RecyclerView. + * For example, when a layout is run, RecyclerView always sets these specs to be + * EXACTLY because a LayoutManager cannot resize RecyclerView during a layout pass. + *

    + * Also, to be able to use the hint in unspecified measure specs, RecyclerView checks the + * API level and sets the size to 0 pre-M to avoid any issue that might be caused by + * corrupt values. Older platforms have no responsibility to provide a size if they set + * mode to unspecified. + */ + private int mWidthMode, mHeightMode; + private int mWidth, mHeight; + void setRecyclerView(RecyclerView recyclerView) { if (recyclerView == null) { mRecyclerView = null; mChildHelper = null; + mWidth = 0; + mHeight = 0; } else { mRecyclerView = recyclerView; mChildHelper = recyclerView.mChildHelper; + mWidth = recyclerView.getWidth(); + mHeight = recyclerView.getHeight(); + } + mWidthMode = MeasureSpec.EXACTLY; + mHeightMode = MeasureSpec.EXACTLY; + } + + void setMeasureSpecs(int wSpec, int hSpec) { + mWidth = MeasureSpec.getSize(wSpec); + mWidthMode = MeasureSpec.getMode(wSpec); + if (mWidthMode == MeasureSpec.UNSPECIFIED && !ALLOW_SIZE_IN_UNSPECIFIED_SPEC) { + mWidth = 0; } + mHeight = MeasureSpec.getSize(hSpec); + mHeightMode = MeasureSpec.getMode(hSpec); + if (mHeightMode == MeasureSpec.UNSPECIFIED && !ALLOW_SIZE_IN_UNSPECIFIED_SPEC) { + mHeight = 0; + } + } + + /** + * Called after a layout is calculated during a measure pass when using auto-measure. + *

    + * It simply traverses all children to calculate a bounding box then calls + * {@link #setMeasuredDimension(Rect, int, int)}. LayoutManagers can override that method + * if they need to handle the bounding box differently. + *

    + * For example, GridLayoutManager override that method to ensure that even if a column is + * empty, the GridLayoutManager still measures wide enough to include it. + * + * @param widthSpec The widthSpec that was passing into RecyclerView's onMeasure + * @param heightSpec The heightSpec that was passing into RecyclerView's onMeasure + */ + void setMeasuredDimensionFromChildren(int widthSpec, int heightSpec) { + final int count = getChildCount(); + if (count == 0) { + mRecyclerView.defaultOnMeasure(widthSpec, heightSpec); + return; + } + int minX = Integer.MAX_VALUE; + int minY = Integer.MAX_VALUE; + int maxX = Integer.MIN_VALUE; + int maxY = Integer.MIN_VALUE; + + for (int i = 0; i < count; i++) { + View child = getChildAt(i); + LayoutParams lp = (LayoutParams) child.getLayoutParams(); + final Rect bounds = mRecyclerView.mTempRect; + getDecoratedBoundsWithMargins(child, bounds); + if (bounds.left < minX) { + minX = bounds.left; + } + if (bounds.right > maxX) { + maxX = bounds.right; + } + if (bounds.top < minY) { + minY = bounds.top; + } + if (bounds.bottom > maxY) { + maxY = bounds.bottom; + } + } + mRecyclerView.mTempRect.set(minX, minY, maxX, maxY); + setMeasuredDimension(mRecyclerView.mTempRect, widthSpec, heightSpec); + } + + /** + * Sets the measured dimensions from the given bounding box of the children and the + * measurement specs that were passed into {@link RecyclerView#onMeasure(int, int)}. It is + * called after the RecyclerView calls + * {@link LayoutManager#onLayoutChildren(Recycler, State)} during a measurement pass. + *

    + * This method should call {@link #setMeasuredDimension(int, int)}. + *

    + * The default implementation adds the RecyclerView's padding to the given bounding box + * then caps the value to be within the given measurement specs. + *

    + * This method is only called if the LayoutManager opted into the auto measurement API. + * + * @param childrenBounds The bounding box of all children + * @param wSpec The widthMeasureSpec that was passed into the RecyclerView. + * @param hSpec The heightMeasureSpec that was passed into the RecyclerView. + * + * @see #setAutoMeasureEnabled(boolean) + */ + public void setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec) { + int usedWidth = childrenBounds.width() + getPaddingLeft() + getPaddingRight(); + int usedHeight = childrenBounds.height() + getPaddingTop() + getPaddingBottom(); + int width = chooseSize(wSpec, usedWidth, getMinimumWidth()); + int height = chooseSize(hSpec, usedHeight, getMinimumHeight()); + setMeasuredDimension(width, height); } /** @@ -4416,6 +6483,30 @@ public class RecyclerView extends ViewGroup { } } + /** + * Chooses a size from the given specs and parameters that is closest to the desired size + * and also complies with the spec. + * + * @param spec The measureSpec + * @param desired The preferred measurement + * @param min The minimum value + * + * @return A size that fits to the given specs + */ + public static int chooseSize(int spec, int desired, int min) { + final int mode = View.MeasureSpec.getMode(spec); + final int size = View.MeasureSpec.getSize(spec); + switch (mode) { + case View.MeasureSpec.EXACTLY: + return size; + case View.MeasureSpec.AT_MOST: + return Math.min(size, Math.max(desired, min)); + case View.MeasureSpec.UNSPECIFIED: + default: + return Math.max(desired, min); + } + } + /** * Checks if RecyclerView is in the middle of a layout or scroll and throws an * {@link IllegalStateException} if it is. @@ -4429,6 +6520,86 @@ public class RecyclerView extends ViewGroup { } } + /** + * Defines whether the layout should be measured by the RecyclerView or the LayoutManager + * wants to handle the layout measurements itself. + *

    + * This method is usually called by the LayoutManager with value {@code true} if it wants + * to support WRAP_CONTENT. If you are using a public LayoutManager but want to customize + * the measurement logic, you can call this method with {@code false} and override + * {@link LayoutManager#onMeasure(int, int)} to implement your custom measurement logic. + *

    + * AutoMeasure is a convenience mechanism for LayoutManagers to easily wrap their content or + * handle various specs provided by the RecyclerView's parent. + * It works by calling {@link LayoutManager#onLayoutChildren(Recycler, State)} during an + * {@link RecyclerView#onMeasure(int, int)} call, then calculating desired dimensions based + * on children's positions. It does this while supporting all existing animation + * capabilities of the RecyclerView. + *

    + * AutoMeasure works as follows: + *

      + *
    1. LayoutManager should call {@code setAutoMeasureEnabled(true)} to enable it. All of + * the framework LayoutManagers use {@code auto-measure}.
    2. + *
    3. When {@link RecyclerView#onMeasure(int, int)} is called, if the provided specs are + * exact, RecyclerView will only call LayoutManager's {@code onMeasure} and return without + * doing any layout calculation.
    4. + *
    5. If one of the layout specs is not {@code EXACT}, the RecyclerView will start the + * layout process in {@code onMeasure} call. It will process all pending Adapter updates and + * decide whether to run a predictive layout or not. If it decides to do so, it will first + * call {@link #onLayoutChildren(Recycler, State)} with {@link State#isPreLayout()} set to + * {@code true}. At this stage, {@link #getWidth()} and {@link #getHeight()} will still + * return the width and height of the RecyclerView as of the last layout calculation. + *

      + * After handling the predictive case, RecyclerView will call + * {@link #onLayoutChildren(Recycler, State)} with {@link State#isMeasuring()} set to + * {@code true} and {@link State#isPreLayout()} set to {@code false}. The LayoutManager can + * access the measurement specs via {@link #getHeight()}, {@link #getHeightMode()}, + * {@link #getWidth()} and {@link #getWidthMode()}.

    6. + *
    7. After the layout calculation, RecyclerView sets the measured width & height by + * calculating the bounding box for the children (+ RecyclerView's padding). The + * LayoutManagers can override {@link #setMeasuredDimension(Rect, int, int)} to choose + * different values. For instance, GridLayoutManager overrides this value to handle the case + * where if it is vertical and has 3 columns but only 2 items, it should still measure its + * width to fit 3 items, not 2.
    8. + *
    9. Any following on measure call to the RecyclerView will run + * {@link #onLayoutChildren(Recycler, State)} with {@link State#isMeasuring()} set to + * {@code true} and {@link State#isPreLayout()} set to {@code false}. RecyclerView will + * take care of which views are actually added / removed / moved / changed for animations so + * that the LayoutManager should not worry about them and handle each + * {@link #onLayoutChildren(Recycler, State)} call as if it is the last one. + *
    10. + *
    11. When measure is complete and RecyclerView's + * {@link #onLayout(boolean, int, int, int, int)} method is called, RecyclerView checks + * whether it already did layout calculations during the measure pass and if so, it re-uses + * that information. It may still decide to call {@link #onLayoutChildren(Recycler, State)} + * if the last measure spec was different from the final dimensions or adapter contents + * have changed between the measure call and the layout call.
    12. + *
    13. Finally, animations are calculated and run as usual.
    14. + *
    + * + * @param enabled True if the Layout should be measured by the + * RecyclerView, false if the LayoutManager wants + * to measure itself. + * + * @see #setMeasuredDimension(Rect, int, int) + * @see #isAutoMeasureEnabled() + */ + public void setAutoMeasureEnabled(boolean enabled) { + mAutoMeasure = enabled; + } + + /** + * Returns whether the LayoutManager uses the automatic measurement API or not. + * + * @return True if the LayoutManager is measured by the RecyclerView or + * false if it measures itself. + * + * @see #setAutoMeasureEnabled(boolean) + */ + public boolean isAutoMeasureEnabled() { + return mAutoMeasure; + } + /** * Returns whether this LayoutManager supports automatic item animations. * A LayoutManager wishing to support item animations should obey certain @@ -4453,15 +6624,78 @@ public class RecyclerView extends ViewGroup { return false; } + void dispatchAttachedToWindow(RecyclerView view) { + mIsAttachedToWindow = true; + onAttachedToWindow(view); + } + + void dispatchDetachedFromWindow(RecyclerView view, Recycler recycler) { + mIsAttachedToWindow = false; + onDetachedFromWindow(view, recycler); + } + + /** + * Returns whether LayoutManager is currently attached to a RecyclerView which is attached + * to a window. + * + * @return True if this LayoutManager is controlling a RecyclerView and the RecyclerView + * is attached to window. + */ + public boolean isAttachedToWindow() { + return mIsAttachedToWindow; + } + + /** + * Causes the Runnable to execute on the next animation time step. + * The runnable will be run on the user interface thread. + *

    + * Calling this method when LayoutManager is not attached to a RecyclerView has no effect. + * + * @param action The Runnable that will be executed. + * + * @see #removeCallbacks + */ + public void postOnAnimation(Runnable action) { + if (mRecyclerView != null) { + ViewCompat.postOnAnimation(mRecyclerView, action); + } + } + + /** + * Removes the specified Runnable from the message queue. + *

    + * Calling this method when LayoutManager is not attached to a RecyclerView has no effect. + * + * @param action The Runnable to remove from the message handling queue + * + * @return true if RecyclerView could ask the Handler to remove the Runnable, + * false otherwise. When the returned value is true, the Runnable + * may or may not have been actually removed from the message queue + * (for instance, if the Runnable was not in the queue already.) + * + * @see #postOnAnimation + */ + public boolean removeCallbacks(Runnable action) { + if (mRecyclerView != null) { + return mRecyclerView.removeCallbacks(action); + } + return false; + } /** * Called when this LayoutManager is both attached to a RecyclerView and that RecyclerView * is attached to a window. - * - *

    Subclass implementations should always call through to the superclass implementation. - *

    + *

    + * If the RecyclerView is re-attached with the same LayoutManager and Adapter, it may not + * call {@link #onLayoutChildren(Recycler, State)} if nothing has changed and a layout was + * not requested on the RecyclerView while it was detached. + *

    + * Subclass implementations should always call through to the superclass implementation. * * @param view The RecyclerView this LayoutManager is bound to + * + * @see #onDetachedFromWindow(RecyclerView, Recycler) */ + @CallSuper public void onAttachedToWindow(RecyclerView view) { } @@ -4477,14 +6711,27 @@ public class RecyclerView extends ViewGroup { /** * Called when this LayoutManager is detached from its parent RecyclerView or when * its parent RecyclerView is detached from its window. - * - *

    Subclass implementations should always call through to the superclass implementation. - *

    + *

    + * LayoutManager should clear all of its View references as another LayoutManager might be + * assigned to the RecyclerView. + *

    + * If the RecyclerView is re-attached with the same LayoutManager and Adapter, it may not + * call {@link #onLayoutChildren(Recycler, State)} if nothing has changed and a layout was + * not requested on the RecyclerView while it was detached. + *

    + * If your LayoutManager has View references that it cleans in on-detach, it should also + * call {@link RecyclerView#requestLayout()} to ensure that it is re-laid out when + * RecyclerView is re-attached. + *

    + * Subclass implementations should always call through to the superclass implementation. * * @param view The RecyclerView this LayoutManager is bound to * @param recycler The recycler to use if you prefer to recycle your children instead of * keeping them around. + * + * @see #onAttachedToWindow(RecyclerView) */ + @CallSuper public void onDetachedFromWindow(RecyclerView view, Recycler recycler) { onDetachedFromWindow(view); } @@ -4510,7 +6757,7 @@ public class RecyclerView extends ViewGroup { * normal layout operation during {@link #onLayoutChildren(Recycler, State)}, the * RecyclerView will have enough information to run those animations in a simple * way. For example, the default ItemAnimator, {@link DefaultItemAnimator}, will - * simple fade views in and out, whether they are actuall added/removed or whether + * simply fade views in and out, whether they are actually added/removed or whether * they are moved on or off the screen due to other add/remove operations. * *

    A LayoutManager wanting a better item animation experience, where items can be @@ -4553,18 +6800,32 @@ public class RecyclerView extends ViewGroup { Log.e(TAG, "You must override onLayoutChildren(Recycler recycler, State state) "); } + /** + * Called after a full layout calculation is finished. The layout calculation may include + * multiple {@link #onLayoutChildren(Recycler, State)} calls due to animations or + * layout measurement but it will include only one {@link #onLayoutCompleted(State)} call. + * This method will be called at the end of {@link View#layout(int, int, int, int)} call. + *

    + * This is a good place for the LayoutManager to do some cleanup like pending scroll + * position, saved state etc. + * + * @param state Transient state of RecyclerView + */ + public void onLayoutCompleted(State state) { + } + /** * Create a default LayoutParams object for a child of the RecyclerView. * *

    LayoutManagers will often want to use a custom LayoutParams type * to store extra information specific to the layout. Client code should subclass - * {@link LayoutParams} for this purpose.

    + * {@link RecyclerView.LayoutParams} for this purpose.

    * *

    Important: if you use your own custom LayoutParams type * you must also override * {@link #checkLayoutParams(LayoutParams)}, - * {@link #generateLayoutParams(ViewGroup.LayoutParams)} and - * {@link #generateLayoutParams(Context, AttributeSet)}.

    + * {@link #generateLayoutParams(android.view.ViewGroup.LayoutParams)} and + * {@link #generateLayoutParams(android.content.Context, android.util.AttributeSet)}.

    * * @return A new LayoutParams for a child view */ @@ -4591,8 +6852,8 @@ public class RecyclerView extends ViewGroup { *

    Important: if you use your own custom LayoutParams type * you must also override * {@link #checkLayoutParams(LayoutParams)}, - * {@link #generateLayoutParams(ViewGroup.LayoutParams)} and - * {@link #generateLayoutParams(Context, AttributeSet)}.

    + * {@link #generateLayoutParams(android.view.ViewGroup.LayoutParams)} and + * {@link #generateLayoutParams(android.content.Context, android.util.AttributeSet)}.

    * * @param lp Source LayoutParams object to copy values from * @return a new LayoutParams object @@ -4614,8 +6875,8 @@ public class RecyclerView extends ViewGroup { *

    Important: if you use your own custom LayoutParams type * you must also override * {@link #checkLayoutParams(LayoutParams)}, - * {@link #generateLayoutParams(ViewGroup.LayoutParams)} and - * {@link #generateLayoutParams(Context, AttributeSet)}.

    + * {@link #generateLayoutParams(android.view.ViewGroup.LayoutParams)} and + * {@link #generateLayoutParams(android.content.Context, android.util.AttributeSet)}.

    * * @param c Context for obtaining styled attributes * @param attrs AttributeSet describing the supplied arguments @@ -4708,7 +6969,7 @@ public class RecyclerView extends ViewGroup { /** *

    Starts a smooth scroll using the provided SmoothScroller.

    *

    Calling this method will cancel any previous smooth scroll request.

    - * @param smoothScroller Unstance which defines how smooth scroll should be animated + * @param smoothScroller Instance which defines how smooth scroll should be animated */ public void startSmoothScroll(SmoothScroller smoothScroller) { if (mSmoothScroller != null && smoothScroller != mSmoothScroller @@ -4730,9 +6991,9 @@ public class RecyclerView extends ViewGroup { /** * Returns the resolved layout direction for this RecyclerView. * - * @return {@link ViewCompat#LAYOUT_DIRECTION_RTL} if the layout + * @return {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_RTL} if the layout * direction is RTL or returns - * {@link ViewCompat#LAYOUT_DIRECTION_LTR} if the layout direction + * {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_LTR} if the layout direction * is not RTL. */ public int getLayoutDirection() { @@ -4743,7 +7004,7 @@ public class RecyclerView extends ViewGroup { * Ends all animations on the view created by the {@link ItemAnimator}. * * @param view The View for which the animations should be ended. - * @see ItemAnimator#endAnimations() + * @see RecyclerView.ItemAnimator#endAnimations() */ public void endAnimation(View view) { if (mRecyclerView.mItemAnimator != null) { @@ -4813,14 +7074,14 @@ public class RecyclerView extends ViewGroup { final ViewHolder holder = getChildViewHolderInt(child); if (disappearing || holder.isRemoved()) { // these views will be hidden at the end of the layout pass. - mRecyclerView.addToDisappearingList(child); + mRecyclerView.mViewInfoStore.addToDisappearedInLayout(holder); } else { // This may look like unnecessary but may happen if layout manager supports // predictive layouts and adapter removed then re-added the same item. // In this case, added version will be visible in the post layout (because add is // deferred) but RV will still bind it to the same View. // So if a View re-appears in post layout pass, remove it from disappearing list. - mRecyclerView.removeFromDisappearingList(child); + mRecyclerView.mViewInfoStore.removeFromDisappearedInLayout(holder); } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (holder.wasReturnedFromScrap() || holder.isScrap()) { @@ -4867,7 +7128,7 @@ public class RecyclerView extends ViewGroup { * Remove a view from the currently attached RecyclerView if needed. LayoutManagers should * use this method to completely remove a child view that is no longer needed. * LayoutManagers should strongly consider recycling removed views using - * {@link Recycler#recycleView(View)}. + * {@link Recycler#recycleView(android.view.View)}. * * @param child View to remove */ @@ -4879,7 +7140,7 @@ public class RecyclerView extends ViewGroup { * Remove a view from the currently attached RecyclerView if needed. LayoutManagers should * use this method to completely remove a child view that is no longer needed. * LayoutManagers should strongly consider recycling removed views using - * {@link Recycler#recycleView(View)}. + * {@link Recycler#recycleView(android.view.View)}. * * @param index Index of the child view to remove */ @@ -4898,19 +7159,29 @@ public class RecyclerView extends ViewGroup { // Only remove non-animating views final int childCount = getChildCount(); for (int i = childCount - 1; i >= 0; i--) { - final View child = getChildAt(i); mChildHelper.removeViewAt(i); } } /** - * Returns the adapter position of the item represented by the given View. + * Returns offset of the RecyclerView's text baseline from the its top boundary. + * + * @return The offset of the RecyclerView's text baseline from the its top boundary; -1 if + * there is no baseline. + */ + public int getBaseline() { + return -1; + } + + /** + * Returns the adapter position of the item represented by the given View. This does not + * contain any adapter changes that might have happened after the last layout. * * @param view The view to query * @return The adapter position of the item which is rendered by this View. */ public int getPosition(View view) { - return ((LayoutParams) view.getLayoutParams()).getViewPosition(); + return ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition(); } /** @@ -4924,7 +7195,36 @@ public class RecyclerView extends ViewGroup { } /** + * Traverses the ancestors of the given view and returns the item view that contains it + * and also a direct child of the LayoutManager. *

    + * Note that this method may return null if the view is a child of the RecyclerView but + * not a child of the LayoutManager (e.g. running a disappear animation). + * + * @param view The view that is a descendant of the LayoutManager. + * + * @return The direct child of the LayoutManager which contains the given view or null if + * the provided view is not a descendant of this LayoutManager. + * + * @see RecyclerView#getChildViewHolder(View) + * @see RecyclerView#findContainingViewHolder(View) + */ + @Nullable + public View findContainingItemView(View view) { + if (mRecyclerView == null) { + return null; + } + View found = mRecyclerView.findContainingItemView(view); + if (found == null) { + return null; + } + if (mChildHelper.isHidden(found)) { + return null; + } + return found; + } + + /** * Finds the view which represents the given adapter position. *

    * This method traverses each child since it has no information about child order. @@ -4935,7 +7235,7 @@ public class RecyclerView extends ViewGroup { * * @param position Position of the item in adapter * @return The child view that represents the given position or null if the position is not - * visible + * laid out */ public View findViewByPosition(int position) { final int childCount = getChildCount(); @@ -4945,7 +7245,7 @@ public class RecyclerView extends ViewGroup { if (vh == null) { continue; } - if (vh.getPosition() == position && !vh.shouldIgnore() && + if (vh.getLayoutPosition() == position && !vh.shouldIgnore() && (mRecyclerView.mState.isPreLayout() || !vh.isRemoved())) { return child; } @@ -4958,12 +7258,12 @@ public class RecyclerView extends ViewGroup { * *

    LayoutManagers may want to perform a lightweight detach operation to rearrange * views currently attached to the RecyclerView. Generally LayoutManager implementations - * will want to use {@link #detachAndScrapView(View, Recycler)} + * will want to use {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)} * so that the detached view may be rebound and reused.

    * *

    If a LayoutManager uses this method to detach a view, it must - * {@link #attachView(View, int, LayoutParams) reattach} - * or {@link #removeDetachedView(View) fully remove} the detached view + * {@link #attachView(android.view.View, int, RecyclerView.LayoutParams) reattach} + * or {@link #removeDetachedView(android.view.View) fully remove} the detached view * before the LayoutManager entry point method called by RecyclerView returns.

    * * @param child Child to detach @@ -4980,12 +7280,12 @@ public class RecyclerView extends ViewGroup { * *

    LayoutManagers may want to perform a lightweight detach operation to rearrange * views currently attached to the RecyclerView. Generally LayoutManager implementations - * will want to use {@link #detachAndScrapView(View, Recycler)} + * will want to use {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)} * so that the detached view may be rebound and reused.

    * *

    If a LayoutManager uses this method to detach a view, it must - * {@link #attachView(View, int, LayoutParams) reattach} - * or {@link #removeDetachedView(View) fully remove} the detached view + * {@link #attachView(android.view.View, int, RecyclerView.LayoutParams) reattach} + * or {@link #removeDetachedView(android.view.View) fully remove} the detached view * before the LayoutManager entry point method called by RecyclerView returns.

    * * @param index Index of the child to detach @@ -5002,9 +7302,9 @@ public class RecyclerView extends ViewGroup { } /** - * Reattach a previously {@link #detachView(View) detached} view. + * Reattach a previously {@link #detachView(android.view.View) detached} view. * This method should not be used to reattach views that were previously - * {@link #detachAndScrapView(View, Recycler)} scrapped}. + * {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)} scrapped}. * * @param child Child to reattach * @param index Intended child index for child @@ -5013,9 +7313,9 @@ public class RecyclerView extends ViewGroup { public void attachView(View child, int index, LayoutParams lp) { ViewHolder vh = getChildViewHolderInt(child); if (vh.isRemoved()) { - mRecyclerView.addToDisappearingList(child); + mRecyclerView.mViewInfoStore.addToDisappearedInLayout(vh); } else { - mRecyclerView.removeFromDisappearingList(child); + mRecyclerView.mViewInfoStore.removeFromDisappearedInLayout(vh); } mChildHelper.attachViewToParent(child, index, lp, vh.isRemoved()); if (DISPATCH_TEMP_DETACH) { @@ -5024,9 +7324,9 @@ public class RecyclerView extends ViewGroup { } /** - * Reattach a previously {@link #detachView(View) detached} view. + * Reattach a previously {@link #detachView(android.view.View) detached} view. * This method should not be used to reattach views that were previously - * {@link #detachAndScrapView(View, Recycler)} scrapped}. + * {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)} scrapped}. * * @param child Child to reattach * @param index Intended child index for child @@ -5036,9 +7336,9 @@ public class RecyclerView extends ViewGroup { } /** - * Reattach a previously {@link #detachView(View) detached} view. + * Reattach a previously {@link #detachView(android.view.View) detached} view. * This method should not be used to reattach views that were previously - * {@link #detachAndScrapView(View, Recycler)} scrapped}. + * {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)} scrapped}. * * @param child Child to reattach */ @@ -5048,7 +7348,7 @@ public class RecyclerView extends ViewGroup { /** * Finish removing a view that was previously temporarily - * {@link #detachView(View) detached}. + * {@link #detachView(android.view.View) detached}. * * @param child Detached child to remove */ @@ -5142,13 +7442,49 @@ public class RecyclerView extends ViewGroup { return mChildHelper != null ? mChildHelper.getChildAt(index) : null; } + /** + * Return the width measurement spec mode of the RecyclerView. + *

    + * This value is set only if the LayoutManager opts into the auto measure api via + * {@link #setAutoMeasureEnabled(boolean)}. + *

    + * When RecyclerView is running a layout, this value is always set to + * {@link View.MeasureSpec#EXACTLY} even if it was measured with a different spec mode. + * + * @return Width measure spec mode. + * + * @see View.MeasureSpec#getMode(int) + * @see View#onMeasure(int, int) + */ + public int getWidthMode() { + return mWidthMode; + } + + /** + * Return the height measurement spec mode of the RecyclerView. + *

    + * This value is set only if the LayoutManager opts into the auto measure api via + * {@link #setAutoMeasureEnabled(boolean)}. + *

    + * When RecyclerView is running a layout, this value is always set to + * {@link View.MeasureSpec#EXACTLY} even if it was measured with a different spec mode. + * + * @return Height measure spec mode. + * + * @see View.MeasureSpec#getMode(int) + * @see View#onMeasure(int, int) + */ + public int getHeightMode() { + return mHeightMode; + } + /** * Return the width of the parent RecyclerView * * @return Width in pixels */ public int getWidth() { - return mRecyclerView != null ? mRecyclerView.getWidth() : 0; + return mWidth; } /** @@ -5157,7 +7493,7 @@ public class RecyclerView extends ViewGroup { * @return Height in pixels */ public int getHeight() { - return mRecyclerView != null ? mRecyclerView.getHeight() : 0; + return mHeight; } /** @@ -5313,7 +7649,7 @@ public class RecyclerView extends ViewGroup { } final ViewHolder vh = getChildViewHolderInt(view); vh.addFlags(ViewHolder.FLAG_IGNORE); - mRecyclerView.mState.onViewIgnored(vh); + mRecyclerView.mViewInfoStore.removeViewHolder(vh); } /** @@ -5355,13 +7691,14 @@ public class RecyclerView extends ViewGroup { } return; } - if (viewHolder.isInvalid() && !viewHolder.isRemoved() && !viewHolder.isChanged() && + if (viewHolder.isInvalid() && !viewHolder.isRemoved() && !mRecyclerView.mAdapter.hasStableIds()) { removeViewAt(index); recycler.recycleViewHolderInternal(viewHolder); } else { detachViewAt(index); recycler.scrapView(view); + mRecyclerView.mViewInfoStore.onViewDetached(viewHolder); } } @@ -5373,23 +7710,33 @@ public class RecyclerView extends ViewGroup { * call remove and invalidate RecyclerView to ensure UI update. * * @param recycler Recycler - * @param remove Whether scrapped views should be removed from ViewGroup or not. This - * method will invalidate RecyclerView if it removes any scrapped child. */ - void removeAndRecycleScrapInt(Recycler recycler, boolean remove) { + void removeAndRecycleScrapInt(Recycler recycler) { final int scrapCount = recycler.getScrapCount(); - for (int i = 0; i < scrapCount; i++) { + // Loop backward, recycler might be changed by removeDetachedView() + for (int i = scrapCount - 1; i >= 0; i--) { final View scrap = recycler.getScrapViewAt(i); - if (getChildViewHolderInt(scrap).shouldIgnore()) { + final ViewHolder vh = getChildViewHolderInt(scrap); + if (vh.shouldIgnore()) { continue; } - if (remove) { + // If the scrap view is animating, we need to cancel them first. If we cancel it + // here, ItemAnimator callback may recycle it which will cause double recycling. + // To avoid this, we mark it as not recycleable before calling the item animator. + // Since removeDetachedView calls a user API, a common mistake (ending animations on + // the view) may recycle it too, so we guard it before we call user APIs. + vh.setIsRecyclable(false); + if (vh.isTmpDetached()) { mRecyclerView.removeDetachedView(scrap, false); } + if (mRecyclerView.mItemAnimator != null) { + mRecyclerView.mItemAnimator.endAnimation(vh); + } + vh.setIsRecyclable(true); recycler.quickRecycleScrapView(scrap); } recycler.clearScrap(); - if (remove && scrapCount > 0) { + if (scrapCount > 0) { mRecyclerView.invalidate(); } } @@ -5412,14 +7759,85 @@ public class RecyclerView extends ViewGroup { final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child); widthUsed += insets.left + insets.right; heightUsed += insets.top + insets.bottom; - - final int widthSpec = getChildMeasureSpec(getWidth(), + final int widthSpec = getChildMeasureSpec(getWidth(), getWidthMode(), getPaddingLeft() + getPaddingRight() + widthUsed, lp.width, canScrollHorizontally()); - final int heightSpec = getChildMeasureSpec(getHeight(), + final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(), getPaddingTop() + getPaddingBottom() + heightUsed, lp.height, canScrollVertically()); - child.measure(widthSpec, heightSpec); + if (shouldMeasureChild(child, widthSpec, heightSpec, lp)) { + child.measure(widthSpec, heightSpec); + } + } + + /** + * RecyclerView internally does its own View measurement caching which should help with + * WRAP_CONTENT. + *

    + * Use this method if the View is already measured once in this layout pass. + */ + boolean shouldReMeasureChild(View child, int widthSpec, int heightSpec, LayoutParams lp) { + return !mMeasurementCacheEnabled + || !isMeasurementUpToDate(child.getMeasuredWidth(), widthSpec, lp.width) + || !isMeasurementUpToDate(child.getMeasuredHeight(), heightSpec, lp.height); + } + + // we may consider making this public + /** + * RecyclerView internally does its own View measurement caching which should help with + * WRAP_CONTENT. + *

    + * Use this method if the View is not yet measured and you need to decide whether to + * measure this View or not. + */ + boolean shouldMeasureChild(View child, int widthSpec, int heightSpec, LayoutParams lp) { + return child.isLayoutRequested() + || !mMeasurementCacheEnabled + || !isMeasurementUpToDate(child.getWidth(), widthSpec, lp.width) + || !isMeasurementUpToDate(child.getHeight(), heightSpec, lp.height); + } + + /** + * In addition to the View Framework's measurement cache, RecyclerView uses its own + * additional measurement cache for its children to avoid re-measuring them when not + * necessary. It is on by default but it can be turned off via + * {@link #setMeasurementCacheEnabled(boolean)}. + * + * @return True if measurement cache is enabled, false otherwise. + * + * @see #setMeasurementCacheEnabled(boolean) + */ + public boolean isMeasurementCacheEnabled() { + return mMeasurementCacheEnabled; + } + + /** + * Sets whether RecyclerView should use its own measurement cache for the children. This is + * a more aggressive cache than the framework uses. + * + * @param measurementCacheEnabled True to enable the measurement cache, false otherwise. + * + * @see #isMeasurementCacheEnabled() + */ + public void setMeasurementCacheEnabled(boolean measurementCacheEnabled) { + mMeasurementCacheEnabled = measurementCacheEnabled; + } + + private static boolean isMeasurementUpToDate(int childSize, int spec, int dimension) { + final int specMode = MeasureSpec.getMode(spec); + final int specSize = MeasureSpec.getSize(spec); + if (dimension > 0 && childSize != dimension) { + return false; + } + switch (specMode) { + case MeasureSpec.UNSPECIFIED: + return true; + case MeasureSpec.AT_MOST: + return specSize >= childSize; + case MeasureSpec.EXACTLY: + return specSize == childSize; + } + return false; } /** @@ -5441,34 +7859,37 @@ public class RecyclerView extends ViewGroup { widthUsed += insets.left + insets.right; heightUsed += insets.top + insets.bottom; - final int widthSpec = getChildMeasureSpec(getWidth(), + final int widthSpec = getChildMeasureSpec(getWidth(), getWidthMode(), getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin + widthUsed, lp.width, canScrollHorizontally()); - final int heightSpec = getChildMeasureSpec(getHeight(), + final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(), getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin + heightUsed, lp.height, canScrollVertically()); - child.measure(widthSpec, heightSpec); + if (shouldMeasureChild(child, widthSpec, heightSpec, lp)) { + child.measure(widthSpec, heightSpec); + } } /** * Calculate a MeasureSpec value for measuring a child view in one dimension. * * @param parentSize Size of the parent view where the child will be placed - * @param padding Total space currently consumed by other elements of parent + * @param padding Total space currently consumed by other elements of the parent * @param childDimension Desired size of the child view, or MATCH_PARENT/WRAP_CONTENT. * Generally obtained from the child view's LayoutParams * @param canScroll true if the parent RecyclerView can scroll in this dimension * * @return a MeasureSpec value for the child view + * @deprecated use {@link #getChildMeasureSpec(int, int, int, int, boolean)} */ + @Deprecated public static int getChildMeasureSpec(int parentSize, int padding, int childDimension, boolean canScroll) { int size = Math.max(0, parentSize - padding); int resultSize = 0; int resultMode = 0; - if (canScroll) { if (childDimension >= 0) { resultSize = childDimension; @@ -5483,8 +7904,9 @@ public class RecyclerView extends ViewGroup { if (childDimension >= 0) { resultSize = childDimension; resultMode = MeasureSpec.EXACTLY; - } else if (childDimension == LayoutParams.FILL_PARENT) { + } else if (childDimension == LayoutParams.MATCH_PARENT) { resultSize = size; + // TODO this should be my spec. resultMode = MeasureSpec.EXACTLY; } else if (childDimension == LayoutParams.WRAP_CONTENT) { resultSize = size; @@ -5494,6 +7916,61 @@ public class RecyclerView extends ViewGroup { return MeasureSpec.makeMeasureSpec(resultSize, resultMode); } + /** + * Calculate a MeasureSpec value for measuring a child view in one dimension. + * + * @param parentSize Size of the parent view where the child will be placed + * @param parentMode The measurement spec mode of the parent + * @param padding Total space currently consumed by other elements of parent + * @param childDimension Desired size of the child view, or MATCH_PARENT/WRAP_CONTENT. + * Generally obtained from the child view's LayoutParams + * @param canScroll true if the parent RecyclerView can scroll in this dimension + * + * @return a MeasureSpec value for the child view + */ + public static int getChildMeasureSpec(int parentSize, int parentMode, int padding, + int childDimension, boolean canScroll) { + int size = Math.max(0, parentSize - padding); + int resultSize = 0; + int resultMode = 0; + if (childDimension >= 0) { + resultSize = childDimension; + resultMode = MeasureSpec.EXACTLY; + } else if (canScroll) { + if (childDimension == LayoutParams.MATCH_PARENT){ + switch (parentMode) { + case MeasureSpec.AT_MOST: + case MeasureSpec.EXACTLY: + resultSize = size; + resultMode = parentMode; + break; + case MeasureSpec.UNSPECIFIED: + resultSize = 0; + resultMode = MeasureSpec.UNSPECIFIED; + break; + } + } else if (childDimension == LayoutParams.WRAP_CONTENT) { + resultSize = 0; + resultMode = MeasureSpec.UNSPECIFIED; + } + } else { + if (childDimension == LayoutParams.MATCH_PARENT) { + resultSize = size; + resultMode = parentMode; + } else if (childDimension == LayoutParams.WRAP_CONTENT) { + resultSize = size; + if (parentMode == MeasureSpec.AT_MOST || parentMode == MeasureSpec.EXACTLY) { + resultMode = MeasureSpec.AT_MOST; + } else { + resultMode = MeasureSpec.UNSPECIFIED; + } + + } + } + //noinspection WrongConstant + return MeasureSpec.makeMeasureSpec(resultSize, resultMode); + } + /** * Returns the measured width of the given child, plus the additional size of * any insets applied by {@link ItemDecoration ItemDecorations}. @@ -5531,6 +8008,8 @@ public class RecyclerView extends ViewGroup { * ignore decoration insets within measurement and layout code. See the following * methods:

    *
      + *
    • {@link #layoutDecoratedWithMargins(View, int, int, int, int)}
    • + *
    • {@link #getDecoratedBoundsWithMargins(View, Rect)}
    • *
    • {@link #measureChild(View, int, int)}
    • *
    • {@link #measureChildWithMargins(View, int, int)}
    • *
    • {@link #getDecoratedLeft(View)}
    • @@ -5548,6 +8027,7 @@ public class RecyclerView extends ViewGroup { * @param bottom Bottom edge, with item decoration insets included * * @see View#layout(int, int, int, int) + * @see #layoutDecoratedWithMargins(View, int, int, int, int) */ public void layoutDecorated(View child, int left, int top, int right, int bottom) { final Rect insets = ((LayoutParams) child.getLayoutParams()).mDecorInsets; @@ -5555,6 +8035,97 @@ public class RecyclerView extends ViewGroup { bottom - insets.bottom); } + /** + * Lay out the given child view within the RecyclerView using coordinates that + * include any current {@link ItemDecoration ItemDecorations} and margins. + * + *

      LayoutManagers should prefer working in sizes and coordinates that include + * item decoration insets whenever possible. This allows the LayoutManager to effectively + * ignore decoration insets within measurement and layout code. See the following + * methods:

      + *
        + *
      • {@link #layoutDecorated(View, int, int, int, int)}
      • + *
      • {@link #measureChild(View, int, int)}
      • + *
      • {@link #measureChildWithMargins(View, int, int)}
      • + *
      • {@link #getDecoratedLeft(View)}
      • + *
      • {@link #getDecoratedTop(View)}
      • + *
      • {@link #getDecoratedRight(View)}
      • + *
      • {@link #getDecoratedBottom(View)}
      • + *
      • {@link #getDecoratedMeasuredWidth(View)}
      • + *
      • {@link #getDecoratedMeasuredHeight(View)}
      • + *
      + * + * @param child Child to lay out + * @param left Left edge, with item decoration insets and left margin included + * @param top Top edge, with item decoration insets and top margin included + * @param right Right edge, with item decoration insets and right margin included + * @param bottom Bottom edge, with item decoration insets and bottom margin included + * + * @see View#layout(int, int, int, int) + * @see #layoutDecorated(View, int, int, int, int) + */ + public void layoutDecoratedWithMargins(View child, int left, int top, int right, + int bottom) { + final LayoutParams lp = (LayoutParams) child.getLayoutParams(); + final Rect insets = lp.mDecorInsets; + child.layout(left + insets.left + lp.leftMargin, top + insets.top + lp.topMargin, + right - insets.right - lp.rightMargin, + bottom - insets.bottom - lp.bottomMargin); + } + + /** + * Calculates the bounding box of the View while taking into account its matrix changes + * (translation, scale etc) with respect to the RecyclerView. + *

      + * If {@code includeDecorInsets} is {@code true}, they are applied first before applying + * the View's matrix so that the decor offsets also go through the same transformation. + * + * @param child The ItemView whose bounding box should be calculated. + * @param includeDecorInsets True if the decor insets should be included in the bounding box + * @param out The rectangle into which the output will be written. + */ + public void getTransformedBoundingBox(View child, boolean includeDecorInsets, Rect out) { + if (includeDecorInsets) { + Rect insets = ((LayoutParams) child.getLayoutParams()).mDecorInsets; + out.set(-insets.left, -insets.top, + child.getWidth() + insets.right, child.getHeight() + insets.bottom); + } else { + out.set(0, 0, child.getWidth(), child.getHeight()); + } + + if (mRecyclerView != null) { + final Matrix childMatrix = ViewCompat.getMatrix(child); + if (childMatrix != null && !childMatrix.isIdentity()) { + final RectF tempRectF = mRecyclerView.mTempRectF; + tempRectF.set(out); + childMatrix.mapRect(tempRectF); + out.set( + (int) Math.floor(tempRectF.left), + (int) Math.floor(tempRectF.top), + (int) Math.ceil(tempRectF.right), + (int) Math.ceil(tempRectF.bottom) + ); + } + } + out.offset(child.getLeft(), child.getTop()); + } + + /** + * Returns the bounds of the view including its decoration and margins. + * + * @param view The view element to check + * @param outBounds A rect that will receive the bounds of the element including its + * decoration and margins. + */ + public void getDecoratedBoundsWithMargins(View view, Rect outBounds) { + final LayoutParams lp = (LayoutParams) view.getLayoutParams(); + final Rect insets = lp.mDecorInsets; + outBounds.set(view.getLeft() - insets.left - lp.leftMargin, + view.getTop() - insets.top - lp.topMargin, + view.getRight() + insets.right + lp.rightMargin, + view.getBottom() + insets.bottom + lp.bottomMargin); + } + /** * Returns the left edge of the given child view within its parent, offset by any applied * {@link ItemDecoration ItemDecorations}. @@ -5706,6 +8277,7 @@ public class RecyclerView extends ViewGroup { * @param state Transient state of RecyclerView * @return The chosen view to be focused */ + @Nullable public View onFocusSearchFailed(View focused, int direction, Recycler recycler, State state) { return null; @@ -5715,7 +8287,7 @@ public class RecyclerView extends ViewGroup { * This method gives a LayoutManager an opportunity to intercept the initial focus search * before the default behavior of {@link FocusFinder} is used. If this method returns * null FocusFinder will attempt to find a focusable child view. If it fails - * then {@link #onFocusSearchFailed(View, int, Recycler, State)} + * then {@link #onFocusSearchFailed(View, int, RecyclerView.Recycler, RecyclerView.State)} * will be called to give the LayoutManager an opportunity to add new views for items * that did not have attached views representing them. The LayoutManager should not add * or remove views from this method. @@ -5733,8 +8305,8 @@ public class RecyclerView extends ViewGroup { /** * Called when a child of the RecyclerView wants a particular rectangle to be positioned - * onto the screen. See {@link ViewParent#requestChildRectangleOnScreen(View, - * Rect, boolean)} for more details. + * onto the screen. See {@link ViewParent#requestChildRectangleOnScreen(android.view.View, + * android.graphics.Rect, boolean)} for more details. * *

      The base implementation will attempt to perform a standard programmatic scroll * to bring the given rect into view, within the padded area of the RecyclerView.

      @@ -5752,10 +8324,10 @@ public class RecyclerView extends ViewGroup { final int parentTop = getPaddingTop(); final int parentRight = getWidth() - getPaddingRight(); final int parentBottom = getHeight() - getPaddingBottom(); - final int childLeft = child.getLeft() + rect.left; - final int childTop = child.getTop() + rect.top; - final int childRight = childLeft + rect.right; - final int childBottom = childTop + rect.bottom; + final int childLeft = child.getLeft() + rect.left - child.getScrollX(); + final int childTop = child.getTop() + rect.top - child.getScrollY(); + final int childRight = childLeft + rect.width(); + final int childBottom = childTop + rect.height(); final int offScreenLeft = Math.min(0, childLeft - parentLeft); final int offScreenTop = Math.min(0, childTop - parentTop); @@ -5763,22 +8335,27 @@ public class RecyclerView extends ViewGroup { final int offScreenBottom = Math.max(0, childBottom - parentBottom); // Favor the "start" layout direction over the end when bringing one side or the other - // of a large rect into view. + // of a large rect into view. If we decide to bring in end because start is already + // visible, limit the scroll such that start won't go out of bounds. final int dx; - if (ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL) { - dx = offScreenRight != 0 ? offScreenRight : offScreenLeft; + if (getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL) { + dx = offScreenRight != 0 ? offScreenRight + : Math.max(offScreenLeft, childRight - parentRight); } else { - dx = offScreenLeft != 0 ? offScreenLeft : offScreenRight; + dx = offScreenLeft != 0 ? offScreenLeft + : Math.min(childLeft - parentLeft, offScreenRight); } - // Favor bringing the top into view over the bottom - final int dy = offScreenTop; -// final int dy = offScreenTop != 0 ? offScreenTop : offScreenBottom; + // Favor bringing the top into view over the bottom. If top is already visible and + // we should scroll to make bottom visible, make sure top does not go out of bounds. + final int dy = offScreenTop != 0 ? offScreenTop + : Math.min(childTop - parentTop, offScreenBottom); + if (dx != 0 || dy != 0) { if (immediate) { parent.scrollBy(dx, dy); } else { -// parent.smoothScrollBy(dx, dy); + parent.smoothScrollBy(dx, dy); } return true; } @@ -5790,7 +8367,8 @@ public class RecyclerView extends ViewGroup { */ @Deprecated public boolean onRequestChildFocus(RecyclerView parent, View child, View focused) { - return false; + // eat the request if we are in the middle of a scroll or layout + return isSmoothScrolling() || parent.isComputingLayout(); } /** @@ -5825,7 +8403,7 @@ public class RecyclerView extends ViewGroup { * @param oldAdapter The previous adapter instance. Will be null if there was previously no * adapter. * @param newAdapter The new adapter instance. Might be null if - * {@link #setAdapter(Adapter)} is called with {@code null}. + * {@link #setAdapter(RecyclerView.Adapter)} is called with {@code null}. */ public void onAdapterChanged(Adapter oldAdapter, Adapter newAdapter) { } @@ -5834,7 +8412,7 @@ public class RecyclerView extends ViewGroup { * Called to populate focusable views within the RecyclerView. * *

      The LayoutManager implementation should return true if the default - * behavior of {@link ViewGroup#addFocusables(ArrayList, int)} should be + * behavior of {@link ViewGroup#addFocusables(java.util.ArrayList, int)} should be * suppressed.

      * *

      The default implementation returns false to trigger RecyclerView @@ -5892,6 +8470,8 @@ public class RecyclerView extends ViewGroup { /** * Called when items have been changed in the adapter. + * To receive payload, override {@link #onItemsUpdated(RecyclerView, int, int, Object)} + * instead, then this callback will not be invoked. * * @param recyclerView * @param positionStart @@ -5900,6 +8480,20 @@ public class RecyclerView extends ViewGroup { public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount) { } + /** + * Called when items have been changed in the adapter and with optional payload. + * Default implementation calls {@link #onItemsUpdated(RecyclerView, int, int)}. + * + * @param recyclerView + * @param positionStart + * @param itemCount + * @param payload + */ + public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount, + Object payload) { + onItemsUpdated(recyclerView, positionStart, itemCount); + } + /** * Called when an item is moved withing the adapter. *

      @@ -6018,41 +8612,11 @@ public class RecyclerView extends ViewGroup { * * @param recycler Recycler * @param state Transient state of RecyclerView - * @param widthSpec Width {@link MeasureSpec} - * @param heightSpec Height {@link MeasureSpec} + * @param widthSpec Width {@link android.view.View.MeasureSpec} + * @param heightSpec Height {@link android.view.View.MeasureSpec} */ public void onMeasure(Recycler recycler, State state, int widthSpec, int heightSpec) { - final int widthMode = MeasureSpec.getMode(widthSpec); - final int heightMode = MeasureSpec.getMode(heightSpec); - final int widthSize = MeasureSpec.getSize(widthSpec); - final int heightSize = MeasureSpec.getSize(heightSpec); - - int width = 0; - int height = 0; - - switch (widthMode) { - case MeasureSpec.EXACTLY: - case MeasureSpec.AT_MOST: - width = widthSize; - break; - case MeasureSpec.UNSPECIFIED: - default: - width = getMinimumWidth(); - break; - } - - switch (heightMode) { - case MeasureSpec.EXACTLY: - case MeasureSpec.AT_MOST: - height = heightSize; - break; - case MeasureSpec.UNSPECIFIED: - default: - height = getMinimumHeight(); - break; - } - - setMeasuredDimension(width, height); + mRecyclerView.defaultOnMeasure(widthSpec, heightSpec); } /** @@ -6141,8 +8705,7 @@ public class RecyclerView extends ViewGroup { // called by accessibility delegate void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfoCompat info) { - onInitializeAccessibilityNodeInfo(mRecyclerView.mRecycler, mRecyclerView.mState, - info); + onInitializeAccessibilityNodeInfo(mRecyclerView.mRecycler, mRecyclerView.mState, info); } /** @@ -6150,13 +8713,13 @@ public class RecyclerView extends ViewGroup { * be populated. *

      * Default implementation adds a {@link - * AccessibilityNodeInfoCompat.CollectionInfoCompat}. + * android.support.v4.view.accessibility.AccessibilityNodeInfoCompat.CollectionInfoCompat}. *

      * You should override - * {@link #getRowCountForAccessibility(Recycler, State)}, - * {@link #getColumnCountForAccessibility(Recycler, State)}, - * {@link #isLayoutHierarchical(Recycler, State)} and - * {@link #getSelectionModeForAccessibility(Recycler, State)} for + * {@link #getRowCountForAccessibility(RecyclerView.Recycler, RecyclerView.State)}, + * {@link #getColumnCountForAccessibility(RecyclerView.Recycler, RecyclerView.State)}, + * {@link #isLayoutHierarchical(RecyclerView.Recycler, RecyclerView.State)} and + * {@link #getSelectionModeForAccessibility(RecyclerView.Recycler, RecyclerView.State)} for * more accurate accessibility information. * * @param recycler The Recycler that can be used to convert view positions into adapter @@ -6165,14 +8728,13 @@ public class RecyclerView extends ViewGroup { * @param info The info that should be filled by the LayoutManager * @see View#onInitializeAccessibilityNodeInfo( *android.view.accessibility.AccessibilityNodeInfo) - * @see #getRowCountForAccessibility(Recycler, State) - * @see #getColumnCountForAccessibility(Recycler, State) - * @see #isLayoutHierarchical(Recycler, State) - * @see #getSelectionModeForAccessibility(Recycler, State) + * @see #getRowCountForAccessibility(RecyclerView.Recycler, RecyclerView.State) + * @see #getColumnCountForAccessibility(RecyclerView.Recycler, RecyclerView.State) + * @see #isLayoutHierarchical(RecyclerView.Recycler, RecyclerView.State) + * @see #getSelectionModeForAccessibility(RecyclerView.Recycler, RecyclerView.State) */ public void onInitializeAccessibilityNodeInfo(Recycler recycler, State state, AccessibilityNodeInfoCompat info) { - info.setClassName(RecyclerView.class.getName()); if (ViewCompat.canScrollVertically(mRecyclerView, -1) || ViewCompat.canScrollHorizontally(mRecyclerView, -1)) { info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD); @@ -6206,7 +8768,7 @@ public class RecyclerView extends ViewGroup { * positions * @param state The current state of RecyclerView * @param event The event instance to initialize - * @see View#onInitializeAccessibilityEvent(AccessibilityEvent) + * @see View#onInitializeAccessibilityEvent(android.view.accessibility.AccessibilityEvent) */ public void onInitializeAccessibilityEvent(Recycler recycler, State state, AccessibilityEvent event) { @@ -6226,10 +8788,13 @@ public class RecyclerView extends ViewGroup { } // called by accessibility delegate - void onInitializeAccessibilityNodeInfoForItem(View host, - AccessibilityNodeInfoCompat info) { - onInitializeAccessibilityNodeInfoForItem(mRecyclerView.mRecycler, mRecyclerView.mState, - host, info); + void onInitializeAccessibilityNodeInfoForItem(View host, AccessibilityNodeInfoCompat info) { + final ViewHolder vh = getChildViewHolderInt(host); + // avoid trying to create accessibility node info for removed children + if (vh != null && !vh.isRemoved() && !mChildHelper.isHidden(vh.itemView)) { + onInitializeAccessibilityNodeInfoForItem(mRecyclerView.mRecycler, + mRecyclerView.mState, host, info); + } } /** @@ -6355,7 +8920,7 @@ public class RecyclerView extends ViewGroup { * @param state The current state of RecyclerView * @param action The action to perform * @param args Optional action arguments - * @see View#performAccessibilityAction(int, Bundle) + * @see View#performAccessibilityAction(int, android.os.Bundle) */ public boolean performAccessibilityAction(Recycler recycler, State state, int action, Bundle args) { @@ -6396,7 +8961,7 @@ public class RecyclerView extends ViewGroup { /** * Called by AccessibilityDelegate when an accessibility action is requested on one of the - * chidren of LayoutManager. + * children of LayoutManager. *

      * Default implementation does not do anything. * @@ -6407,21 +8972,81 @@ public class RecyclerView extends ViewGroup { * @param action The action to perform * @param args Optional action arguments * @return true if action is handled - * @see View#performAccessibilityAction(int, Bundle) + * @see View#performAccessibilityAction(int, android.os.Bundle) */ public boolean performAccessibilityActionForItem(Recycler recycler, State state, View view, int action, Bundle args) { return false; } - } - private void removeFromDisappearingList(View child) { - mDisappearingViewsInLayoutPass.remove(child); - } + /** + * Parse the xml attributes to get the most common properties used by layout managers. + * + * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_android_orientation + * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_spanCount + * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_reverseLayout + * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_stackFromEnd + * + * @return an object containing the properties as specified in the attrs. + */ + public static Properties getProperties(Context context, AttributeSet attrs, + int defStyleAttr, int defStyleRes) { + Properties properties = new Properties(); + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecyclerView, + defStyleAttr, defStyleRes); + properties.orientation = a.getInt(R.styleable.RecyclerView_android_orientation, VERTICAL); + properties.spanCount = a.getInt(R.styleable.RecyclerView_spanCount, 1); + properties.reverseLayout = a.getBoolean(R.styleable.RecyclerView_reverseLayout, false); + properties.stackFromEnd = a.getBoolean(R.styleable.RecyclerView_stackFromEnd, false); + a.recycle(); + return properties; + } - private void addToDisappearingList(View child) { - if (!mDisappearingViewsInLayoutPass.contains(child)) { - mDisappearingViewsInLayoutPass.add(child); + void setExactMeasureSpecsFrom(RecyclerView recyclerView) { + setMeasureSpecs( + MeasureSpec.makeMeasureSpec(recyclerView.getWidth(), MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(recyclerView.getHeight(), MeasureSpec.EXACTLY) + ); + } + + /** + * Internal API to allow LayoutManagers to be measured twice. + *

      + * This is not public because LayoutManagers should be able to handle their layouts in one + * pass but it is very convenient to make existing LayoutManagers support wrapping content + * when both orientations are undefined. + *

      + * This API will be removed after default LayoutManagers properly implement wrap content in + * non-scroll orientation. + */ + boolean shouldMeasureTwice() { + return false; + } + + boolean hasFlexibleChildInBothOrientations() { + final int childCount = getChildCount(); + for (int i = 0; i < childCount; i++) { + final View child = getChildAt(i); + final ViewGroup.LayoutParams lp = child.getLayoutParams(); + if (lp.width < 0 && lp.height < 0) { + return true; + } + } + return false; + } + + /** + * Some general properties that a LayoutManager may want to use. + */ + public static class Properties { + /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_android_orientation */ + public int orientation; + /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_spanCount */ + public int spanCount; + /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_reverseLayout */ + public boolean reverseLayout; + /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_stackFromEnd */ + public boolean stackFromEnd; } } @@ -6431,9 +9056,9 @@ public class RecyclerView extends ViewGroup { * between items, highlights, visual grouping boundaries and more. * *

      All ItemDecorations are drawn in the order they were added, before the item - * views (in {@link ItemDecoration#onDraw(Canvas, RecyclerView, State) onDraw()} + * views (in {@link ItemDecoration#onDraw(Canvas, RecyclerView, RecyclerView.State) onDraw()} * and after the items (in {@link ItemDecoration#onDrawOver(Canvas, RecyclerView, - * State)}.

      + * RecyclerView.State)}.

      */ public static abstract class ItemDecoration { /** @@ -6451,7 +9076,7 @@ public class RecyclerView extends ViewGroup { /** * @deprecated - * Override {@link #onDraw(Canvas, RecyclerView, State)} + * Override {@link #onDraw(Canvas, RecyclerView, RecyclerView.State)} */ @Deprecated public void onDraw(Canvas c, RecyclerView parent) { @@ -6472,7 +9097,7 @@ public class RecyclerView extends ViewGroup { /** * @deprecated - * Override {@link #onDrawOver(Canvas, RecyclerView, State)} + * Override {@link #onDrawOver(Canvas, RecyclerView, RecyclerView.State)} */ @Deprecated public void onDrawOver(Canvas c, RecyclerView parent) { @@ -6493,9 +9118,15 @@ public class RecyclerView extends ViewGroup { * the number of pixels that the item view should be inset by, similar to padding or margin. * The default implementation sets the bounds of outRect to 0 and returns. * - *

      If this ItemDecoration does not affect the positioning of item views it should set + *

      + * If this ItemDecoration does not affect the positioning of item views, it should set * all four fields of outRect (left, top, right, bottom) to zero - * before returning.

      + * before returning. + * + *

      + * If you need to access Adapter for additional data, you can call + * {@link RecyclerView#getChildAdapterPosition(View)} to get the adapter position of the + * View. * * @param outRect Rect to receive the output. * @param view The child view to decorate @@ -6503,7 +9134,7 @@ public class RecyclerView extends ViewGroup { * @param state The current state of RecyclerView. */ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) { - getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewPosition(), + getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(), parent); } } @@ -6517,8 +9148,10 @@ public class RecyclerView extends ViewGroup { * manipulation of item views within the RecyclerView. OnItemTouchListeners may intercept * a touch interaction already in progress even if the RecyclerView is already handling that * gesture stream itself for the purposes of scrolling.

      + * + * @see SimpleOnItemTouchListener */ - public interface OnItemTouchListener { + public static interface OnItemTouchListener { /** * Silently observe and/or take over touch events sent to the RecyclerView * before they are handled by either the RecyclerView itself or its child views. @@ -6543,15 +9176,53 @@ public class RecyclerView extends ViewGroup { * the RecyclerView's coordinate system. */ public void onTouchEvent(RecyclerView rv, MotionEvent e); + + /** + * Called when a child of RecyclerView does not want RecyclerView and its ancestors to + * intercept touch events with + * {@link ViewGroup#onInterceptTouchEvent(MotionEvent)}. + * + * @param disallowIntercept True if the child does not want the parent to + * intercept touch events. + * @see ViewParent#requestDisallowInterceptTouchEvent(boolean) + */ + public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept); } /** - * An OnScrollListener can be set on a RecyclerView to receive messages - * when a scrolling event has occurred on that RecyclerView. - * - * @see RecyclerView#setOnScrollListener(OnScrollListener) + * An implementation of {@link RecyclerView.OnItemTouchListener} that has empty method bodies and + * default return values. + *

      + * You may prefer to extend this class if you don't need to override all methods. Another + * benefit of using this class is future compatibility. As the interface may change, we'll + * always provide a default implementation on this class so that your code won't break when + * you update to a new version of the support library. */ - abstract static public class OnScrollListener { + public static class SimpleOnItemTouchListener implements RecyclerView.OnItemTouchListener { + @Override + public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { + return false; + } + + @Override + public void onTouchEvent(RecyclerView rv, MotionEvent e) { + } + + @Override + public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { + } + } + + + /** + * An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event + * has occurred on that RecyclerView. + *

      + * @see RecyclerView#addOnScrollListener(OnScrollListener) + * @see RecyclerView#clearOnChildAttachStateChangeListeners() + * + */ + public abstract static class OnScrollListener { /** * Callback method to be invoked when RecyclerView's scroll state changes. * @@ -6564,6 +9235,9 @@ public class RecyclerView extends ViewGroup { /** * Callback method to be invoked when the RecyclerView has been scrolled. This will be * called after the scroll has completed. + *

      + * This callback will also be called if visible item range changes after a layout + * calculation. In that case, dx and dy will be 0. * * @param recyclerView The RecyclerView which scrolled. * @param dx The amount of horizontal scroll. @@ -6583,11 +9257,37 @@ public class RecyclerView extends ViewGroup { /** * This method is called whenever the view in the ViewHolder is recycled. * + * RecyclerView calls this method right before clearing ViewHolder's internal data and + * sending it to RecycledViewPool. This way, if ViewHolder was holding valid information + * before being recycled, you can call {@link ViewHolder#getAdapterPosition()} to get + * its adapter position. + * * @param holder The ViewHolder containing the view that was recycled */ public void onViewRecycled(ViewHolder holder); } + /** + * A Listener interface that can be attached to a RecylcerView to get notified + * whenever a ViewHolder is attached to or detached from RecyclerView. + */ + public interface OnChildAttachStateChangeListener { + + /** + * Called when a view is attached to the RecyclerView. + * + * @param view The View which is attached to the RecyclerView + */ + public void onChildViewAttachedToWindow(View view); + + /** + * Called when a view is detached from RecyclerView. + * + * @param view The View which is being detached from the RecyclerView + */ + public void onChildViewDetachedFromWindow(View view); + } + /** * A ViewHolder describes an item view and metadata about its place within the RecyclerView. * @@ -6653,12 +9353,6 @@ public class RecyclerView extends ViewGroup { */ static final int FLAG_RETURNED_FROM_SCRAP = 1 << 5; - /** - * This ViewHolder's contents have changed. This flag is used as an indication that - * change animations may be used, if supported by the ItemAnimator. - */ - static final int FLAG_CHANGED = 1 << 6; - /** * This ViewHolder is fully managed by the LayoutManager. We do not scrap, recycle or remove * it unless LayoutManager is replaced. @@ -6666,13 +9360,76 @@ public class RecyclerView extends ViewGroup { */ static final int FLAG_IGNORE = 1 << 7; + /** + * When the View is detached form the parent, we set this flag so that we can take correct + * action when we need to remove it or add it back. + */ + static final int FLAG_TMP_DETACHED = 1 << 8; + + /** + * Set when we can no longer determine the adapter position of this ViewHolder until it is + * rebound to a new position. It is different than FLAG_INVALID because FLAG_INVALID is + * set even when the type does not match. Also, FLAG_ADAPTER_POSITION_UNKNOWN is set as soon + * as adapter notification arrives vs FLAG_INVALID is set lazily before layout is + * re-calculated. + */ + static final int FLAG_ADAPTER_POSITION_UNKNOWN = 1 << 9; + + /** + * Set when a addChangePayload(null) is called + */ + static final int FLAG_ADAPTER_FULLUPDATE = 1 << 10; + + /** + * Used by ItemAnimator when a ViewHolder's position changes + */ + static final int FLAG_MOVED = 1 << 11; + + /** + * Used by ItemAnimator when a ViewHolder appears in pre-layout + */ + static final int FLAG_APPEARED_IN_PRE_LAYOUT = 1 << 12; + + /** + * Used when a ViewHolder starts the layout pass as a hidden ViewHolder but is re-used from + * hidden list (as if it was scrap) without being recycled in between. + * + * When a ViewHolder is hidden, there are 2 paths it can be re-used: + * a) Animation ends, view is recycled and used from the recycle pool. + * b) LayoutManager asks for the View for that position while the ViewHolder is hidden. + * + * This flag is used to represent "case b" where the ViewHolder is reused without being + * recycled (thus "bounced" from the hidden list). This state requires special handling + * because the ViewHolder must be added to pre layout maps for animations as if it was + * already there. + */ + static final int FLAG_BOUNCED_FROM_HIDDEN_LIST = 1 << 13; + private int mFlags; + private static final List FULLUPDATE_PAYLOADS = Collections.EMPTY_LIST; + + List mPayloads = null; + List mUnmodifiedPayloads = null; + private int mIsRecyclableCount = 0; // If non-null, view is currently considered scrap and may be reused for other data by the // scrap container. private Recycler mScrapContainer = null; + // Keeps whether this ViewHolder lives in Change scrap or Attached scrap + private boolean mInChangeScrap = false; + + // Saves isImportantForAccessibility value for the view item while it's in hidden state and + // marked as unimportant for accessibility. + private int mWasImportantForAccessibilityBeforeHidden = + ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO; + + /** + * Is set when VH is bound from the adapter and cleaned right before it is sent to + * {@link RecycledViewPool}. + */ + RecyclerView mOwnerRecyclerView; public ViewHolder(View itemView) { if (itemView == null) { @@ -6718,10 +9475,74 @@ public class RecyclerView extends ViewGroup { return (mFlags & FLAG_IGNORE) != 0; } + /** + * @deprecated This method is deprecated because its meaning is ambiguous due to the async + * handling of adapter updates. Please use {@link #getLayoutPosition()} or + * {@link #getAdapterPosition()} depending on your use case. + * + * @see #getLayoutPosition() + * @see #getAdapterPosition() + */ + @Deprecated public final int getPosition() { return mPreLayoutPosition == NO_POSITION ? mPosition : mPreLayoutPosition; } + /** + * Returns the position of the ViewHolder in terms of the latest layout pass. + *

      + * This position is mostly used by RecyclerView components to be consistent while + * RecyclerView lazily processes adapter updates. + *

      + * For performance and animation reasons, RecyclerView batches all adapter updates until the + * next layout pass. This may cause mismatches between the Adapter position of the item and + * the position it had in the latest layout calculations. + *

      + * LayoutManagers should always call this method while doing calculations based on item + * positions. All methods in {@link RecyclerView.LayoutManager}, {@link RecyclerView.State}, + * {@link RecyclerView.Recycler} that receive a position expect it to be the layout position + * of the item. + *

      + * If LayoutManager needs to call an external method that requires the adapter position of + * the item, it can use {@link #getAdapterPosition()} or + * {@link RecyclerView.Recycler#convertPreLayoutPositionToPostLayout(int)}. + * + * @return Returns the adapter position of the ViewHolder in the latest layout pass. + * @see #getAdapterPosition() + */ + public final int getLayoutPosition() { + return mPreLayoutPosition == NO_POSITION ? mPosition : mPreLayoutPosition; + } + + /** + * Returns the Adapter position of the item represented by this ViewHolder. + *

      + * Note that this might be different than the {@link #getLayoutPosition()} if there are + * pending adapter updates but a new layout pass has not happened yet. + *

      + * RecyclerView does not handle any adapter updates until the next layout traversal. This + * may create temporary inconsistencies between what user sees on the screen and what + * adapter contents have. This inconsistency is not important since it will be less than + * 16ms but it might be a problem if you want to use ViewHolder position to access the + * adapter. Sometimes, you may need to get the exact adapter position to do + * some actions in response to user events. In that case, you should use this method which + * will calculate the Adapter position of the ViewHolder. + *

      + * Note that if you've called {@link RecyclerView.Adapter#notifyDataSetChanged()}, until the + * next layout pass, the return value of this method will be {@link #NO_POSITION}. + * + * @return The adapter position of the item if it still exists in the adapter. + * {@link RecyclerView#NO_POSITION} if item has been removed from the adapter, + * {@link RecyclerView.Adapter#notifyDataSetChanged()} has been called after the last + * layout pass or the ViewHolder has already been recycled. + */ + public final int getAdapterPosition() { + if (mOwnerRecyclerView == null) { + return NO_POSITION; + } + return mOwnerRecyclerView.getAdapterPositionFor(this); + } + /** * When LayoutManager supports animations, RecyclerView tracks 3 positions for ViewHolders * to perform animations. @@ -6740,7 +9561,7 @@ public class RecyclerView extends ViewGroup { /** * Returns The itemId represented by this ViewHolder. * - * @return The the item's id if adapter has stable ids, {@link RecyclerView#NO_ID} + * @return The item's id if adapter has stable ids, {@link RecyclerView#NO_ID} * otherwise */ public final long getItemId() { @@ -6770,12 +9591,17 @@ public class RecyclerView extends ViewGroup { mFlags = mFlags & ~FLAG_RETURNED_FROM_SCRAP; } + void clearTmpDetachFlag() { + mFlags = mFlags & ~FLAG_TMP_DETACHED; + } + void stopIgnoring() { mFlags = mFlags & ~FLAG_IGNORE; } - void setScrapContainer(Recycler recycler) { + void setScrapContainer(Recycler recycler, boolean isChangeScrap) { mScrapContainer = recycler; + mInChangeScrap = isChangeScrap; } boolean isInvalid() { @@ -6786,10 +9612,6 @@ public class RecyclerView extends ViewGroup { return (mFlags & FLAG_UPDATE) != 0; } - boolean isChanged() { - return (mFlags & FLAG_CHANGED) != 0; - } - boolean isBound() { return (mFlags & FLAG_BOUND) != 0; } @@ -6798,6 +9620,18 @@ public class RecyclerView extends ViewGroup { return (mFlags & FLAG_REMOVED) != 0; } + boolean hasAnyOfTheFlags(int flags) { + return (mFlags & flags) != 0; + } + + boolean isTmpDetached() { + return (mFlags & FLAG_TMP_DETACHED) != 0; + } + + boolean isAdapterPositionUnknown() { + return (mFlags & FLAG_ADAPTER_POSITION_UNKNOWN) != 0 || isInvalid(); + } + void setFlags(int flags, int mask) { mFlags = (mFlags & ~mask) | (flags & mask); } @@ -6806,6 +9640,43 @@ public class RecyclerView extends ViewGroup { mFlags |= flags; } + void addChangePayload(Object payload) { + if (payload == null) { + addFlags(FLAG_ADAPTER_FULLUPDATE); + } else if ((mFlags & FLAG_ADAPTER_FULLUPDATE) == 0) { + createPayloadsIfNeeded(); + mPayloads.add(payload); + } + } + + private void createPayloadsIfNeeded() { + if (mPayloads == null) { + mPayloads = new ArrayList(); + mUnmodifiedPayloads = Collections.unmodifiableList(mPayloads); + } + } + + void clearPayload() { + if (mPayloads != null) { + mPayloads.clear(); + } + mFlags = mFlags & ~FLAG_ADAPTER_FULLUPDATE; + } + + List getUnmodifiedPayloads() { + if ((mFlags & FLAG_ADAPTER_FULLUPDATE) == 0) { + if (mPayloads == null || mPayloads.size() == 0) { + // Initial state, no update being called. + return FULLUPDATE_PAYLOADS; + } + // there are none-null payloads + return mUnmodifiedPayloads; + } else { + // a full update has been called. + return FULLUPDATE_PAYLOADS; + } + } + void resetInternal() { mFlags = 0; mPosition = NO_POSITION; @@ -6815,6 +9686,28 @@ public class RecyclerView extends ViewGroup { mIsRecyclableCount = 0; mShadowedHolder = null; mShadowingHolder = null; + clearPayload(); + mWasImportantForAccessibilityBeforeHidden = ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO; + } + + /** + * Called when the child view enters the hidden state + */ + private void onEnteredHiddenState() { + // While the view item is in hidden state, make it invisible for the accessibility. + mWasImportantForAccessibilityBeforeHidden = + ViewCompat.getImportantForAccessibility(itemView); + ViewCompat.setImportantForAccessibility(itemView, + ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); + } + + /** + * Called when the child view leaves the hidden state + */ + private void onLeftHiddenState() { + ViewCompat.setImportantForAccessibility( + itemView, mWasImportantForAccessibilityBeforeHidden); + mWasImportantForAccessibilityBeforeHidden = ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO; } @Override @@ -6822,14 +9715,19 @@ public class RecyclerView extends ViewGroup { final StringBuilder sb = new StringBuilder("ViewHolder{" + Integer.toHexString(hashCode()) + " position=" + mPosition + " id=" + mItemId + ", oldPos=" + mOldPosition + ", pLpos:" + mPreLayoutPosition); - if (isScrap()) sb.append(" scrap"); + if (isScrap()) { + sb.append(" scrap ") + .append(mInChangeScrap ? "[changeScrap]" : "[attachedScrap]"); + } if (isInvalid()) sb.append(" invalid"); if (!isBound()) sb.append(" unbound"); if (needsUpdate()) sb.append(" update"); if (isRemoved()) sb.append(" removed"); if (shouldIgnore()) sb.append(" ignored"); - if (isChanged()) sb.append(" changed"); + if (isTmpDetached()) sb.append(" tmpDetached"); if (!isRecyclable()) sb.append(" not recyclable(" + mIsRecyclableCount + ")"); + if (isAdapterPositionUnknown()) sb.append(" undefined adapter position"); + if (itemView.getParent() == null) sb.append(" no parent"); sb.append("}"); return sb.toString(); @@ -6875,15 +9773,93 @@ public class RecyclerView extends ViewGroup { return (mFlags & FLAG_NOT_RECYCLABLE) == 0 && !ViewCompat.hasTransientState(itemView); } + + /** + * Returns whether we have animations referring to this view holder or not. + * This is similar to isRecyclable flag but does not check transient state. + */ + private boolean shouldBeKeptAsChild() { + return (mFlags & FLAG_NOT_RECYCLABLE) != 0; + } + + /** + * @return True if ViewHolder is not referenced by RecyclerView animations but has + * transient state which will prevent it from being recycled. + */ + private boolean doesTransientStatePreventRecycling() { + return (mFlags & FLAG_NOT_RECYCLABLE) == 0 && ViewCompat.hasTransientState(itemView); + } + + boolean isUpdated() { + return (mFlags & FLAG_UPDATE) != 0; + } + } + + private int getAdapterPositionFor(ViewHolder viewHolder) { + if (viewHolder.hasAnyOfTheFlags( ViewHolder.FLAG_INVALID | + ViewHolder.FLAG_REMOVED | ViewHolder.FLAG_ADAPTER_POSITION_UNKNOWN) + || !viewHolder.isBound()) { + return RecyclerView.NO_POSITION; + } + return mAdapterHelper.applyPendingUpdatesToPosition(viewHolder.mPosition); + } + + // NestedScrollingChild + + @Override + public void setNestedScrollingEnabled(boolean enabled) { + getScrollingChildHelper().setNestedScrollingEnabled(enabled); + } + + @Override + public boolean isNestedScrollingEnabled() { + return getScrollingChildHelper().isNestedScrollingEnabled(); + } + + @Override + public boolean startNestedScroll(int axes) { + return getScrollingChildHelper().startNestedScroll(axes); + } + + @Override + public void stopNestedScroll() { + getScrollingChildHelper().stopNestedScroll(); + } + + @Override + public boolean hasNestedScrollingParent() { + return getScrollingChildHelper().hasNestedScrollingParent(); + } + + @Override + public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, + int dyUnconsumed, int[] offsetInWindow) { + return getScrollingChildHelper().dispatchNestedScroll(dxConsumed, dyConsumed, + dxUnconsumed, dyUnconsumed, offsetInWindow); + } + + @Override + public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) { + return getScrollingChildHelper().dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow); + } + + @Override + public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) { + return getScrollingChildHelper().dispatchNestedFling(velocityX, velocityY, consumed); + } + + @Override + public boolean dispatchNestedPreFling(float velocityX, float velocityY) { + return getScrollingChildHelper().dispatchNestedPreFling(velocityX, velocityY); } /** - * {@link MarginLayoutParams LayoutParams} subclass for children of + * {@link android.view.ViewGroup.MarginLayoutParams LayoutParams} subclass for children of * {@link RecyclerView}. Custom {@link LayoutManager layout managers} are encouraged * to create their own subclass of this LayoutParams class * to store any additional required per-child view metadata about the layout. */ - public static class LayoutParams extends MarginLayoutParams { + public static class LayoutParams extends android.view.ViewGroup.MarginLayoutParams { ViewHolder mViewHolder; final Rect mDecorInsets = new Rect(); boolean mInsetsDirty = true; @@ -6951,17 +9927,38 @@ public class RecyclerView extends ViewGroup { * @return true if the item the view corresponds to was changed in the data set */ public boolean isItemChanged() { - return mViewHolder.isChanged(); + return mViewHolder.isUpdated(); } /** - * Returns the position that the view this LayoutParams is attached to corresponds to. - * - * @return the adapter position this view was bound from + * @deprecated use {@link #getViewLayoutPosition()} or {@link #getViewAdapterPosition()} */ + @Deprecated public int getViewPosition() { return mViewHolder.getPosition(); } + + /** + * Returns the adapter position that the view this LayoutParams is attached to corresponds + * to as of latest layout calculation. + * + * @return the adapter position this view as of latest layout pass + */ + public int getViewLayoutPosition() { + return mViewHolder.getLayoutPosition(); + } + + /** + * Returns the up-to-date adapter position that the view this LayoutParams is attached to + * corresponds to. + * + * @return the up-to-date adapter position this view. It may return + * {@link RecyclerView#NO_POSITION} if item represented by this View has been removed or + * its up-to-date position cannot be calculated. + */ + public int getViewAdapterPosition() { + return mViewHolder.getAdapterPosition(); + } } /** @@ -6977,6 +9974,12 @@ public class RecyclerView extends ViewGroup { // do nothing } + public void onItemRangeChanged(int positionStart, int itemCount, Object payload) { + // fallback to onItemRangeChanged(positionStart, itemCount) if app + // does not override this method. + onItemRangeChanged(positionStart, itemCount); + } + public void onItemRangeInserted(int positionStart, int itemCount) { // do nothing } @@ -7020,8 +10023,8 @@ public class RecyclerView extends ViewGroup { * Starts a smooth scroll for the given target position. *

      In each animation step, {@link RecyclerView} will check * for the target view and call either - * {@link #onTargetFound(View, State, Action)} or - * {@link #onSeekTargetStep(int, int, State, Action)} until + * {@link #onTargetFound(android.view.View, RecyclerView.State, SmoothScroller.Action)} or + * {@link #onSeekTargetStep(int, int, RecyclerView.State, SmoothScroller.Action)} until * SmoothScroller is stopped.

      * *

      Note that if RecyclerView finds the target view, it will automatically stop the @@ -7047,8 +10050,10 @@ public class RecyclerView extends ViewGroup { } /** - * @return The LayoutManager to which this SmoothScroller is attached + * @return The LayoutManager to which this SmoothScroller is attached. Will return + * null after the SmoothScroller is stopped. */ + @Nullable public LayoutManager getLayoutManager() { return mLayoutManager; } @@ -7056,8 +10061,8 @@ public class RecyclerView extends ViewGroup { /** * Stops running the SmoothScroller in each animation callback. Note that this does not * cancel any existing {@link Action} updated by - * {@link #onTargetFound(View, State, Action)} or - * {@link #onSeekTargetStep(int, int, State, Action)}. + * {@link #onTargetFound(android.view.View, RecyclerView.State, SmoothScroller.Action)} or + * {@link #onSeekTargetStep(int, int, RecyclerView.State, SmoothScroller.Action)}. */ final protected void stop() { if (!mRunning) { @@ -7106,15 +10111,16 @@ public class RecyclerView extends ViewGroup { } private void onAnimation(int dx, int dy) { - if (!mRunning || mTargetPosition == RecyclerView.NO_POSITION) { + final RecyclerView recyclerView = mRecyclerView; + if (!mRunning || mTargetPosition == RecyclerView.NO_POSITION || recyclerView == null) { stop(); } mPendingInitialRun = false; if (mTargetView != null) { // verify target position if (getChildPosition(mTargetView) == mTargetPosition) { - onTargetFound(mTargetView, mRecyclerView.mState, mRecyclingAction); - mRecyclingAction.runIfNecessary(mRecyclerView); + onTargetFound(mTargetView, recyclerView.mState, mRecyclingAction); + mRecyclingAction.runIfNecessary(recyclerView); stop(); } else { Log.e(TAG, "Passed over target position while smooth scrolling."); @@ -7122,27 +10128,37 @@ public class RecyclerView extends ViewGroup { } } if (mRunning) { - onSeekTargetStep(dx, dy, mRecyclerView.mState, mRecyclingAction); - mRecyclingAction.runIfNecessary(mRecyclerView); + onSeekTargetStep(dx, dy, recyclerView.mState, mRecyclingAction); + boolean hadJumpTarget = mRecyclingAction.hasJumpTarget(); + mRecyclingAction.runIfNecessary(recyclerView); + if (hadJumpTarget) { + // It is not stopped so needs to be restarted + if (mRunning) { + mPendingInitialRun = true; + recyclerView.mViewFlinger.postOnAnimation(); + } else { + stop(); // done + } + } } } /** - * @see RecyclerView#getChildPosition(View) + * @see RecyclerView#getChildLayoutPosition(android.view.View) */ public int getChildPosition(View view) { - return mRecyclerView.getChildPosition(view); + return mRecyclerView.getChildLayoutPosition(view); } /** - * @see LayoutManager#getChildCount() + * @see RecyclerView.LayoutManager#getChildCount() */ public int getChildCount() { return mRecyclerView.mLayout.getChildCount(); } /** - * @see LayoutManager#findViewByPosition(int) + * @see RecyclerView.LayoutManager#findViewByPosition(int) */ public View findViewByPosition(int position) { return mRecyclerView.mLayout.findViewByPosition(position); @@ -7150,7 +10166,9 @@ public class RecyclerView extends ViewGroup { /** * @see RecyclerView#scrollToPosition(int) + * @deprecated Use {@link Action#jumpTo(int)}. */ + @Deprecated public void instantScrollToPosition(int position) { mRecyclerView.scrollToPosition(position); } @@ -7169,10 +10187,10 @@ public class RecyclerView extends ViewGroup { * @param scrollVector The vector that points to the target scroll position */ protected void normalize(PointF scrollVector) { - final double magnitute = Math.sqrt(scrollVector.x * scrollVector.x + scrollVector.y * - scrollVector.y); - scrollVector.x /= magnitute; - scrollVector.y /= magnitute; + final double magnitude = Math.sqrt(scrollVector.x * scrollVector.x + scrollVector.y + * scrollVector.y); + scrollVector.x /= magnitude; + scrollVector.y /= magnitude; } /** @@ -7193,7 +10211,7 @@ public class RecyclerView extends ViewGroup { * provided {@link Action} to define the next scroll.

      * * @param dx Last scroll amount horizontally - * @param dy Last scroll amount verticaully + * @param dy Last scroll amount vertically * @param state Transient state of RecyclerView * @param action If you want to trigger a new smooth scroll and cancel the previous one, * update this object. @@ -7208,7 +10226,6 @@ public class RecyclerView extends ViewGroup { * @param state Transient state of RecyclerView * @param action Action instance that you should update to define final scroll action * towards the targetView - * @return An {@link Action} to finalize the smooth scrolling */ abstract protected void onTargetFound(View targetView, State state, Action action); @@ -7225,6 +10242,8 @@ public class RecyclerView extends ViewGroup { private int mDuration; + private int mJumpToPosition = NO_POSITION; + private Interpolator mInterpolator; private boolean changed = false; @@ -7263,7 +10282,38 @@ public class RecyclerView extends ViewGroup { mDuration = duration; mInterpolator = interpolator; } + + /** + * Instead of specifying pixels to scroll, use the target position to jump using + * {@link RecyclerView#scrollToPosition(int)}. + *

      + * You may prefer using this method if scroll target is really far away and you prefer + * to jump to a location and smooth scroll afterwards. + *

      + * Note that calling this method takes priority over other update methods such as + * {@link #update(int, int, int, Interpolator)}, {@link #setX(float)}, + * {@link #setY(float)} and #{@link #setInterpolator(Interpolator)}. If you call + * {@link #jumpTo(int)}, the other changes will not be considered for this animation + * frame. + * + * @param targetPosition The target item position to scroll to using instant scrolling. + */ + public void jumpTo(int targetPosition) { + mJumpToPosition = targetPosition; + } + + boolean hasJumpTarget() { + return mJumpToPosition >= 0; + } + private void runIfNecessary(RecyclerView recyclerView) { + if (mJumpToPosition >= 0) { + final int position = mJumpToPosition; + mJumpToPosition = NO_POSITION; + recyclerView.jumpToPositionForSmoothScroller(position); + changed = false; + return; + } if (changed) { validate(); if (mInterpolator == null) { @@ -7355,6 +10405,30 @@ public class RecyclerView extends ViewGroup { changed = true; } } + + /** + * An interface which is optionally implemented by custom {@link RecyclerView.LayoutManager} + * to provide a hint to a {@link SmoothScroller} about the location of the target position. + */ + public interface ScrollVectorProvider { + /** + * Should calculate the vector that points to the direction where the target position + * can be found. + *

      + * This method is used by the {@link LinearSmoothScroller} to initiate a scroll towards + * the target position. + *

      + * The magnitude of the vector is not important. It is always normalized before being + * used by the {@link LinearSmoothScroller}. + *

      + * LayoutManager should not check whether the position exists in the adapter or not. + * + * @param targetPosition the target position to which the returned vector should point + * + * @return the scroll vector for a given position. + */ + PointF computeScrollVectorForPosition(int targetPosition); + } } static class AdapterDataObservable extends Observable { @@ -7373,12 +10447,16 @@ public class RecyclerView extends ViewGroup { } public void notifyItemRangeChanged(int positionStart, int itemCount) { + notifyItemRangeChanged(positionStart, itemCount, null); + } + + public void notifyItemRangeChanged(int positionStart, int itemCount, Object payload) { // since onItemRangeChanged() is implemented by the app, it could do anything, including // removing itself from {@link mObservers} - and that could cause problems if // an iterator is used on the ArrayList {@link mObservers}. // to avoid such problems, just march thru the list in the reverse order. for (int i = mObservers.size() - 1; i >= 0; i--) { - mObservers.get(i).onItemRangeChanged(positionStart, itemCount); + mObservers.get(i).onItemRangeChanged(positionStart, itemCount, payload); } } @@ -7409,16 +10487,21 @@ public class RecyclerView extends ViewGroup { } } - static class SavedState extends BaseSavedState { + /** + * This is public so that the CREATOR can be access on cold launch. + * @hide + */ + public static class SavedState extends AbsSavedState { Parcelable mLayoutState; /** * called by CREATOR */ - SavedState(Parcel in) { - super(in); - mLayoutState = in.readParcelable(LayoutManager.class.getClassLoader()); + SavedState(Parcel in, ClassLoader loader) { + super(in, loader); + mLayoutState = in.readParcelable( + loader != null ? loader : LayoutManager.class.getClassLoader()); } /** @@ -7438,18 +10521,18 @@ public class RecyclerView extends ViewGroup { mLayoutState = other.mLayoutState; } - public static final Creator CREATOR - = new Creator() { - @Override - public SavedState createFromParcel(Parcel in) { - return new SavedState(in); - } + public static final Creator CREATOR = ParcelableCompat.newCreator( + new ParcelableCompatCreatorCallbacks() { + @Override + public SavedState createFromParcel(Parcel in, ClassLoader loader) { + return new SavedState(in, loader); + } - @Override - public SavedState[] newArray(int size) { - return new SavedState[size]; - } - }; + @Override + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }); } /** *

      Contains useful information about the current RecyclerView state like target scroll @@ -7462,14 +10545,28 @@ public class RecyclerView extends ViewGroup { * data between your components without needing to manage their lifecycles.

      */ public static class State { + static final int STEP_START = 1; + static final int STEP_LAYOUT = 1 << 1; + static final int STEP_ANIMATIONS = 1 << 2; + + void assertLayoutStep(int accepted) { + if ((accepted & mLayoutStep) == 0) { + throw new IllegalStateException("Layout state should be one of " + + Integer.toBinaryString(accepted) + " but it is " + + Integer.toBinaryString(mLayoutStep)); + } + } + + @IntDef(flag = true, value = { + STEP_START, STEP_LAYOUT, STEP_ANIMATIONS + }) + @Retention(RetentionPolicy.SOURCE) + @interface LayoutState {} private int mTargetPosition = RecyclerView.NO_POSITION; - ArrayMap mPreLayoutHolderMap = - new ArrayMap(); - ArrayMap mPostLayoutHolderMap = - new ArrayMap(); - // nullable - ArrayMap mOldChangedHolders = new ArrayMap(); + + @LayoutState + private int mLayoutStep = STEP_START; private SparseArray mData; @@ -7497,6 +10594,21 @@ public class RecyclerView extends ViewGroup { private boolean mRunPredictiveAnimations = false; + private boolean mTrackOldChangeHolders = false; + + private boolean mIsMeasuring = false; + + /** + * This data is saved before a layout calculation happens. After the layout is finished, + * if the previously focused view has been replaced with another view for the same item, we + * move the focus to the new item automatically. + */ + int mFocusedItemPosition; + long mFocusedItemId; + // when a sub child has focus, record its id and see if we can directly request focus on + // that one instead + int mFocusedSubChildId; + State reset() { mTargetPosition = RecyclerView.NO_POSITION; if (mData != null) { @@ -7504,9 +10616,32 @@ public class RecyclerView extends ViewGroup { } mItemCount = 0; mStructureChanged = false; + mIsMeasuring = false; return this; } + /** + * Returns true if the RecyclerView is currently measuring the layout. This value is + * {@code true} only if the LayoutManager opted into the auto measure API and RecyclerView + * has non-exact measurement specs. + *

      + * Note that if the LayoutManager supports predictive animations and it is calculating the + * pre-layout step, this value will be {@code false} even if the RecyclerView is in + * {@code onMeasure} call. This is because pre-layout means the previous state of the + * RecyclerView and measurements made for that state cannot change the RecyclerView's size. + * LayoutManager is always guaranteed to receive another call to + * {@link LayoutManager#onLayoutChildren(Recycler, State)} when this happens. + * + * @return True if the RecyclerView is currently calculating its bounds, false otherwise. + */ + public boolean isMeasuring() { + return mIsMeasuring; + } + + /** + * Returns true if + * @return + */ public boolean isPreLayout() { return mInPreLayout; } @@ -7633,34 +10768,10 @@ public class RecyclerView extends ViewGroup { mItemCount; } - public void onViewRecycled(ViewHolder holder) { - mPreLayoutHolderMap.remove(holder); - mPostLayoutHolderMap.remove(holder); - if (mOldChangedHolders != null) { - removeFrom(mOldChangedHolders, holder); - } - // holder cannot be in new list. - } - - public void onViewIgnored(ViewHolder holder) { - onViewRecycled(holder); - } - - private void removeFrom(ArrayMap holderMap, ViewHolder holder) { - for (int i = holderMap.size() - 1; i >= 0; i --) { - if (holder == holderMap.valueAt(i)) { - holderMap.removeAt(i); - return; - } - } - } - @Override public String toString() { return "State{" + "mTargetPosition=" + mTargetPosition + - ", mPreLayoutHolderMap=" + mPreLayoutHolderMap + - ", mPostLayoutHolderMap=" + mPostLayoutHolderMap + ", mData=" + mData + ", mItemCount=" + mItemCount + ", mPreviousLayoutItemCount=" + mPreviousLayoutItemCount + @@ -7674,6 +10785,28 @@ public class RecyclerView extends ViewGroup { } } + /** + * This class defines the behavior of fling if the developer wishes to handle it. + *

      + * Subclasses of {@link OnFlingListener} can be used to implement custom fling behavior. + * + * @see #setOnFlingListener(OnFlingListener) + */ + public static abstract class OnFlingListener { + + /** + * Override this to handle a fling given the velocities in both x and y directions. + * Note that this method will only be called if the associated {@link LayoutManager} + * supports scrolling and the fling is not handled by nested scrolls first. + * + * @param velocityX the fling velocity on the X axis + * @param velocityY the fling velocity on the Y axis + * + * @return true if the fling washandled, false otherwise. + */ + public abstract boolean onFling(int velocityX, int velocityY); + } + /** * Internal listener that manages items after animations finish. This is how items are * retained (not recycled) during animations, but allowed to be recycled afterwards. @@ -7683,70 +10816,21 @@ public class RecyclerView extends ViewGroup { private class ItemAnimatorRestoreListener implements ItemAnimator.ItemAnimatorListener { @Override - public void onRemoveFinished(ViewHolder item) { + public void onAnimationFinished(ViewHolder item) { item.setIsRecyclable(true); - removeAnimatingView(item.itemView); - removeDetachedView(item.itemView, false); - } - - @Override - public void onAddFinished(ViewHolder item) { - item.setIsRecyclable(true); - if (item.isRecyclable()) { - removeAnimatingView(item.itemView); - } - } - - @Override - public void onMoveFinished(ViewHolder item) { - item.setIsRecyclable(true); - if (item.isRecyclable()) { - removeAnimatingView(item.itemView); - } - } - - @Override - public void onChangeFinished(ViewHolder item) { - item.setIsRecyclable(true); - /** - * We check both shadowed and shadowing because a ViewHolder may get both roles at the - * same time. - * - * Assume this flow: - * item X is represented by VH_1. Then itemX changes, so we create VH_2 . - * RV sets the following and calls item animator: - * VH_1.shadowed = VH_2; - * VH_1.mChanged = true; - * VH_2.shadowing =VH_1; - * - * Then, before the first change finishes, item changes again so we create VH_3. - * RV sets the following and calls item animator: - * VH_2.shadowed = VH_3 - * VH_2.mChanged = true - * VH_3.shadowing = VH_2 - * - * Because VH_2 already has an animation, it will be cancelled. At this point VH_2 has - * both shadowing and shadowed fields set. Shadowing information is obsolete now - * because the first animation where VH_2 is newViewHolder is not valid anymore. - * We ended up in this case because VH_2 played both roles. On the other hand, - * we DO NOT want to clear its changed flag. - * - * If second change was simply reverting first change, we would find VH_1 in - * {@link Recycler#getScrapViewForPosition(int, int, boolean)} and recycle it before - * re-using - */ if (item.mShadowedHolder != null && item.mShadowingHolder == null) { // old vh item.mShadowedHolder = null; - item.setFlags(~ViewHolder.FLAG_CHANGED, item.mFlags); } // always null this because an OldViewHolder can never become NewViewHolder w/o being // recycled. item.mShadowingHolder = null; - if (item.isRecyclable()) { - removeAnimatingView(item.itemView); + if (!item.shouldBeKeptAsChild()) { + if (!removeAnimatingView(item.itemView) && item.isTmpDetached()) { + removeDetachedView(item.itemView, false); + } } } - }; + } /** * This class defines the animations that take place on items as changes are made @@ -7754,22 +10838,78 @@ public class RecyclerView extends ViewGroup { * * Subclasses of ItemAnimator can be used to implement custom animations for actions on * ViewHolder items. The RecyclerView will manage retaining these items while they - * are being animated, but implementors must call the appropriate "Starting" - * ({@link #dispatchRemoveStarting(ViewHolder)}, {@link #dispatchMoveStarting(ViewHolder)}, - * {@link #dispatchChangeStarting(ViewHolder, boolean)}, or - * {@link #dispatchAddStarting(ViewHolder)}) - * and "Finished" ({@link #dispatchRemoveFinished(ViewHolder)}, - * {@link #dispatchMoveFinished(ViewHolder)}, - * {@link #dispatchChangeFinished(ViewHolder, boolean)}, - * or {@link #dispatchAddFinished(ViewHolder)}) methods when each item animation is - * being started and ended. + * are being animated, but implementors must call {@link #dispatchAnimationFinished(ViewHolder)} + * when a ViewHolder's animation is finished. In other words, there must be a matching + * {@link #dispatchAnimationFinished(ViewHolder)} call for each + * {@link #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) animateAppearance()}, + * {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateChange()} + * {@link #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) animatePersistence()}, + * and + * {@link #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateDisappearance()} call. * - *

      By default, RecyclerView uses {@link DefaultItemAnimator}

      + *

      By default, RecyclerView uses {@link DefaultItemAnimator}.

      * * @see #setItemAnimator(ItemAnimator) */ + @SuppressWarnings("UnusedParameters") public static abstract class ItemAnimator { + /** + * The Item represented by this ViewHolder is updated. + *

      + * @see #recordPreLayoutInformation(State, ViewHolder, int, List) + */ + public static final int FLAG_CHANGED = ViewHolder.FLAG_UPDATE; + + /** + * The Item represented by this ViewHolder is removed from the adapter. + *

      + * @see #recordPreLayoutInformation(State, ViewHolder, int, List) + */ + public static final int FLAG_REMOVED = ViewHolder.FLAG_REMOVED; + + /** + * Adapter {@link Adapter#notifyDataSetChanged()} has been called and the content + * represented by this ViewHolder is invalid. + *

      + * @see #recordPreLayoutInformation(State, ViewHolder, int, List) + */ + public static final int FLAG_INVALIDATED = ViewHolder.FLAG_INVALID; + + /** + * The position of the Item represented by this ViewHolder has been changed. This flag is + * not bound to {@link Adapter#notifyItemMoved(int, int)}. It might be set in response to + * any adapter change that may have a side effect on this item. (e.g. The item before this + * one has been removed from the Adapter). + *

      + * @see #recordPreLayoutInformation(State, ViewHolder, int, List) + */ + public static final int FLAG_MOVED = ViewHolder.FLAG_MOVED; + + /** + * This ViewHolder was not laid out but has been added to the layout in pre-layout state + * by the {@link LayoutManager}. This means that the item was already in the Adapter but + * invisible and it may become visible in the post layout phase. LayoutManagers may prefer + * to add new items in pre-layout to specify their virtual location when they are invisible + * (e.g. to specify the item should animate in from below the visible area). + *

      + * @see #recordPreLayoutInformation(State, ViewHolder, int, List) + */ + public static final int FLAG_APPEARED_IN_PRE_LAYOUT + = ViewHolder.FLAG_APPEARED_IN_PRE_LAYOUT; + + /** + * The set of flags that might be passed to + * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. + */ + @IntDef(flag=true, value={ + FLAG_CHANGED, FLAG_REMOVED, FLAG_MOVED, FLAG_INVALIDATED, + FLAG_APPEARED_IN_PRE_LAYOUT + }) + @Retention(RetentionPolicy.SOURCE) + public @interface AdapterChanges {} private ItemAnimatorListener mListener = null; private ArrayList mFinishedListeners = new ArrayList(); @@ -7779,8 +10919,6 @@ public class RecyclerView extends ViewGroup { private long mMoveDuration = 250; private long mChangeDuration = 250; - private boolean mSupportsChangeAnimations = false; - /** * Gets the current duration for which all move animations will run. * @@ -7853,36 +10991,6 @@ public class RecyclerView extends ViewGroup { mChangeDuration = changeDuration; } - /** - * Returns whether this ItemAnimator supports animations of change events. - * - * @return true if change animations are supported, false otherwise - */ - public boolean getSupportsChangeAnimations() { - return mSupportsChangeAnimations; - } - - /** - * Sets whether this ItemAnimator supports animations of item change events. - * By default, ItemAnimator only supports animations when items are added or removed. - * By setting this property to true, actions on the data set which change the - * contents of items may also be animated. What those animations are is left - * up to the discretion of the ItemAnimator subclass, in its - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} implementation. - * The value of this property is false by default. - * - * @see Adapter#notifyItemChanged(int) - * @see Adapter#notifyItemRangeChanged(int, int) - * - * @param supportsChangeAnimations true if change animations are supported by - * this ItemAnimator, false otherwise. If the property is false, the ItemAnimator - * will not receive a call to - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} when changes occur. - */ - public void setSupportsChangeAnimations(boolean supportsChangeAnimations) { - mSupportsChangeAnimations = supportsChangeAnimations; - } - /** * Internal only: * Sets the listener that must be called when the animator is finished @@ -7895,220 +11003,280 @@ public class RecyclerView extends ViewGroup { mListener = listener; } + /** + * Called by the RecyclerView before the layout begins. Item animator should record + * necessary information about the View before it is potentially rebound, moved or removed. + *

      + * The data returned from this method will be passed to the related animate** + * methods. + *

      + * Note that this method may be called after pre-layout phase if LayoutManager adds new + * Views to the layout in pre-layout pass. + *

      + * The default implementation returns an {@link ItemHolderInfo} which holds the bounds of + * the View and the adapter change flags. + * + * @param state The current State of RecyclerView which includes some useful data + * about the layout that will be calculated. + * @param viewHolder The ViewHolder whose information should be recorded. + * @param changeFlags Additional information about what changes happened in the Adapter + * about the Item represented by this ViewHolder. For instance, if + * item is deleted from the adapter, {@link #FLAG_REMOVED} will be set. + * @param payloads The payload list that was previously passed to + * {@link Adapter#notifyItemChanged(int, Object)} or + * {@link Adapter#notifyItemRangeChanged(int, int, Object)}. + * + * @return An ItemHolderInfo instance that preserves necessary information about the + * ViewHolder. This object will be passed back to related animate** methods + * after layout is complete. + * + * @see #recordPostLayoutInformation(State, ViewHolder) + * @see #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * @see #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * @see #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) + * @see #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) + */ + public @NonNull ItemHolderInfo recordPreLayoutInformation(@NonNull State state, + @NonNull ViewHolder viewHolder, @AdapterChanges int changeFlags, + @NonNull List payloads) { + return obtainHolderInfo().setFrom(viewHolder); + } + + /** + * Called by the RecyclerView after the layout is complete. Item animator should record + * necessary information about the View's final state. + *

      + * The data returned from this method will be passed to the related animate** + * methods. + *

      + * The default implementation returns an {@link ItemHolderInfo} which holds the bounds of + * the View. + * + * @param state The current State of RecyclerView which includes some useful data about + * the layout that will be calculated. + * @param viewHolder The ViewHolder whose information should be recorded. + * + * @return An ItemHolderInfo that preserves necessary information about the ViewHolder. + * This object will be passed back to related animate** methods when + * RecyclerView decides how items should be animated. + * + * @see #recordPreLayoutInformation(State, ViewHolder, int, List) + * @see #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * @see #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * @see #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) + * @see #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) + */ + public @NonNull ItemHolderInfo recordPostLayoutInformation(@NonNull State state, + @NonNull ViewHolder viewHolder) { + return obtainHolderInfo().setFrom(viewHolder); + } + + /** + * Called by the RecyclerView when a ViewHolder has disappeared from the layout. + *

      + * This means that the View was a child of the LayoutManager when layout started but has + * been removed by the LayoutManager. It might have been removed from the adapter or simply + * become invisible due to other factors. You can distinguish these two cases by checking + * the change flags that were passed to + * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. + *

      + * Note that when a ViewHolder both changes and disappears in the same layout pass, the + * animation callback method which will be called by the RecyclerView depends on the + * ItemAnimator's decision whether to re-use the same ViewHolder or not, and also the + * LayoutManager's decision whether to layout the changed version of a disappearing + * ViewHolder or not. RecyclerView will call + * {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateChange} instead of {@code animateDisappearance} if and only if the ItemAnimator + * returns {@code false} from + * {@link #canReuseUpdatedViewHolder(ViewHolder) canReuseUpdatedViewHolder} and the + * LayoutManager lays out a new disappearing view that holds the updated information. + * Built-in LayoutManagers try to avoid laying out updated versions of disappearing views. + *

      + * If LayoutManager supports predictive animations, it might provide a target disappear + * location for the View by laying it out in that location. When that happens, + * RecyclerView will call {@link #recordPostLayoutInformation(State, ViewHolder)} and the + * response of that call will be passed to this method as the postLayoutInfo. + *

      + * ItemAnimator must call {@link #dispatchAnimationFinished(ViewHolder)} when the animation + * is complete (or instantly call {@link #dispatchAnimationFinished(ViewHolder)} if it + * decides not to animate the view). + * + * @param viewHolder The ViewHolder which should be animated + * @param preLayoutInfo The information that was returned from + * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. + * @param postLayoutInfo The information that was returned from + * {@link #recordPostLayoutInformation(State, ViewHolder)}. Might be + * null if the LayoutManager did not layout the item. + * + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + public abstract boolean animateDisappearance(@NonNull ViewHolder viewHolder, + @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo); + + /** + * Called by the RecyclerView when a ViewHolder is added to the layout. + *

      + * In detail, this means that the ViewHolder was not a child when the layout started + * but has been added by the LayoutManager. It might be newly added to the adapter or + * simply become visible due to other factors. + *

      + * ItemAnimator must call {@link #dispatchAnimationFinished(ViewHolder)} when the animation + * is complete (or instantly call {@link #dispatchAnimationFinished(ViewHolder)} if it + * decides not to animate the view). + * + * @param viewHolder The ViewHolder which should be animated + * @param preLayoutInfo The information that was returned from + * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. + * Might be null if Item was just added to the adapter or + * LayoutManager does not support predictive animations or it could + * not predict that this ViewHolder will become visible. + * @param postLayoutInfo The information that was returned from {@link + * #recordPreLayoutInformation(State, ViewHolder, int, List)}. + * + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + public abstract boolean animateAppearance(@NonNull ViewHolder viewHolder, + @Nullable ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo); + + /** + * Called by the RecyclerView when a ViewHolder is present in both before and after the + * layout and RecyclerView has not received a {@link Adapter#notifyItemChanged(int)} call + * for it or a {@link Adapter#notifyDataSetChanged()} call. + *

      + * This ViewHolder still represents the same data that it was representing when the layout + * started but its position / size may be changed by the LayoutManager. + *

      + * If the Item's layout position didn't change, RecyclerView still calls this method because + * it does not track this information (or does not necessarily know that an animation is + * not required). Your ItemAnimator should handle this case and if there is nothing to + * animate, it should call {@link #dispatchAnimationFinished(ViewHolder)} and return + * false. + *

      + * ItemAnimator must call {@link #dispatchAnimationFinished(ViewHolder)} when the animation + * is complete (or instantly call {@link #dispatchAnimationFinished(ViewHolder)} if it + * decides not to animate the view). + * + * @param viewHolder The ViewHolder which should be animated + * @param preLayoutInfo The information that was returned from + * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. + * @param postLayoutInfo The information that was returned from {@link + * #recordPreLayoutInformation(State, ViewHolder, int, List)}. + * + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + public abstract boolean animatePersistence(@NonNull ViewHolder viewHolder, + @NonNull ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo); + + /** + * Called by the RecyclerView when an adapter item is present both before and after the + * layout and RecyclerView has received a {@link Adapter#notifyItemChanged(int)} call + * for it. This method may also be called when + * {@link Adapter#notifyDataSetChanged()} is called and adapter has stable ids so that + * RecyclerView could still rebind views to the same ViewHolders. If viewType changes when + * {@link Adapter#notifyDataSetChanged()} is called, this method will not be called, + * instead, {@link #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo)} will be + * called for the new ViewHolder and the old one will be recycled. + *

      + * If this method is called due to a {@link Adapter#notifyDataSetChanged()} call, there is + * a good possibility that item contents didn't really change but it is rebound from the + * adapter. {@link DefaultItemAnimator} will skip animating the View if its location on the + * screen didn't change and your animator should handle this case as well and avoid creating + * unnecessary animations. + *

      + * When an item is updated, ItemAnimator has a chance to ask RecyclerView to keep the + * previous presentation of the item as-is and supply a new ViewHolder for the updated + * presentation (see: {@link #canReuseUpdatedViewHolder(ViewHolder, List)}. + * This is useful if you don't know the contents of the Item and would like + * to cross-fade the old and the new one ({@link DefaultItemAnimator} uses this technique). + *

      + * When you are writing a custom item animator for your layout, it might be more performant + * and elegant to re-use the same ViewHolder and animate the content changes manually. + *

      + * When {@link Adapter#notifyItemChanged(int)} is called, the Item's view type may change. + * If the Item's view type has changed or ItemAnimator returned false for + * this ViewHolder when {@link #canReuseUpdatedViewHolder(ViewHolder, List)} was called, the + * oldHolder and newHolder will be different ViewHolder instances + * which represent the same Item. In that case, only the new ViewHolder is visible + * to the LayoutManager but RecyclerView keeps old ViewHolder attached for animations. + *

      + * ItemAnimator must call {@link #dispatchAnimationFinished(ViewHolder)} for each distinct + * ViewHolder when their animation is complete + * (or instantly call {@link #dispatchAnimationFinished(ViewHolder)} if it decides not to + * animate the view). + *

      + * If oldHolder and newHolder are the same instance, you should call + * {@link #dispatchAnimationFinished(ViewHolder)} only once. + *

      + * Note that when a ViewHolder both changes and disappears in the same layout pass, the + * animation callback method which will be called by the RecyclerView depends on the + * ItemAnimator's decision whether to re-use the same ViewHolder or not, and also the + * LayoutManager's decision whether to layout the changed version of a disappearing + * ViewHolder or not. RecyclerView will call + * {@code animateChange} instead of + * {@link #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateDisappearance} if and only if the ItemAnimator returns {@code false} from + * {@link #canReuseUpdatedViewHolder(ViewHolder) canReuseUpdatedViewHolder} and the + * LayoutManager lays out a new disappearing view that holds the updated information. + * Built-in LayoutManagers try to avoid laying out updated versions of disappearing views. + * + * @param oldHolder The ViewHolder before the layout is started, might be the same + * instance with newHolder. + * @param newHolder The ViewHolder after the layout is finished, might be the same + * instance with oldHolder. + * @param preLayoutInfo The information that was returned from + * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. + * @param postLayoutInfo The information that was returned from {@link + * #recordPreLayoutInformation(State, ViewHolder, int, List)}. + * + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + public abstract boolean animateChange(@NonNull ViewHolder oldHolder, + @NonNull ViewHolder newHolder, + @NonNull ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo); + + @AdapterChanges static int buildAdapterChangeFlagsForAnimations(ViewHolder viewHolder) { + int flags = viewHolder.mFlags & (FLAG_INVALIDATED | FLAG_REMOVED | FLAG_CHANGED); + if (viewHolder.isInvalid()) { + return FLAG_INVALIDATED; + } + if ((flags & FLAG_INVALIDATED) == 0) { + final int oldPos = viewHolder.getOldPosition(); + final int pos = viewHolder.getAdapterPosition(); + if (oldPos != NO_POSITION && pos != NO_POSITION && oldPos != pos){ + flags |= FLAG_MOVED; + } + } + return flags; + } + /** * Called when there are pending animations waiting to be started. This state - * is governed by the return values from {@link #animateAdd(ViewHolder) animateAdd()}, - * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, and - * {@link #animateRemove(ViewHolder) animateRemove()}, which inform the - * RecyclerView that the ItemAnimator wants to be called later to start the - * associated animations. runPendingAnimations() will be scheduled to be run - * on the next frame. + * is governed by the return values from + * {@link #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateAppearance()}, + * {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateChange()} + * {@link #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animatePersistence()}, and + * {@link #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateDisappearance()}, which inform the RecyclerView that the ItemAnimator wants to be + * called later to start the associated animations. runPendingAnimations() will be scheduled + * to be run on the next frame. */ abstract public void runPendingAnimations(); - /** - * Called when an item is removed from the RecyclerView. Implementors can choose - * whether and how to animate that change, but must always call - * {@link #dispatchRemoveFinished(ViewHolder)} when done, either - * immediately (if no animation will occur) or after the animation actually finishes. - * The return value indicates whether an animation has been set up and whether the - * ItemAnimator's {@link #runPendingAnimations()} method should be called at the - * next opportunity. This mechanism allows ItemAnimator to set up individual animations - * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, - * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, - * {@link #animateRemove(ViewHolder) animateRemove()}, and - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, - * then start the animations together in the later call to {@link #runPendingAnimations()}. - * - *

      This method may also be called for disappearing items which continue to exist in the - * RecyclerView, but for which the system does not have enough information to animate - * them out of view. In that case, the default animation for removing items is run - * on those items as well.

      - * - * @param holder The item that is being removed. - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - abstract public boolean animateRemove(ViewHolder holder); - - /** - * Called when an item is added to the RecyclerView. Implementors can choose - * whether and how to animate that change, but must always call - * {@link #dispatchAddFinished(ViewHolder)} when done, either - * immediately (if no animation will occur) or after the animation actually finishes. - * The return value indicates whether an animation has been set up and whether the - * ItemAnimator's {@link #runPendingAnimations()} method should be called at the - * next opportunity. This mechanism allows ItemAnimator to set up individual animations - * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, - * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, - * {@link #animateRemove(ViewHolder) animateRemove()}, and - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, - * then start the animations together in the later call to {@link #runPendingAnimations()}. - * - *

      This method may also be called for appearing items which were already in the - * RecyclerView, but for which the system does not have enough information to animate - * them into view. In that case, the default animation for adding items is run - * on those items as well.

      - * - * @param holder The item that is being added. - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - abstract public boolean animateAdd(ViewHolder holder); - - /** - * Called when an item is moved in the RecyclerView. Implementors can choose - * whether and how to animate that change, but must always call - * {@link #dispatchMoveFinished(ViewHolder)} when done, either - * immediately (if no animation will occur) or after the animation actually finishes. - * The return value indicates whether an animation has been set up and whether the - * ItemAnimator's {@link #runPendingAnimations()} method should be called at the - * next opportunity. This mechanism allows ItemAnimator to set up individual animations - * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, - * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, - * {@link #animateRemove(ViewHolder) animateRemove()}, and - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, - * then start the animations together in the later call to {@link #runPendingAnimations()}. - * - * @param holder The item that is being moved. - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - abstract public boolean animateMove(ViewHolder holder, int fromX, int fromY, - int toX, int toY); - - /** - * Called when an item is changed in the RecyclerView, as indicated by a call to - * {@link Adapter#notifyItemChanged(int)} or - * {@link Adapter#notifyItemRangeChanged(int, int)}. - *

      - * Implementers can choose whether and how to animate changes, but must always call - * {@link #dispatchChangeFinished(ViewHolder, boolean)} for each non-null ViewHolder, - * either immediately (if no animation will occur) or after the animation actually finishes. - * The return value indicates whether an animation has been set up and whether the - * ItemAnimator's {@link #runPendingAnimations()} method should be called at the - * next opportunity. This mechanism allows ItemAnimator to set up individual animations - * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, - * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, - * {@link #animateRemove(ViewHolder) animateRemove()}, and - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, - * then start the animations together in the later call to {@link #runPendingAnimations()}. - * - * @param oldHolder The original item that changed. - * @param newHolder The new item that was created with the changed content. Might be null - * @param fromLeft Left of the old view holder - * @param fromTop Top of the old view holder - * @param toLeft Left of the new view holder - * @param toTop Top of the new view holder - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - abstract public boolean animateChange(ViewHolder oldHolder, - ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop); - - - /** - * Method to be called by subclasses when a remove animation is done. - * - * @param item The item which has been removed - */ - public final void dispatchRemoveFinished(ViewHolder item) { - onRemoveFinished(item); - if (mListener != null) { - mListener.onRemoveFinished(item); - } - } - - /** - * Method to be called by subclasses when a move animation is done. - * - * @param item The item which has been moved - */ - public final void dispatchMoveFinished(ViewHolder item) { - onMoveFinished(item); - if (mListener != null) { - mListener.onMoveFinished(item); - } - } - - /** - * Method to be called by subclasses when an add animation is done. - * - * @param item The item which has been added - */ - public final void dispatchAddFinished(ViewHolder item) { - onAddFinished(item); - if (mListener != null) { - mListener.onAddFinished(item); - } - } - - /** - * Method to be called by subclasses when a change animation is done. - * - * @see #animateChange(ViewHolder, ViewHolder, int, int, int, int) - * @param item The item which has been changed (this method must be called for - * each non-null ViewHolder passed into - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}). - * @param oldItem true if this is the old item that was changed, false if - * it is the new item that replaced the old item. - */ - public final void dispatchChangeFinished(ViewHolder item, boolean oldItem) { - onChangeFinished(item, oldItem); - if (mListener != null) { - mListener.onChangeFinished(item); - } - } - - /** - * Method to be called by subclasses when a remove animation is being started. - * - * @param item The item being removed - */ - public final void dispatchRemoveStarting(ViewHolder item) { - onRemoveStarting(item); - } - - /** - * Method to be called by subclasses when a move animation is being started. - * - * @param item The item being moved - */ - public final void dispatchMoveStarting(ViewHolder item) { - onMoveStarting(item); - } - - /** - * Method to be called by subclasses when an add animation is being started. - * - * @param item The item being added - */ - public final void dispatchAddStarting(ViewHolder item) { - onAddStarting(item); - } - - /** - * Method to be called by subclasses when a change animation is being started. - * - * @param item The item which has been changed (this method must be called for - * each non-null ViewHolder passed into - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}). - * @param oldItem true if this is the old item that was changed, false if - * it is the new item that replaced the old item. - */ - public final void dispatchChangeStarting(ViewHolder item, boolean oldItem) { - onChangeStarting(item, oldItem); - } - /** * Method called when an animation on a view should be ended immediately. * This could happen when other events, like scrolling, occur, so that * animating views can be quickly put into their proper end locations. * Implementations should ensure that any animations running on the item * are canceled and affected properties are set to their end values. - * Also, appropriate dispatch methods (e.g., {@link #dispatchAddFinished(ViewHolder)} - * should be called since the animations are effectively done when this - * method is called. + * Also, {@link #dispatchAnimationFinished(ViewHolder)} should be called for each finished + * animation since the animations are effectively done when this method is called. * * @param item The item for which an animation should be stopped. */ @@ -8120,9 +11288,8 @@ public class RecyclerView extends ViewGroup { * animating views can be quickly put into their proper end locations. * Implementations should ensure that any animations running on any items * are canceled and affected properties are set to their end values. - * Also, appropriate dispatch methods (e.g., {@link #dispatchAddFinished(ViewHolder)} - * should be called since the animations are effectively done when this - * method is called. + * Also, {@link #dispatchAnimationFinished(ViewHolder)} should be called for each finished + * animation since the animations are effectively done when this method is called. */ abstract public void endAnimations(); @@ -8135,9 +11302,85 @@ public class RecyclerView extends ViewGroup { */ abstract public boolean isRunning(); + /** + * Method to be called by subclasses when an animation is finished. + *

      + * For each call RecyclerView makes to + * {@link #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateAppearance()}, + * {@link #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animatePersistence()}, or + * {@link #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateDisappearance()}, there + * should + * be a matching {@link #dispatchAnimationFinished(ViewHolder)} call by the subclass. + *

      + * For {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateChange()}, subclass should call this method for both the oldHolder + * and newHolder (if they are not the same instance). + * + * @param viewHolder The ViewHolder whose animation is finished. + * @see #onAnimationFinished(ViewHolder) + */ + public final void dispatchAnimationFinished(ViewHolder viewHolder) { + onAnimationFinished(viewHolder); + if (mListener != null) { + mListener.onAnimationFinished(viewHolder); + } + } + + /** + * Called after {@link #dispatchAnimationFinished(ViewHolder)} is called by the + * ItemAnimator. + * + * @param viewHolder The ViewHolder whose animation is finished. There might still be other + * animations running on this ViewHolder. + * @see #dispatchAnimationFinished(ViewHolder) + */ + public void onAnimationFinished(ViewHolder viewHolder) { + } + + /** + * Method to be called by subclasses when an animation is started. + *

      + * For each call RecyclerView makes to + * {@link #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateAppearance()}, + * {@link #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animatePersistence()}, or + * {@link #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateDisappearance()}, there should be a matching + * {@link #dispatchAnimationStarted(ViewHolder)} call by the subclass. + *

      + * For {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) + * animateChange()}, subclass should call this method for both the oldHolder + * and newHolder (if they are not the same instance). + *

      + * If your ItemAnimator decides not to animate a ViewHolder, it should call + * {@link #dispatchAnimationFinished(ViewHolder)} without calling + * {@link #dispatchAnimationStarted(ViewHolder)}. + * + * @param viewHolder The ViewHolder whose animation is starting. + * @see #onAnimationStarted(ViewHolder) + */ + public final void dispatchAnimationStarted(ViewHolder viewHolder) { + onAnimationStarted(viewHolder); + } + + /** + * Called when a new animation is started on the given ViewHolder. + * + * @param viewHolder The ViewHolder which started animating. Note that the ViewHolder + * might already be animating and this might be another animation. + * @see #dispatchAnimationStarted(ViewHolder) + */ + public void onAnimationStarted(ViewHolder viewHolder) { + + } + /** * Like {@link #isRunning()}, this method returns whether there are any item - * animations currently running. Addtionally, the listener passed in will be called + * animations currently running. Additionally, the listener passed in will be called * when there are no item animations running, either immediately (before the method * returns) if no animations are currently running, or when the currently running * animations are {@link #dispatchAnimationsFinished() finished}. @@ -8164,15 +11407,58 @@ public class RecyclerView extends ViewGroup { } /** - * The interface to be implemented by listeners to animation events from this - * ItemAnimator. This is used internally and is not intended for developers to - * create directly. + * When an item is changed, ItemAnimator can decide whether it wants to re-use + * the same ViewHolder for animations or RecyclerView should create a copy of the + * item and ItemAnimator will use both to run the animation (e.g. cross-fade). + *

      + * Note that this method will only be called if the {@link ViewHolder} still has the same + * type ({@link Adapter#getItemViewType(int)}). Otherwise, ItemAnimator will always receive + * both {@link ViewHolder}s in the + * {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo)} method. + *

      + * If your application is using change payloads, you can override + * {@link #canReuseUpdatedViewHolder(ViewHolder, List)} to decide based on payloads. + * + * @param viewHolder The ViewHolder which represents the changed item's old content. + * + * @return True if RecyclerView should just rebind to the same ViewHolder or false if + * RecyclerView should create a new ViewHolder and pass this ViewHolder to the + * ItemAnimator to animate. Default implementation returns true. + * + * @see #canReuseUpdatedViewHolder(ViewHolder, List) */ - interface ItemAnimatorListener { - void onRemoveFinished(ViewHolder item); - void onAddFinished(ViewHolder item); - void onMoveFinished(ViewHolder item); - void onChangeFinished(ViewHolder item); + public boolean canReuseUpdatedViewHolder(@NonNull ViewHolder viewHolder) { + return true; + } + + /** + * When an item is changed, ItemAnimator can decide whether it wants to re-use + * the same ViewHolder for animations or RecyclerView should create a copy of the + * item and ItemAnimator will use both to run the animation (e.g. cross-fade). + *

      + * Note that this method will only be called if the {@link ViewHolder} still has the same + * type ({@link Adapter#getItemViewType(int)}). Otherwise, ItemAnimator will always receive + * both {@link ViewHolder}s in the + * {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo)} method. + * + * @param viewHolder The ViewHolder which represents the changed item's old content. + * @param payloads A non-null list of merged payloads that were sent with change + * notifications. Can be empty if the adapter is invalidated via + * {@link RecyclerView.Adapter#notifyDataSetChanged()}. The same list of + * payloads will be passed into + * {@link RecyclerView.Adapter#onBindViewHolder(ViewHolder, int, List)} + * method if this method returns true. + * + * @return True if RecyclerView should just rebind to the same ViewHolder or false if + * RecyclerView should create a new ViewHolder and pass this ViewHolder to the + * ItemAnimator to animate. Default implementation calls + * {@link #canReuseUpdatedViewHolder(ViewHolder)}. + * + * @see #canReuseUpdatedViewHolder(ViewHolder) + */ + public boolean canReuseUpdatedViewHolder(@NonNull ViewHolder viewHolder, + @NonNull List payloads) { + return canReuseUpdatedViewHolder(viewHolder); } /** @@ -8187,6 +11473,28 @@ public class RecyclerView extends ViewGroup { mFinishedListeners.clear(); } + /** + * Returns a new {@link ItemHolderInfo} which will be used to store information about the + * ViewHolder. This information will later be passed into animate** methods. + *

      + * You can override this method if you want to extend {@link ItemHolderInfo} and provide + * your own instances. + * + * @return A new {@link ItemHolderInfo}. + */ + public ItemHolderInfo obtainHolderInfo() { + return new ItemHolderInfo(); + } + + /** + * The interface to be implemented by listeners to animation events from this + * ItemAnimator. This is used internally and is not intended for developers to + * create directly. + */ + interface ItemAnimatorListener { + void onAnimationFinished(ViewHolder item); + } + /** * This interface is used to inform listeners when all pending or running animations * in an ItemAnimator are finished. This can be used, for example, to delay an action @@ -8199,105 +11507,117 @@ public class RecyclerView extends ViewGroup { } /** - * Called when a remove animation is being started on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. + * A simple data structure that holds information about an item's bounds. + * This information is used in calculating item animations. Default implementation of + * {@link #recordPreLayoutInformation(RecyclerView.State, ViewHolder, int, List)} and + * {@link #recordPostLayoutInformation(RecyclerView.State, ViewHolder)} returns this data + * structure. You can extend this class if you would like to keep more information about + * the Views. + *

      + * If you want to provide your own implementation but still use `super` methods to record + * basic information, you can override {@link #obtainHolderInfo()} to provide your own + * instances. */ - public void onRemoveStarting(ViewHolder item) {} + public static class ItemHolderInfo { - /** - * Called when a remove animation has ended on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - public void onRemoveFinished(ViewHolder item) {} + /** + * The left edge of the View (excluding decorations) + */ + public int left; - /** - * Called when an add animation is being started on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - public void onAddStarting(ViewHolder item) {} + /** + * The top edge of the View (excluding decorations) + */ + public int top; - /** - * Called when an add animation has ended on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - public void onAddFinished(ViewHolder item) {} + /** + * The right edge of the View (excluding decorations) + */ + public int right; - /** - * Called when a move animation is being started on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - public void onMoveStarting(ViewHolder item) {} + /** + * The bottom edge of the View (excluding decorations) + */ + public int bottom; - /** - * Called when a move animation has ended on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - public void onMoveFinished(ViewHolder item) {} + /** + * The change flags that were passed to + * {@link #recordPreLayoutInformation(RecyclerView.State, ViewHolder, int, List)}. + */ + @AdapterChanges + public int changeFlags; - /** - * Called when a change animation is being started on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - * @param oldItem true if this is the old item that was changed, false if - * it is the new item that replaced the old item. - */ - public void onChangeStarting(ViewHolder item, boolean oldItem) {} + public ItemHolderInfo() { + } - /** - * Called when a change animation has ended on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - * @param oldItem true if this is the old item that was changed, false if - * it is the new item that replaced the old item. - */ - public void onChangeFinished(ViewHolder item, boolean oldItem) {} + /** + * Sets the {@link #left}, {@link #top}, {@link #right} and {@link #bottom} values from + * the given ViewHolder. Clears all {@link #changeFlags}. + * + * @param holder The ViewHolder whose bounds should be copied. + * @return This {@link ItemHolderInfo} + */ + public ItemHolderInfo setFrom(RecyclerView.ViewHolder holder) { + return setFrom(holder, 0); + } + /** + * Sets the {@link #left}, {@link #top}, {@link #right} and {@link #bottom} values from + * the given ViewHolder and sets the {@link #changeFlags} to the given flags parameter. + * + * @param holder The ViewHolder whose bounds should be copied. + * @param flags The adapter change flags that were passed into + * {@link #recordPreLayoutInformation(RecyclerView.State, ViewHolder, int, + * List)}. + * @return This {@link ItemHolderInfo} + */ + public ItemHolderInfo setFrom(RecyclerView.ViewHolder holder, + @AdapterChanges int flags) { + final View view = holder.itemView; + this.left = view.getLeft(); + this.top = view.getTop(); + this.right = view.getRight(); + this.bottom = view.getBottom(); + return this; + } + } + } + + @Override + protected int getChildDrawingOrder(int childCount, int i) { + if (mChildDrawingOrderCallback == null) { + return super.getChildDrawingOrder(childCount, i); + } else { + return mChildDrawingOrderCallback.onGetChildDrawingOrder(childCount, i); + } } /** - * Internal data structure that holds information about an item's bounds. - * This information is used in calculating item animations. + * A callback interface that can be used to alter the drawing order of RecyclerView children. + *

      + * It works using the {@link ViewGroup#getChildDrawingOrder(int, int)} method, so any case + * that applies to that method also applies to this callback. For example, changing the drawing + * order of two views will not have any effect if their elevation values are different since + * elevation overrides the result of this callback. */ - private static class ItemHolderInfo { - ViewHolder holder; - int left, top, right, bottom; + public interface ChildDrawingOrderCallback { + /** + * Returns the index of the child to draw for this iteration. Override this + * if you want to change the drawing order of children. By default, it + * returns i. + * + * @param i The current iteration. + * @return The index of the child to draw this iteration. + * + * @see RecyclerView#setChildDrawingOrderCallback(RecyclerView.ChildDrawingOrderCallback) + */ + int onGetChildDrawingOrder(int childCount, int i); + } - ItemHolderInfo(ViewHolder holder, int left, int top, int right, int bottom) { - this.holder = holder; - this.left = left; - this.top = top; - this.right = right; - this.bottom = bottom; + private NestedScrollingChildHelper getScrollingChildHelper() { + if (mScrollingChildHelper == null) { + mScrollingChildHelper = new NestedScrollingChildHelper(this); } + return mScrollingChildHelper; } } diff --git a/app/src/main/java/android/support/v7/widget/RecyclerViewAccessibilityDelegate.java b/app/src/main/java/android/support/v7/widget/RecyclerViewAccessibilityDelegate.java index ed7dfd6f63..1283f03b8b 100644 --- a/app/src/main/java/android/support/v7/widget/RecyclerViewAccessibilityDelegate.java +++ b/app/src/main/java/android/support/v7/widget/RecyclerViewAccessibilityDelegate.java @@ -35,12 +35,16 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp mRecyclerView = recyclerView; } + private boolean shouldIgnore() { + return mRecyclerView.hasPendingAdapterUpdates(); + } + @Override public boolean performAccessibilityAction(View host, int action, Bundle args) { if (super.performAccessibilityAction(host, action, args)) { return true; } - if (mRecyclerView.getLayoutManager() != null) { + if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { return mRecyclerView.getLayoutManager().performAccessibilityAction(action, args); } @@ -51,7 +55,7 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { super.onInitializeAccessibilityNodeInfo(host, info); info.setClassName(RecyclerView.class.getName()); - if (mRecyclerView.getLayoutManager() != null) { + if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { mRecyclerView.getLayoutManager().onInitializeAccessibilityNodeInfo(info); } } @@ -60,7 +64,7 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { super.onInitializeAccessibilityEvent(host, event); event.setClassName(RecyclerView.class.getName()); - if (host instanceof RecyclerView) { + if (host instanceof RecyclerView && !shouldIgnore()) { RecyclerView rv = (RecyclerView) host; if (rv.getLayoutManager() != null) { rv.getLayoutManager().onInitializeAccessibilityEvent(event); @@ -68,7 +72,12 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp } } - AccessibilityDelegateCompat getItemDelegate() { + /** + * Gets the AccessibilityDelegate for an individual item in the RecyclerView. + * A basic item delegate is provided by default, but you can override this + * method to provide a custom per-item delegate. + */ + public AccessibilityDelegateCompat getItemDelegate() { return mItemDelegate; } @@ -76,7 +85,7 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp @Override public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { super.onInitializeAccessibilityNodeInfo(host, info); - if (mRecyclerView.getLayoutManager() != null) { + if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { mRecyclerView.getLayoutManager(). onInitializeAccessibilityNodeInfoForItem(host, info); } @@ -87,7 +96,7 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp if (super.performAccessibilityAction(host, action, args)) { return true; } - if (mRecyclerView.getLayoutManager() != null) { + if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { return mRecyclerView.getLayoutManager(). performAccessibilityActionForItem(host, action, args); } diff --git a/app/src/main/java/android/support/v7/widget/ScrollbarHelper.java b/app/src/main/java/android/support/v7/widget/ScrollbarHelper.java index 0903f6414a..724fac8a66 100644 --- a/app/src/main/java/android/support/v7/widget/ScrollbarHelper.java +++ b/app/src/main/java/android/support/v7/widget/ScrollbarHelper.java @@ -33,17 +33,20 @@ class ScrollbarHelper { endChild == null) { return 0; } - final int minPosition = Math.min(lm.getPosition(startChild), lm.getPosition(endChild)); - final int maxPosition = Math.max(lm.getPosition(startChild), lm.getPosition(endChild)); + final int minPosition = Math.min(lm.getPosition(startChild), + lm.getPosition(endChild)); + final int maxPosition = Math.max(lm.getPosition(startChild), + lm.getPosition(endChild)); final int itemsBefore = reverseLayout ? Math.max(0, state.getItemCount() - maxPosition - 1) - : Math.max(0, minPosition - 1); + : Math.max(0, minPosition); if (!smoothScrollbarEnabled) { return itemsBefore; } final int laidOutArea = Math.abs(orientation.getDecoratedEnd(endChild) - orientation.getDecoratedStart(startChild)); - final int itemRange = Math.abs(lm.getPosition(startChild) - lm.getPosition(endChild)) + 1; + final int itemRange = Math.abs(lm.getPosition(startChild) - + lm.getPosition(endChild)) + 1; final float avgSizePerRow = (float) laidOutArea / itemRange; return Math.round(itemsBefore * avgSizePerRow + (orientation.getStartAfterPadding() @@ -86,7 +89,8 @@ class ScrollbarHelper { // smooth scrollbar enabled. try to estimate better. final int laidOutArea = orientation.getDecoratedEnd(endChild) - orientation.getDecoratedStart(startChild); - final int laidOutRange = Math.abs(lm.getPosition(startChild) - lm.getPosition(endChild)) + final int laidOutRange = Math.abs(lm.getPosition(startChild) - + lm.getPosition(endChild)) + 1; // estimate a size for full list. return (int) ((float) laidOutArea / laidOutRange * state.getItemCount()); diff --git a/app/src/main/java/android/support/v7/widget/SimpleItemAnimator.java b/app/src/main/java/android/support/v7/widget/SimpleItemAnimator.java new file mode 100644 index 0000000000..2db75413e7 --- /dev/null +++ b/app/src/main/java/android/support/v7/widget/SimpleItemAnimator.java @@ -0,0 +1,442 @@ +package android.support.v7.widget; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.widget.RecyclerView.Adapter; +import android.support.v7.widget.RecyclerView.ViewHolder; +import android.support.v7.widget.RecyclerView.ItemAnimator.ItemHolderInfo; +import android.util.Log; +import android.view.View; + +import java.util.List; + +/** + * A wrapper class for ItemAnimator that records View bounds and decides whether it should run + * move, change, add or remove animations. This class also replicates the original ItemAnimator + * API. + *

      + * It uses {@link ItemHolderInfo} to track the bounds information of the Views. If you would like + * to + * extend this class, you can override {@link #obtainHolderInfo()} method to provide your own info + * class that extends {@link ItemHolderInfo}. + */ +abstract public class SimpleItemAnimator extends RecyclerView.ItemAnimator { + + private static final boolean DEBUG = false; + + private static final String TAG = "SimpleItemAnimator"; + + boolean mSupportsChangeAnimations = true; + + /** + * Returns whether this ItemAnimator supports animations of change events. + * + * @return true if change animations are supported, false otherwise + */ + @SuppressWarnings("unused") + public boolean getSupportsChangeAnimations() { + return mSupportsChangeAnimations; + } + + /** + * Sets whether this ItemAnimator supports animations of item change events. + * If you set this property to false, actions on the data set which change the + * contents of items will not be animated. What those animations do is left + * up to the discretion of the ItemAnimator subclass, in its + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} implementation. + * The value of this property is true by default. + * + * @param supportsChangeAnimations true if change animations are supported by + * this ItemAnimator, false otherwise. If the property is false, + * the ItemAnimator + * will not receive a call to + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, + * int)} when changes occur. + * @see Adapter#notifyItemChanged(int) + * @see Adapter#notifyItemRangeChanged(int, int) + */ + public void setSupportsChangeAnimations(boolean supportsChangeAnimations) { + mSupportsChangeAnimations = supportsChangeAnimations; + } + + /** + * {@inheritDoc} + * + * @return True if change animations are not supported or the ViewHolder is invalid, + * false otherwise. + * + * @see #setSupportsChangeAnimations(boolean) + */ + @Override + public boolean canReuseUpdatedViewHolder(@NonNull RecyclerView.ViewHolder viewHolder) { + return !mSupportsChangeAnimations || viewHolder.isInvalid(); + } + + @Override + public boolean animateDisappearance(@NonNull ViewHolder viewHolder, + @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo) { + int oldLeft = preLayoutInfo.left; + int oldTop = preLayoutInfo.top; + View disappearingItemView = viewHolder.itemView; + int newLeft = postLayoutInfo == null ? disappearingItemView.getLeft() : postLayoutInfo.left; + int newTop = postLayoutInfo == null ? disappearingItemView.getTop() : postLayoutInfo.top; + if (!viewHolder.isRemoved() && (oldLeft != newLeft || oldTop != newTop)) { + disappearingItemView.layout(newLeft, newTop, + newLeft + disappearingItemView.getWidth(), + newTop + disappearingItemView.getHeight()); + if (DEBUG) { + Log.d(TAG, "DISAPPEARING: " + viewHolder + " with view " + disappearingItemView); + } + return animateMove(viewHolder, oldLeft, oldTop, newLeft, newTop); + } else { + if (DEBUG) { + Log.d(TAG, "REMOVED: " + viewHolder + " with view " + disappearingItemView); + } + return animateRemove(viewHolder); + } + } + + @Override + public boolean animateAppearance(@NonNull ViewHolder viewHolder, + @Nullable ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) { + if (preLayoutInfo != null && (preLayoutInfo.left != postLayoutInfo.left + || preLayoutInfo.top != postLayoutInfo.top)) { + // slide items in if before/after locations differ + if (DEBUG) { + Log.d(TAG, "APPEARING: " + viewHolder + " with view " + viewHolder); + } + return animateMove(viewHolder, preLayoutInfo.left, preLayoutInfo.top, + postLayoutInfo.left, postLayoutInfo.top); + } else { + if (DEBUG) { + Log.d(TAG, "ADDED: " + viewHolder + " with view " + viewHolder); + } + return animateAdd(viewHolder); + } + } + + @Override + public boolean animatePersistence(@NonNull ViewHolder viewHolder, + @NonNull ItemHolderInfo preInfo, @NonNull ItemHolderInfo postInfo) { + if (preInfo.left != postInfo.left || preInfo.top != postInfo.top) { + if (DEBUG) { + Log.d(TAG, "PERSISTENT: " + viewHolder + + " with view " + viewHolder.itemView); + } + return animateMove(viewHolder, + preInfo.left, preInfo.top, postInfo.left, postInfo.top); + } + dispatchMoveFinished(viewHolder); + return false; + } + + @Override + public boolean animateChange(@NonNull ViewHolder oldHolder, @NonNull ViewHolder newHolder, + @NonNull ItemHolderInfo preInfo, @NonNull ItemHolderInfo postInfo) { + if (DEBUG) { + Log.d(TAG, "CHANGED: " + oldHolder + " with view " + oldHolder.itemView); + } + final int fromLeft = preInfo.left; + final int fromTop = preInfo.top; + final int toLeft, toTop; + if (newHolder.shouldIgnore()) { + toLeft = preInfo.left; + toTop = preInfo.top; + } else { + toLeft = postInfo.left; + toTop = postInfo.top; + } + return animateChange(oldHolder, newHolder, fromLeft, fromTop, toLeft, toTop); + } + + /** + * Called when an item is removed from the RecyclerView. Implementors can choose + * whether and how to animate that change, but must always call + * {@link #dispatchRemoveFinished(ViewHolder)} when done, either + * immediately (if no animation will occur) or after the animation actually finishes. + * The return value indicates whether an animation has been set up and whether the + * ItemAnimator's {@link #runPendingAnimations()} method should be called at the + * next opportunity. This mechanism allows ItemAnimator to set up individual animations + * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, + * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, + * {@link #animateRemove(ViewHolder) animateRemove()}, and + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, + * then start the animations together in the later call to {@link #runPendingAnimations()}. + * + *

      This method may also be called for disappearing items which continue to exist in the + * RecyclerView, but for which the system does not have enough information to animate + * them out of view. In that case, the default animation for removing items is run + * on those items as well.

      + * + * @param holder The item that is being removed. + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + abstract public boolean animateRemove(ViewHolder holder); + + /** + * Called when an item is added to the RecyclerView. Implementors can choose + * whether and how to animate that change, but must always call + * {@link #dispatchAddFinished(ViewHolder)} when done, either + * immediately (if no animation will occur) or after the animation actually finishes. + * The return value indicates whether an animation has been set up and whether the + * ItemAnimator's {@link #runPendingAnimations()} method should be called at the + * next opportunity. This mechanism allows ItemAnimator to set up individual animations + * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, + * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, + * {@link #animateRemove(ViewHolder) animateRemove()}, and + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, + * then start the animations together in the later call to {@link #runPendingAnimations()}. + * + *

      This method may also be called for appearing items which were already in the + * RecyclerView, but for which the system does not have enough information to animate + * them into view. In that case, the default animation for adding items is run + * on those items as well.

      + * + * @param holder The item that is being added. + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + abstract public boolean animateAdd(ViewHolder holder); + + /** + * Called when an item is moved in the RecyclerView. Implementors can choose + * whether and how to animate that change, but must always call + * {@link #dispatchMoveFinished(ViewHolder)} when done, either + * immediately (if no animation will occur) or after the animation actually finishes. + * The return value indicates whether an animation has been set up and whether the + * ItemAnimator's {@link #runPendingAnimations()} method should be called at the + * next opportunity. This mechanism allows ItemAnimator to set up individual animations + * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, + * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, + * {@link #animateRemove(ViewHolder) animateRemove()}, and + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, + * then start the animations together in the later call to {@link #runPendingAnimations()}. + * + * @param holder The item that is being moved. + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + abstract public boolean animateMove(ViewHolder holder, int fromX, int fromY, + int toX, int toY); + + /** + * Called when an item is changed in the RecyclerView, as indicated by a call to + * {@link Adapter#notifyItemChanged(int)} or + * {@link Adapter#notifyItemRangeChanged(int, int)}. + *

      + * Implementers can choose whether and how to animate changes, but must always call + * {@link #dispatchChangeFinished(ViewHolder, boolean)} for each non-null distinct ViewHolder, + * either immediately (if no animation will occur) or after the animation actually finishes. + * If the {@code oldHolder} is the same ViewHolder as the {@code newHolder}, you must call + * {@link #dispatchChangeFinished(ViewHolder, boolean)} once and only once. In that case, the + * second parameter of {@code dispatchChangeFinished} is ignored. + *

      + * The return value indicates whether an animation has been set up and whether the + * ItemAnimator's {@link #runPendingAnimations()} method should be called at the + * next opportunity. This mechanism allows ItemAnimator to set up individual animations + * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, + * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, + * {@link #animateRemove(ViewHolder) animateRemove()}, and + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, + * then start the animations together in the later call to {@link #runPendingAnimations()}. + * + * @param oldHolder The original item that changed. + * @param newHolder The new item that was created with the changed content. Might be null + * @param fromLeft Left of the old view holder + * @param fromTop Top of the old view holder + * @param toLeft Left of the new view holder + * @param toTop Top of the new view holder + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + abstract public boolean animateChange(ViewHolder oldHolder, + ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop); + + /** + * Method to be called by subclasses when a remove animation is done. + * + * @param item The item which has been removed + * @see RecyclerView.ItemAnimator#animateDisappearance(ViewHolder, ItemHolderInfo, + * ItemHolderInfo) + */ + public final void dispatchRemoveFinished(ViewHolder item) { + onRemoveFinished(item); + dispatchAnimationFinished(item); + } + + /** + * Method to be called by subclasses when a move animation is done. + * + * @param item The item which has been moved + * @see RecyclerView.ItemAnimator#animateDisappearance(ViewHolder, ItemHolderInfo, + * ItemHolderInfo) + * @see RecyclerView.ItemAnimator#animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) + * @see RecyclerView.ItemAnimator#animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) + */ + public final void dispatchMoveFinished(ViewHolder item) { + onMoveFinished(item); + dispatchAnimationFinished(item); + } + + /** + * Method to be called by subclasses when an add animation is done. + * + * @param item The item which has been added + */ + public final void dispatchAddFinished(ViewHolder item) { + onAddFinished(item); + dispatchAnimationFinished(item); + } + + /** + * Method to be called by subclasses when a change animation is done. + * + * @param item The item which has been changed (this method must be called for + * each non-null ViewHolder passed into + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}). + * @param oldItem true if this is the old item that was changed, false if + * it is the new item that replaced the old item. + * @see #animateChange(ViewHolder, ViewHolder, int, int, int, int) + */ + public final void dispatchChangeFinished(ViewHolder item, boolean oldItem) { + onChangeFinished(item, oldItem); + dispatchAnimationFinished(item); + } + + /** + * Method to be called by subclasses when a remove animation is being started. + * + * @param item The item being removed + */ + public final void dispatchRemoveStarting(ViewHolder item) { + onRemoveStarting(item); + } + + /** + * Method to be called by subclasses when a move animation is being started. + * + * @param item The item being moved + */ + public final void dispatchMoveStarting(ViewHolder item) { + onMoveStarting(item); + } + + /** + * Method to be called by subclasses when an add animation is being started. + * + * @param item The item being added + */ + public final void dispatchAddStarting(ViewHolder item) { + onAddStarting(item); + } + + /** + * Method to be called by subclasses when a change animation is being started. + * + * @param item The item which has been changed (this method must be called for + * each non-null ViewHolder passed into + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}). + * @param oldItem true if this is the old item that was changed, false if + * it is the new item that replaced the old item. + */ + public final void dispatchChangeStarting(ViewHolder item, boolean oldItem) { + onChangeStarting(item, oldItem); + } + + /** + * Called when a remove animation is being started on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + @SuppressWarnings("UnusedParameters") + public void onRemoveStarting(ViewHolder item) { + } + + /** + * Called when a remove animation has ended on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + public void onRemoveFinished(ViewHolder item) { + } + + /** + * Called when an add animation is being started on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + @SuppressWarnings("UnusedParameters") + public void onAddStarting(ViewHolder item) { + } + + /** + * Called when an add animation has ended on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + public void onAddFinished(ViewHolder item) { + } + + /** + * Called when a move animation is being started on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + @SuppressWarnings("UnusedParameters") + public void onMoveStarting(ViewHolder item) { + } + + /** + * Called when a move animation has ended on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + public void onMoveFinished(ViewHolder item) { + } + + /** + * Called when a change animation is being started on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + * @param oldItem true if this is the old item that was changed, false if + * it is the new item that replaced the old item. + */ + @SuppressWarnings("UnusedParameters") + public void onChangeStarting(ViewHolder item, boolean oldItem) { + } + + /** + * Called when a change animation has ended on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + * @param oldItem true if this is the old item that was changed, false if + * it is the new item that replaced the old item. + */ + public void onChangeFinished(ViewHolder item, boolean oldItem) { + } +} diff --git a/app/src/main/java/android/support/v7/widget/SnapHelper.java b/app/src/main/java/android/support/v7/widget/SnapHelper.java new file mode 100644 index 0000000000..a2c557d873 --- /dev/null +++ b/app/src/main/java/android/support/v7/widget/SnapHelper.java @@ -0,0 +1,274 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.widget; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.widget.RecyclerView.LayoutManager; +import android.util.DisplayMetrics; +import android.view.View; +import android.view.animation.DecelerateInterpolator; +import android.widget.Scroller; +import android.support.v7.widget.RecyclerView.SmoothScroller.ScrollVectorProvider; + +/** + * Class intended to support snapping for a {@link RecyclerView}. + *

      + * SnapHelper tries to handle fling as well but for this to work properly, the + * {@link RecyclerView.LayoutManager} must implement the {@link ScrollVectorProvider} interface or + * you should override {@link #onFling(int, int)} and handle fling manually. + */ +public abstract class SnapHelper extends RecyclerView.OnFlingListener { + + private static final float MILLISECONDS_PER_INCH = 100f; + + private RecyclerView mRecyclerView; + private Scroller mGravityScroller; + + // Handles the snap on scroll case. + private final RecyclerView.OnScrollListener mScrollListener = + new RecyclerView.OnScrollListener() { + @Override + public void onScrollStateChanged(RecyclerView recyclerView, int newState) { + super.onScrollStateChanged(recyclerView, newState); + if (newState == RecyclerView.SCROLL_STATE_IDLE) { + snapToTargetExistingView(); + } + } + }; + + @Override + public boolean onFling(int velocityX, int velocityY) { + LayoutManager layoutManager = mRecyclerView.getLayoutManager(); + if (layoutManager == null) { + return false; + } + RecyclerView.Adapter adapter = mRecyclerView.getAdapter(); + if (adapter == null) { + return false; + } + int minFlingVelocity = mRecyclerView.getMinFlingVelocity(); + return (Math.abs(velocityY) > minFlingVelocity || Math.abs(velocityX) > minFlingVelocity) + && snapFromFling(layoutManager, velocityX, velocityY); + } + + /** + * Attaches the {@link SnapHelper} to the provided RecyclerView, by calling + * {@link RecyclerView#setOnFlingListener(RecyclerView.OnFlingListener)}. + * You can call this method with {@code null} to detach it from the current RecyclerView. + * + * @param recyclerView The RecyclerView instance to which you want to add this helper or + * {@code null} if you want to remove SnapHelper from the current + * RecyclerView. + * + * @throws IllegalArgumentException if there is already a {@link RecyclerView.OnFlingListener} + * attached to the provided {@link RecyclerView}. + * + */ + public void attachToRecyclerView(@Nullable RecyclerView recyclerView) + throws IllegalStateException { + if (mRecyclerView == recyclerView) { + return; // nothing to do + } + if (mRecyclerView != null) { + destroyCallbacks(); + } + mRecyclerView = recyclerView; + if (mRecyclerView != null) { + setupCallbacks(); + mGravityScroller = new Scroller(mRecyclerView.getContext(), + new DecelerateInterpolator()); + snapToTargetExistingView(); + } + } + + /** + * Called when an instance of a {@link RecyclerView} is attached. + */ + private void setupCallbacks() throws IllegalStateException { + if (mRecyclerView.getOnFlingListener() != null) { + throw new IllegalStateException("An instance of OnFlingListener already set."); + } + mRecyclerView.addOnScrollListener(mScrollListener); + mRecyclerView.setOnFlingListener(this); + } + + /** + * Called when the instance of a {@link RecyclerView} is detached. + */ + private void destroyCallbacks() { + mRecyclerView.removeOnScrollListener(mScrollListener); + mRecyclerView.setOnFlingListener(null); + } + + /** + * Calculated the estimated scroll distance in each direction given velocities on both axes. + * + * @param velocityX Fling velocity on the horizontal axis. + * @param velocityY Fling velocity on the vertical axis. + * + * @return array holding the calculated distances in x and y directions + * respectively. + */ + public int[] calculateScrollDistance(int velocityX, int velocityY) { + int[] outDist = new int[2]; + mGravityScroller.fling(0, 0, velocityX, velocityY, + Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE); + outDist[0] = mGravityScroller.getFinalX(); + outDist[1] = mGravityScroller.getFinalY(); + return outDist; + } + + /** + * Helper method to facilitate for snapping triggered by a fling. + * + * @param layoutManager The {@link LayoutManager} associated with the attached + * {@link RecyclerView}. + * @param velocityX Fling velocity on the horizontal axis. + * @param velocityY Fling velocity on the vertical axis. + * + * @return true if it is handled, false otherwise. + */ + private boolean snapFromFling(@NonNull LayoutManager layoutManager, int velocityX, + int velocityY) { + if (!(layoutManager instanceof ScrollVectorProvider)) { + return false; + } + + RecyclerView.SmoothScroller smoothScroller = createSnapScroller(layoutManager); + if (smoothScroller == null) { + return false; + } + + int targetPosition = findTargetSnapPosition(layoutManager, velocityX, velocityY); + if (targetPosition == RecyclerView.NO_POSITION) { + return false; + } + + smoothScroller.setTargetPosition(targetPosition); + layoutManager.startSmoothScroll(smoothScroller); + return true; + } + + /** + * Snaps to a target view which currently exists in the attached {@link RecyclerView}. This + * method is used to snap the view when the {@link RecyclerView} is first attached; when + * snapping was triggered by a scroll and when the fling is at its final stages. + */ + private void snapToTargetExistingView() { + if (mRecyclerView == null) { + return; + } + LayoutManager layoutManager = mRecyclerView.getLayoutManager(); + if (layoutManager == null) { + return; + } + View snapView = findSnapView(layoutManager); + if (snapView == null) { + return; + } + int[] snapDistance = calculateDistanceToFinalSnap(layoutManager, snapView); + if (snapDistance[0] != 0 || snapDistance[1] != 0) { + mRecyclerView.smoothScrollBy(snapDistance[0], snapDistance[1]); + } + } + + /** + * Creates a scroller to be used in the snapping implementation. + * + * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached + * {@link RecyclerView}. + * + * @return a {@link LinearSmoothScroller} which will handle the scrolling. + */ + @Nullable + private LinearSmoothScroller createSnapScroller(LayoutManager layoutManager) { + if (!(layoutManager instanceof ScrollVectorProvider)) { + return null; + } + return new LinearSmoothScroller(mRecyclerView.getContext()) { + @Override + protected void onTargetFound(View targetView, RecyclerView.State state, Action action) { + int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), + targetView); + final int dx = snapDistances[0]; + final int dy = snapDistances[1]; + final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy))); + if (time > 0) { + action.update(dx, dy, time, mDecelerateInterpolator); + } + } + + @Override + protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { + return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; + } + }; + } + + /** + * Override this method to snap to a particular point within the target view or the container + * view on any axis. + *

      + * This method is called when the {@link SnapHelper} has intercepted a fling and it needs + * to know the exact distance required to scroll by in order to snap to the target view. + * + * @param layoutManager the {@link RecyclerView.LayoutManager} associated with the attached + * {@link RecyclerView} + * @param targetView the target view that is chosen as the view to snap + * + * @return the output coordinates the put the result into. out[0] is the distance + * on horizontal axis and out[1] is the distance on vertical axis. + */ + @SuppressWarnings("WeakerAccess") + @Nullable + public abstract int[] calculateDistanceToFinalSnap(@NonNull LayoutManager layoutManager, + @NonNull View targetView); + + /** + * Override this method to provide a particular target view for snapping. + *

      + * This method is called when the {@link SnapHelper} is ready to start snapping and requires + * a target view to snap to. It will be explicitly called when the scroll state becomes idle + * after a scroll. It will also be called when the {@link SnapHelper} is preparing to snap + * after a fling and requires a reference view from the current set of child views. + *

      + * If this method returns {@code null}, SnapHelper will not snap to any view. + * + * @param layoutManager the {@link RecyclerView.LayoutManager} associated with the attached + * {@link RecyclerView} + * + * @return the target view to which to snap on fling or end of scroll + */ + @SuppressWarnings("WeakerAccess") + @Nullable + public abstract View findSnapView(LayoutManager layoutManager); + + /** + * Override to provide a particular adapter target position for snapping. + * + * @param layoutManager the {@link RecyclerView.LayoutManager} associated with the attached + * {@link RecyclerView} + * @param velocityX fling velocity on the horizontal axis + * @param velocityY fling velocity on the vertical axis + * + * @return the target adapter position to you want to snap or {@link RecyclerView#NO_POSITION} + * if no snapping should happen + */ + public abstract int findTargetSnapPosition(LayoutManager layoutManager, int velocityX, + int velocityY); +} \ No newline at end of file diff --git a/app/src/main/java/android/support/v7/widget/StaggeredGridLayoutManager.java b/app/src/main/java/android/support/v7/widget/StaggeredGridLayoutManager.java index 0389012358..12745fe04f 100644 --- a/app/src/main/java/android/support/v7/widget/StaggeredGridLayoutManager.java +++ b/app/src/main/java/android/support/v7/widget/StaggeredGridLayoutManager.java @@ -16,11 +16,19 @@ package android.support.v7.widget; +import static android.support.v7.widget.LayoutState.ITEM_DIRECTION_HEAD; +import static android.support.v7.widget.LayoutState.ITEM_DIRECTION_TAIL; +import static android.support.v7.widget.LayoutState.LAYOUT_END; +import static android.support.v7.widget.LayoutState.LAYOUT_START; +import static android.support.v7.widget.RecyclerView.NO_POSITION; + import android.content.Context; import android.graphics.PointF; import android.graphics.Rect; import android.os.Parcel; import android.os.Parcelable; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.view.ViewCompat; import android.support.v4.view.accessibility.AccessibilityEventCompat; import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; @@ -36,12 +44,6 @@ import java.util.Arrays; import java.util.BitSet; import java.util.List; -import static android.support.v7.widget.LayoutState.ITEM_DIRECTION_HEAD; -import static android.support.v7.widget.LayoutState.ITEM_DIRECTION_TAIL; -import static android.support.v7.widget.LayoutState.LAYOUT_END; -import static android.support.v7.widget.LayoutState.LAYOUT_START; -import static android.support.v7.widget.RecyclerView.NO_POSITION; - /** * A LayoutManager that lays out children in a staggered grid formation. * It supports horizontal & vertical layout as well as an ability to layout children in reverse. @@ -50,9 +52,10 @@ import static android.support.v7.widget.RecyclerView.NO_POSITION; * StaggeredGridLayoutManager can offset spans independently or move items between spans. You can * control this behavior via {@link #setGapStrategy(int)}. */ -public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { +public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager implements + RecyclerView.SmoothScroller.ScrollVectorProvider { - public static final String TAG = "StaggeredGridLayoutManager"; + private static final String TAG = "StaggeredGridLayoutManager"; private static final boolean DEBUG = false; @@ -65,6 +68,10 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { */ public static final int GAP_HANDLING_NONE = 0; + /** + * @deprecated No longer supported. + */ + @SuppressWarnings("unused") @Deprecated public static final int GAP_HANDLING_LAZY = 1; @@ -74,21 +81,28 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { * and move items to correct positions with animations. *

      * For example, if LayoutManager ends up with the following layout due to adapter changes: - * + *

            * AAA
            * _BC
            * DDD
      -     * 
      +     * 
      + *

      * It will animate to the following state: - * + *

            * AAA
            * BC_
            * DDD
      -     * 
      +     * 
      */ public static final int GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS = 2; private static final int INVALID_OFFSET = Integer.MIN_VALUE; + /** + * While trying to find next view to focus, LayoutManager will not try to scroll more + * than this factor times the total space of the list. If layout is vertical, total space is the + * height minus padding, if layout is horizontal, total space is the width minus padding. + */ + private static final float MAX_SCROLL_FACTOR = 1 / 3f; /** * Number of spans @@ -101,7 +115,9 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { * Primary orientation is the layout's orientation, secondary orientation is the orientation * for spans. Having both makes code much cleaner for calculations. */ + @NonNull OrientationHelper mPrimaryOrientation; + @NonNull OrientationHelper mSecondaryOrientation; private int mOrientation; @@ -111,7 +127,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { */ private int mSizePerSpan; - private LayoutState mLayoutState; + @NonNull + private final LayoutState mLayoutState; private boolean mReverseLayout = false; @@ -167,7 +184,12 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { /** * Re-used measurement specs. updated by onLayout. */ - private int mFullSizeSpec, mWidthSpec, mHeightSpec; + private int mFullSizeSpec; + + /** + * Re-used rectangle to get child decor offsets. + */ + private final Rect mTmpRect = new Rect(); /** * Re-used anchor info. @@ -188,13 +210,29 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { */ private boolean mSmoothScrollbarEnabled = true; - private final Runnable checkForGapsRunnable = new Runnable() { + private final Runnable mCheckForGapsRunnable = new Runnable() { @Override public void run() { checkForGaps(); } }; + /** + * Constructor used when layout manager is set in XML by RecyclerView attribute + * "layoutManager". Defaults to single column and vertical. + */ + @SuppressWarnings("unused") + public StaggeredGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, + int defStyleRes) { + Properties properties = getProperties(context, attrs, defStyleAttr, defStyleRes); + setOrientation(properties.orientation); + setSpanCount(properties.spanCount); + setReverseLayout(properties.reverseLayout); + setAutoMeasureEnabled(mGapStrategy != GAP_HANDLING_NONE); + mLayoutState = new LayoutState(); + createOrientationHelpers(); + } + /** * Creates a StaggeredGridLayoutManager with given parameters. * @@ -205,6 +243,15 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { public StaggeredGridLayoutManager(int spanCount, int orientation) { mOrientation = orientation; setSpanCount(spanCount); + setAutoMeasureEnabled(mGapStrategy != GAP_HANDLING_NONE); + mLayoutState = new LayoutState(); + createOrientationHelpers(); + } + + private void createOrientationHelpers() { + mPrimaryOrientation = OrientationHelper.createOrientationHelper(this, mOrientation); + mSecondaryOrientation = OrientationHelper + .createOrientationHelper(this, 1 - mOrientation); } /** @@ -213,9 +260,9 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { * When a full span item is laid out in reverse direction, it sets a flag which we check when * scroll is stopped (or re-layout happens) and re-layout after first valid item. */ - private void checkForGaps() { - if (getChildCount() == 0 || mGapStrategy == GAP_HANDLING_NONE) { - return; + private boolean checkForGaps() { + if (getChildCount() == 0 || mGapStrategy == GAP_HANDLING_NONE || !isAttachedToWindow()) { + return false; } final int minPos, maxPos; if (mShouldReverseLayout) { @@ -231,23 +278,23 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { mLazySpanLookup.clear(); requestSimpleAnimationsInNextLayout(); requestLayout(); - return; + return true; } } if (!mLaidOutInvalidFullSpan) { - return; + return false; } int invalidGapDir = mShouldReverseLayout ? LAYOUT_START : LAYOUT_END; final LazySpanLookup.FullSpanItem invalidFsi = mLazySpanLookup - .getFirstFullSpanItemInRange(minPos, maxPos + 1, invalidGapDir); + .getFirstFullSpanItemInRange(minPos, maxPos + 1, invalidGapDir, true); if (invalidFsi == null) { mLaidOutInvalidFullSpan = false; mLazySpanLookup.forceInvalidateAfter(maxPos + 1); - return; + return false; } final LazySpanLookup.FullSpanItem validFsi = mLazySpanLookup .getFirstFullSpanItemInRange(minPos, invalidFsi.mPosition, - invalidGapDir * -1); + invalidGapDir * -1, true); if (validFsi == null) { mLazySpanLookup.forceInvalidateAfter(invalidFsi.mPosition); } else { @@ -255,6 +302,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } requestSimpleAnimationsInNextLayout(); requestLayout(); + return true; } @Override @@ -266,9 +314,12 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { @Override public void onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycler) { + removeCallbacks(mCheckForGapsRunnable); for (int i = 0; i < mSpanCount; i++) { mSpans[i].clear(); } + // SGLM will require fresh layout call to recover state after detach + view.requestLayout(); } /** @@ -286,11 +337,11 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { final int preferredSpanDir = mOrientation == VERTICAL && isLayoutRTL() ? 1 : -1; if (mShouldReverseLayout) { - firstChildIndex = endChildIndex - 1; + firstChildIndex = endChildIndex; childLimit = startChildIndex - 1; } else { firstChildIndex = startChildIndex; - childLimit = endChildIndex; + childLimit = endChildIndex + 1; } final int nextChildDiff = firstChildIndex < childLimit ? 1 : -1; for (int i = firstChildIndex; i != childLimit; i += nextChildDiff) { @@ -343,10 +394,16 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { private boolean checkSpanForGap(Span span) { if (mShouldReverseLayout) { if (span.getEndLine() < mPrimaryOrientation.getEndAfterPadding()) { - return true; + // if it is full span, it is OK + final View endView = span.mViews.get(span.mViews.size() - 1); + final LayoutParams lp = span.getLayoutParams(endView); + return !lp.mFullSpan; } } else if (span.getStartLine() > mPrimaryOrientation.getStartAfterPadding()) { - return true; + // if it is full span, it is OK + final View startView = span.mViews.get(0); + final LayoutParams lp = span.getLayoutParams(startView); + return !lp.mFullSpan; } return false; } @@ -389,12 +446,9 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return; } mOrientation = orientation; - if (mPrimaryOrientation != null && mSecondaryOrientation != null) { - // swap - OrientationHelper tmp = mPrimaryOrientation; - mPrimaryOrientation = mSecondaryOrientation; - mSecondaryOrientation = tmp; - } + OrientationHelper tmp = mPrimaryOrientation; + mPrimaryOrientation = mSecondaryOrientation; + mSecondaryOrientation = tmp; requestLayout(); } @@ -458,6 +512,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { + "or GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS"); } mGapStrategy = gapStrategy; + setAutoMeasureEnabled(mGapStrategy != GAP_HANDLING_NONE); requestLayout(); } @@ -488,15 +543,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { requestLayout(); } - private void ensureOrientationHelper() { - if (mPrimaryOrientation == null) { - mPrimaryOrientation = OrientationHelper.createOrientationHelper(this, mOrientation); - mSecondaryOrientation = OrientationHelper - .createOrientationHelper(this, 1 - mOrientation); - mLayoutState = new LayoutState(); - } - } - /** * Calculates the views' layout order. (e.g. from end to start or start to end) * RTL layout support is applied automatically. So if layout is RTL and @@ -528,24 +574,56 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } @Override - public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { - ensureOrientationHelper(); - // Update adapter size. - mLazySpanLookup.mAdapterSize = state.getItemCount(); - - final AnchorInfo anchorInfo = mAnchorInfo; - anchorInfo.reset(); - - if (mPendingSavedState != null) { - applyPendingSavedState(anchorInfo); + public void setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec) { + // we don't like it to wrap content in our non-scroll direction. + final int width, height; + final int horizontalPadding = getPaddingLeft() + getPaddingRight(); + final int verticalPadding = getPaddingTop() + getPaddingBottom(); + if (mOrientation == VERTICAL) { + final int usedHeight = childrenBounds.height() + verticalPadding; + height = chooseSize(hSpec, usedHeight, getMinimumHeight()); + width = chooseSize(wSpec, mSizePerSpan * mSpanCount + horizontalPadding, + getMinimumWidth()); } else { - resolveShouldLayoutReverse(); - anchorInfo.mLayoutFromEnd = mShouldReverseLayout; + final int usedWidth = childrenBounds.width() + horizontalPadding; + width = chooseSize(wSpec, usedWidth, getMinimumWidth()); + height = chooseSize(hSpec, mSizePerSpan * mSpanCount + verticalPadding, + getMinimumHeight()); + } + setMeasuredDimension(width, height); + } + + @Override + public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { + onLayoutChildren(recycler, state, true); + } + + + private void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state, + boolean shouldCheckForGaps) { + final AnchorInfo anchorInfo = mAnchorInfo; + if (mPendingSavedState != null || mPendingScrollPosition != NO_POSITION) { + if (state.getItemCount() == 0) { + removeAndRecycleAllViews(recycler); + anchorInfo.reset(); + return; + } } - updateAnchorInfoForLayout(state, anchorInfo); + if (!anchorInfo.mValid || mPendingScrollPosition != NO_POSITION || + mPendingSavedState != null) { + anchorInfo.reset(); + if (mPendingSavedState != null) { + applyPendingSavedState(anchorInfo); + } else { + resolveShouldLayoutReverse(); + anchorInfo.mLayoutFromEnd = mShouldReverseLayout; + } - if (mPendingSavedState == null) { + updateAnchorInfoForLayout(state, anchorInfo); + anchorInfo.mValid = true; + } + if (mPendingSavedState == null && mPendingScrollPosition == NO_POSITION) { if (anchorInfo.mLayoutFromEnd != mLastLayoutFromEnd || isLayoutRTL() != mLastLayoutRTL) { mLazySpanLookup.clear(); @@ -570,26 +648,30 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } } detachAndScrapAttachedViews(recycler); + mLayoutState.mRecycle = false; mLaidOutInvalidFullSpan = false; - updateMeasureSpecs(); + updateMeasureSpecs(mSecondaryOrientation.getTotalSpace()); + updateLayoutState(anchorInfo.mPosition, state); if (anchorInfo.mLayoutFromEnd) { // Layout start. - updateLayoutStateToFillStart(anchorInfo.mPosition, state); + setLayoutStateDirection(LAYOUT_START); fill(recycler, mLayoutState, state); // Layout end. - updateLayoutStateToFillEnd(anchorInfo.mPosition, state); - mLayoutState.mCurrentPosition += mLayoutState.mItemDirection; + setLayoutStateDirection(LAYOUT_END); + mLayoutState.mCurrentPosition = anchorInfo.mPosition + mLayoutState.mItemDirection; fill(recycler, mLayoutState, state); } else { // Layout end. - updateLayoutStateToFillEnd(anchorInfo.mPosition, state); + setLayoutStateDirection(LAYOUT_END); fill(recycler, mLayoutState, state); // Layout start. - updateLayoutStateToFillStart(anchorInfo.mPosition, state); - mLayoutState.mCurrentPosition += mLayoutState.mItemDirection; + setLayoutStateDirection(LAYOUT_START); + mLayoutState.mCurrentPosition = anchorInfo.mPosition + mLayoutState.mItemDirection; fill(recycler, mLayoutState, state); } + repositionToWrapContentIfNecessary(); + if (getChildCount() > 0) { if (mShouldReverseLayout) { fixEndGap(recycler, state, true); @@ -599,18 +681,85 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { fixEndGap(recycler, state, false); } } - - if (!state.isPreLayout()) { - if (getChildCount() > 0 && mPendingScrollPosition != NO_POSITION && - mLaidOutInvalidFullSpan) { - ViewCompat.postOnAnimation(getChildAt(0), checkForGapsRunnable); + boolean hasGaps = false; + if (shouldCheckForGaps && !state.isPreLayout()) { + final boolean needToCheckForGaps = mGapStrategy != GAP_HANDLING_NONE + && getChildCount() > 0 + && (mLaidOutInvalidFullSpan || hasGapsToFix() != null); + if (needToCheckForGaps) { + removeCallbacks(mCheckForGapsRunnable); + if (checkForGaps()) { + hasGaps = true; + } } - mPendingScrollPosition = NO_POSITION; - mPendingScrollPositionOffset = INVALID_OFFSET; + } + if (state.isPreLayout()) { + mAnchorInfo.reset(); } mLastLayoutFromEnd = anchorInfo.mLayoutFromEnd; mLastLayoutRTL = isLayoutRTL(); + if (hasGaps) { + mAnchorInfo.reset(); + onLayoutChildren(recycler, state, false); + } + } + + @Override + public void onLayoutCompleted(RecyclerView.State state) { + super.onLayoutCompleted(state); + mPendingScrollPosition = NO_POSITION; + mPendingScrollPositionOffset = INVALID_OFFSET; mPendingSavedState = null; // we don't need this anymore + mAnchorInfo.reset(); + } + + private void repositionToWrapContentIfNecessary() { + if (mSecondaryOrientation.getMode() == View.MeasureSpec.EXACTLY) { + return; // nothing to do + } + float maxSize = 0; + final int childCount = getChildCount(); + for (int i = 0; i < childCount; i ++) { + View child = getChildAt(i); + float size = mSecondaryOrientation.getDecoratedMeasurement(child); + if (size < maxSize) { + continue; + } + LayoutParams layoutParams = (LayoutParams) child.getLayoutParams(); + if (layoutParams.isFullSpan()) { + size = 1f * size / mSpanCount; + } + maxSize = Math.max(maxSize, size); + } + int before = mSizePerSpan; + int desired = Math.round(maxSize * mSpanCount); + if (mSecondaryOrientation.getMode() == View.MeasureSpec.AT_MOST) { + desired = Math.min(desired, mSecondaryOrientation.getTotalSpace()); + } + updateMeasureSpecs(desired); + if (mSizePerSpan == before) { + return; // nothing has changed + } + for (int i = 0; i < childCount; i ++) { + View child = getChildAt(i); + final LayoutParams lp = (LayoutParams) child.getLayoutParams(); + if (lp.mFullSpan) { + continue; + } + if (isLayoutRTL() && mOrientation == VERTICAL) { + int newOffset = -(mSpanCount - 1 - lp.mSpan.mIndex) * mSizePerSpan; + int prevOffset = -(mSpanCount - 1 - lp.mSpan.mIndex) * before; + child.offsetLeftAndRight(newOffset - prevOffset); + } else { + int newOffset = lp.mSpan.mIndex * mSizePerSpan; + int prevOffset = lp.mSpan.mIndex * before; + if (mOrientation == VERTICAL) { + child.offsetLeftAndRight(newOffset - prevOffset); + } else { + child.offsetTopAndBottom(newOffset - prevOffset); + } + } + } } private void applyPendingSavedState(AnchorInfo anchorInfo) { @@ -699,7 +848,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { // child anchorInfo.mPosition = mShouldReverseLayout ? getLastChildPosition() : getFirstChildPosition(); - if (mPendingScrollPositionOffset != INVALID_OFFSET) { if (anchorInfo.mLayoutFromEnd) { final int target = mPrimaryOrientation.getEndAfterPadding() - @@ -758,17 +906,11 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return true; } - void updateMeasureSpecs() { - mSizePerSpan = mSecondaryOrientation.getTotalSpace() / mSpanCount; + void updateMeasureSpecs(int totalSpace) { + mSizePerSpan = totalSpace / mSpanCount; + //noinspection ResourceType mFullSizeSpec = View.MeasureSpec.makeMeasureSpec( - mSecondaryOrientation.getTotalSpace(), View.MeasureSpec.EXACTLY); - if (mOrientation == VERTICAL) { - mWidthSpec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan, View.MeasureSpec.EXACTLY); - mHeightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); - } else { - mHeightSpec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan, View.MeasureSpec.EXACTLY); - mWidthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); - } + totalSpace, mSecondaryOrientation.getMode()); } @Override @@ -914,8 +1056,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return 0; } return ScrollbarHelper.computeScrollOffset(state, mPrimaryOrientation, - findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled) - , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled), + findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled, true) + , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled, true), this, mSmoothScrollbarEnabled, mShouldReverseLayout); } @@ -934,8 +1076,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return 0; } return ScrollbarHelper.computeScrollExtent(state, mPrimaryOrientation, - findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled) - , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled), + findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled, true) + , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled, true), this, mSmoothScrollbarEnabled); } @@ -954,8 +1096,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return 0; } return ScrollbarHelper.computeScrollRange(state, mPrimaryOrientation, - findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled) - , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled), + findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled, true) + , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled, true), this, mSmoothScrollbarEnabled); } @@ -964,27 +1106,48 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return computeScrollRange(state); } - private void measureChildWithDecorationsAndMargin(View child, LayoutParams lp) { + private void measureChildWithDecorationsAndMargin(View child, LayoutParams lp, + boolean alreadyMeasured) { if (lp.mFullSpan) { if (mOrientation == VERTICAL) { - measureChildWithDecorationsAndMargin(child, mFullSizeSpec, mHeightSpec); + measureChildWithDecorationsAndMargin(child, mFullSizeSpec, + getChildMeasureSpec(getHeight(), getHeightMode(), 0, lp.height, true), + alreadyMeasured); } else { - measureChildWithDecorationsAndMargin(child, mWidthSpec, mFullSizeSpec); + measureChildWithDecorationsAndMargin(child, + getChildMeasureSpec(getWidth(), getWidthMode(), 0, lp.width, true), + mFullSizeSpec, alreadyMeasured); } } else { - measureChildWithDecorationsAndMargin(child, mWidthSpec, mHeightSpec); + if (mOrientation == VERTICAL) { + measureChildWithDecorationsAndMargin(child, + getChildMeasureSpec(mSizePerSpan, getWidthMode(), 0, lp.width, false), + getChildMeasureSpec(getHeight(), getHeightMode(), 0, lp.height, true), + alreadyMeasured); + } else { + measureChildWithDecorationsAndMargin(child, + getChildMeasureSpec(getWidth(), getWidthMode(), 0, lp.width, true), + getChildMeasureSpec(mSizePerSpan, getHeightMode(), 0, lp.height, false), + alreadyMeasured); + } } } private void measureChildWithDecorationsAndMargin(View child, int widthSpec, - int heightSpec) { - final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child); + int heightSpec, boolean alreadyMeasured) { + calculateItemDecorationsForChild(child, mTmpRect); LayoutParams lp = (LayoutParams) child.getLayoutParams(); - widthSpec = updateSpecWithExtra(widthSpec, lp.leftMargin + insets.left, - lp.rightMargin + insets.right); - heightSpec = updateSpecWithExtra(heightSpec, lp.topMargin + insets.top, - lp.bottomMargin + insets.bottom); - child.measure(widthSpec, heightSpec); + widthSpec = updateSpecWithExtra(widthSpec, lp.leftMargin + mTmpRect.left, + lp.rightMargin + mTmpRect.right); + heightSpec = updateSpecWithExtra(heightSpec, lp.topMargin + mTmpRect.top, + lp.bottomMargin + mTmpRect.bottom); + final boolean measure = alreadyMeasured + ? shouldReMeasureChild(child, widthSpec, heightSpec, lp) + : shouldMeasureChild(child, widthSpec, heightSpec, lp); + if (measure) { + child.measure(widthSpec, heightSpec); + } + } private int updateSpecWithExtra(int spec, int startInset, int endInset) { @@ -994,7 +1157,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { final int mode = View.MeasureSpec.getMode(spec); if (mode == View.MeasureSpec.AT_MOST || mode == View.MeasureSpec.EXACTLY) { return View.MeasureSpec.makeMeasureSpec( - View.MeasureSpec.getSize(spec) - startInset - endInset, mode); + Math.max(0, View.MeasureSpec.getSize(spec) - startInset - endInset), mode); } return spec; } @@ -1087,8 +1250,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { if (getChildCount() > 0) { final AccessibilityRecordCompat record = AccessibilityEventCompat .asRecord(event); - final View start = findFirstVisibleItemClosestToStart(false); - final View end = findFirstVisibleItemClosestToEnd(false); + final View start = findFirstVisibleItemClosestToStart(false, true); + final View end = findFirstVisibleItemClosestToEnd(false, true); if (start == null || end == null) { return; } @@ -1106,11 +1269,12 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { /** * Finds the first fully visible child to be used as an anchor child if span count changes when - * state is restored. + * state is restored. If no children is fully visible, returns a partially visible child instead + * of returning null. */ int findFirstVisibleItemPositionInt() { - final View first = mShouldReverseLayout ? findFirstVisibleItemClosestToEnd(true) : - findFirstVisibleItemClosestToStart(true); + final View first = mShouldReverseLayout ? findFirstVisibleItemClosestToEnd(true, true) : + findFirstVisibleItemClosestToStart(true, true); return first == null ? NO_POSITION : getPosition(first); } @@ -1132,36 +1296,71 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return super.getColumnCountForAccessibility(recycler, state); } - View findFirstVisibleItemClosestToStart(boolean fullyVisible) { + /** + * This is for internal use. Not necessarily the child closest to start but the first child + * we find that matches the criteria. + * This method does not do any sorting based on child's start coordinate, instead, it uses + * children order. + */ + View findFirstVisibleItemClosestToStart(boolean fullyVisible, boolean acceptPartiallyVisible) { final int boundsStart = mPrimaryOrientation.getStartAfterPadding(); final int boundsEnd = mPrimaryOrientation.getEndAfterPadding(); final int limit = getChildCount(); - for (int i = 0; i < limit; i ++) { + View partiallyVisible = null; + for (int i = 0; i < limit; i++) { final View child = getChildAt(i); - if ((!fullyVisible || mPrimaryOrientation.getDecoratedStart(child) >= boundsStart) - && mPrimaryOrientation.getDecoratedEnd(child) <= boundsEnd) { + final int childStart = mPrimaryOrientation.getDecoratedStart(child); + final int childEnd = mPrimaryOrientation.getDecoratedEnd(child); + if(childEnd <= boundsStart || childStart >= boundsEnd) { + continue; // not visible at all + } + if (childStart >= boundsStart || !fullyVisible) { + // when checking for start, it is enough even if part of the child's top is visible + // as long as fully visible is not requested. return child; } + if (acceptPartiallyVisible && partiallyVisible == null) { + partiallyVisible = child; + } } - return null; + return partiallyVisible; } - View findFirstVisibleItemClosestToEnd(boolean fullyVisible) { + /** + * This is for internal use. Not necessarily the child closest to bottom but the first child + * we find that matches the criteria. + * This method does not do any sorting based on child's end coordinate, instead, it uses + * children order. + */ + View findFirstVisibleItemClosestToEnd(boolean fullyVisible, boolean acceptPartiallyVisible) { final int boundsStart = mPrimaryOrientation.getStartAfterPadding(); final int boundsEnd = mPrimaryOrientation.getEndAfterPadding(); - for (int i = getChildCount() - 1; i >= 0; i --) { + View partiallyVisible = null; + for (int i = getChildCount() - 1; i >= 0; i--) { final View child = getChildAt(i); - if (mPrimaryOrientation.getDecoratedStart(child) >= boundsStart && (!fullyVisible - || mPrimaryOrientation.getDecoratedEnd(child) <= boundsEnd)) { + final int childStart = mPrimaryOrientation.getDecoratedStart(child); + final int childEnd = mPrimaryOrientation.getDecoratedEnd(child); + if(childEnd <= boundsStart || childStart >= boundsEnd) { + continue; // not visible at all + } + if (childEnd <= boundsEnd || !fullyVisible) { + // when checking for end, it is enough even if part of the child's bottom is visible + // as long as fully visible is not requested. return child; } + if (acceptPartiallyVisible && partiallyVisible == null) { + partiallyVisible = child; + } } - return null; + return partiallyVisible; } private void fixEndGap(RecyclerView.Recycler recycler, RecyclerView.State state, boolean canOffsetChildren) { - final int maxEndLine = getMaxEnd(mPrimaryOrientation.getEndAfterPadding()); + final int maxEndLine = getMaxEnd(Integer.MIN_VALUE); + if (maxEndLine == Integer.MIN_VALUE) { + return; + } int gap = mPrimaryOrientation.getEndAfterPadding() - maxEndLine; int fixOffset; if (gap > 0) { @@ -1177,7 +1376,10 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { private void fixStartGap(RecyclerView.Recycler recycler, RecyclerView.State state, boolean canOffsetChildren) { - final int minStartLine = getMinStart(mPrimaryOrientation.getStartAfterPadding()); + final int minStartLine = getMinStart(Integer.MAX_VALUE); + if (minStartLine == Integer.MAX_VALUE) { + return; + } int gap = minStartLine - mPrimaryOrientation.getStartAfterPadding(); int fixOffset; if (gap > 0) { @@ -1191,40 +1393,41 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } } - private void updateLayoutStateToFillStart(int anchorPosition, RecyclerView.State state) { + private void updateLayoutState(int anchorPosition, RecyclerView.State state) { mLayoutState.mAvailable = 0; mLayoutState.mCurrentPosition = anchorPosition; + int startExtra = 0; + int endExtra = 0; if (isSmoothScrolling()) { final int targetPos = state.getTargetScrollPosition(); - if (mShouldReverseLayout == targetPos < anchorPosition) { - mLayoutState.mExtra = 0; - } else { - mLayoutState.mExtra = mPrimaryOrientation.getTotalSpace(); + if (targetPos != NO_POSITION) { + if (mShouldReverseLayout == targetPos < anchorPosition) { + endExtra = mPrimaryOrientation.getTotalSpace(); + } else { + startExtra = mPrimaryOrientation.getTotalSpace(); + } } - } else { - mLayoutState.mExtra = 0; } - mLayoutState.mLayoutDirection = LAYOUT_START; - mLayoutState.mItemDirection = mShouldReverseLayout ? ITEM_DIRECTION_TAIL - : ITEM_DIRECTION_HEAD; + + // Line of the furthest row. + final boolean clipToPadding = getClipToPadding(); + if (clipToPadding) { + mLayoutState.mStartLine = mPrimaryOrientation.getStartAfterPadding() - startExtra; + mLayoutState.mEndLine = mPrimaryOrientation.getEndAfterPadding() + endExtra; + } else { + mLayoutState.mEndLine = mPrimaryOrientation.getEnd() + endExtra; + mLayoutState.mStartLine = -startExtra; + } + mLayoutState.mStopInFocusable = false; + mLayoutState.mRecycle = true; + mLayoutState.mInfinite = mPrimaryOrientation.getMode() == View.MeasureSpec.UNSPECIFIED && + mPrimaryOrientation.getEnd() == 0; } - private void updateLayoutStateToFillEnd(int anchorPosition, RecyclerView.State state) { - mLayoutState.mAvailable = 0; - mLayoutState.mCurrentPosition = anchorPosition; - if (isSmoothScrolling()) { - final int targetPos = state.getTargetScrollPosition(); - if (mShouldReverseLayout == targetPos > anchorPosition) { - mLayoutState.mExtra = 0; - } else { - mLayoutState.mExtra = mPrimaryOrientation.getTotalSpace(); - } - } else { - mLayoutState.mExtra = 0; - } - mLayoutState.mLayoutDirection = LAYOUT_END; - mLayoutState.mItemDirection = mShouldReverseLayout ? ITEM_DIRECTION_HEAD - : ITEM_DIRECTION_TAIL; + private void setLayoutStateDirection(int direction) { + mLayoutState.mLayoutDirection = direction; + mLayoutState.mItemDirection = (mShouldReverseLayout == (direction == LAYOUT_START)) ? + ITEM_DIRECTION_TAIL : ITEM_DIRECTION_HEAD; } @Override @@ -1265,7 +1468,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } @Override - public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount) { + public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount, + Object payload) { handleUpdate(positionStart, itemCount, AdapterHelper.UpdateOp.UPDATE); } @@ -1274,7 +1478,23 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { */ private void handleUpdate(int positionStart, int itemCountOrToPosition, int cmd) { int minPosition = mShouldReverseLayout ? getLastChildPosition() : getFirstChildPosition(); - mLazySpanLookup.invalidateAfter(positionStart); + final int affectedRangeEnd;// exclusive + final int affectedRangeStart;// inclusive + + if (cmd == AdapterHelper.UpdateOp.MOVE) { + if (positionStart < itemCountOrToPosition) { + affectedRangeEnd = itemCountOrToPosition + 1; + affectedRangeStart = positionStart; + } else { + affectedRangeEnd = positionStart + 1; + affectedRangeStart = itemCountOrToPosition; + } + } else { + affectedRangeStart = positionStart; + affectedRangeEnd = positionStart + itemCountOrToPosition; + } + + mLazySpanLookup.invalidateAfter(affectedRangeStart); switch (cmd) { case AdapterHelper.UpdateOp.ADD: mLazySpanLookup.offsetForAddition(positionStart, itemCountOrToPosition); @@ -1289,12 +1509,12 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { break; } - if (positionStart + itemCountOrToPosition <= minPosition) { + if (affectedRangeEnd <= minPosition) { return; - } + int maxPosition = mShouldReverseLayout ? getFirstChildPosition() : getLastChildPosition(); - if (positionStart <= maxPosition) { + if (affectedRangeStart <= maxPosition) { requestLayout(); } } @@ -1304,45 +1524,41 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { mRemainingSpans.set(0, mSpanCount, true); // The target position we are trying to reach. final int targetLine; - /* - * The line until which we can recycle, as long as we add views. - * Keep in mind, it is still the line in layout direction which means; to calculate the - * actual recycle line, we should subtract/add the size in orientation. - */ - final int recycleLine; - // Line of the furthest row. - if (layoutState.mLayoutDirection == LAYOUT_END) { - // ignore padding for recycler - recycleLine = mPrimaryOrientation.getEndAfterPadding() + mLayoutState.mAvailable; - targetLine = recycleLine + mLayoutState.mExtra + mPrimaryOrientation.getEndPadding(); - } else { // LAYOUT_START - // ignore padding for recycler - recycleLine = mPrimaryOrientation.getStartAfterPadding() - mLayoutState.mAvailable; - targetLine = recycleLine - mLayoutState.mExtra - - mPrimaryOrientation.getStartAfterPadding(); + // Line of the furthest row. + if (mLayoutState.mInfinite) { + if (layoutState.mLayoutDirection == LAYOUT_END) { + targetLine = Integer.MAX_VALUE; + } else { // LAYOUT_START + targetLine = Integer.MIN_VALUE; + } + } else { + if (layoutState.mLayoutDirection == LAYOUT_END) { + targetLine = layoutState.mEndLine + layoutState.mAvailable; + } else { // LAYOUT_START + targetLine = layoutState.mStartLine - layoutState.mAvailable; + } } + updateAllRemainingSpans(layoutState.mLayoutDirection, targetLine); + if (DEBUG) { + Log.d(TAG, "FILLING targetLine: " + targetLine + "," + + "remaining spans:" + mRemainingSpans + ", state: " + layoutState); + } // the default coordinate to add new view. final int defaultNewViewLine = mShouldReverseLayout ? mPrimaryOrientation.getEndAfterPadding() : mPrimaryOrientation.getStartAfterPadding(); - - while (layoutState.hasMore(state) && !mRemainingSpans.isEmpty()) { + boolean added = false; + while (layoutState.hasMore(state) + && (mLayoutState.mInfinite || !mRemainingSpans.isEmpty())) { View view = layoutState.next(recycler); LayoutParams lp = ((LayoutParams) view.getLayoutParams()); - if (layoutState.mLayoutDirection == LAYOUT_END) { - addView(view); - } else { - addView(view, 0); - } - measureChildWithDecorationsAndMargin(view, lp); - - final int position = lp.getViewPosition(); + final int position = lp.getViewLayoutPosition(); final int spanIndex = mLazySpanLookup.getSpan(position); Span currentSpan; - boolean assignSpan = spanIndex == LayoutParams.INVALID_SPAN_ID; + final boolean assignSpan = spanIndex == LayoutParams.INVALID_SPAN_ID; if (assignSpan) { currentSpan = lp.mFullSpan ? mSpans[0] : getNextSpan(layoutState); mLazySpanLookup.setSpan(position, currentSpan); @@ -1355,9 +1571,17 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } currentSpan = mSpans[spanIndex]; } + // assign span before measuring so that item decorators can get updated span index + lp.mSpan = currentSpan; + if (layoutState.mLayoutDirection == LAYOUT_END) { + addView(view); + } else { + addView(view, 0); + } + measureChildWithDecorationsAndMargin(view, lp, false); + final int start; final int end; - if (layoutState.mLayoutDirection == LAYOUT_END) { start = lp.mFullSpan ? getMaxEnd(defaultNewViewLine) : currentSpan.getEndLine(defaultNewViewLine); @@ -1383,16 +1607,41 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } // check if this item may create gaps in the future - if (lp.mFullSpan && layoutState.mItemDirection == ITEM_DIRECTION_HEAD && assignSpan) { - mLaidOutInvalidFullSpan = true; + if (lp.mFullSpan && layoutState.mItemDirection == ITEM_DIRECTION_HEAD) { + if (assignSpan) { + mLaidOutInvalidFullSpan = true; + } else { + final boolean hasInvalidGap; + if (layoutState.mLayoutDirection == LAYOUT_END) { + hasInvalidGap = !areAllEndsEqual(); + } else { // layoutState.mLayoutDirection == LAYOUT_START + hasInvalidGap = !areAllStartsEqual(); + } + if (hasInvalidGap) { + final LazySpanLookup.FullSpanItem fullSpanItem = mLazySpanLookup + .getFullSpanItem(position); + if (fullSpanItem != null) { + fullSpanItem.mHasUnwantedGapAfter = true; + } + mLaidOutInvalidFullSpan = true; + } + } + } + attachViewToSpans(view, lp, layoutState); + final int otherStart; + final int otherEnd; + if (isLayoutRTL() && mOrientation == VERTICAL) { + otherEnd = lp.mFullSpan ? mSecondaryOrientation.getEndAfterPadding() : + mSecondaryOrientation.getEndAfterPadding() + - (mSpanCount - 1 - currentSpan.mIndex) * mSizePerSpan; + otherStart = otherEnd - mSecondaryOrientation.getDecoratedMeasurement(view); + } else { + otherStart = lp.mFullSpan ? mSecondaryOrientation.getStartAfterPadding() + : currentSpan.mIndex * mSizePerSpan + + mSecondaryOrientation.getStartAfterPadding(); + otherEnd = otherStart + mSecondaryOrientation.getDecoratedMeasurement(view); } - lp.mSpan = currentSpan; - attachViewToSpans(view, lp, layoutState); - final int otherStart = lp.mFullSpan ? mSecondaryOrientation.getStartAfterPadding() - : currentSpan.mIndex * mSizePerSpan + - mSecondaryOrientation.getStartAfterPadding(); - final int otherEnd = otherStart + mSecondaryOrientation.getDecoratedMeasurement(view); if (mOrientation == VERTICAL) { layoutDecoratedWithMargins(view, otherStart, start, otherEnd, end); } else { @@ -1404,18 +1653,28 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } else { updateRemainingSpans(currentSpan, mLayoutState.mLayoutDirection, targetLine); } - recycle(recycler, mLayoutState, currentSpan, recycleLine); + recycle(recycler, mLayoutState); + if (mLayoutState.mStopInFocusable && view.isFocusable()) { + if (lp.mFullSpan) { + mRemainingSpans.clear(); + } else { + mRemainingSpans.set(currentSpan.mIndex, false); + } + } + added = true; } - if (DEBUG) { - Log.d(TAG, "fill, " + getChildCount()); + if (!added) { + recycle(recycler, mLayoutState); } + final int diff; if (mLayoutState.mLayoutDirection == LAYOUT_START) { final int minStart = getMinStart(mPrimaryOrientation.getStartAfterPadding()); - return Math.max(0, mLayoutState.mAvailable + (recycleLine - minStart)); + diff = mPrimaryOrientation.getStartAfterPadding() - minStart; } else { - final int max = getMaxEnd(mPrimaryOrientation.getEndAfterPadding()); - return Math.max(0, mLayoutState.mAvailable + (max - recycleLine)); + final int maxEnd = getMaxEnd(mPrimaryOrientation.getEndAfterPadding()); + diff = maxEnd - mPrimaryOrientation.getEndAfterPadding(); } + return diff > 0 ? Math.min(layoutState.mAvailable, diff) : 0; } private LazySpanLookup.FullSpanItem createFullSpanItemFromEnd(int newItemTop) { @@ -1452,19 +1711,43 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } } - private void recycle(RecyclerView.Recycler recycler, LayoutState layoutState, - Span updatedSpan, int recycleLine) { - if (layoutState.mLayoutDirection == LAYOUT_START) { - // calculate recycle line - int maxStart = getMaxStart(updatedSpan.getStartLine()); - recycleFromEnd(recycler, Math.max(recycleLine, maxStart) + - (mPrimaryOrientation.getEnd() - mPrimaryOrientation.getStartAfterPadding())); - } else { - // calculate recycle line - int minEnd = getMinEnd(updatedSpan.getEndLine()); - recycleFromStart(recycler, Math.min(recycleLine, minEnd) - - (mPrimaryOrientation.getEnd() - mPrimaryOrientation.getStartAfterPadding())); + private void recycle(RecyclerView.Recycler recycler, LayoutState layoutState) { + if (!layoutState.mRecycle || layoutState.mInfinite) { + return; } + if (layoutState.mAvailable == 0) { + // easy, recycle line is still valid + if (layoutState.mLayoutDirection == LAYOUT_START) { + recycleFromEnd(recycler, layoutState.mEndLine); + } else { + recycleFromStart(recycler, layoutState.mStartLine); + } + } else { + // scrolling case, recycle line can be shifted by how much space we could cover + // by adding new views + if (layoutState.mLayoutDirection == LAYOUT_START) { + // calculate recycle line + int scrolled = layoutState.mStartLine - getMaxStart(layoutState.mStartLine); + final int line; + if (scrolled < 0) { + line = layoutState.mEndLine; + } else { + line = layoutState.mEndLine - Math.min(scrolled, layoutState.mAvailable); + } + recycleFromEnd(recycler, line); + } else { + // calculate recycle line + int scrolled = getMinEnd(layoutState.mEndLine) - layoutState.mEndLine; + final int line; + if (scrolled < 0) { + line = layoutState.mStartLine; + } else { + line = layoutState.mStartLine + Math.min(scrolled, layoutState.mAvailable); + } + recycleFromStart(recycler, line); + } + } + } private void appendViewToAllSpans(View view) { @@ -1481,18 +1764,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } } - private void layoutDecoratedWithMargins(View child, int left, int top, int right, int bottom) { - LayoutParams lp = (LayoutParams) child.getLayoutParams(); - if (DEBUG) { - Log.d(TAG, "layout decorated pos: " + lp.getViewPosition() + ", span:" - + lp.getSpanIndex() + ", fullspan:" + lp.mFullSpan - + ". l:" + left + ",t:" + top - + ", r:" + right + ", b:" + bottom); - } - layoutDecorated(child, left + lp.leftMargin, top + lp.topMargin, right - lp.rightMargin - , bottom - lp.bottomMargin); - } - private void updateAllRemainingSpans(int layoutDir, int targetLine) { for (int i = 0; i < mSpanCount; i++) { if (mSpans[i].mViews.isEmpty()) { @@ -1506,12 +1777,12 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { final int deletedSize = span.getDeletedSize(); if (layoutDir == LAYOUT_START) { final int line = span.getStartLine(); - if (line + deletedSize < targetLine) { + if (line + deletedSize <= targetLine) { mRemainingSpans.set(span.mIndex, false); } } else { final int line = span.getEndLine(); - if (line - deletedSize > targetLine) { + if (line - deletedSize >= targetLine) { mRemainingSpans.set(span.mIndex, false); } } @@ -1539,6 +1810,26 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return minStart; } + boolean areAllEndsEqual() { + int end = mSpans[0].getEndLine(Span.INVALID_LINE); + for (int i = 1; i < mSpanCount; i++) { + if (mSpans[i].getEndLine(Span.INVALID_LINE) != end) { + return false; + } + } + return true; + } + + boolean areAllStartsEqual() { + int start = mSpans[0].getStartLine(Span.INVALID_LINE); + for (int i = 1; i < mSpanCount; i++) { + if (mSpans[i].getStartLine(Span.INVALID_LINE) != start) { + return false; + } + } + return true; + } + private int getMaxEnd(int def) { int maxEnd = mSpans[0].getEndLine(def); for (int i = 1; i < mSpanCount; i++) { @@ -1562,18 +1853,25 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } private void recycleFromStart(RecyclerView.Recycler recycler, int line) { - if (DEBUG) { - Log.d(TAG, "recycling from start for line " + line); - } while (getChildCount() > 0) { View child = getChildAt(0); - if (mPrimaryOrientation.getDecoratedEnd(child) < line) { + if (mPrimaryOrientation.getDecoratedEnd(child) <= line && + mPrimaryOrientation.getTransformedEndWithDecoration(child) <= line) { LayoutParams lp = (LayoutParams) child.getLayoutParams(); + // Don't recycle the last View in a span not to lose span's start/end lines if (lp.mFullSpan) { + for (int j = 0; j < mSpanCount; j++) { + if (mSpans[j].mViews.size() == 1) { + return; + } + } for (int j = 0; j < mSpanCount; j++) { mSpans[j].popStart(); } } else { + if (lp.mSpan.mViews.size() == 1) { + return; + } lp.mSpan.popStart(); } removeAndRecycleView(child, recycler); @@ -1588,13 +1886,23 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { int i; for (i = childCount - 1; i >= 0; i--) { View child = getChildAt(i); - if (mPrimaryOrientation.getDecoratedStart(child) > line) { + if (mPrimaryOrientation.getDecoratedStart(child) >= line && + mPrimaryOrientation.getTransformedStartWithDecoration(child) >= line) { LayoutParams lp = (LayoutParams) child.getLayoutParams(); + // Don't recycle the last View in a span not to lose span's start/end lines if (lp.mFullSpan) { + for (int j = 0; j < mSpanCount; j++) { + if (mSpans[j].mViews.size() == 1) { + return; + } + } for (int j = 0; j < mSpanCount; j++) { mSpans[j].popEnd(); } } else { + if (lp.mSpan.mViews.size() == 1) { + return; + } lp.mSpan.popEnd(); } removeAndRecycleView(child, recycler); @@ -1688,23 +1996,27 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return position < firstChildPos != mShouldReverseLayout ? LAYOUT_START : LAYOUT_END; } + @Override + public PointF computeScrollVectorForPosition(int targetPosition) { + final int direction = calculateScrollDirectionForPosition(targetPosition); + PointF outVector = new PointF(); + if (direction == 0) { + return null; + } + if (mOrientation == HORIZONTAL) { + outVector.x = direction; + outVector.y = 0; + } else { + outVector.x = 0; + outVector.y = direction; + } + return outVector; + } + @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { - LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) { - @Override - public PointF computeScrollVectorForPosition(int targetPosition) { - final int direction = calculateScrollDirectionForPosition(targetPosition); - if (direction == 0) { - return null; - } - if (mOrientation == HORIZONTAL) { - return new PointF(direction, 0); - } else { - return new PointF(0, direction); - } - } - }; + LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()); scroller.setTargetPosition(position); startSmoothScroll(scroller); } @@ -1742,23 +2054,21 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } int scrollBy(int dt, RecyclerView.Recycler recycler, RecyclerView.State state) { - ensureOrientationHelper(); final int referenceChildPosition; + final int layoutDir; if (dt > 0) { // layout towards end - mLayoutState.mLayoutDirection = LAYOUT_END; - mLayoutState.mItemDirection = mShouldReverseLayout ? ITEM_DIRECTION_HEAD - : ITEM_DIRECTION_TAIL; + layoutDir = LAYOUT_END; referenceChildPosition = getLastChildPosition(); } else { - mLayoutState.mLayoutDirection = LAYOUT_START; - mLayoutState.mItemDirection = mShouldReverseLayout ? ITEM_DIRECTION_TAIL - : ITEM_DIRECTION_HEAD; + layoutDir = LAYOUT_START; referenceChildPosition = getFirstChildPosition(); } + mLayoutState.mRecycle = true; + updateLayoutState(referenceChildPosition, state); + setLayoutStateDirection(layoutDir); mLayoutState.mCurrentPosition = referenceChildPosition + mLayoutState.mItemDirection; final int absDt = Math.abs(dt); mLayoutState.mAvailable = absDt; - mLayoutState.mExtra = isSmoothScrolling() ? mPrimaryOrientation.getTotalSpace() : 0; int consumed = fill(recycler, mLayoutState, state); final int totalScroll; if (absDt < consumed) { @@ -1821,10 +2131,16 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return 0; } + @SuppressWarnings("deprecation") @Override public RecyclerView.LayoutParams generateDefaultLayoutParams() { - return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, - ViewGroup.LayoutParams.WRAP_CONTENT); + if (mOrientation == HORIZONTAL) { + return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.MATCH_PARENT); + } else { + return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + } } @Override @@ -1850,9 +2166,123 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return mOrientation; } + @Nullable + @Override + public View onFocusSearchFailed(View focused, int direction, RecyclerView.Recycler recycler, + RecyclerView.State state) { + if (getChildCount() == 0) { + return null; + } + + final View directChild = findContainingItemView(focused); + if (directChild == null) { + return null; + } + + resolveShouldLayoutReverse(); + final int layoutDir = convertFocusDirectionToLayoutDirection(direction); + if (layoutDir == LayoutState.INVALID_LAYOUT) { + return null; + } + LayoutParams prevFocusLayoutParams = (LayoutParams) directChild.getLayoutParams(); + boolean prevFocusFullSpan = prevFocusLayoutParams.mFullSpan; + final Span prevFocusSpan = prevFocusLayoutParams.mSpan; + final int referenceChildPosition; + if (layoutDir == LAYOUT_END) { // layout towards end + referenceChildPosition = getLastChildPosition(); + } else { + referenceChildPosition = getFirstChildPosition(); + } + updateLayoutState(referenceChildPosition, state); + setLayoutStateDirection(layoutDir); + + mLayoutState.mCurrentPosition = referenceChildPosition + mLayoutState.mItemDirection; + mLayoutState.mAvailable = (int) (MAX_SCROLL_FACTOR * mPrimaryOrientation.getTotalSpace()); + mLayoutState.mStopInFocusable = true; + mLayoutState.mRecycle = false; + fill(recycler, mLayoutState, state); + mLastLayoutFromEnd = mShouldReverseLayout; + if (!prevFocusFullSpan) { + View view = prevFocusSpan.getFocusableViewAfter(referenceChildPosition, layoutDir); + if (view != null && view != directChild) { + return view; + } + } + // either could not find from the desired span or prev view is full span. + // traverse all spans + if (preferLastSpan(layoutDir)) { + for (int i = mSpanCount - 1; i >= 0; i --) { + View view = mSpans[i].getFocusableViewAfter(referenceChildPosition, layoutDir); + if (view != null && view != directChild) { + return view; + } + } + } else { + for (int i = 0; i < mSpanCount; i ++) { + View view = mSpans[i].getFocusableViewAfter(referenceChildPosition, layoutDir); + if (view != null && view != directChild) { + return view; + } + } + } + return null; + } + + /** + * Converts a focusDirection to orientation. + * + * @param focusDirection One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN}, + * {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT}, + * {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD} + * or 0 for not applicable + * @return {@link LayoutState#LAYOUT_START} or {@link LayoutState#LAYOUT_END} if focus direction + * is applicable to current state, {@link LayoutState#INVALID_LAYOUT} otherwise. + */ + private int convertFocusDirectionToLayoutDirection(int focusDirection) { + switch (focusDirection) { + case View.FOCUS_BACKWARD: + if (mOrientation == VERTICAL) { + return LayoutState.LAYOUT_START; + } else if (isLayoutRTL()) { + return LayoutState.LAYOUT_END; + } else { + return LayoutState.LAYOUT_START; + } + case View.FOCUS_FORWARD: + if (mOrientation == VERTICAL) { + return LayoutState.LAYOUT_END; + } else if (isLayoutRTL()) { + return LayoutState.LAYOUT_START; + } else { + return LayoutState.LAYOUT_END; + } + case View.FOCUS_UP: + return mOrientation == VERTICAL ? LayoutState.LAYOUT_START + : LayoutState.INVALID_LAYOUT; + case View.FOCUS_DOWN: + return mOrientation == VERTICAL ? LayoutState.LAYOUT_END + : LayoutState.INVALID_LAYOUT; + case View.FOCUS_LEFT: + return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_START + : LayoutState.INVALID_LAYOUT; + case View.FOCUS_RIGHT: + return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_END + : LayoutState.INVALID_LAYOUT; + default: + if (DEBUG) { + Log.d(TAG, "Unknown focus request:" + focusDirection); + } + return LayoutState.INVALID_LAYOUT; + } + + } /** * LayoutParams used by StaggeredGridLayoutManager. + *

      + * Note that if the orientation is {@link #VERTICAL}, the width parameter is ignored and if the + * orientation is {@link #HORIZONTAL} the height parameter is ignored because child view is + * expected to fill all of the space given to it. */ public static class LayoutParams extends RecyclerView.LayoutParams { @@ -1926,7 +2356,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { class Span { static final int INVALID_LINE = Integer.MIN_VALUE; - private ArrayList mViews = new ArrayList(); + private ArrayList mViews = new ArrayList<>(); int mCachedStart = INVALID_LINE; int mCachedEnd = INVALID_LINE; int mDeletedSize = 0; @@ -1953,7 +2383,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { mCachedStart = mPrimaryOrientation.getDecoratedStart(startView); if (lp.mFullSpan) { LazySpanLookup.FullSpanItem fsi = mLazySpanLookup - .getFullSpanItem(lp.getViewPosition()); + .getFullSpanItem(lp.getViewLayoutPosition()); if (fsi != null && fsi.mGapDir == LAYOUT_START) { mCachedStart -= fsi.getGapForSpan(mIndex); } @@ -1987,7 +2417,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { mCachedEnd = mPrimaryOrientation.getDecoratedEnd(endView); if (lp.mFullSpan) { LazySpanLookup.FullSpanItem fsi = mLazySpanLookup - .getFullSpanItem(lp.getViewPosition()); + .getFullSpanItem(lp.getViewLayoutPosition()); if (fsi != null && fsi.mGapDir == LAYOUT_END) { mCachedEnd += fsi.getGapForSpan(mIndex); } @@ -2042,7 +2472,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return; } if ((reverseLayout && reference < mPrimaryOrientation.getEndAfterPadding()) || - (!reverseLayout && reference > mPrimaryOrientation.getStartAfterPadding()) ) { + (!reverseLayout && reference > mPrimaryOrientation.getStartAfterPadding())) { return; } if (offset != INVALID_OFFSET) { @@ -2110,67 +2540,28 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } } - // normalized offset is how much this span can scroll - int getNormalizedOffset(int dt, int targetStart, int targetEnd) { - if (mViews.size() == 0) { - return 0; - } - if (dt < 0) { - final int endSpace = getEndLine() - targetEnd; - if (endSpace <= 0) { - return 0; - } - return -dt > endSpace ? -endSpace : dt; - } else { - final int startSpace = targetStart - getStartLine(); - if (startSpace <= 0) { - return 0; - } - return startSpace < dt ? startSpace : dt; - } - } - - /** - * Returns if there is no child between start-end lines - * - * @param start The start line - * @param end The end line - * @return true if a new child can be added between start and end - */ - boolean isEmpty(int start, int end) { - final int count = mViews.size(); - for (int i = 0; i < count; i++) { - final View view = mViews.get(i); - if (mPrimaryOrientation.getDecoratedStart(view) < end && - mPrimaryOrientation.getDecoratedEnd(view) > start) { - return false; - } - } - return true; - } - public int findFirstVisibleItemPosition() { return mReverseLayout - ? findOneVisibleChild(mViews.size() -1, -1, false) + ? findOneVisibleChild(mViews.size() - 1, -1, false) : findOneVisibleChild(0, mViews.size(), false); } public int findFirstCompletelyVisibleItemPosition() { return mReverseLayout - ? findOneVisibleChild(mViews.size() -1, -1, true) + ? findOneVisibleChild(mViews.size() - 1, -1, true) : findOneVisibleChild(0, mViews.size(), true); } public int findLastVisibleItemPosition() { return mReverseLayout ? findOneVisibleChild(0, mViews.size(), false) - : findOneVisibleChild(mViews.size() -1, -1, false); + : findOneVisibleChild(mViews.size() - 1, -1, false); } public int findLastCompletelyVisibleItemPosition() { return mReverseLayout ? findOneVisibleChild(0, mViews.size(), true) - : findOneVisibleChild(mViews.size() -1, -1, true); + : findOneVisibleChild(mViews.size() - 1, -1, true); } int findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible) { @@ -2193,6 +2584,36 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } return NO_POSITION; } + + /** + * Depending on the layout direction, returns the View that is after the given position. + */ + public View getFocusableViewAfter(int referenceChildPosition, int layoutDir) { + View candidate = null; + if (layoutDir == LAYOUT_START) { + final int limit = mViews.size(); + for (int i = 0; i < limit; i++) { + final View view = mViews.get(i); + if (view.isFocusable() && + (getPosition(view) > referenceChildPosition == mReverseLayout) ) { + candidate = view; + } else { + break; + } + } + } else { + for (int i = mViews.size() - 1; i >= 0; i--) { + final View view = mViews.get(i); + if (view.isFocusable() && + (getPosition(view) > referenceChildPosition == !mReverseLayout)) { + candidate = view; + } else { + break; + } + } + } + return candidate; + } } /** @@ -2203,7 +2624,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { private static final int MIN_SIZE = 10; int[] mData; - int mAdapterSize; // we don't want to grow beyond that, unless it grows List mFullSpanItems; @@ -2261,9 +2681,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { while (len <= position) { len *= 2; } - if (len > mAdapterSize) { - len = mAdapterSize; - } return len; } @@ -2373,7 +2790,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { public void addFullSpanItem(FullSpanItem fullSpanItem) { if (mFullSpanItems == null) { - mFullSpanItems = new ArrayList(); + mFullSpanItems = new ArrayList<>(); } final int size = mFullSpanItems.size(); for (int i = 0; i < size; i++) { @@ -2411,17 +2828,23 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { * @param minPos inclusive * @param maxPos exclusive * @param gapDir if not 0, returns FSIs on in that direction + * @param hasUnwantedGapAfter If true, when full span item has unwanted gaps, it will be + * returned even if its gap direction does not match. */ - public FullSpanItem getFirstFullSpanItemInRange(int minPos, int maxPos, int gapDir) { + public FullSpanItem getFirstFullSpanItemInRange(int minPos, int maxPos, int gapDir, + boolean hasUnwantedGapAfter) { if (mFullSpanItems == null) { return null; } - for (int i = 0; i < mFullSpanItems.size(); i++) { + final int limit = mFullSpanItems.size(); + for (int i = 0; i < limit; i++) { FullSpanItem fsi = mFullSpanItems.get(i); if (fsi.mPosition >= maxPos) { return null; } - if (fsi.mPosition >= minPos && (gapDir == 0 || fsi.mGapDir == gapDir)) { + if (fsi.mPosition >= minPos + && (gapDir == 0 || fsi.mGapDir == gapDir || + (hasUnwantedGapAfter && fsi.mHasUnwantedGapAfter))) { return fsi; } } @@ -2436,10 +2859,15 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { int mPosition; int mGapDir; int[] mGapPerSpan; + // A full span may be laid out in primary direction but may have gaps due to + // invalidation of views after it. This is recorded during a reverse scroll and if + // view is still on the screen after scroll stops, we have to recalculate layout + boolean mHasUnwantedGapAfter; public FullSpanItem(Parcel in) { mPosition = in.readInt(); mGapDir = in.readInt(); + mHasUnwantedGapAfter = in.readInt() == 1; int spanCount = in.readInt(); if (spanCount > 0) { mGapPerSpan = new int[spanCount]; @@ -2454,10 +2882,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return mGapPerSpan == null ? 0 : mGapPerSpan[spanIndex]; } - public void invalidateSpanGaps() { - mGapPerSpan = null; - } - @Override public int describeContents() { return 0; @@ -2467,6 +2891,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { public void writeToParcel(Parcel dest, int flags) { dest.writeInt(mPosition); dest.writeInt(mGapDir); + dest.writeInt(mHasUnwantedGapAfter ? 1 : 0); if (mGapPerSpan != null && mGapPerSpan.length > 0) { dest.writeInt(mGapPerSpan.length); dest.writeIntArray(mGapPerSpan); @@ -2480,12 +2905,13 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { return "FullSpanItem{" + "mPosition=" + mPosition + ", mGapDir=" + mGapDir + + ", mHasUnwantedGapAfter=" + mHasUnwantedGapAfter + ", mGapPerSpan=" + Arrays.toString(mGapPerSpan) + '}'; } - public static final Creator CREATOR - = new Creator() { + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { @Override public FullSpanItem createFromParcel(Parcel in) { return new FullSpanItem(in); @@ -2499,7 +2925,10 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { } } - static class SavedState implements Parcelable { + /** + * @hide + */ + public static class SavedState implements Parcelable { int mAnchorPosition; int mVisibleAnchorPosition; // Replacement for span info when spans are invalidated @@ -2532,6 +2961,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { mReverseLayout = in.readInt() == 1; mAnchorLayoutFromEnd = in.readInt() == 1; mLastLayoutRTL = in.readInt() == 1; + //noinspection unchecked mFullSpanItems = in.readArrayList( LazySpanLookup.FullSpanItem.class.getClassLoader()); } @@ -2587,8 +3017,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { dest.writeList(mFullSpanItems); } - public static final Creator CREATOR - = new Creator() { + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { @Override public SavedState createFromParcel(Parcel in) { return new SavedState(in); @@ -2604,18 +3034,24 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { /** * Data class to hold the information about an anchor position which is used in onLayout call. */ - private class AnchorInfo { + class AnchorInfo { int mPosition; int mOffset; boolean mLayoutFromEnd; boolean mInvalidateOffsets; + boolean mValid; + + public AnchorInfo() { + reset(); + } void reset() { mPosition = NO_POSITION; mOffset = INVALID_OFFSET; mLayoutFromEnd = false; mInvalidateOffsets = false; + mValid = false; } void assignCoordinateFromPadding() { diff --git a/app/src/main/java/android/support/v7/widget/ViewInfoStore.java b/app/src/main/java/android/support/v7/widget/ViewInfoStore.java new file mode 100644 index 0000000000..f01a38222c --- /dev/null +++ b/app/src/main/java/android/support/v7/widget/ViewInfoStore.java @@ -0,0 +1,331 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.support.v7.widget; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; +import android.support.v4.util.ArrayMap; +import android.support.v4.util.LongSparseArray; +import android.support.v4.util.Pools; +import android.view.View; + +import static android.support.v7.widget.RecyclerView.ViewHolder; +import static android.support.v7.widget.RecyclerView.ItemAnimator.ItemHolderInfo; + +import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_APPEAR_PRE_AND_POST; +import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_APPEAR_AND_DISAPPEAR; +import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_PRE_AND_POST; +import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_DISAPPEARED; +import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_APPEAR; +import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_PRE; +import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_POST; +/** + * This class abstracts all tracking for Views to run animations + * + * @hide + */ +class ViewInfoStore { + + private static final boolean DEBUG = false; + + /** + * View data records for pre-layout + */ + @VisibleForTesting + final ArrayMap mLayoutHolderMap = new ArrayMap<>(); + + @VisibleForTesting + final LongSparseArray mOldChangedHolders = new LongSparseArray<>(); + + /** + * Clears the state and all existing tracking data + */ + void clear() { + mLayoutHolderMap.clear(); + mOldChangedHolders.clear(); + } + + /** + * Adds the item information to the prelayout tracking + * @param holder The ViewHolder whose information is being saved + * @param info The information to save + */ + void addToPreLayout(ViewHolder holder, ItemHolderInfo info) { + InfoRecord record = mLayoutHolderMap.get(holder); + if (record == null) { + record = InfoRecord.obtain(); + mLayoutHolderMap.put(holder, record); + } + record.preInfo = info; + record.flags |= FLAG_PRE; + } + + boolean isDisappearing(ViewHolder holder) { + final InfoRecord record = mLayoutHolderMap.get(holder); + return record != null && ((record.flags & FLAG_DISAPPEARED) != 0); + } + + /** + * Finds the ItemHolderInfo for the given ViewHolder in preLayout list and removes it. + * + * @param vh The ViewHolder whose information is being queried + * @return The ItemHolderInfo for the given ViewHolder or null if it does not exist + */ + @Nullable + ItemHolderInfo popFromPreLayout(ViewHolder vh) { + return popFromLayoutStep(vh, FLAG_PRE); + } + + /** + * Finds the ItemHolderInfo for the given ViewHolder in postLayout list and removes it. + * + * @param vh The ViewHolder whose information is being queried + * @return The ItemHolderInfo for the given ViewHolder or null if it does not exist + */ + @Nullable + ItemHolderInfo popFromPostLayout(ViewHolder vh) { + return popFromLayoutStep(vh, FLAG_POST); + } + + private ItemHolderInfo popFromLayoutStep(ViewHolder vh, int flag) { + int index = mLayoutHolderMap.indexOfKey(vh); + if (index < 0) { + return null; + } + final InfoRecord record = mLayoutHolderMap.valueAt(index); + if (record != null && (record.flags & flag) != 0) { + record.flags &= ~flag; + final ItemHolderInfo info; + if (flag == FLAG_PRE) { + info = record.preInfo; + } else if (flag == FLAG_POST) { + info = record.postInfo; + } else { + throw new IllegalArgumentException("Must provide flag PRE or POST"); + } + // if not pre-post flag is left, clear. + if ((record.flags & (FLAG_PRE | FLAG_POST)) == 0) { + mLayoutHolderMap.removeAt(index); + InfoRecord.recycle(record); + } + return info; + } + return null; + } + + /** + * Adds the given ViewHolder to the oldChangeHolders list + * @param key The key to identify the ViewHolder. + * @param holder The ViewHolder to store + */ + void addToOldChangeHolders(long key, ViewHolder holder) { + mOldChangedHolders.put(key, holder); + } + + /** + * Adds the given ViewHolder to the appeared in pre layout list. These are Views added by the + * LayoutManager during a pre-layout pass. We distinguish them from other views that were + * already in the pre-layout so that ItemAnimator can choose to run a different animation for + * them. + * + * @param holder The ViewHolder to store + * @param info The information to save + */ + void addToAppearedInPreLayoutHolders(ViewHolder holder, ItemHolderInfo info) { + InfoRecord record = mLayoutHolderMap.get(holder); + if (record == null) { + record = InfoRecord.obtain(); + mLayoutHolderMap.put(holder, record); + } + record.flags |= FLAG_APPEAR; + record.preInfo = info; + } + + /** + * Checks whether the given ViewHolder is in preLayout list + * @param viewHolder The ViewHolder to query + * + * @return True if the ViewHolder is present in preLayout, false otherwise + */ + boolean isInPreLayout(ViewHolder viewHolder) { + final InfoRecord record = mLayoutHolderMap.get(viewHolder); + return record != null && (record.flags & FLAG_PRE) != 0; + } + + /** + * Queries the oldChangeHolder list for the given key. If they are not tracked, simply returns + * null. + * @param key The key to be used to find the ViewHolder. + * + * @return A ViewHolder if exists or null if it does not exist. + */ + ViewHolder getFromOldChangeHolders(long key) { + return mOldChangedHolders.get(key); + } + + /** + * Adds the item information to the post layout list + * @param holder The ViewHolder whose information is being saved + * @param info The information to save + */ + void addToPostLayout(ViewHolder holder, ItemHolderInfo info) { + InfoRecord record = mLayoutHolderMap.get(holder); + if (record == null) { + record = InfoRecord.obtain(); + mLayoutHolderMap.put(holder, record); + } + record.postInfo = info; + record.flags |= FLAG_POST; + } + + /** + * A ViewHolder might be added by the LayoutManager just to animate its disappearance. + * This list holds such items so that we can animate / recycle these ViewHolders properly. + * + * @param holder The ViewHolder which disappeared during a layout. + */ + void addToDisappearedInLayout(ViewHolder holder) { + InfoRecord record = mLayoutHolderMap.get(holder); + if (record == null) { + record = InfoRecord.obtain(); + mLayoutHolderMap.put(holder, record); + } + record.flags |= FLAG_DISAPPEARED; + } + + /** + * Removes a ViewHolder from disappearing list. + * @param holder The ViewHolder to be removed from the disappearing list. + */ + void removeFromDisappearedInLayout(ViewHolder holder) { + InfoRecord record = mLayoutHolderMap.get(holder); + if (record == null) { + return; + } + record.flags &= ~FLAG_DISAPPEARED; + } + + void process(ProcessCallback callback) { + for (int index = mLayoutHolderMap.size() - 1; index >= 0; index --) { + final ViewHolder viewHolder = mLayoutHolderMap.keyAt(index); + final InfoRecord record = mLayoutHolderMap.removeAt(index); + if ((record.flags & FLAG_APPEAR_AND_DISAPPEAR) == FLAG_APPEAR_AND_DISAPPEAR) { + // Appeared then disappeared. Not useful for animations. + callback.unused(viewHolder); + } else if ((record.flags & FLAG_DISAPPEARED) != 0) { + // Set as "disappeared" by the LayoutManager (addDisappearingView) + if (record.preInfo == null) { + // similar to appear disappear but happened between different layout passes. + // this can happen when the layout manager is using auto-measure + callback.unused(viewHolder); + } else { + callback.processDisappeared(viewHolder, record.preInfo, record.postInfo); + } + } else if ((record.flags & FLAG_APPEAR_PRE_AND_POST) == FLAG_APPEAR_PRE_AND_POST) { + // Appeared in the layout but not in the adapter (e.g. entered the viewport) + callback.processAppeared(viewHolder, record.preInfo, record.postInfo); + } else if ((record.flags & FLAG_PRE_AND_POST) == FLAG_PRE_AND_POST) { + // Persistent in both passes. Animate persistence + callback.processPersistent(viewHolder, record.preInfo, record.postInfo); + } else if ((record.flags & FLAG_PRE) != 0) { + // Was in pre-layout, never been added to post layout + callback.processDisappeared(viewHolder, record.preInfo, null); + } else if ((record.flags & FLAG_POST) != 0) { + // Was not in pre-layout, been added to post layout + callback.processAppeared(viewHolder, record.preInfo, record.postInfo); + } else if ((record.flags & FLAG_APPEAR) != 0) { + // Scrap view. RecyclerView will handle removing/recycling this. + } else if (DEBUG) { + throw new IllegalStateException("record without any reasonable flag combination:/"); + } + InfoRecord.recycle(record); + } + } + + /** + * Removes the ViewHolder from all list + * @param holder The ViewHolder which we should stop tracking + */ + void removeViewHolder(ViewHolder holder) { + for (int i = mOldChangedHolders.size() - 1; i >= 0; i--) { + if (holder == mOldChangedHolders.valueAt(i)) { + mOldChangedHolders.removeAt(i); + break; + } + } + final InfoRecord info = mLayoutHolderMap.remove(holder); + if (info != null) { + InfoRecord.recycle(info); + } + } + + void onDetach() { + InfoRecord.drainCache(); + } + + public void onViewDetached(ViewHolder viewHolder) { + removeFromDisappearedInLayout(viewHolder); + } + + interface ProcessCallback { + void processDisappeared(ViewHolder viewHolder, @NonNull ItemHolderInfo preInfo, + @Nullable ItemHolderInfo postInfo); + void processAppeared(ViewHolder viewHolder, @Nullable ItemHolderInfo preInfo, + ItemHolderInfo postInfo); + void processPersistent(ViewHolder viewHolder, @NonNull ItemHolderInfo preInfo, + @NonNull ItemHolderInfo postInfo); + void unused(ViewHolder holder); + } + + static class InfoRecord { + // disappearing list + static final int FLAG_DISAPPEARED = 1; + // appear in pre layout list + static final int FLAG_APPEAR = 1 << 1; + // pre layout, this is necessary to distinguish null item info + static final int FLAG_PRE = 1 << 2; + // post layout, this is necessary to distinguish null item info + static final int FLAG_POST = 1 << 3; + static final int FLAG_APPEAR_AND_DISAPPEAR = FLAG_APPEAR | FLAG_DISAPPEARED; + static final int FLAG_PRE_AND_POST = FLAG_PRE | FLAG_POST; + static final int FLAG_APPEAR_PRE_AND_POST = FLAG_APPEAR | FLAG_PRE | FLAG_POST; + int flags; + @Nullable ItemHolderInfo preInfo; + @Nullable ItemHolderInfo postInfo; + static Pools.Pool sPool = new Pools.SimplePool<>(20); + + private InfoRecord() { + } + + static InfoRecord obtain() { + InfoRecord record = sPool.acquire(); + return record == null ? new InfoRecord() : record; + } + + static void recycle(InfoRecord record) { + record.flags = 0; + record.preInfo = null; + record.postInfo = null; + sPool.release(record); + } + + static void drainCache() { + //noinspection StatementWithEmptyBody + while (sPool.acquire() != null); + } + } +} diff --git a/app/src/main/java/android/support/v7/widget/helper/ItemTouchHelper.java b/app/src/main/java/android/support/v7/widget/helper/ItemTouchHelper.java new file mode 100644 index 0000000000..5bbc4587a4 --- /dev/null +++ b/app/src/main/java/android/support/v7/widget/helper/ItemTouchHelper.java @@ -0,0 +1,2408 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.widget.helper; + +import android.content.res.Resources; +import android.graphics.Canvas; +import android.graphics.Rect; +import android.os.Build; +import android.support.annotation.Nullable; +import android.support.v4.animation.AnimatorCompatHelper; +import android.support.v4.animation.AnimatorListenerCompat; +import android.support.v4.animation.AnimatorUpdateListenerCompat; +import android.support.v4.animation.ValueAnimatorCompat; +import android.support.v4.view.GestureDetectorCompat; +import android.support.v4.view.MotionEventCompat; +import android.support.v4.view.VelocityTrackerCompat; +import android.support.v4.view.ViewCompat; +import android.support.v7.recyclerview.R; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.RecyclerView.OnItemTouchListener; +import android.support.v7.widget.RecyclerView.ViewHolder; +import android.util.Log; +import android.view.GestureDetector; +import android.view.HapticFeedbackConstants; +import android.view.MotionEvent; +import android.view.VelocityTracker; +import android.view.View; +import android.view.ViewConfiguration; +import android.view.ViewParent; +import android.view.animation.Interpolator; + +import java.util.ArrayList; +import java.util.List; + +/** + * This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView. + *

      + * It works with a RecyclerView and a Callback class, which configures what type of interactions + * are enabled and also receives events when user performs these actions. + *

      + * Depending on which functionality you support, you should override + * {@link Callback#onMove(RecyclerView, ViewHolder, ViewHolder)} and / or + * {@link Callback#onSwiped(ViewHolder, int)}. + *

      + * This class is designed to work with any LayoutManager but for certain situations, it can be + * optimized for your custom LayoutManager by extending methods in the + * {@link ItemTouchHelper.Callback} class or implementing {@link ItemTouchHelper.ViewDropHandler} + * interface in your LayoutManager. + *

      + * By default, ItemTouchHelper moves the items' translateX/Y properties to reposition them. On + * platforms older than Honeycomb, ItemTouchHelper uses canvas translations and View's visibility + * property to move items in response to touch events. You can customize these behaviors by + * overriding {@link Callback#onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int, + * boolean)} + * or {@link Callback#onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, + * boolean)}. + *

      + * Most of the time, you only need to override onChildDraw but due to limitations of + * platform prior to Honeycomb, you may need to implement onChildDrawOver as well. + */ +public class ItemTouchHelper extends RecyclerView.ItemDecoration + implements RecyclerView.OnChildAttachStateChangeListener { + + /** + * Up direction, used for swipe & drag control. + */ + public static final int UP = 1; + + /** + * Down direction, used for swipe & drag control. + */ + public static final int DOWN = 1 << 1; + + /** + * Left direction, used for swipe & drag control. + */ + public static final int LEFT = 1 << 2; + + /** + * Right direction, used for swipe & drag control. + */ + public static final int RIGHT = 1 << 3; + + // If you change these relative direction values, update Callback#convertToAbsoluteDirection, + // Callback#convertToRelativeDirection. + /** + * Horizontal start direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout + * direction. Used for swipe & drag control. + */ + public static final int START = LEFT << 2; + + /** + * Horizontal end direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout + * direction. Used for swipe & drag control. + */ + public static final int END = RIGHT << 2; + + /** + * ItemTouchHelper is in idle state. At this state, either there is no related motion event by + * the user or latest motion events have not yet triggered a swipe or drag. + */ + public static final int ACTION_STATE_IDLE = 0; + + /** + * A View is currently being swiped. + */ + public static final int ACTION_STATE_SWIPE = 1; + + /** + * A View is currently being dragged. + */ + public static final int ACTION_STATE_DRAG = 2; + + /** + * Animation type for views which are swiped successfully. + */ + public static final int ANIMATION_TYPE_SWIPE_SUCCESS = 1 << 1; + + /** + * Animation type for views which are not completely swiped thus will animate back to their + * original position. + */ + public static final int ANIMATION_TYPE_SWIPE_CANCEL = 1 << 2; + + /** + * Animation type for views that were dragged and now will animate to their final position. + */ + public static final int ANIMATION_TYPE_DRAG = 1 << 3; + + private static final String TAG = "ItemTouchHelper"; + + private static final boolean DEBUG = false; + + private static final int ACTIVE_POINTER_ID_NONE = -1; + + private static final int DIRECTION_FLAG_COUNT = 8; + + private static final int ACTION_MODE_IDLE_MASK = (1 << DIRECTION_FLAG_COUNT) - 1; + + private static final int ACTION_MODE_SWIPE_MASK = ACTION_MODE_IDLE_MASK << DIRECTION_FLAG_COUNT; + + private static final int ACTION_MODE_DRAG_MASK = ACTION_MODE_SWIPE_MASK << DIRECTION_FLAG_COUNT; + + /** + * The unit we are using to track velocity + */ + private static final int PIXELS_PER_SECOND = 1000; + + /** + * Views, whose state should be cleared after they are detached from RecyclerView. + * This is necessary after swipe dismissing an item. We wait until animator finishes its job + * to clean these views. + */ + final List mPendingCleanup = new ArrayList(); + + /** + * Re-use array to calculate dx dy for a ViewHolder + */ + private final float[] mTmpPosition = new float[2]; + + /** + * Currently selected view holder + */ + ViewHolder mSelected = null; + + /** + * The reference coordinates for the action start. For drag & drop, this is the time long + * press is completed vs for swipe, this is the initial touch point. + */ + float mInitialTouchX; + + float mInitialTouchY; + + /** + * Set when ItemTouchHelper is assigned to a RecyclerView. + */ + float mSwipeEscapeVelocity; + + /** + * Set when ItemTouchHelper is assigned to a RecyclerView. + */ + float mMaxSwipeVelocity; + + /** + * The diff between the last event and initial touch. + */ + float mDx; + + float mDy; + + /** + * The coordinates of the selected view at the time it is selected. We record these values + * when action starts so that we can consistently position it even if LayoutManager moves the + * View. + */ + float mSelectedStartX; + + float mSelectedStartY; + + /** + * The pointer we are tracking. + */ + int mActivePointerId = ACTIVE_POINTER_ID_NONE; + + /** + * Developer callback which controls the behavior of ItemTouchHelper. + */ + Callback mCallback; + + /** + * Current mode. + */ + int mActionState = ACTION_STATE_IDLE; + + /** + * The direction flags obtained from unmasking + * {@link Callback#getAbsoluteMovementFlags(RecyclerView, ViewHolder)} for the current + * action state. + */ + int mSelectedFlags; + + /** + * When a View is dragged or swiped and needs to go back to where it was, we create a Recover + * Animation and animate it to its location using this custom Animator, instead of using + * framework Animators. + * Using framework animators has the side effect of clashing with ItemAnimator, creating + * jumpy UIs. + */ + List mRecoverAnimations = new ArrayList(); + + private int mSlop; + + private RecyclerView mRecyclerView; + + /** + * When user drags a view to the edge, we start scrolling the LayoutManager as long as View + * is partially out of bounds. + */ + private final Runnable mScrollRunnable = new Runnable() { + @Override + public void run() { + if (mSelected != null && scrollIfNecessary()) { + if (mSelected != null) { //it might be lost during scrolling + moveIfNecessary(mSelected); + } + mRecyclerView.removeCallbacks(mScrollRunnable); + ViewCompat.postOnAnimation(mRecyclerView, this); + } + } + }; + + /** + * Used for detecting fling swipe + */ + private VelocityTracker mVelocityTracker; + + //re-used list for selecting a swap target + private List mSwapTargets; + + //re used for for sorting swap targets + private List mDistances; + + /** + * If drag & drop is supported, we use child drawing order to bring them to front. + */ + private RecyclerView.ChildDrawingOrderCallback mChildDrawingOrderCallback = null; + + /** + * This keeps a reference to the child dragged by the user. Even after user stops dragging, + * until view reaches its final position (end of recover animation), we keep a reference so + * that it can be drawn above other children. + */ + private View mOverdrawChild = null; + + /** + * We cache the position of the overdraw child to avoid recalculating it each time child + * position callback is called. This value is invalidated whenever a child is attached or + * detached. + */ + private int mOverdrawChildPosition = -1; + + /** + * Used to detect long press. + */ + private GestureDetectorCompat mGestureDetector; + + private final OnItemTouchListener mOnItemTouchListener + = new OnItemTouchListener() { + @Override + public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) { + mGestureDetector.onTouchEvent(event); + if (DEBUG) { + Log.d(TAG, "intercept: x:" + event.getX() + ",y:" + event.getY() + ", " + event); + } + final int action = MotionEventCompat.getActionMasked(event); + if (action == MotionEvent.ACTION_DOWN) { + mActivePointerId = event.getPointerId(0); + mInitialTouchX = event.getX(); + mInitialTouchY = event.getY(); + obtainVelocityTracker(); + if (mSelected == null) { + final RecoverAnimation animation = findAnimation(event); + if (animation != null) { + mInitialTouchX -= animation.mX; + mInitialTouchY -= animation.mY; + endRecoverAnimation(animation.mViewHolder, true); + if (mPendingCleanup.remove(animation.mViewHolder.itemView)) { + mCallback.clearView(mRecyclerView, animation.mViewHolder); + } + select(animation.mViewHolder, animation.mActionState); + updateDxDy(event, mSelectedFlags, 0); + } + } + } else if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { + mActivePointerId = ACTIVE_POINTER_ID_NONE; + select(null, ACTION_STATE_IDLE); + } else if (mActivePointerId != ACTIVE_POINTER_ID_NONE) { + // in a non scroll orientation, if distance change is above threshold, we + // can select the item + final int index = event.findPointerIndex(mActivePointerId); + if (DEBUG) { + Log.d(TAG, "pointer index " + index); + } + if (index >= 0) { + checkSelectForSwipe(action, event, index); + } + } + if (mVelocityTracker != null) { + mVelocityTracker.addMovement(event); + } + return mSelected != null; + } + + @Override + public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) { + mGestureDetector.onTouchEvent(event); + if (DEBUG) { + Log.d(TAG, + "on touch: x:" + mInitialTouchX + ",y:" + mInitialTouchY + ", :" + event); + } + if (mVelocityTracker != null) { + mVelocityTracker.addMovement(event); + } + if (mActivePointerId == ACTIVE_POINTER_ID_NONE) { + return; + } + final int action = MotionEventCompat.getActionMasked(event); + final int activePointerIndex = event.findPointerIndex(mActivePointerId); + if (activePointerIndex >= 0) { + checkSelectForSwipe(action, event, activePointerIndex); + } + ViewHolder viewHolder = mSelected; + if (viewHolder == null) { + return; + } + switch (action) { + case MotionEvent.ACTION_MOVE: { + // Find the index of the active pointer and fetch its position + if (activePointerIndex >= 0) { + updateDxDy(event, mSelectedFlags, activePointerIndex); + moveIfNecessary(viewHolder); + mRecyclerView.removeCallbacks(mScrollRunnable); + mScrollRunnable.run(); + mRecyclerView.invalidate(); + } + break; + } + case MotionEvent.ACTION_CANCEL: + if (mVelocityTracker != null) { + mVelocityTracker.clear(); + } + // fall through + case MotionEvent.ACTION_UP: + select(null, ACTION_STATE_IDLE); + mActivePointerId = ACTIVE_POINTER_ID_NONE; + break; + case MotionEvent.ACTION_POINTER_UP: { + final int pointerIndex = MotionEventCompat.getActionIndex(event); + final int pointerId = event.getPointerId(pointerIndex); + if (pointerId == mActivePointerId) { + // This was our active pointer going up. Choose a new + // active pointer and adjust accordingly. + final int newPointerIndex = pointerIndex == 0 ? 1 : 0; + mActivePointerId = event.getPointerId(newPointerIndex); + updateDxDy(event, mSelectedFlags, pointerIndex); + } + break; + } + } + } + + @Override + public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { + if (!disallowIntercept) { + return; + } + select(null, ACTION_STATE_IDLE); + } + }; + + /** + * Temporary rect instance that is used when we need to lookup Item decorations. + */ + private Rect mTmpRect; + + /** + * When user started to drag scroll. Reset when we don't scroll + */ + private long mDragScrollStartTimeInMs; + + /** + * Creates an ItemTouchHelper that will work with the given Callback. + *

      + * You can attach ItemTouchHelper to a RecyclerView via + * {@link #attachToRecyclerView(RecyclerView)}. Upon attaching, it will add an item decoration, + * an onItemTouchListener and a Child attach / detach listener to the RecyclerView. + * + * @param callback The Callback which controls the behavior of this touch helper. + */ + public ItemTouchHelper(Callback callback) { + mCallback = callback; + } + + private static boolean hitTest(View child, float x, float y, float left, float top) { + return x >= left && + x <= left + child.getWidth() && + y >= top && + y <= top + child.getHeight(); + } + + /** + * Attaches the ItemTouchHelper to the provided RecyclerView. If TouchHelper is already + * attached to a RecyclerView, it will first detach from the previous one. You can call this + * method with {@code null} to detach it from the current RecyclerView. + * + * @param recyclerView The RecyclerView instance to which you want to add this helper or + * {@code null} if you want to remove ItemTouchHelper from the current + * RecyclerView. + */ + public void attachToRecyclerView(@Nullable RecyclerView recyclerView) { + if (mRecyclerView == recyclerView) { + return; // nothing to do + } + if (mRecyclerView != null) { + destroyCallbacks(); + } + mRecyclerView = recyclerView; + if (mRecyclerView != null) { + final Resources resources = recyclerView.getResources(); + mSwipeEscapeVelocity = resources + .getDimension(R.dimen.item_touch_helper_swipe_escape_velocity); + mMaxSwipeVelocity = resources + .getDimension(R.dimen.item_touch_helper_swipe_escape_max_velocity); + setupCallbacks(); + } + } + + private void setupCallbacks() { + ViewConfiguration vc = ViewConfiguration.get(mRecyclerView.getContext()); + mSlop = vc.getScaledTouchSlop(); + mRecyclerView.addItemDecoration(this); + mRecyclerView.addOnItemTouchListener(mOnItemTouchListener); + mRecyclerView.addOnChildAttachStateChangeListener(this); + initGestureDetector(); + } + + private void destroyCallbacks() { + mRecyclerView.removeItemDecoration(this); + mRecyclerView.removeOnItemTouchListener(mOnItemTouchListener); + mRecyclerView.removeOnChildAttachStateChangeListener(this); + // clean all attached + final int recoverAnimSize = mRecoverAnimations.size(); + for (int i = recoverAnimSize - 1; i >= 0; i--) { + final RecoverAnimation recoverAnimation = mRecoverAnimations.get(0); + mCallback.clearView(mRecyclerView, recoverAnimation.mViewHolder); + } + mRecoverAnimations.clear(); + mOverdrawChild = null; + mOverdrawChildPosition = -1; + releaseVelocityTracker(); + } + + private void initGestureDetector() { + if (mGestureDetector != null) { + return; + } + mGestureDetector = new GestureDetectorCompat(mRecyclerView.getContext(), + new ItemTouchHelperGestureListener()); + } + + private void getSelectedDxDy(float[] outPosition) { + if ((mSelectedFlags & (LEFT | RIGHT)) != 0) { + outPosition[0] = mSelectedStartX + mDx - mSelected.itemView.getLeft(); + } else { + outPosition[0] = ViewCompat.getTranslationX(mSelected.itemView); + } + if ((mSelectedFlags & (UP | DOWN)) != 0) { + outPosition[1] = mSelectedStartY + mDy - mSelected.itemView.getTop(); + } else { + outPosition[1] = ViewCompat.getTranslationY(mSelected.itemView); + } + } + + @Override + public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { + float dx = 0, dy = 0; + if (mSelected != null) { + getSelectedDxDy(mTmpPosition); + dx = mTmpPosition[0]; + dy = mTmpPosition[1]; + } + mCallback.onDrawOver(c, parent, mSelected, + mRecoverAnimations, mActionState, dx, dy); + } + + @Override + public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { + // we don't know if RV changed something so we should invalidate this index. + mOverdrawChildPosition = -1; + float dx = 0, dy = 0; + if (mSelected != null) { + getSelectedDxDy(mTmpPosition); + dx = mTmpPosition[0]; + dy = mTmpPosition[1]; + } + mCallback.onDraw(c, parent, mSelected, + mRecoverAnimations, mActionState, dx, dy); + } + + /** + * Starts dragging or swiping the given View. Call with null if you want to clear it. + * + * @param selected The ViewHolder to drag or swipe. Can be null if you want to cancel the + * current action + * @param actionState The type of action + */ + private void select(ViewHolder selected, int actionState) { + if (selected == mSelected && actionState == mActionState) { + return; + } + mDragScrollStartTimeInMs = Long.MIN_VALUE; + final int prevActionState = mActionState; + // prevent duplicate animations + endRecoverAnimation(selected, true); + mActionState = actionState; + if (actionState == ACTION_STATE_DRAG) { + // we remove after animation is complete. this means we only elevate the last drag + // child but that should perform good enough as it is very hard to start dragging a + // new child before the previous one settles. + mOverdrawChild = selected.itemView; + addChildDrawingOrderCallback(); + } + int actionStateMask = (1 << (DIRECTION_FLAG_COUNT + DIRECTION_FLAG_COUNT * actionState)) + - 1; + boolean preventLayout = false; + + if (mSelected != null) { + final ViewHolder prevSelected = mSelected; + if (prevSelected.itemView.getParent() != null) { + final int swipeDir = prevActionState == ACTION_STATE_DRAG ? 0 + : swipeIfNecessary(prevSelected); + releaseVelocityTracker(); + // find where we should animate to + final float targetTranslateX, targetTranslateY; + int animationType; + switch (swipeDir) { + case LEFT: + case RIGHT: + case START: + case END: + targetTranslateY = 0; + targetTranslateX = Math.signum(mDx) * mRecyclerView.getWidth(); + break; + case UP: + case DOWN: + targetTranslateX = 0; + targetTranslateY = Math.signum(mDy) * mRecyclerView.getHeight(); + break; + default: + targetTranslateX = 0; + targetTranslateY = 0; + } + if (prevActionState == ACTION_STATE_DRAG) { + animationType = ANIMATION_TYPE_DRAG; + } else if (swipeDir > 0) { + animationType = ANIMATION_TYPE_SWIPE_SUCCESS; + } else { + animationType = ANIMATION_TYPE_SWIPE_CANCEL; + } + getSelectedDxDy(mTmpPosition); + final float currentTranslateX = mTmpPosition[0]; + final float currentTranslateY = mTmpPosition[1]; + final RecoverAnimation rv = new RecoverAnimation(prevSelected, animationType, + prevActionState, currentTranslateX, currentTranslateY, + targetTranslateX, targetTranslateY) { + @Override + public void onAnimationEnd(ValueAnimatorCompat animation) { + super.onAnimationEnd(animation); + if (this.mOverridden) { + return; + } + if (swipeDir <= 0) { + // this is a drag or failed swipe. recover immediately + mCallback.clearView(mRecyclerView, prevSelected); + // full cleanup will happen on onDrawOver + } else { + // wait until remove animation is complete. + mPendingCleanup.add(prevSelected.itemView); + mIsPendingCleanup = true; + if (swipeDir > 0) { + // Animation might be ended by other animators during a layout. + // We defer callback to avoid editing adapter during a layout. + postDispatchSwipe(this, swipeDir); + } + } + // removed from the list after it is drawn for the last time + if (mOverdrawChild == prevSelected.itemView) { + removeChildDrawingOrderCallbackIfNecessary(prevSelected.itemView); + } + } + }; + final long duration = mCallback.getAnimationDuration(mRecyclerView, animationType, + targetTranslateX - currentTranslateX, targetTranslateY - currentTranslateY); + rv.setDuration(duration); + mRecoverAnimations.add(rv); + rv.start(); + preventLayout = true; + } else { + removeChildDrawingOrderCallbackIfNecessary(prevSelected.itemView); + mCallback.clearView(mRecyclerView, prevSelected); + } + mSelected = null; + } + if (selected != null) { + mSelectedFlags = + (mCallback.getAbsoluteMovementFlags(mRecyclerView, selected) & actionStateMask) + >> (mActionState * DIRECTION_FLAG_COUNT); + mSelectedStartX = selected.itemView.getLeft(); + mSelectedStartY = selected.itemView.getTop(); + mSelected = selected; + + if (actionState == ACTION_STATE_DRAG) { + mSelected.itemView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); + } + } + final ViewParent rvParent = mRecyclerView.getParent(); + if (rvParent != null) { + rvParent.requestDisallowInterceptTouchEvent(mSelected != null); + } + if (!preventLayout) { + mRecyclerView.getLayoutManager().requestSimpleAnimationsInNextLayout(); + } + mCallback.onSelectedChanged(mSelected, mActionState); + mRecyclerView.invalidate(); + } + + private void postDispatchSwipe(final RecoverAnimation anim, final int swipeDir) { + // wait until animations are complete. + mRecyclerView.post(new Runnable() { + @Override + public void run() { + if (mRecyclerView != null && mRecyclerView.isAttachedToWindow() && + !anim.mOverridden && + anim.mViewHolder.getAdapterPosition() != RecyclerView.NO_POSITION) { + final RecyclerView.ItemAnimator animator = mRecyclerView.getItemAnimator(); + // if animator is running or we have other active recover animations, we try + // not to call onSwiped because DefaultItemAnimator is not good at merging + // animations. Instead, we wait and batch. + if ((animator == null || !animator.isRunning(null)) + && !hasRunningRecoverAnim()) { + mCallback.onSwiped(anim.mViewHolder, swipeDir); + } else { + mRecyclerView.post(this); + } + } + } + }); + } + + private boolean hasRunningRecoverAnim() { + final int size = mRecoverAnimations.size(); + for (int i = 0; i < size; i++) { + if (!mRecoverAnimations.get(i).mEnded) { + return true; + } + } + return false; + } + + /** + * If user drags the view to the edge, trigger a scroll if necessary. + */ + private boolean scrollIfNecessary() { + if (mSelected == null) { + mDragScrollStartTimeInMs = Long.MIN_VALUE; + return false; + } + final long now = System.currentTimeMillis(); + final long scrollDuration = mDragScrollStartTimeInMs + == Long.MIN_VALUE ? 0 : now - mDragScrollStartTimeInMs; + RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager(); + if (mTmpRect == null) { + mTmpRect = new Rect(); + } + int scrollX = 0; + int scrollY = 0; + lm.calculateItemDecorationsForChild(mSelected.itemView, mTmpRect); + if (lm.canScrollHorizontally()) { + int curX = (int) (mSelectedStartX + mDx); + final int leftDiff = curX - mTmpRect.left - mRecyclerView.getPaddingLeft(); + if (mDx < 0 && leftDiff < 0) { + scrollX = leftDiff; + } else if (mDx > 0) { + final int rightDiff = + curX + mSelected.itemView.getWidth() + mTmpRect.right + - (mRecyclerView.getWidth() - mRecyclerView.getPaddingRight()); + if (rightDiff > 0) { + scrollX = rightDiff; + } + } + } + if (lm.canScrollVertically()) { + int curY = (int) (mSelectedStartY + mDy); + final int topDiff = curY - mTmpRect.top - mRecyclerView.getPaddingTop(); + if (mDy < 0 && topDiff < 0) { + scrollY = topDiff; + } else if (mDy > 0) { + final int bottomDiff = curY + mSelected.itemView.getHeight() + mTmpRect.bottom - + (mRecyclerView.getHeight() - mRecyclerView.getPaddingBottom()); + if (bottomDiff > 0) { + scrollY = bottomDiff; + } + } + } + if (scrollX != 0) { + scrollX = mCallback.interpolateOutOfBoundsScroll(mRecyclerView, + mSelected.itemView.getWidth(), scrollX, + mRecyclerView.getWidth(), scrollDuration); + } + if (scrollY != 0) { + scrollY = mCallback.interpolateOutOfBoundsScroll(mRecyclerView, + mSelected.itemView.getHeight(), scrollY, + mRecyclerView.getHeight(), scrollDuration); + } + if (scrollX != 0 || scrollY != 0) { + if (mDragScrollStartTimeInMs == Long.MIN_VALUE) { + mDragScrollStartTimeInMs = now; + } + mRecyclerView.scrollBy(scrollX, scrollY); + return true; + } + mDragScrollStartTimeInMs = Long.MIN_VALUE; + return false; + } + + private List findSwapTargets(ViewHolder viewHolder) { + if (mSwapTargets == null) { + mSwapTargets = new ArrayList(); + mDistances = new ArrayList(); + } else { + mSwapTargets.clear(); + mDistances.clear(); + } + final int margin = mCallback.getBoundingBoxMargin(); + final int left = Math.round(mSelectedStartX + mDx) - margin; + final int top = Math.round(mSelectedStartY + mDy) - margin; + final int right = left + viewHolder.itemView.getWidth() + 2 * margin; + final int bottom = top + viewHolder.itemView.getHeight() + 2 * margin; + final int centerX = (left + right) / 2; + final int centerY = (top + bottom) / 2; + final RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager(); + final int childCount = lm.getChildCount(); + for (int i = 0; i < childCount; i++) { + View other = lm.getChildAt(i); + if (other == viewHolder.itemView) { + continue;//myself! + } + if (other.getBottom() < top || other.getTop() > bottom + || other.getRight() < left || other.getLeft() > right) { + continue; + } + final ViewHolder otherVh = mRecyclerView.getChildViewHolder(other); + if (mCallback.canDropOver(mRecyclerView, mSelected, otherVh)) { + // find the index to add + final int dx = Math.abs(centerX - (other.getLeft() + other.getRight()) / 2); + final int dy = Math.abs(centerY - (other.getTop() + other.getBottom()) / 2); + final int dist = dx * dx + dy * dy; + + int pos = 0; + final int cnt = mSwapTargets.size(); + for (int j = 0; j < cnt; j++) { + if (dist > mDistances.get(j)) { + pos++; + } else { + break; + } + } + mSwapTargets.add(pos, otherVh); + mDistances.add(pos, dist); + } + } + return mSwapTargets; + } + + /** + * Checks if we should swap w/ another view holder. + */ + private void moveIfNecessary(ViewHolder viewHolder) { + if (mRecyclerView.isLayoutRequested()) { + return; + } + if (mActionState != ACTION_STATE_DRAG) { + return; + } + + final float threshold = mCallback.getMoveThreshold(viewHolder); + final int x = (int) (mSelectedStartX + mDx); + final int y = (int) (mSelectedStartY + mDy); + if (Math.abs(y - viewHolder.itemView.getTop()) < viewHolder.itemView.getHeight() * threshold + && Math.abs(x - viewHolder.itemView.getLeft()) + < viewHolder.itemView.getWidth() * threshold) { + return; + } + List swapTargets = findSwapTargets(viewHolder); + if (swapTargets.size() == 0) { + return; + } + // may swap. + ViewHolder target = mCallback.chooseDropTarget(viewHolder, swapTargets, x, y); + if (target == null) { + mSwapTargets.clear(); + mDistances.clear(); + return; + } + final int toPosition = target.getAdapterPosition(); + final int fromPosition = viewHolder.getAdapterPosition(); + if (mCallback.onMove(mRecyclerView, viewHolder, target)) { + // keep target visible + mCallback.onMoved(mRecyclerView, viewHolder, fromPosition, + target, toPosition, x, y); + } + } + + @Override + public void onChildViewAttachedToWindow(View view) { + } + + @Override + public void onChildViewDetachedFromWindow(View view) { + removeChildDrawingOrderCallbackIfNecessary(view); + final ViewHolder holder = mRecyclerView.getChildViewHolder(view); + if (holder == null) { + return; + } + if (mSelected != null && holder == mSelected) { + select(null, ACTION_STATE_IDLE); + } else { + endRecoverAnimation(holder, false); // this may push it into pending cleanup list. + if (mPendingCleanup.remove(holder.itemView)) { + mCallback.clearView(mRecyclerView, holder); + } + } + } + + /** + * Returns the animation type or 0 if cannot be found. + */ + private int endRecoverAnimation(ViewHolder viewHolder, boolean override) { + final int recoverAnimSize = mRecoverAnimations.size(); + for (int i = recoverAnimSize - 1; i >= 0; i--) { + final RecoverAnimation anim = mRecoverAnimations.get(i); + if (anim.mViewHolder == viewHolder) { + anim.mOverridden |= override; + if (!anim.mEnded) { + anim.cancel(); + } + mRecoverAnimations.remove(i); + return anim.mAnimationType; + } + } + return 0; + } + + @Override + public void getItemOffsets(Rect outRect, View view, RecyclerView parent, + RecyclerView.State state) { + outRect.setEmpty(); + } + + private void obtainVelocityTracker() { + if (mVelocityTracker != null) { + mVelocityTracker.recycle(); + } + mVelocityTracker = VelocityTracker.obtain(); + } + + private void releaseVelocityTracker() { + if (mVelocityTracker != null) { + mVelocityTracker.recycle(); + mVelocityTracker = null; + } + } + + private ViewHolder findSwipedView(MotionEvent motionEvent) { + final RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager(); + if (mActivePointerId == ACTIVE_POINTER_ID_NONE) { + return null; + } + final int pointerIndex = motionEvent.findPointerIndex(mActivePointerId); + final float dx = motionEvent.getX(pointerIndex) - mInitialTouchX; + final float dy = motionEvent.getY(pointerIndex) - mInitialTouchY; + final float absDx = Math.abs(dx); + final float absDy = Math.abs(dy); + + if (absDx < mSlop && absDy < mSlop) { + return null; + } + if (absDx > absDy && lm.canScrollHorizontally()) { + return null; + } else if (absDy > absDx && lm.canScrollVertically()) { + return null; + } + View child = findChildView(motionEvent); + if (child == null) { + return null; + } + return mRecyclerView.getChildViewHolder(child); + } + + /** + * Checks whether we should select a View for swiping. + */ + private boolean checkSelectForSwipe(int action, MotionEvent motionEvent, int pointerIndex) { + if (mSelected != null || action != MotionEvent.ACTION_MOVE + || mActionState == ACTION_STATE_DRAG || !mCallback.isItemViewSwipeEnabled()) { + return false; + } + if (mRecyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) { + return false; + } + final ViewHolder vh = findSwipedView(motionEvent); + if (vh == null) { + return false; + } + final int movementFlags = mCallback.getAbsoluteMovementFlags(mRecyclerView, vh); + + final int swipeFlags = (movementFlags & ACTION_MODE_SWIPE_MASK) + >> (DIRECTION_FLAG_COUNT * ACTION_STATE_SWIPE); + + if (swipeFlags == 0) { + return false; + } + + // mDx and mDy are only set in allowed directions. We use custom x/y here instead of + // updateDxDy to avoid swiping if user moves more in the other direction + final float x = motionEvent.getX(pointerIndex); + final float y = motionEvent.getY(pointerIndex); + + // Calculate the distance moved + final float dx = x - mInitialTouchX; + final float dy = y - mInitialTouchY; + // swipe target is chose w/o applying flags so it does not really check if swiping in that + // direction is allowed. This why here, we use mDx mDy to check slope value again. + final float absDx = Math.abs(dx); + final float absDy = Math.abs(dy); + + if (absDx < mSlop && absDy < mSlop) { + return false; + } + if (absDx > absDy) { + if (dx < 0 && (swipeFlags & LEFT) == 0) { + return false; + } + if (dx > 0 && (swipeFlags & RIGHT) == 0) { + return false; + } + } else { + if (dy < 0 && (swipeFlags & UP) == 0) { + return false; + } + if (dy > 0 && (swipeFlags & DOWN) == 0) { + return false; + } + } + mDx = mDy = 0f; + mActivePointerId = motionEvent.getPointerId(0); + select(vh, ACTION_STATE_SWIPE); + return true; + } + + private View findChildView(MotionEvent event) { + // first check elevated views, if none, then call RV + final float x = event.getX(); + final float y = event.getY(); + if (mSelected != null) { + final View selectedView = mSelected.itemView; + if (hitTest(selectedView, x, y, mSelectedStartX + mDx, mSelectedStartY + mDy)) { + return selectedView; + } + } + for (int i = mRecoverAnimations.size() - 1; i >= 0; i--) { + final RecoverAnimation anim = mRecoverAnimations.get(i); + final View view = anim.mViewHolder.itemView; + if (hitTest(view, x, y, anim.mX, anim.mY)) { + return view; + } + } + return mRecyclerView.findChildViewUnder(x, y); + } + + /** + * Starts dragging the provided ViewHolder. By default, ItemTouchHelper starts a drag when a + * View is long pressed. You can disable that behavior by overriding + * {@link ItemTouchHelper.Callback#isLongPressDragEnabled()}. + *

      + * For this method to work: + *

        + *
      • The provided ViewHolder must be a child of the RecyclerView to which this + * ItemTouchHelper + * is attached.
      • + *
      • {@link ItemTouchHelper.Callback} must have dragging enabled.
      • + *
      • There must be a previous touch event that was reported to the ItemTouchHelper + * through RecyclerView's ItemTouchListener mechanism. As long as no other ItemTouchListener + * grabs previous events, this should work as expected.
      • + *
      + * + * For example, if you would like to let your user to be able to drag an Item by touching one + * of its descendants, you may implement it as follows: + *
      +     *     viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() {
      +     *         public boolean onTouch(View v, MotionEvent event) {
      +     *             if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
      +     *                 mItemTouchHelper.startDrag(viewHolder);
      +     *             }
      +     *             return false;
      +     *         }
      +     *     });
      +     * 
      + *

      + * + * @param viewHolder The ViewHolder to start dragging. It must be a direct child of + * RecyclerView. + * @see ItemTouchHelper.Callback#isItemViewSwipeEnabled() + */ + public void startDrag(ViewHolder viewHolder) { + if (!mCallback.hasDragFlag(mRecyclerView, viewHolder)) { + Log.e(TAG, "Start drag has been called but swiping is not enabled"); + return; + } + if (viewHolder.itemView.getParent() != mRecyclerView) { + Log.e(TAG, "Start drag has been called with a view holder which is not a child of " + + "the RecyclerView which is controlled by this ItemTouchHelper."); + return; + } + obtainVelocityTracker(); + mDx = mDy = 0f; + select(viewHolder, ACTION_STATE_DRAG); + } + + /** + * Starts swiping the provided ViewHolder. By default, ItemTouchHelper starts swiping a View + * when user swipes their finger (or mouse pointer) over the View. You can disable this + * behavior + * by overriding {@link ItemTouchHelper.Callback} + *

      + * For this method to work: + *

        + *
      • The provided ViewHolder must be a child of the RecyclerView to which this + * ItemTouchHelper is attached.
      • + *
      • {@link ItemTouchHelper.Callback} must have swiping enabled.
      • + *
      • There must be a previous touch event that was reported to the ItemTouchHelper + * through RecyclerView's ItemTouchListener mechanism. As long as no other ItemTouchListener + * grabs previous events, this should work as expected.
      • + *
      + * + * For example, if you would like to let your user to be able to swipe an Item by touching one + * of its descendants, you may implement it as follows: + *
      +     *     viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() {
      +     *         public boolean onTouch(View v, MotionEvent event) {
      +     *             if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
      +     *                 mItemTouchHelper.startSwipe(viewHolder);
      +     *             }
      +     *             return false;
      +     *         }
      +     *     });
      +     * 
      + * + * @param viewHolder The ViewHolder to start swiping. It must be a direct child of + * RecyclerView. + */ + public void startSwipe(ViewHolder viewHolder) { + if (!mCallback.hasSwipeFlag(mRecyclerView, viewHolder)) { + Log.e(TAG, "Start swipe has been called but dragging is not enabled"); + return; + } + if (viewHolder.itemView.getParent() != mRecyclerView) { + Log.e(TAG, "Start swipe has been called with a view holder which is not a child of " + + "the RecyclerView controlled by this ItemTouchHelper."); + return; + } + obtainVelocityTracker(); + mDx = mDy = 0f; + select(viewHolder, ACTION_STATE_SWIPE); + } + + private RecoverAnimation findAnimation(MotionEvent event) { + if (mRecoverAnimations.isEmpty()) { + return null; + } + View target = findChildView(event); + for (int i = mRecoverAnimations.size() - 1; i >= 0; i--) { + final RecoverAnimation anim = mRecoverAnimations.get(i); + if (anim.mViewHolder.itemView == target) { + return anim; + } + } + return null; + } + + private void updateDxDy(MotionEvent ev, int directionFlags, int pointerIndex) { + final float x = ev.getX(pointerIndex); + final float y = ev.getY(pointerIndex); + + // Calculate the distance moved + mDx = x - mInitialTouchX; + mDy = y - mInitialTouchY; + if ((directionFlags & LEFT) == 0) { + mDx = Math.max(0, mDx); + } + if ((directionFlags & RIGHT) == 0) { + mDx = Math.min(0, mDx); + } + if ((directionFlags & UP) == 0) { + mDy = Math.max(0, mDy); + } + if ((directionFlags & DOWN) == 0) { + mDy = Math.min(0, mDy); + } + } + + private int swipeIfNecessary(ViewHolder viewHolder) { + if (mActionState == ACTION_STATE_DRAG) { + return 0; + } + final int originalMovementFlags = mCallback.getMovementFlags(mRecyclerView, viewHolder); + final int absoluteMovementFlags = mCallback.convertToAbsoluteDirection( + originalMovementFlags, + ViewCompat.getLayoutDirection(mRecyclerView)); + final int flags = (absoluteMovementFlags + & ACTION_MODE_SWIPE_MASK) >> (ACTION_STATE_SWIPE * DIRECTION_FLAG_COUNT); + if (flags == 0) { + return 0; + } + final int originalFlags = (originalMovementFlags + & ACTION_MODE_SWIPE_MASK) >> (ACTION_STATE_SWIPE * DIRECTION_FLAG_COUNT); + int swipeDir; + if (Math.abs(mDx) > Math.abs(mDy)) { + if ((swipeDir = checkHorizontalSwipe(viewHolder, flags)) > 0) { + // if swipe dir is not in original flags, it should be the relative direction + if ((originalFlags & swipeDir) == 0) { + // convert to relative + return Callback.convertToRelativeDirection(swipeDir, + ViewCompat.getLayoutDirection(mRecyclerView)); + } + return swipeDir; + } + if ((swipeDir = checkVerticalSwipe(viewHolder, flags)) > 0) { + return swipeDir; + } + } else { + if ((swipeDir = checkVerticalSwipe(viewHolder, flags)) > 0) { + return swipeDir; + } + if ((swipeDir = checkHorizontalSwipe(viewHolder, flags)) > 0) { + // if swipe dir is not in original flags, it should be the relative direction + if ((originalFlags & swipeDir) == 0) { + // convert to relative + return Callback.convertToRelativeDirection(swipeDir, + ViewCompat.getLayoutDirection(mRecyclerView)); + } + return swipeDir; + } + } + return 0; + } + + private int checkHorizontalSwipe(ViewHolder viewHolder, int flags) { + if ((flags & (LEFT | RIGHT)) != 0) { + final int dirFlag = mDx > 0 ? RIGHT : LEFT; + if (mVelocityTracker != null && mActivePointerId > -1) { + mVelocityTracker.computeCurrentVelocity(PIXELS_PER_SECOND, + mCallback.getSwipeVelocityThreshold(mMaxSwipeVelocity)); + final float xVelocity = VelocityTrackerCompat + .getXVelocity(mVelocityTracker, mActivePointerId); + final float yVelocity = VelocityTrackerCompat + .getYVelocity(mVelocityTracker, mActivePointerId); + final int velDirFlag = xVelocity > 0f ? RIGHT : LEFT; + final float absXVelocity = Math.abs(xVelocity); + if ((velDirFlag & flags) != 0 && dirFlag == velDirFlag && + absXVelocity >= mCallback.getSwipeEscapeVelocity(mSwipeEscapeVelocity) && + absXVelocity > Math.abs(yVelocity)) { + return velDirFlag; + } + } + + final float threshold = mRecyclerView.getWidth() * mCallback + .getSwipeThreshold(viewHolder); + + if ((flags & dirFlag) != 0 && Math.abs(mDx) > threshold) { + return dirFlag; + } + } + return 0; + } + + private int checkVerticalSwipe(ViewHolder viewHolder, int flags) { + if ((flags & (UP | DOWN)) != 0) { + final int dirFlag = mDy > 0 ? DOWN : UP; + if (mVelocityTracker != null && mActivePointerId > -1) { + mVelocityTracker.computeCurrentVelocity(PIXELS_PER_SECOND, + mCallback.getSwipeVelocityThreshold(mMaxSwipeVelocity)); + final float xVelocity = VelocityTrackerCompat + .getXVelocity(mVelocityTracker, mActivePointerId); + final float yVelocity = VelocityTrackerCompat + .getYVelocity(mVelocityTracker, mActivePointerId); + final int velDirFlag = yVelocity > 0f ? DOWN : UP; + final float absYVelocity = Math.abs(yVelocity); + if ((velDirFlag & flags) != 0 && velDirFlag == dirFlag && + absYVelocity >= mCallback.getSwipeEscapeVelocity(mSwipeEscapeVelocity) && + absYVelocity > Math.abs(xVelocity)) { + return velDirFlag; + } + } + + final float threshold = mRecyclerView.getHeight() * mCallback + .getSwipeThreshold(viewHolder); + if ((flags & dirFlag) != 0 && Math.abs(mDy) > threshold) { + return dirFlag; + } + } + return 0; + } + + private void addChildDrawingOrderCallback() { + if (Build.VERSION.SDK_INT >= 21) { + return;// we use elevation on Lollipop + } + if (mChildDrawingOrderCallback == null) { + mChildDrawingOrderCallback = new RecyclerView.ChildDrawingOrderCallback() { + @Override + public int onGetChildDrawingOrder(int childCount, int i) { + if (mOverdrawChild == null) { + return i; + } + int childPosition = mOverdrawChildPosition; + if (childPosition == -1) { + childPosition = mRecyclerView.indexOfChild(mOverdrawChild); + mOverdrawChildPosition = childPosition; + } + if (i == childCount - 1) { + return childPosition; + } + return i < childPosition ? i : i + 1; + } + }; + } + mRecyclerView.setChildDrawingOrderCallback(mChildDrawingOrderCallback); + } + + private void removeChildDrawingOrderCallbackIfNecessary(View view) { + if (view == mOverdrawChild) { + mOverdrawChild = null; + // only remove if we've added + if (mChildDrawingOrderCallback != null) { + mRecyclerView.setChildDrawingOrderCallback(null); + } + } + } + + /** + * An interface which can be implemented by LayoutManager for better integration with + * {@link ItemTouchHelper}. + */ + public static interface ViewDropHandler { + + /** + * Called by the {@link ItemTouchHelper} after a View is dropped over another View. + *

      + * A LayoutManager should implement this interface to get ready for the upcoming move + * operation. + *

      + * For example, LinearLayoutManager sets up a "scrollToPositionWithOffset" calls so that + * the View under drag will be used as an anchor View while calculating the next layout, + * making layout stay consistent. + * + * @param view The View which is being dragged. It is very likely that user is still + * dragging this View so there might be other + * {@link #prepareForDrop(View, View, int, int)} after this one. + * @param target The target view which is being dropped on. + * @param x The left offset of the View that is being dragged. This value + * includes the movement caused by the user. + * @param y The top offset of the View that is being dragged. This value + * includes the movement caused by the user. + */ + public void prepareForDrop(View view, View target, int x, int y); + } + + /** + * This class is the contract between ItemTouchHelper and your application. It lets you control + * which touch behaviors are enabled per each ViewHolder and also receive callbacks when user + * performs these actions. + *

      + * To control which actions user can take on each view, you should override + * {@link #getMovementFlags(RecyclerView, ViewHolder)} and return appropriate set + * of direction flags. ({@link #LEFT}, {@link #RIGHT}, {@link #START}, {@link #END}, + * {@link #UP}, {@link #DOWN}). You can use + * {@link #makeMovementFlags(int, int)} to easily construct it. Alternatively, you can use + * {@link SimpleCallback}. + *

      + * If user drags an item, ItemTouchHelper will call + * {@link Callback#onMove(RecyclerView, ViewHolder, ViewHolder) + * onMove(recyclerView, dragged, target)}. + * Upon receiving this callback, you should move the item from the old position + * ({@code dragged.getAdapterPosition()}) to new position ({@code target.getAdapterPosition()}) + * in your adapter and also call {@link RecyclerView.Adapter#notifyItemMoved(int, int)}. + * To control where a View can be dropped, you can override + * {@link #canDropOver(RecyclerView, ViewHolder, ViewHolder)}. When a + * dragging View overlaps multiple other views, Callback chooses the closest View with which + * dragged View might have changed positions. Although this approach works for many use cases, + * if you have a custom LayoutManager, you can override + * {@link #chooseDropTarget(ViewHolder, java.util.List, int, int)} to select a + * custom drop target. + *

      + * When a View is swiped, ItemTouchHelper animates it until it goes out of bounds, then calls + * {@link #onSwiped(ViewHolder, int)}. At this point, you should update your + * adapter (e.g. remove the item) and call related Adapter#notify event. + */ + @SuppressWarnings("UnusedParameters") + public abstract static class Callback { + + public static final int DEFAULT_DRAG_ANIMATION_DURATION = 200; + + public static final int DEFAULT_SWIPE_ANIMATION_DURATION = 250; + + static final int RELATIVE_DIR_FLAGS = START | END | + ((START | END) << DIRECTION_FLAG_COUNT) | + ((START | END) << (2 * DIRECTION_FLAG_COUNT)); + + private static final ItemTouchUIUtil sUICallback; + + private static final int ABS_HORIZONTAL_DIR_FLAGS = LEFT | RIGHT | + ((LEFT | RIGHT) << DIRECTION_FLAG_COUNT) | + ((LEFT | RIGHT) << (2 * DIRECTION_FLAG_COUNT)); + + private static final Interpolator sDragScrollInterpolator = new Interpolator() { + @Override + public float getInterpolation(float t) { + return t * t * t * t * t; + } + }; + + private static final Interpolator sDragViewScrollCapInterpolator = new Interpolator() { + @Override + public float getInterpolation(float t) { + t -= 1.0f; + return t * t * t * t * t + 1.0f; + } + }; + + /** + * Drag scroll speed keeps accelerating until this many milliseconds before being capped. + */ + private static final long DRAG_SCROLL_ACCELERATION_LIMIT_TIME_MS = 2000; + + private int mCachedMaxScrollSpeed = -1; + + static { + if (Build.VERSION.SDK_INT >= 21) { + sUICallback = new ItemTouchUIUtilImpl.Lollipop(); + } else if (Build.VERSION.SDK_INT >= 11) { + sUICallback = new ItemTouchUIUtilImpl.Honeycomb(); + } else { + sUICallback = new ItemTouchUIUtilImpl.Gingerbread(); + } + } + + /** + * Returns the {@link ItemTouchUIUtil} that is used by the {@link Callback} class for + * visual + * changes on Views in response to user interactions. {@link ItemTouchUIUtil} has different + * implementations for different platform versions. + *

      + * By default, {@link Callback} applies these changes on + * {@link RecyclerView.ViewHolder#itemView}. + *

      + * For example, if you have a use case where you only want the text to move when user + * swipes over the view, you can do the following: + *

      +         *     public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder){
      +         *         getDefaultUIUtil().clearView(((ItemTouchViewHolder) viewHolder).textView);
      +         *     }
      +         *     public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
      +         *         if (viewHolder != null){
      +         *             getDefaultUIUtil().onSelected(((ItemTouchViewHolder) viewHolder).textView);
      +         *         }
      +         *     }
      +         *     public void onChildDraw(Canvas c, RecyclerView recyclerView,
      +         *             RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState,
      +         *             boolean isCurrentlyActive) {
      +         *         getDefaultUIUtil().onDraw(c, recyclerView,
      +         *                 ((ItemTouchViewHolder) viewHolder).textView, dX, dY,
      +         *                 actionState, isCurrentlyActive);
      +         *         return true;
      +         *     }
      +         *     public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
      +         *             RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState,
      +         *             boolean isCurrentlyActive) {
      +         *         getDefaultUIUtil().onDrawOver(c, recyclerView,
      +         *                 ((ItemTouchViewHolder) viewHolder).textView, dX, dY,
      +         *                 actionState, isCurrentlyActive);
      +         *         return true;
      +         *     }
      +         * 
      + * + * @return The {@link ItemTouchUIUtil} instance that is used by the {@link Callback} + */ + public static ItemTouchUIUtil getDefaultUIUtil() { + return sUICallback; + } + + /** + * Replaces a movement direction with its relative version by taking layout direction into + * account. + * + * @param flags The flag value that include any number of movement flags. + * @param layoutDirection The layout direction of the View. Can be obtained from + * {@link ViewCompat#getLayoutDirection(android.view.View)}. + * @return Updated flags which uses relative flags ({@link #START}, {@link #END}) instead + * of {@link #LEFT}, {@link #RIGHT}. + * @see #convertToAbsoluteDirection(int, int) + */ + public static int convertToRelativeDirection(int flags, int layoutDirection) { + int masked = flags & ABS_HORIZONTAL_DIR_FLAGS; + if (masked == 0) { + return flags;// does not have any abs flags, good. + } + flags &= ~masked; //remove left / right. + if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) { + // no change. just OR with 2 bits shifted mask and return + flags |= masked << 2; // START is 2 bits after LEFT, END is 2 bits after RIGHT. + return flags; + } else { + // add RIGHT flag as START + flags |= ((masked << 1) & ~ABS_HORIZONTAL_DIR_FLAGS); + // first clean RIGHT bit then add LEFT flag as END + flags |= ((masked << 1) & ABS_HORIZONTAL_DIR_FLAGS) << 2; + } + return flags; + } + + /** + * Convenience method to create movement flags. + *

      + * For instance, if you want to let your items be drag & dropped vertically and swiped + * left to be dismissed, you can call this method with: + * makeMovementFlags(UP | DOWN, LEFT); + * + * @param dragFlags The directions in which the item can be dragged. + * @param swipeFlags The directions in which the item can be swiped. + * @return Returns an integer composed of the given drag and swipe flags. + */ + public static int makeMovementFlags(int dragFlags, int swipeFlags) { + return makeFlag(ACTION_STATE_IDLE, swipeFlags | dragFlags) | + makeFlag(ACTION_STATE_SWIPE, swipeFlags) | makeFlag(ACTION_STATE_DRAG, + dragFlags); + } + + /** + * Shifts the given direction flags to the offset of the given action state. + * + * @param actionState The action state you want to get flags in. Should be one of + * {@link #ACTION_STATE_IDLE}, {@link #ACTION_STATE_SWIPE} or + * {@link #ACTION_STATE_DRAG}. + * @param directions The direction flags. Can be composed from {@link #UP}, {@link #DOWN}, + * {@link #RIGHT}, {@link #LEFT} {@link #START} and {@link #END}. + * @return And integer that represents the given directions in the provided actionState. + */ + public static int makeFlag(int actionState, int directions) { + return directions << (actionState * DIRECTION_FLAG_COUNT); + } + + /** + * Should return a composite flag which defines the enabled move directions in each state + * (idle, swiping, dragging). + *

      + * Instead of composing this flag manually, you can use {@link #makeMovementFlags(int, + * int)} + * or {@link #makeFlag(int, int)}. + *

      + * This flag is composed of 3 sets of 8 bits, where first 8 bits are for IDLE state, next + * 8 bits are for SWIPE state and third 8 bits are for DRAG state. + * Each 8 bit sections can be constructed by simply OR'ing direction flags defined in + * {@link ItemTouchHelper}. + *

      + * For example, if you want it to allow swiping LEFT and RIGHT but only allow starting to + * swipe by swiping RIGHT, you can return: + *

      +         *      makeFlag(ACTION_STATE_IDLE, RIGHT) | makeFlag(ACTION_STATE_SWIPE, LEFT | RIGHT);
      +         * 
      + * This means, allow right movement while IDLE and allow right and left movement while + * swiping. + * + * @param recyclerView The RecyclerView to which ItemTouchHelper is attached. + * @param viewHolder The ViewHolder for which the movement information is necessary. + * @return flags specifying which movements are allowed on this ViewHolder. + * @see #makeMovementFlags(int, int) + * @see #makeFlag(int, int) + */ + public abstract int getMovementFlags(RecyclerView recyclerView, + ViewHolder viewHolder); + + /** + * Converts a given set of flags to absolution direction which means {@link #START} and + * {@link #END} are replaced with {@link #LEFT} and {@link #RIGHT} depending on the layout + * direction. + * + * @param flags The flag value that include any number of movement flags. + * @param layoutDirection The layout direction of the RecyclerView. + * @return Updated flags which includes only absolute direction values. + */ + public int convertToAbsoluteDirection(int flags, int layoutDirection) { + int masked = flags & RELATIVE_DIR_FLAGS; + if (masked == 0) { + return flags;// does not have any relative flags, good. + } + flags &= ~masked; //remove start / end + if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) { + // no change. just OR with 2 bits shifted mask and return + flags |= masked >> 2; // START is 2 bits after LEFT, END is 2 bits after RIGHT. + return flags; + } else { + // add START flag as RIGHT + flags |= ((masked >> 1) & ~RELATIVE_DIR_FLAGS); + // first clean start bit then add END flag as LEFT + flags |= ((masked >> 1) & RELATIVE_DIR_FLAGS) >> 2; + } + return flags; + } + + final int getAbsoluteMovementFlags(RecyclerView recyclerView, + ViewHolder viewHolder) { + final int flags = getMovementFlags(recyclerView, viewHolder); + return convertToAbsoluteDirection(flags, ViewCompat.getLayoutDirection(recyclerView)); + } + + private boolean hasDragFlag(RecyclerView recyclerView, ViewHolder viewHolder) { + final int flags = getAbsoluteMovementFlags(recyclerView, viewHolder); + return (flags & ACTION_MODE_DRAG_MASK) != 0; + } + + private boolean hasSwipeFlag(RecyclerView recyclerView, + ViewHolder viewHolder) { + final int flags = getAbsoluteMovementFlags(recyclerView, viewHolder); + return (flags & ACTION_MODE_SWIPE_MASK) != 0; + } + + /** + * Return true if the current ViewHolder can be dropped over the the target ViewHolder. + *

      + * This method is used when selecting drop target for the dragged View. After Views are + * eliminated either via bounds check or via this method, resulting set of views will be + * passed to {@link #chooseDropTarget(ViewHolder, java.util.List, int, int)}. + *

      + * Default implementation returns true. + * + * @param recyclerView The RecyclerView to which ItemTouchHelper is attached to. + * @param current The ViewHolder that user is dragging. + * @param target The ViewHolder which is below the dragged ViewHolder. + * @return True if the dragged ViewHolder can be replaced with the target ViewHolder, false + * otherwise. + */ + public boolean canDropOver(RecyclerView recyclerView, ViewHolder current, + ViewHolder target) { + return true; + } + + /** + * Called when ItemTouchHelper wants to move the dragged item from its old position to + * the new position. + *

      + * If this method returns true, ItemTouchHelper assumes {@code viewHolder} has been moved + * to the adapter position of {@code target} ViewHolder + * ({@link ViewHolder#getAdapterPosition() + * ViewHolder#getAdapterPosition()}). + *

      + * If you don't support drag & drop, this method will never be called. + * + * @param recyclerView The RecyclerView to which ItemTouchHelper is attached to. + * @param viewHolder The ViewHolder which is being dragged by the user. + * @param target The ViewHolder over which the currently active item is being + * dragged. + * @return True if the {@code viewHolder} has been moved to the adapter position of + * {@code target}. + * @see #onMoved(RecyclerView, ViewHolder, int, ViewHolder, int, int, int) + */ + public abstract boolean onMove(RecyclerView recyclerView, + ViewHolder viewHolder, ViewHolder target); + + /** + * Returns whether ItemTouchHelper should start a drag and drop operation if an item is + * long pressed. + *

      + * Default value returns true but you may want to disable this if you want to start + * dragging on a custom view touch using {@link #startDrag(ViewHolder)}. + * + * @return True if ItemTouchHelper should start dragging an item when it is long pressed, + * false otherwise. Default value is true. + * @see #startDrag(ViewHolder) + */ + public boolean isLongPressDragEnabled() { + return true; + } + + /** + * Returns whether ItemTouchHelper should start a swipe operation if a pointer is swiped + * over the View. + *

      + * Default value returns true but you may want to disable this if you want to start + * swiping on a custom view touch using {@link #startSwipe(ViewHolder)}. + * + * @return True if ItemTouchHelper should start swiping an item when user swipes a pointer + * over the View, false otherwise. Default value is true. + * @see #startSwipe(ViewHolder) + */ + public boolean isItemViewSwipeEnabled() { + return true; + } + + /** + * When finding views under a dragged view, by default, ItemTouchHelper searches for views + * that overlap with the dragged View. By overriding this method, you can extend or shrink + * the search box. + * + * @return The extra margin to be added to the hit box of the dragged View. + */ + public int getBoundingBoxMargin() { + return 0; + } + + /** + * Returns the fraction that the user should move the View to be considered as swiped. + * The fraction is calculated with respect to RecyclerView's bounds. + *

      + * Default value is .5f, which means, to swipe a View, user must move the View at least + * half of RecyclerView's width or height, depending on the swipe direction. + * + * @param viewHolder The ViewHolder that is being dragged. + * @return A float value that denotes the fraction of the View size. Default value + * is .5f . + */ + public float getSwipeThreshold(ViewHolder viewHolder) { + return .5f; + } + + /** + * Returns the fraction that the user should move the View to be considered as it is + * dragged. After a view is moved this amount, ItemTouchHelper starts checking for Views + * below it for a possible drop. + * + * @param viewHolder The ViewHolder that is being dragged. + * @return A float value that denotes the fraction of the View size. Default value is + * .5f . + */ + public float getMoveThreshold(ViewHolder viewHolder) { + return .5f; + } + + /** + * Defines the minimum velocity which will be considered as a swipe action by the user. + *

      + * You can increase this value to make it harder to swipe or decrease it to make it easier. + * Keep in mind that ItemTouchHelper also checks the perpendicular velocity and makes sure + * current direction velocity is larger then the perpendicular one. Otherwise, user's + * movement is ambiguous. You can change the threshold by overriding + * {@link #getSwipeVelocityThreshold(float)}. + *

      + * The velocity is calculated in pixels per second. + *

      + * The default framework value is passed as a parameter so that you can modify it with a + * multiplier. + * + * @param defaultValue The default value (in pixels per second) used by the + * ItemTouchHelper. + * @return The minimum swipe velocity. The default implementation returns the + * defaultValue parameter. + * @see #getSwipeVelocityThreshold(float) + * @see #getSwipeThreshold(ViewHolder) + */ + public float getSwipeEscapeVelocity(float defaultValue) { + return defaultValue; + } + + /** + * Defines the maximum velocity ItemTouchHelper will ever calculate for pointer movements. + *

      + * To consider a movement as swipe, ItemTouchHelper requires it to be larger than the + * perpendicular movement. If both directions reach to the max threshold, none of them will + * be considered as a swipe because it is usually an indication that user rather tried to + * scroll then swipe. + *

      + * The velocity is calculated in pixels per second. + *

      + * You can customize this behavior by changing this method. If you increase the value, it + * will be easier for the user to swipe diagonally and if you decrease the value, user will + * need to make a rather straight finger movement to trigger a swipe. + * + * @param defaultValue The default value(in pixels per second) used by the ItemTouchHelper. + * @return The velocity cap for pointer movements. The default implementation returns the + * defaultValue parameter. + * @see #getSwipeEscapeVelocity(float) + */ + public float getSwipeVelocityThreshold(float defaultValue) { + return defaultValue; + } + + /** + * Called by ItemTouchHelper to select a drop target from the list of ViewHolders that + * are under the dragged View. + *

      + * Default implementation filters the View with which dragged item have changed position + * in the drag direction. For instance, if the view is dragged UP, it compares the + * view.getTop() of the two views before and after drag started. If that value + * is different, the target view passes the filter. + *

      + * Among these Views which pass the test, the one closest to the dragged view is chosen. + *

      + * This method is called on the main thread every time user moves the View. If you want to + * override it, make sure it does not do any expensive operations. + * + * @param selected The ViewHolder being dragged by the user. + * @param dropTargets The list of ViewHolder that are under the dragged View and + * candidate as a drop. + * @param curX The updated left value of the dragged View after drag translations + * are applied. This value does not include margins added by + * {@link RecyclerView.ItemDecoration}s. + * @param curY The updated top value of the dragged View after drag translations + * are applied. This value does not include margins added by + * {@link RecyclerView.ItemDecoration}s. + * @return A ViewHolder to whose position the dragged ViewHolder should be + * moved to. + */ + public ViewHolder chooseDropTarget(ViewHolder selected, + List dropTargets, int curX, int curY) { + int right = curX + selected.itemView.getWidth(); + int bottom = curY + selected.itemView.getHeight(); + ViewHolder winner = null; + int winnerScore = -1; + final int dx = curX - selected.itemView.getLeft(); + final int dy = curY - selected.itemView.getTop(); + final int targetsSize = dropTargets.size(); + for (int i = 0; i < targetsSize; i++) { + final ViewHolder target = dropTargets.get(i); + if (dx > 0) { + int diff = target.itemView.getRight() - right; + if (diff < 0 && target.itemView.getRight() > selected.itemView.getRight()) { + final int score = Math.abs(diff); + if (score > winnerScore) { + winnerScore = score; + winner = target; + } + } + } + if (dx < 0) { + int diff = target.itemView.getLeft() - curX; + if (diff > 0 && target.itemView.getLeft() < selected.itemView.getLeft()) { + final int score = Math.abs(diff); + if (score > winnerScore) { + winnerScore = score; + winner = target; + } + } + } + if (dy < 0) { + int diff = target.itemView.getTop() - curY; + if (diff > 0 && target.itemView.getTop() < selected.itemView.getTop()) { + final int score = Math.abs(diff); + if (score > winnerScore) { + winnerScore = score; + winner = target; + } + } + } + + if (dy > 0) { + int diff = target.itemView.getBottom() - bottom; + if (diff < 0 && target.itemView.getBottom() > selected.itemView.getBottom()) { + final int score = Math.abs(diff); + if (score > winnerScore) { + winnerScore = score; + winner = target; + } + } + } + } + return winner; + } + + /** + * Called when a ViewHolder is swiped by the user. + *

      + * If you are returning relative directions ({@link #START} , {@link #END}) from the + * {@link #getMovementFlags(RecyclerView, ViewHolder)} method, this method + * will also use relative directions. Otherwise, it will use absolute directions. + *

      + * If you don't support swiping, this method will never be called. + *

      + * ItemTouchHelper will keep a reference to the View until it is detached from + * RecyclerView. + * As soon as it is detached, ItemTouchHelper will call + * {@link #clearView(RecyclerView, ViewHolder)}. + * + * @param viewHolder The ViewHolder which has been swiped by the user. + * @param direction The direction to which the ViewHolder is swiped. It is one of + * {@link #UP}, {@link #DOWN}, + * {@link #LEFT} or {@link #RIGHT}. If your + * {@link #getMovementFlags(RecyclerView, ViewHolder)} + * method + * returned relative flags instead of {@link #LEFT} / {@link #RIGHT}; + * `direction` will be relative as well. ({@link #START} or {@link + * #END}). + */ + public abstract void onSwiped(ViewHolder viewHolder, int direction); + + /** + * Called when the ViewHolder swiped or dragged by the ItemTouchHelper is changed. + *

      + * If you override this method, you should call super. + * + * @param viewHolder The new ViewHolder that is being swiped or dragged. Might be null if + * it is cleared. + * @param actionState One of {@link ItemTouchHelper#ACTION_STATE_IDLE}, + * {@link ItemTouchHelper#ACTION_STATE_SWIPE} or + * {@link ItemTouchHelper#ACTION_STATE_DRAG}. + * @see #clearView(RecyclerView, RecyclerView.ViewHolder) + */ + public void onSelectedChanged(ViewHolder viewHolder, int actionState) { + if (viewHolder != null) { + sUICallback.onSelected(viewHolder.itemView); + } + } + + private int getMaxDragScroll(RecyclerView recyclerView) { + if (mCachedMaxScrollSpeed == -1) { + mCachedMaxScrollSpeed = recyclerView.getResources().getDimensionPixelSize( + R.dimen.item_touch_helper_max_drag_scroll_per_frame); + } + return mCachedMaxScrollSpeed; + } + + /** + * Called when {@link #onMove(RecyclerView, ViewHolder, ViewHolder)} returns true. + *

      + * ItemTouchHelper does not create an extra Bitmap or View while dragging, instead, it + * modifies the existing View. Because of this reason, it is important that the View is + * still part of the layout after it is moved. This may not work as intended when swapped + * Views are close to RecyclerView bounds or there are gaps between them (e.g. other Views + * which were not eligible for dropping over). + *

      + * This method is responsible to give necessary hint to the LayoutManager so that it will + * keep the View in visible area. For example, for LinearLayoutManager, this is as simple + * as calling {@link LinearLayoutManager#scrollToPositionWithOffset(int, int)}. + * + * Default implementation calls {@link RecyclerView#scrollToPosition(int)} if the View's + * new position is likely to be out of bounds. + *

      + * It is important to ensure the ViewHolder will stay visible as otherwise, it might be + * removed by the LayoutManager if the move causes the View to go out of bounds. In that + * case, drag will end prematurely. + * + * @param recyclerView The RecyclerView controlled by the ItemTouchHelper. + * @param viewHolder The ViewHolder under user's control. + * @param fromPos The previous adapter position of the dragged item (before it was + * moved). + * @param target The ViewHolder on which the currently active item has been dropped. + * @param toPos The new adapter position of the dragged item. + * @param x The updated left value of the dragged View after drag translations + * are applied. This value does not include margins added by + * {@link RecyclerView.ItemDecoration}s. + * @param y The updated top value of the dragged View after drag translations + * are applied. This value does not include margins added by + * {@link RecyclerView.ItemDecoration}s. + */ + public void onMoved(final RecyclerView recyclerView, + final ViewHolder viewHolder, int fromPos, final ViewHolder target, int toPos, int x, + int y) { + final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); + if (layoutManager instanceof ViewDropHandler) { + ((ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView, + target.itemView, x, y); + return; + } + + // if layout manager cannot handle it, do some guesswork + if (layoutManager.canScrollHorizontally()) { + final int minLeft = layoutManager.getDecoratedLeft(target.itemView); + if (minLeft <= recyclerView.getPaddingLeft()) { + recyclerView.scrollToPosition(toPos); + } + final int maxRight = layoutManager.getDecoratedRight(target.itemView); + if (maxRight >= recyclerView.getWidth() - recyclerView.getPaddingRight()) { + recyclerView.scrollToPosition(toPos); + } + } + + if (layoutManager.canScrollVertically()) { + final int minTop = layoutManager.getDecoratedTop(target.itemView); + if (minTop <= recyclerView.getPaddingTop()) { + recyclerView.scrollToPosition(toPos); + } + final int maxBottom = layoutManager.getDecoratedBottom(target.itemView); + if (maxBottom >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) { + recyclerView.scrollToPosition(toPos); + } + } + } + + private void onDraw(Canvas c, RecyclerView parent, ViewHolder selected, + List recoverAnimationList, + int actionState, float dX, float dY) { + final int recoverAnimSize = recoverAnimationList.size(); + for (int i = 0; i < recoverAnimSize; i++) { + final ItemTouchHelper.RecoverAnimation anim = recoverAnimationList.get(i); + anim.update(); + final int count = c.save(); + onChildDraw(c, parent, anim.mViewHolder, anim.mX, anim.mY, anim.mActionState, + false); + c.restoreToCount(count); + } + if (selected != null) { + final int count = c.save(); + onChildDraw(c, parent, selected, dX, dY, actionState, true); + c.restoreToCount(count); + } + } + + private void onDrawOver(Canvas c, RecyclerView parent, ViewHolder selected, + List recoverAnimationList, + int actionState, float dX, float dY) { + final int recoverAnimSize = recoverAnimationList.size(); + for (int i = 0; i < recoverAnimSize; i++) { + final ItemTouchHelper.RecoverAnimation anim = recoverAnimationList.get(i); + final int count = c.save(); + onChildDrawOver(c, parent, anim.mViewHolder, anim.mX, anim.mY, anim.mActionState, + false); + c.restoreToCount(count); + } + if (selected != null) { + final int count = c.save(); + onChildDrawOver(c, parent, selected, dX, dY, actionState, true); + c.restoreToCount(count); + } + boolean hasRunningAnimation = false; + for (int i = recoverAnimSize - 1; i >= 0; i--) { + final RecoverAnimation anim = recoverAnimationList.get(i); + if (anim.mEnded && !anim.mIsPendingCleanup) { + recoverAnimationList.remove(i); + } else if (!anim.mEnded) { + hasRunningAnimation = true; + } + } + if (hasRunningAnimation) { + parent.invalidate(); + } + } + + /** + * Called by the ItemTouchHelper when the user interaction with an element is over and it + * also completed its animation. + *

      + * This is a good place to clear all changes on the View that was done in + * {@link #onSelectedChanged(RecyclerView.ViewHolder, int)}, + * {@link #onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int, + * boolean)} or + * {@link #onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, boolean)}. + * + * @param recyclerView The RecyclerView which is controlled by the ItemTouchHelper. + * @param viewHolder The View that was interacted by the user. + */ + public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) { + sUICallback.clearView(viewHolder.itemView); + } + + /** + * Called by ItemTouchHelper on RecyclerView's onDraw callback. + *

      + * If you would like to customize how your View's respond to user interactions, this is + * a good place to override. + *

      + * Default implementation translates the child by the given dX, + * dY. + * ItemTouchHelper also takes care of drawing the child after other children if it is being + * dragged. This is done using child re-ordering mechanism. On platforms prior to L, this + * is + * achieved via {@link android.view.ViewGroup#getChildDrawingOrder(int, int)} and on L + * and after, it changes View's elevation value to be greater than all other children.) + * + * @param c The canvas which RecyclerView is drawing its children + * @param recyclerView The RecyclerView to which ItemTouchHelper is attached to + * @param viewHolder The ViewHolder which is being interacted by the User or it was + * interacted and simply animating to its original position + * @param dX The amount of horizontal displacement caused by user's action + * @param dY The amount of vertical displacement caused by user's action + * @param actionState The type of interaction on the View. Is either {@link + * #ACTION_STATE_DRAG} or {@link #ACTION_STATE_SWIPE}. + * @param isCurrentlyActive True if this view is currently being controlled by the user or + * false it is simply animating back to its original state. + * @see #onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, + * boolean) + */ + public void onChildDraw(Canvas c, RecyclerView recyclerView, + ViewHolder viewHolder, + float dX, float dY, int actionState, boolean isCurrentlyActive) { + sUICallback.onDraw(c, recyclerView, viewHolder.itemView, dX, dY, actionState, + isCurrentlyActive); + } + + /** + * Called by ItemTouchHelper on RecyclerView's onDraw callback. + *

      + * If you would like to customize how your View's respond to user interactions, this is + * a good place to override. + *

      + * Default implementation translates the child by the given dX, + * dY. + * ItemTouchHelper also takes care of drawing the child after other children if it is being + * dragged. This is done using child re-ordering mechanism. On platforms prior to L, this + * is + * achieved via {@link android.view.ViewGroup#getChildDrawingOrder(int, int)} and on L + * and after, it changes View's elevation value to be greater than all other children.) + * + * @param c The canvas which RecyclerView is drawing its children + * @param recyclerView The RecyclerView to which ItemTouchHelper is attached to + * @param viewHolder The ViewHolder which is being interacted by the User or it was + * interacted and simply animating to its original position + * @param dX The amount of horizontal displacement caused by user's action + * @param dY The amount of vertical displacement caused by user's action + * @param actionState The type of interaction on the View. Is either {@link + * #ACTION_STATE_DRAG} or {@link #ACTION_STATE_SWIPE}. + * @param isCurrentlyActive True if this view is currently being controlled by the user or + * false it is simply animating back to its original state. + * @see #onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, + * boolean) + */ + public void onChildDrawOver(Canvas c, RecyclerView recyclerView, + ViewHolder viewHolder, + float dX, float dY, int actionState, boolean isCurrentlyActive) { + sUICallback.onDrawOver(c, recyclerView, viewHolder.itemView, dX, dY, actionState, + isCurrentlyActive); + } + + /** + * Called by the ItemTouchHelper when user action finished on a ViewHolder and now the View + * will be animated to its final position. + *

      + * Default implementation uses ItemAnimator's duration values. If + * animationType is {@link #ANIMATION_TYPE_DRAG}, it returns + * {@link RecyclerView.ItemAnimator#getMoveDuration()}, otherwise, it returns + * {@link RecyclerView.ItemAnimator#getRemoveDuration()}. If RecyclerView does not have + * any {@link RecyclerView.ItemAnimator} attached, this method returns + * {@code DEFAULT_DRAG_ANIMATION_DURATION} or {@code DEFAULT_SWIPE_ANIMATION_DURATION} + * depending on the animation type. + * + * @param recyclerView The RecyclerView to which the ItemTouchHelper is attached to. + * @param animationType The type of animation. Is one of {@link #ANIMATION_TYPE_DRAG}, + * {@link #ANIMATION_TYPE_SWIPE_CANCEL} or + * {@link #ANIMATION_TYPE_SWIPE_SUCCESS}. + * @param animateDx The horizontal distance that the animation will offset + * @param animateDy The vertical distance that the animation will offset + * @return The duration for the animation + */ + public long getAnimationDuration(RecyclerView recyclerView, int animationType, + float animateDx, float animateDy) { + final RecyclerView.ItemAnimator itemAnimator = recyclerView.getItemAnimator(); + if (itemAnimator == null) { + return animationType == ANIMATION_TYPE_DRAG ? DEFAULT_DRAG_ANIMATION_DURATION + : DEFAULT_SWIPE_ANIMATION_DURATION; + } else { + return animationType == ANIMATION_TYPE_DRAG ? itemAnimator.getMoveDuration() + : itemAnimator.getRemoveDuration(); + } + } + + /** + * Called by the ItemTouchHelper when user is dragging a view out of bounds. + *

      + * You can override this method to decide how much RecyclerView should scroll in response + * to this action. Default implementation calculates a value based on the amount of View + * out of bounds and the time it spent there. The longer user keeps the View out of bounds, + * the faster the list will scroll. Similarly, the larger portion of the View is out of + * bounds, the faster the RecyclerView will scroll. + * + * @param recyclerView The RecyclerView instance to which ItemTouchHelper is + * attached to. + * @param viewSize The total size of the View in scroll direction, excluding + * item decorations. + * @param viewSizeOutOfBounds The total size of the View that is out of bounds. This value + * is negative if the View is dragged towards left or top edge. + * @param totalSize The total size of RecyclerView in the scroll direction. + * @param msSinceStartScroll The time passed since View is kept out of bounds. + * @return The amount that RecyclerView should scroll. Keep in mind that this value will + * be passed to {@link RecyclerView#scrollBy(int, int)} method. + */ + public int interpolateOutOfBoundsScroll(RecyclerView recyclerView, + int viewSize, int viewSizeOutOfBounds, + int totalSize, long msSinceStartScroll) { + final int maxScroll = getMaxDragScroll(recyclerView); + final int absOutOfBounds = Math.abs(viewSizeOutOfBounds); + final int direction = (int) Math.signum(viewSizeOutOfBounds); + // might be negative if other direction + float outOfBoundsRatio = Math.min(1f, 1f * absOutOfBounds / viewSize); + final int cappedScroll = (int) (direction * maxScroll * + sDragViewScrollCapInterpolator.getInterpolation(outOfBoundsRatio)); + final float timeRatio; + if (msSinceStartScroll > DRAG_SCROLL_ACCELERATION_LIMIT_TIME_MS) { + timeRatio = 1f; + } else { + timeRatio = (float) msSinceStartScroll / DRAG_SCROLL_ACCELERATION_LIMIT_TIME_MS; + } + final int value = (int) (cappedScroll * sDragScrollInterpolator + .getInterpolation(timeRatio)); + if (value == 0) { + return viewSizeOutOfBounds > 0 ? 1 : -1; + } + return value; + } + } + + /** + * A simple wrapper to the default Callback which you can construct with drag and swipe + * directions and this class will handle the flag callbacks. You should still override onMove + * or + * onSwiped depending on your use case. + * + *

      +     * ItemTouchHelper mIth = new ItemTouchHelper(
      +     *     new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
      +     *         ItemTouchHelper.LEFT) {
      +     *         public abstract boolean onMove(RecyclerView recyclerView,
      +     *             ViewHolder viewHolder, ViewHolder target) {
      +     *             final int fromPos = viewHolder.getAdapterPosition();
      +     *             final int toPos = target.getAdapterPosition();
      +     *             // move item in `fromPos` to `toPos` in adapter.
      +     *             return true;// true if moved, false otherwise
      +     *         }
      +     *         public void onSwiped(ViewHolder viewHolder, int direction) {
      +     *             // remove from adapter
      +     *         }
      +     * });
      +     * 
      + */ + public abstract static class SimpleCallback extends Callback { + + private int mDefaultSwipeDirs; + + private int mDefaultDragDirs; + + /** + * Creates a Callback for the given drag and swipe allowance. These values serve as + * defaults + * and if you want to customize behavior per ViewHolder, you can override + * {@link #getSwipeDirs(RecyclerView, ViewHolder)} + * and / or {@link #getDragDirs(RecyclerView, ViewHolder)}. + * + * @param dragDirs Binary OR of direction flags in which the Views can be dragged. Must be + * composed of {@link #LEFT}, {@link #RIGHT}, {@link #START}, {@link + * #END}, + * {@link #UP} and {@link #DOWN}. + * @param swipeDirs Binary OR of direction flags in which the Views can be swiped. Must be + * composed of {@link #LEFT}, {@link #RIGHT}, {@link #START}, {@link + * #END}, + * {@link #UP} and {@link #DOWN}. + */ + public SimpleCallback(int dragDirs, int swipeDirs) { + mDefaultSwipeDirs = swipeDirs; + mDefaultDragDirs = dragDirs; + } + + /** + * Updates the default swipe directions. For example, you can use this method to toggle + * certain directions depending on your use case. + * + * @param defaultSwipeDirs Binary OR of directions in which the ViewHolders can be swiped. + */ + public void setDefaultSwipeDirs(int defaultSwipeDirs) { + mDefaultSwipeDirs = defaultSwipeDirs; + } + + /** + * Updates the default drag directions. For example, you can use this method to toggle + * certain directions depending on your use case. + * + * @param defaultDragDirs Binary OR of directions in which the ViewHolders can be dragged. + */ + public void setDefaultDragDirs(int defaultDragDirs) { + mDefaultDragDirs = defaultDragDirs; + } + + /** + * Returns the swipe directions for the provided ViewHolder. + * Default implementation returns the swipe directions that was set via constructor or + * {@link #setDefaultSwipeDirs(int)}. + * + * @param recyclerView The RecyclerView to which the ItemTouchHelper is attached to. + * @param viewHolder The RecyclerView for which the swipe direction is queried. + * @return A binary OR of direction flags. + */ + public int getSwipeDirs(RecyclerView recyclerView, ViewHolder viewHolder) { + return mDefaultSwipeDirs; + } + + /** + * Returns the drag directions for the provided ViewHolder. + * Default implementation returns the drag directions that was set via constructor or + * {@link #setDefaultDragDirs(int)}. + * + * @param recyclerView The RecyclerView to which the ItemTouchHelper is attached to. + * @param viewHolder The RecyclerView for which the swipe direction is queried. + * @return A binary OR of direction flags. + */ + public int getDragDirs(RecyclerView recyclerView, ViewHolder viewHolder) { + return mDefaultDragDirs; + } + + @Override + public int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder) { + return makeMovementFlags(getDragDirs(recyclerView, viewHolder), + getSwipeDirs(recyclerView, viewHolder)); + } + } + + private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener { + + @Override + public boolean onDown(MotionEvent e) { + return true; + } + + @Override + public void onLongPress(MotionEvent e) { + View child = findChildView(e); + if (child != null) { + ViewHolder vh = mRecyclerView.getChildViewHolder(child); + if (vh != null) { + if (!mCallback.hasDragFlag(mRecyclerView, vh)) { + return; + } + int pointerId = e.getPointerId(0); + // Long press is deferred. + // Check w/ active pointer id to avoid selecting after motion + // event is canceled. + if (pointerId == mActivePointerId) { + final int index = e.findPointerIndex(mActivePointerId); + final float x = e.getX(index); + final float y = e.getY(index); + mInitialTouchX = x; + mInitialTouchY = y; + mDx = mDy = 0f; + if (DEBUG) { + Log.d(TAG, + "onlong press: x:" + mInitialTouchX + ",y:" + mInitialTouchY); + } + if (mCallback.isLongPressDragEnabled()) { + select(vh, ACTION_STATE_DRAG); + } + } + } + } + } + } + + private class RecoverAnimation implements AnimatorListenerCompat { + + final float mStartDx; + + final float mStartDy; + + final float mTargetX; + + final float mTargetY; + + final ViewHolder mViewHolder; + + final int mActionState; + + private final ValueAnimatorCompat mValueAnimator; + + private final int mAnimationType; + + public boolean mIsPendingCleanup; + + float mX; + + float mY; + + // if user starts touching a recovering view, we put it into interaction mode again, + // instantly. + boolean mOverridden = false; + + private boolean mEnded = false; + + private float mFraction; + + public RecoverAnimation(ViewHolder viewHolder, int animationType, + int actionState, float startDx, float startDy, float targetX, float targetY) { + mActionState = actionState; + mAnimationType = animationType; + mViewHolder = viewHolder; + mStartDx = startDx; + mStartDy = startDy; + mTargetX = targetX; + mTargetY = targetY; + mValueAnimator = AnimatorCompatHelper.emptyValueAnimator(); + mValueAnimator.addUpdateListener( + new AnimatorUpdateListenerCompat() { + @Override + public void onAnimationUpdate(ValueAnimatorCompat animation) { + setFraction(animation.getAnimatedFraction()); + } + }); + mValueAnimator.setTarget(viewHolder.itemView); + mValueAnimator.addListener(this); + setFraction(0f); + } + + public void setDuration(long duration) { + mValueAnimator.setDuration(duration); + } + + public void start() { + mViewHolder.setIsRecyclable(false); + mValueAnimator.start(); + } + + public void cancel() { + mValueAnimator.cancel(); + } + + public void setFraction(float fraction) { + mFraction = fraction; + } + + /** + * We run updates on onDraw method but use the fraction from animator callback. + * This way, we can sync translate x/y values w/ the animators to avoid one-off frames. + */ + public void update() { + if (mStartDx == mTargetX) { + mX = ViewCompat.getTranslationX(mViewHolder.itemView); + } else { + mX = mStartDx + mFraction * (mTargetX - mStartDx); + } + if (mStartDy == mTargetY) { + mY = ViewCompat.getTranslationY(mViewHolder.itemView); + } else { + mY = mStartDy + mFraction * (mTargetY - mStartDy); + } + } + + @Override + public void onAnimationStart(ValueAnimatorCompat animation) { + + } + + @Override + public void onAnimationEnd(ValueAnimatorCompat animation) { + if (!mEnded) { + mViewHolder.setIsRecyclable(true); + } + mEnded = true; + } + + @Override + public void onAnimationCancel(ValueAnimatorCompat animation) { + setFraction(1f); //make sure we recover the view's state. + } + + @Override + public void onAnimationRepeat(ValueAnimatorCompat animation) { + + } + } +} \ No newline at end of file diff --git a/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtil.java b/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtil.java new file mode 100644 index 0000000000..520a95e994 --- /dev/null +++ b/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtil.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.widget.helper; + +import android.graphics.Canvas; +import android.support.v7.widget.RecyclerView; +import android.view.View; + +/** + * Utility class for {@link ItemTouchHelper} which handles item transformations for different + * API versions. + *

      + * This class has methods that map to {@link ItemTouchHelper.Callback}'s drawing methods. Default + * implementations in {@link ItemTouchHelper.Callback} call these methods with + * {@link RecyclerView.ViewHolder#itemView} and {@link ItemTouchUIUtil} makes necessary changes + * on the View depending on the API level. You can access the instance of {@link ItemTouchUIUtil} + * via {@link ItemTouchHelper.Callback#getDefaultUIUtil()} and call its methods with the children + * of ViewHolder that you want to apply default effects. + * + * @see ItemTouchHelper.Callback#getDefaultUIUtil() + */ +public interface ItemTouchUIUtil { + + /** + * The default implementation for {@link ItemTouchHelper.Callback#onChildDraw(Canvas, + * RecyclerView, RecyclerView.ViewHolder, float, float, int, boolean)} + */ + void onDraw(Canvas c, RecyclerView recyclerView, View view, + float dX, float dY, int actionState, boolean isCurrentlyActive); + + /** + * The default implementation for {@link ItemTouchHelper.Callback#onChildDrawOver(Canvas, + * RecyclerView, RecyclerView.ViewHolder, float, float, int, boolean)} + */ + void onDrawOver(Canvas c, RecyclerView recyclerView, View view, + float dX, float dY, int actionState, boolean isCurrentlyActive); + + /** + * The default implementation for {@link ItemTouchHelper.Callback#clearView(RecyclerView, + * RecyclerView.ViewHolder)} + */ + void clearView(View view); + + /** + * The default implementation for {@link ItemTouchHelper.Callback#onSelectedChanged( + * RecyclerView.ViewHolder, int)} + */ + void onSelected(View view); +} + diff --git a/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtilImpl.java b/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtilImpl.java new file mode 100644 index 0000000000..ea25477bef --- /dev/null +++ b/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtilImpl.java @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.widget.helper; + +import android.graphics.Canvas; +import android.support.v4.view.ViewCompat; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.support.v7.recyclerview.R; + + +/** + * Package private class to keep implementations. Putting them inside ItemTouchUIUtil makes them + * public API, which is not desired in this case. + */ +class ItemTouchUIUtilImpl { + static class Lollipop extends Honeycomb { + @Override + public void onDraw(Canvas c, RecyclerView recyclerView, View view, + float dX, float dY, int actionState, boolean isCurrentlyActive) { + if (isCurrentlyActive) { + Object originalElevation = view.getTag(R.id.item_touch_helper_previous_elevation); + if (originalElevation == null) { + originalElevation = ViewCompat.getElevation(view); + float newElevation = 1f + findMaxElevation(recyclerView, view); + ViewCompat.setElevation(view, newElevation); + view.setTag(R.id.item_touch_helper_previous_elevation, originalElevation); + } + } + super.onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive); + } + + private float findMaxElevation(RecyclerView recyclerView, View itemView) { + final int childCount = recyclerView.getChildCount(); + float max = 0; + for (int i = 0; i < childCount; i++) { + final View child = recyclerView.getChildAt(i); + if (child == itemView) { + continue; + } + final float elevation = ViewCompat.getElevation(child); + if (elevation > max) { + max = elevation; + } + } + return max; + } + + @Override + public void clearView(View view) { + final Object tag = view.getTag(R.id.item_touch_helper_previous_elevation); + if (tag != null && tag instanceof Float) { + ViewCompat.setElevation(view, (Float) tag); + } + view.setTag(R.id.item_touch_helper_previous_elevation, null); + super.clearView(view); + } + } + + static class Honeycomb implements ItemTouchUIUtil { + + @Override + public void clearView(View view) { + ViewCompat.setTranslationX(view, 0f); + ViewCompat.setTranslationY(view, 0f); + } + + @Override + public void onSelected(View view) { + + } + + @Override + public void onDraw(Canvas c, RecyclerView recyclerView, View view, + float dX, float dY, int actionState, boolean isCurrentlyActive) { + ViewCompat.setTranslationX(view, dX); + ViewCompat.setTranslationY(view, dY); + } + + @Override + public void onDrawOver(Canvas c, RecyclerView recyclerView, + View view, float dX, float dY, int actionState, boolean isCurrentlyActive) { + + } + } + + static class Gingerbread implements ItemTouchUIUtil { + + private void draw(Canvas c, RecyclerView parent, View view, + float dX, float dY) { + c.save(); + c.translate(dX, dY); + parent.drawChild(c, view, 0); + c.restore(); + } + + @Override + public void clearView(View view) { + view.setVisibility(View.VISIBLE); + } + + @Override + public void onSelected(View view) { + view.setVisibility(View.INVISIBLE); + } + + @Override + public void onDraw(Canvas c, RecyclerView recyclerView, View view, + float dX, float dY, int actionState, boolean isCurrentlyActive) { + if (actionState != ItemTouchHelper.ACTION_STATE_DRAG) { + draw(c, recyclerView, view, dX, dY); + } + } + + @Override + public void onDrawOver(Canvas c, RecyclerView recyclerView, + View view, float dX, float dY, + int actionState, boolean isCurrentlyActive) { + if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) { + draw(c, recyclerView, view, dX, dY); + } + } + } +} diff --git a/app/src/main/java/android/support/v7/widget/util/SortedListAdapterCallback.java b/app/src/main/java/android/support/v7/widget/util/SortedListAdapterCallback.java new file mode 100644 index 0000000000..4921541b3c --- /dev/null +++ b/app/src/main/java/android/support/v7/widget/util/SortedListAdapterCallback.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.support.v7.widget.util; + +import android.support.v7.util.SortedList; +import android.support.v7.widget.RecyclerView; + +/** + * A {@link SortedList.Callback} implementation that can bind a {@link SortedList} to a + * {@link RecyclerView.Adapter}. + */ +public abstract class SortedListAdapterCallback extends SortedList.Callback { + + final RecyclerView.Adapter mAdapter; + + /** + * Creates a {@link SortedList.Callback} that will forward data change events to the provided + * Adapter. + * + * @param adapter The Adapter instance which should receive events from the SortedList. + */ + public SortedListAdapterCallback(RecyclerView.Adapter adapter) { + mAdapter = adapter; + } + + @Override + public void onInserted(int position, int count) { + mAdapter.notifyItemRangeInserted(position, count); + } + + @Override + public void onRemoved(int position, int count) { + mAdapter.notifyItemRangeRemoved(position, count); + } + + @Override + public void onMoved(int fromPosition, int toPosition) { + mAdapter.notifyItemMoved(fromPosition, toPosition); + } + + @Override + public void onChanged(int position, int count) { + mAdapter.notifyItemRangeChanged(position, count); + } +} From 190296e533a2e7290eb76de7f47805b7535d1d74 Mon Sep 17 00:00:00 2001 From: huangzhuanghua <401742778@qq.com> Date: Tue, 25 Oct 2016 11:34:32 +0800 Subject: [PATCH 11/11] ... --- .../support/v7/util/AsyncListUtil.java | 592 -- .../v7/util/BatchingListUpdateCallback.java | 123 - .../android/support/v7/util/DiffUtil.java | 856 --- .../support/v7/util/ListUpdateCallback.java | 55 - .../support/v7/util/MessageThreadUtil.java | 283 - .../android/support/v7/util/SortedList.java | 821 --- .../android/support/v7/util/ThreadUtil.java | 45 - .../android/support/v7/util/TileList.java | 105 - .../support/v7/widget/AdapterHelper.java | 136 +- .../support/v7/widget/ChildHelper.java | 91 +- .../v7/widget/DefaultItemAnimator.java | 147 +- .../support/v7/widget/GridLayoutManager.java | 488 +- .../support/v7/widget/LayoutState.java | 43 +- .../v7/widget/LinearLayoutManager.java | 618 +- .../v7/widget/LinearSmoothScroller.java | 65 +- .../support/v7/widget/LinearSnapHelper.java | 286 - .../support/v7/widget/OpReorderer.java | 12 +- .../support/v7/widget/OrientationHelper.java | 105 +- .../support/v7/widget/RecyclerView.java | 5976 ++++------------- .../RecyclerViewAccessibilityDelegate.java | 21 +- .../support/v7/widget/ScrollbarHelper.java | 14 +- .../support/v7/widget/SimpleItemAnimator.java | 442 -- .../android/support/v7/widget/SnapHelper.java | 274 - .../v7/widget/StaggeredGridLayoutManager.java | 1046 +-- .../support/v7/widget/ViewInfoStore.java | 331 - .../v7/widget/helper/ItemTouchHelper.java | 2408 ------- .../v7/widget/helper/ItemTouchUIUtil.java | 64 - .../v7/widget/helper/ItemTouchUIUtilImpl.java | 138 - .../util/SortedListAdapterCallback.java | 59 - 29 files changed, 2022 insertions(+), 13622 deletions(-) delete mode 100644 app/src/main/java/android/support/v7/util/AsyncListUtil.java delete mode 100644 app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java delete mode 100644 app/src/main/java/android/support/v7/util/DiffUtil.java delete mode 100644 app/src/main/java/android/support/v7/util/ListUpdateCallback.java delete mode 100644 app/src/main/java/android/support/v7/util/MessageThreadUtil.java delete mode 100644 app/src/main/java/android/support/v7/util/SortedList.java delete mode 100644 app/src/main/java/android/support/v7/util/ThreadUtil.java delete mode 100644 app/src/main/java/android/support/v7/util/TileList.java delete mode 100644 app/src/main/java/android/support/v7/widget/LinearSnapHelper.java delete mode 100644 app/src/main/java/android/support/v7/widget/SimpleItemAnimator.java delete mode 100644 app/src/main/java/android/support/v7/widget/SnapHelper.java delete mode 100644 app/src/main/java/android/support/v7/widget/ViewInfoStore.java delete mode 100644 app/src/main/java/android/support/v7/widget/helper/ItemTouchHelper.java delete mode 100644 app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtil.java delete mode 100644 app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtilImpl.java delete mode 100644 app/src/main/java/android/support/v7/widget/util/SortedListAdapterCallback.java diff --git a/app/src/main/java/android/support/v7/util/AsyncListUtil.java b/app/src/main/java/android/support/v7/util/AsyncListUtil.java deleted file mode 100644 index c2a66b4fe8..0000000000 --- a/app/src/main/java/android/support/v7/util/AsyncListUtil.java +++ /dev/null @@ -1,592 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import android.support.annotation.UiThread; -import android.support.annotation.WorkerThread; -import android.util.Log; -import android.util.SparseBooleanArray; -import android.util.SparseIntArray; - -/** - * A utility class that supports asynchronous content loading. - *

      - * It can be used to load Cursor data in chunks without querying the Cursor on the UI Thread while - * keeping UI and cache synchronous for better user experience. - *

      - * It loads the data on a background thread and keeps only a limited number of fixed sized - * chunks in memory at all times. - *

      - * {@link AsyncListUtil} queries the currently visible range through {@link ViewCallback}, - * loads the required data items in the background through {@link DataCallback}, and notifies a - * {@link ViewCallback} when the data is loaded. It may load some extra items for smoother - * scrolling. - *

      - * Note that this class uses a single thread to load the data, so it suitable to load data from - * secondary storage such as disk, but not from network. - *

      - * This class is designed to work with {@link android.support.v7.widget.RecyclerView}, but it does - * not depend on it and can be used with other list views. - * - */ -public class AsyncListUtil { - private static final String TAG = "AsyncListUtil"; - - private static final boolean DEBUG = false; - - final Class mTClass; - final int mTileSize; - final DataCallback mDataCallback; - final ViewCallback mViewCallback; - - final TileList mTileList; - - final ThreadUtil.MainThreadCallback mMainThreadProxy; - final ThreadUtil.BackgroundCallback mBackgroundProxy; - - final int[] mTmpRange = new int[2]; - final int[] mPrevRange = new int[2]; - final int[] mTmpRangeExtended = new int[2]; - - private boolean mAllowScrollHints; - private int mScrollHint = ViewCallback.HINT_SCROLL_NONE; - - private int mItemCount = 0; - - int mDisplayedGeneration = 0; - int mRequestedGeneration = mDisplayedGeneration; - - final private SparseIntArray mMissingPositions = new SparseIntArray(); - - private void log(String s, Object... args) { - Log.d(TAG, "[MAIN] " + String.format(s, args)); - } - - /** - * Creates an AsyncListUtil. - * - * @param klass Class of the data item. - * @param tileSize Number of item per chunk loaded at once. - * @param dataCallback Data access callback. - * @param viewCallback Callback for querying visible item range and update notifications. - */ - public AsyncListUtil(Class klass, int tileSize, DataCallback dataCallback, - ViewCallback viewCallback) { - mTClass = klass; - mTileSize = tileSize; - mDataCallback = dataCallback; - mViewCallback = viewCallback; - - mTileList = new TileList(mTileSize); - - ThreadUtil threadUtil = new MessageThreadUtil(); - mMainThreadProxy = threadUtil.getMainThreadProxy(mMainThreadCallback); - mBackgroundProxy = threadUtil.getBackgroundProxy(mBackgroundCallback); - - refresh(); - } - - private boolean isRefreshPending() { - return mRequestedGeneration != mDisplayedGeneration; - } - - /** - * Updates the currently visible item range. - * - *

      - * Identifies the data items that have not been loaded yet and initiates loading them in the - * background. Should be called from the view's scroll listener (such as - * {@link android.support.v7.widget.RecyclerView.OnScrollListener#onScrolled}). - */ - public void onRangeChanged() { - if (isRefreshPending()) { - return; // Will update range will the refresh result arrives. - } - updateRange(); - mAllowScrollHints = true; - } - - /** - * Forces reloading the data. - *

      - * Discards all the cached data and reloads all required data items for the currently visible - * range. To be called when the data item count and/or contents has changed. - */ - public void refresh() { - mMissingPositions.clear(); - mBackgroundProxy.refresh(++mRequestedGeneration); - } - - /** - * Returns the data item at the given position or null if it has not been loaded - * yet. - * - *

      - * If this method has been called for a specific position and returned null, then - * {@link ViewCallback#onItemLoaded(int)} will be called when it finally loads. Note that if - * this position stays outside of the cached item range (as defined by - * {@link ViewCallback#extendRangeInto} method), then the callback will never be called for - * this position. - * - * @param position Item position. - * - * @return The data item at the given position or null if it has not been loaded - * yet. - */ - public T getItem(int position) { - if (position < 0 || position >= mItemCount) { - throw new IndexOutOfBoundsException(position + " is not within 0 and " + mItemCount); - } - T item = mTileList.getItemAt(position); - if (item == null && !isRefreshPending()) { - mMissingPositions.put(position, 0); - } - return item; - } - - /** - * Returns the number of items in the data set. - * - *

      - * This is the number returned by a recent call to - * {@link DataCallback#refreshData()}. - * - * @return Number of items. - */ - public int getItemCount() { - return mItemCount; - } - - private void updateRange() { - mViewCallback.getItemRangeInto(mTmpRange); - if (mTmpRange[0] > mTmpRange[1] || mTmpRange[0] < 0) { - return; - } - if (mTmpRange[1] >= mItemCount) { - // Invalid range may arrive soon after the refresh. - return; - } - - if (!mAllowScrollHints) { - mScrollHint = ViewCallback.HINT_SCROLL_NONE; - } else if (mTmpRange[0] > mPrevRange[1] || mPrevRange[0] > mTmpRange[1]) { - // Ranges do not intersect, long leap not a scroll. - mScrollHint = ViewCallback.HINT_SCROLL_NONE; - } else if (mTmpRange[0] < mPrevRange[0]) { - mScrollHint = ViewCallback.HINT_SCROLL_DESC; - } else if (mTmpRange[0] > mPrevRange[0]) { - mScrollHint = ViewCallback.HINT_SCROLL_ASC; - } - - mPrevRange[0] = mTmpRange[0]; - mPrevRange[1] = mTmpRange[1]; - - mViewCallback.extendRangeInto(mTmpRange, mTmpRangeExtended, mScrollHint); - mTmpRangeExtended[0] = Math.min(mTmpRange[0], Math.max(mTmpRangeExtended[0], 0)); - mTmpRangeExtended[1] = - Math.max(mTmpRange[1], Math.min(mTmpRangeExtended[1], mItemCount - 1)); - - mBackgroundProxy.updateRange(mTmpRange[0], mTmpRange[1], - mTmpRangeExtended[0], mTmpRangeExtended[1], mScrollHint); - } - - private final ThreadUtil.MainThreadCallback - mMainThreadCallback = new ThreadUtil.MainThreadCallback() { - @Override - public void updateItemCount(int generation, int itemCount) { - if (DEBUG) { - log("updateItemCount: size=%d, gen #%d", itemCount, generation); - } - if (!isRequestedGeneration(generation)) { - return; - } - mItemCount = itemCount; - mViewCallback.onDataRefresh(); - mDisplayedGeneration = mRequestedGeneration; - recycleAllTiles(); - - mAllowScrollHints = false; // Will be set to true after a first real scroll. - // There will be no scroll event if the size change does not affect the current range. - updateRange(); - } - - @Override - public void addTile(int generation, TileList.Tile tile) { - if (!isRequestedGeneration(generation)) { - if (DEBUG) { - log("recycling an older generation tile @%d", tile.mStartPosition); - } - mBackgroundProxy.recycleTile(tile); - return; - } - TileList.Tile duplicate = mTileList.addOrReplace(tile); - if (duplicate != null) { - Log.e(TAG, "duplicate tile @" + duplicate.mStartPosition); - mBackgroundProxy.recycleTile(duplicate); - } - if (DEBUG) { - log("gen #%d, added tile @%d, total tiles: %d", - generation, tile.mStartPosition, mTileList.size()); - } - int endPosition = tile.mStartPosition + tile.mItemCount; - int index = 0; - while (index < mMissingPositions.size()) { - final int position = mMissingPositions.keyAt(index); - if (tile.mStartPosition <= position && position < endPosition) { - mMissingPositions.removeAt(index); - mViewCallback.onItemLoaded(position); - } else { - index++; - } - } - } - - @Override - public void removeTile(int generation, int position) { - if (!isRequestedGeneration(generation)) { - return; - } - TileList.Tile tile = mTileList.removeAtPos(position); - if (tile == null) { - Log.e(TAG, "tile not found @" + position); - return; - } - if (DEBUG) { - log("recycling tile @%d, total tiles: %d", tile.mStartPosition, mTileList.size()); - } - mBackgroundProxy.recycleTile(tile); - } - - private void recycleAllTiles() { - if (DEBUG) { - log("recycling all %d tiles", mTileList.size()); - } - for (int i = 0; i < mTileList.size(); i++) { - mBackgroundProxy.recycleTile(mTileList.getAtIndex(i)); - } - mTileList.clear(); - } - - private boolean isRequestedGeneration(int generation) { - return generation == mRequestedGeneration; - } - }; - - private final ThreadUtil.BackgroundCallback - mBackgroundCallback = new ThreadUtil.BackgroundCallback() { - - private TileList.Tile mRecycledRoot; - - final SparseBooleanArray mLoadedTiles = new SparseBooleanArray(); - - private int mGeneration; - private int mItemCount; - - private int mFirstRequiredTileStart; - private int mLastRequiredTileStart; - - @Override - public void refresh(int generation) { - mGeneration = generation; - mLoadedTiles.clear(); - mItemCount = mDataCallback.refreshData(); - mMainThreadProxy.updateItemCount(mGeneration, mItemCount); - } - - @Override - public void updateRange(int rangeStart, int rangeEnd, int extRangeStart, int extRangeEnd, - int scrollHint) { - if (DEBUG) { - log("updateRange: %d..%d extended to %d..%d, scroll hint: %d", - rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint); - } - - if (rangeStart > rangeEnd) { - return; - } - - final int firstVisibleTileStart = getTileStart(rangeStart); - final int lastVisibleTileStart = getTileStart(rangeEnd); - - mFirstRequiredTileStart = getTileStart(extRangeStart); - mLastRequiredTileStart = getTileStart(extRangeEnd); - if (DEBUG) { - log("requesting tile range: %d..%d", - mFirstRequiredTileStart, mLastRequiredTileStart); - } - - // All pending tile requests are removed by ThreadUtil at this point. - // Re-request all required tiles in the most optimal order. - if (scrollHint == ViewCallback.HINT_SCROLL_DESC) { - requestTiles(mFirstRequiredTileStart, lastVisibleTileStart, scrollHint, true); - requestTiles(lastVisibleTileStart + mTileSize, mLastRequiredTileStart, scrollHint, - false); - } else { - requestTiles(firstVisibleTileStart, mLastRequiredTileStart, scrollHint, false); - requestTiles(mFirstRequiredTileStart, firstVisibleTileStart - mTileSize, scrollHint, - true); - } - } - - private int getTileStart(int position) { - return position - position % mTileSize; - } - - private void requestTiles(int firstTileStart, int lastTileStart, int scrollHint, - boolean backwards) { - for (int i = firstTileStart; i <= lastTileStart; i += mTileSize) { - int tileStart = backwards ? (lastTileStart + firstTileStart - i) : i; - if (DEBUG) { - log("requesting tile @%d", tileStart); - } - mBackgroundProxy.loadTile(tileStart, scrollHint); - } - } - - @Override - public void loadTile(int position, int scrollHint) { - if (isTileLoaded(position)) { - if (DEBUG) { - log("already loaded tile @%d", position); - } - return; - } - TileList.Tile tile = acquireTile(); - tile.mStartPosition = position; - tile.mItemCount = Math.min(mTileSize, mItemCount - tile.mStartPosition); - mDataCallback.fillData(tile.mItems, tile.mStartPosition, tile.mItemCount); - flushTileCache(scrollHint); - addTile(tile); - } - - @Override - public void recycleTile(TileList.Tile tile) { - if (DEBUG) { - log("recycling tile @%d", tile.mStartPosition); - } - mDataCallback.recycleData(tile.mItems, tile.mItemCount); - - tile.mNext = mRecycledRoot; - mRecycledRoot = tile; - } - - private TileList.Tile acquireTile() { - if (mRecycledRoot != null) { - TileList.Tile result = mRecycledRoot; - mRecycledRoot = mRecycledRoot.mNext; - return result; - } - return new TileList.Tile(mTClass, mTileSize); - } - - private boolean isTileLoaded(int position) { - return mLoadedTiles.get(position); - } - - private void addTile(TileList.Tile tile) { - mLoadedTiles.put(tile.mStartPosition, true); - mMainThreadProxy.addTile(mGeneration, tile); - if (DEBUG) { - log("loaded tile @%d, total tiles: %d", tile.mStartPosition, mLoadedTiles.size()); - } - } - - private void removeTile(int position) { - mLoadedTiles.delete(position); - mMainThreadProxy.removeTile(mGeneration, position); - if (DEBUG) { - log("flushed tile @%d, total tiles: %s", position, mLoadedTiles.size()); - } - } - - private void flushTileCache(int scrollHint) { - final int cacheSizeLimit = mDataCallback.getMaxCachedTiles(); - while (mLoadedTiles.size() >= cacheSizeLimit) { - int firstLoadedTileStart = mLoadedTiles.keyAt(0); - int lastLoadedTileStart = mLoadedTiles.keyAt(mLoadedTiles.size() - 1); - int startMargin = mFirstRequiredTileStart - firstLoadedTileStart; - int endMargin = lastLoadedTileStart - mLastRequiredTileStart; - if (startMargin > 0 && (startMargin >= endMargin || - (scrollHint == ViewCallback.HINT_SCROLL_ASC))) { - removeTile(firstLoadedTileStart); - } else if (endMargin > 0 && (startMargin < endMargin || - (scrollHint == ViewCallback.HINT_SCROLL_DESC))){ - removeTile(lastLoadedTileStart); - } else { - // Could not flush on either side, bail out. - return; - } - } - } - - private void log(String s, Object... args) { - Log.d(TAG, "[BKGR] " + String.format(s, args)); - } - }; - - /** - * The callback that provides data access for {@link AsyncListUtil}. - * - *

      - * All methods are called on the background thread. - */ - public static abstract class DataCallback { - - /** - * Refresh the data set and return the new data item count. - * - *

      - * If the data is being accessed through {@link android.database.Cursor} this is where - * the new cursor should be created. - * - * @return Data item count. - */ - @WorkerThread - public abstract int refreshData(); - - /** - * Fill the given tile. - * - *

      - * The provided tile might be a recycled tile, in which case it will already have objects. - * It is suggested to re-use these objects if possible in your use case. - * - * @param startPosition The start position in the list. - * @param itemCount The data item count. - * @param data The data item array to fill into. Should not be accessed beyond - * itemCount. - */ - @WorkerThread - public abstract void fillData(T[] data, int startPosition, int itemCount); - - /** - * Recycle the objects created in {@link #fillData} if necessary. - * - * - * @param data Array of data items. Should not be accessed beyond itemCount. - * @param itemCount The data item count. - */ - @WorkerThread - public void recycleData(T[] data, int itemCount) { - } - - /** - * Returns tile cache size limit (in tiles). - * - *

      - * The actual number of cached tiles will be the maximum of this value and the number of - * tiles that is required to cover the range returned by - * {@link ViewCallback#extendRangeInto(int[], int[], int)}. - *

      - * For example, if this method returns 10, and the most - * recent call to {@link ViewCallback#extendRangeInto(int[], int[], int)} returned - * {100, 179}, and the tile size is 5, then the maximum number of cached tiles will be 16. - *

      - * However, if the tile size is 20, then the maximum number of cached tiles will be 10. - *

      - * The default implementation returns 10. - * - * @return Maximum cache size. - */ - @WorkerThread - public int getMaxCachedTiles() { - return 10; - } - } - - /** - * The callback that links {@link AsyncListUtil} with the list view. - * - *

      - * All methods are called on the main thread. - */ - public static abstract class ViewCallback { - - /** - * No scroll direction hint available. - */ - public static final int HINT_SCROLL_NONE = 0; - - /** - * Scrolling in descending order (from higher to lower positions in the order of the backing - * storage). - */ - public static final int HINT_SCROLL_DESC = 1; - - /** - * Scrolling in ascending order (from lower to higher positions in the order of the backing - * storage). - */ - public static final int HINT_SCROLL_ASC = 2; - - /** - * Compute the range of visible item positions. - *

      - * outRange[0] is the position of the first visible item (in the order of the backing - * storage). - *

      - * outRange[1] is the position of the last visible item (in the order of the backing - * storage). - *

      - * Negative positions and positions greater or equal to {@link #getItemCount} are invalid. - * If the returned range contains invalid positions it is ignored (no item will be loaded). - * - * @param outRange The visible item range. - */ - @UiThread - public abstract void getItemRangeInto(int[] outRange); - - /** - * Compute a wider range of items that will be loaded for smoother scrolling. - * - *

      - * If there is no scroll hint, the default implementation extends the visible range by half - * its length in both directions. If there is a scroll hint, the range is extended by - * its full length in the scroll direction, and by half in the other direction. - *

      - * For example, if range is {100, 200} and scrollHint - * is {@link #HINT_SCROLL_ASC}, then outRange will be {50, 300}. - *

      - * However, if scrollHint is {@link #HINT_SCROLL_NONE}, then - * outRange will be {50, 250} - * - * @param range Visible item range. - * @param outRange Extended range. - * @param scrollHint The scroll direction hint. - */ - @UiThread - public void extendRangeInto(int[] range, int[] outRange, int scrollHint) { - final int fullRange = range[1] - range[0] + 1; - final int halfRange = fullRange / 2; - outRange[0] = range[0] - (scrollHint == HINT_SCROLL_DESC ? fullRange : halfRange); - outRange[1] = range[1] + (scrollHint == HINT_SCROLL_ASC ? fullRange : halfRange); - } - - /** - * Called when the entire data set has changed. - */ - @UiThread - public abstract void onDataRefresh(); - - /** - * Called when an item at the given position is loaded. - * @param position Item position. - */ - @UiThread - public abstract void onItemLoaded(int position); - } -} diff --git a/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java b/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java deleted file mode 100644 index c8bc1a4b8b..0000000000 --- a/app/src/main/java/android/support/v7/util/BatchingListUpdateCallback.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package android.support.v7.util; - -/** - * Wraps a {@link ListUpdateCallback} callback and batches operations that can be merged. - *

      - * For instance, when 2 add operations comes that adds 2 consecutive elements, - * BatchingListUpdateCallback merges them and calls the wrapped callback only once. - *

      - * This is a general purpose class and is also used by - * {@link android.support.v7.util.DiffUtil.DiffResult DiffResult} and - * {@link SortedList} to minimize the number of updates that are dispatched. - *

      - * If you use this class to batch updates, you must call {@link #dispatchLastEvent()} when the - * stream of update events drain. - */ -public class BatchingListUpdateCallback implements ListUpdateCallback { - private static final int TYPE_NONE = 0; - private static final int TYPE_ADD = 1; - private static final int TYPE_REMOVE = 2; - private static final int TYPE_CHANGE = 3; - - final ListUpdateCallback mWrapped; - - int mLastEventType = TYPE_NONE; - int mLastEventPosition = -1; - int mLastEventCount = -1; - Object mLastEventPayload = null; - - public BatchingListUpdateCallback(ListUpdateCallback callback) { - mWrapped = callback; - } - - /** - * BatchingListUpdateCallback holds onto the last event to see if it can be merged with the - * next one. When stream of events finish, you should call this method to dispatch the last - * event. - */ - public void dispatchLastEvent() { - if (mLastEventType == TYPE_NONE) { - return; - } - switch (mLastEventType) { - case TYPE_ADD: - mWrapped.onInserted(mLastEventPosition, mLastEventCount); - break; - case TYPE_REMOVE: - mWrapped.onRemoved(mLastEventPosition, mLastEventCount); - break; - case TYPE_CHANGE: - mWrapped.onChanged(mLastEventPosition, mLastEventCount, mLastEventPayload); - break; - } - mLastEventPayload = null; - mLastEventType = TYPE_NONE; - } - - @Override - public void onInserted(int position, int count) { - if (mLastEventType == TYPE_ADD && position >= mLastEventPosition - && position <= mLastEventPosition + mLastEventCount) { - mLastEventCount += count; - mLastEventPosition = Math.min(position, mLastEventPosition); - return; - } - dispatchLastEvent(); - mLastEventPosition = position; - mLastEventCount = count; - mLastEventType = TYPE_ADD; - } - - @Override - public void onRemoved(int position, int count) { - if (mLastEventType == TYPE_REMOVE && mLastEventPosition >= position && - mLastEventPosition <= position + count) { - mLastEventCount += count; - mLastEventPosition = position; - return; - } - dispatchLastEvent(); - mLastEventPosition = position; - mLastEventCount = count; - mLastEventType = TYPE_REMOVE; - } - - @Override - public void onMoved(int fromPosition, int toPosition) { - dispatchLastEvent(); // moves are not merged - mWrapped.onMoved(fromPosition, toPosition); - } - - @Override - public void onChanged(int position, int count, Object payload) { - if (mLastEventType == TYPE_CHANGE && - !(position > mLastEventPosition + mLastEventCount - || position + count < mLastEventPosition || mLastEventPayload != payload)) { - // take potential overlap into account - int previousEnd = mLastEventPosition + mLastEventCount; - mLastEventPosition = Math.min(position, mLastEventPosition); - mLastEventCount = Math.max(previousEnd, position + count) - mLastEventPosition; - return; - } - dispatchLastEvent(); - mLastEventPosition = position; - mLastEventCount = count; - mLastEventPayload = payload; - mLastEventType = TYPE_CHANGE; - } -} diff --git a/app/src/main/java/android/support/v7/util/DiffUtil.java b/app/src/main/java/android/support/v7/util/DiffUtil.java deleted file mode 100644 index 6f0a078fa7..0000000000 --- a/app/src/main/java/android/support/v7/util/DiffUtil.java +++ /dev/null @@ -1,856 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import android.support.annotation.Nullable; -import android.support.annotation.VisibleForTesting; -import android.support.v7.widget.RecyclerView; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * DiffUtil is a utility class that can calculate the difference between two lists and output a - * list of update operations that converts the first list into the second one. - *

      - * It can be used to calculate updates for a RecyclerView Adapter. - *

      - * DiffUtil uses Eugene W. Myers's difference algorithm to calculate the minimal number of updates - * to convert one list into another. Myers's algorithm does not handle items that are moved so - * DiffUtil runs a second pass on the result to detect items that were moved. - *

      - * If the lists are large, this operation may take significant time so you are advised to run this - * on a background thread, get the {@link DiffResult} then apply it on the RecyclerView on the main - * thread. - *

      - * This algorithm is optimized for space and uses O(N) space to find the minimal - * number of addition and removal operations between the two lists. It has O(N + D^2) expected time - * performance where D is the length of the edit script. - *

      - * If move detection is enabled, it takes an additional O(N^2) time where N is the total number of - * added and removed items. If your lists are already sorted by the same constraint (e.g. a created - * timestamp for a list of posts), you can disable move detection to improve performance. - *

      - * The actual runtime of the algorithm significantly depends on the number of changes in the list - * and the cost of your comparison methods. Below are some average run times for reference: - * (The test list is composed of random UUID Strings and the tests are run on Nexus 5X with M) - *

        - *
      • 100 items and 10 modifications: avg: 0.39 ms, median: 0.35 ms - *
      • 100 items and 100 modifications: 3.82 ms, median: 3.75 ms - *
      • 100 items and 100 modifications without moves: 2.09 ms, median: 2.06 ms - *
      • 1000 items and 50 modifications: avg: 4.67 ms, median: 4.59 ms - *
      • 1000 items and 50 modifications without moves: avg: 3.59 ms, median: 3.50 ms - *
      • 1000 items and 200 modifications: 27.07 ms, median: 26.92 ms - *
      • 1000 items and 200 modifications without moves: 13.54 ms, median: 13.36 ms - *
      - *

      - * Due to implementation constraints, the max size of the list can be 2^26. - */ -public class DiffUtil { - - private DiffUtil() { - // utility class, no instance. - } - - private static final Comparator SNAKE_COMPARATOR = new Comparator() { - @Override - public int compare(Snake o1, Snake o2) { - int cmpX = o1.x - o2.x; - return cmpX == 0 ? o1.y - o2.y : cmpX; - } - }; - - // Myers' algorithm uses two lists as axis labels. In DiffUtil's implementation, `x` axis is - // used for old list and `y` axis is used for new list. - - /** - * Calculates the list of update operations that can covert one list into the other one. - * - * @param cb The callback that acts as a gateway to the backing list data - * - * @return A DiffResult that contains the information about the edit sequence to convert the - * old list into the new list. - */ - public static DiffResult calculateDiff(Callback cb) { - return calculateDiff(cb, true); - } - - /** - * Calculates the list of update operations that can covert one list into the other one. - *

      - * If your old and new lists are sorted by the same constraint and items never move (swap - * positions), you can disable move detection which takes O(N^2) time where - * N is the number of added, moved, removed items. - * - * @param cb The callback that acts as a gateway to the backing list data - * @param detectMoves True if DiffUtil should try to detect moved items, false otherwise. - * - * @return A DiffResult that contains the information about the edit sequence to convert the - * old list into the new list. - */ - public static DiffResult calculateDiff(Callback cb, boolean detectMoves) { - final int oldSize = cb.getOldListSize(); - final int newSize = cb.getNewListSize(); - - final List snakes = new ArrayList<>(); - - // instead of a recursive implementation, we keep our own stack to avoid potential stack - // overflow exceptions - final List stack = new ArrayList<>(); - - stack.add(new Range(0, oldSize, 0, newSize)); - - final int max = oldSize + newSize + Math.abs(oldSize - newSize); - // allocate forward and backward k-lines. K lines are diagonal lines in the matrix. (see the - // paper for details) - // These arrays lines keep the max reachable position for each k-line. - final int[] forward = new int[max * 2]; - final int[] backward = new int[max * 2]; - - // We pool the ranges to avoid allocations for each recursive call. - final List rangePool = new ArrayList<>(); - while (!stack.isEmpty()) { - final Range range = stack.remove(stack.size() - 1); - final Snake snake = diffPartial(cb, range.oldListStart, range.oldListEnd, - range.newListStart, range.newListEnd, forward, backward, max); - if (snake != null) { - if (snake.size > 0) { - snakes.add(snake); - } - // offset the snake to convert its coordinates from the Range's area to global - snake.x += range.oldListStart; - snake.y += range.newListStart; - - // add new ranges for left and right - final Range left = rangePool.isEmpty() ? new Range() : rangePool.remove( - rangePool.size() - 1); - left.oldListStart = range.oldListStart; - left.newListStart = range.newListStart; - if (snake.reverse) { - left.oldListEnd = snake.x; - left.newListEnd = snake.y; - } else { - if (snake.removal) { - left.oldListEnd = snake.x - 1; - left.newListEnd = snake.y; - } else { - left.oldListEnd = snake.x; - left.newListEnd = snake.y - 1; - } - } - stack.add(left); - - // re-use range for right - //noinspection UnnecessaryLocalVariable - final Range right = range; - if (snake.reverse) { - if (snake.removal) { - right.oldListStart = snake.x + snake.size + 1; - right.newListStart = snake.y + snake.size; - } else { - right.oldListStart = snake.x + snake.size; - right.newListStart = snake.y + snake.size + 1; - } - } else { - right.oldListStart = snake.x + snake.size; - right.newListStart = snake.y + snake.size; - } - stack.add(right); - } else { - rangePool.add(range); - } - - } - // sort snakes - Collections.sort(snakes, SNAKE_COMPARATOR); - - return new DiffResult(cb, snakes, forward, backward, detectMoves); - - } - - private static Snake diffPartial(Callback cb, int startOld, int endOld, - int startNew, int endNew, int[] forward, int[] backward, int kOffset) { - final int oldSize = endOld - startOld; - final int newSize = endNew - startNew; - - if (endOld - startOld < 1 || endNew - startNew < 1) { - return null; - } - - final int delta = oldSize - newSize; - final int dLimit = (oldSize + newSize + 1) / 2; - Arrays.fill(forward, kOffset - dLimit - 1, kOffset + dLimit + 1, 0); - Arrays.fill(backward, kOffset - dLimit - 1 + delta, kOffset + dLimit + 1 + delta, oldSize); - final boolean checkInFwd = delta % 2 != 0; - for (int d = 0; d <= dLimit; d++) { - for (int k = -d; k <= d; k += 2) { - // find forward path - // we can reach k from k - 1 or k + 1. Check which one is further in the graph - int x; - final boolean removal; - if (k == -d || k != d && forward[kOffset + k - 1] < forward[kOffset + k + 1]) { - x = forward[kOffset + k + 1]; - removal = false; - } else { - x = forward[kOffset + k - 1] + 1; - removal = true; - } - // set y based on x - int y = x - k; - // move diagonal as long as items match - while (x < oldSize && y < newSize - && cb.areItemsTheSame(startOld + x, startNew + y)) { - x++; - y++; - } - forward[kOffset + k] = x; - if (checkInFwd && k >= delta - d + 1 && k <= delta + d - 1) { - if (forward[kOffset + k] >= backward[kOffset + k]) { - Snake outSnake = new Snake(); - outSnake.x = backward[kOffset + k]; - outSnake.y = outSnake.x - k; - outSnake.size = forward[kOffset + k] - backward[kOffset + k]; - outSnake.removal = removal; - outSnake.reverse = false; - return outSnake; - } - } - } - for (int k = -d; k <= d; k += 2) { - // find reverse path at k + delta, in reverse - final int backwardK = k + delta; - int x; - final boolean removal; - if (backwardK == d + delta || backwardK != -d + delta - && backward[kOffset + backwardK - 1] < backward[kOffset + backwardK + 1]) { - x = backward[kOffset + backwardK - 1]; - removal = false; - } else { - x = backward[kOffset + backwardK + 1] - 1; - removal = true; - } - - // set y based on x - int y = x - backwardK; - // move diagonal as long as items match - while (x > 0 && y > 0 - && cb.areItemsTheSame(startOld + x - 1, startNew + y - 1)) { - x--; - y--; - } - backward[kOffset + backwardK] = x; - if (!checkInFwd && k + delta >= -d && k + delta <= d) { - if (forward[kOffset + backwardK] >= backward[kOffset + backwardK]) { - Snake outSnake = new Snake(); - outSnake.x = backward[kOffset + backwardK]; - outSnake.y = outSnake.x - backwardK; - outSnake.size = - forward[kOffset + backwardK] - backward[kOffset + backwardK]; - outSnake.removal = removal; - outSnake.reverse = true; - return outSnake; - } - } - } - } - throw new IllegalStateException("DiffUtil hit an unexpected case while trying to calculate" - + " the optimal path. Please make sure your data is not changing during the" - + " diff calculation."); - } - - /** - * A Callback class used by DiffUtil while calculating the diff between two lists. - */ - public abstract static class Callback { - /** - * Returns the size of the old list. - * - * @return The size of the old list. - */ - public abstract int getOldListSize(); - - /** - * Returns the size of the new list. - * - * @return The size of the new list. - */ - public abstract int getNewListSize(); - - /** - * Called by the DiffUtil to decide whether two object represent the same Item. - *

      - * For example, if your items have unique ids, this method should check their id equality. - * - * @param oldItemPosition The position of the item in the old list - * @param newItemPosition The position of the item in the new list - * @return True if the two items represent the same object or false if they are different. - */ - public abstract boolean areItemsTheSame(int oldItemPosition, int newItemPosition); - - /** - * Called by the DiffUtil when it wants to check whether two items have the same data. - * DiffUtil uses this information to detect if the contents of an item has changed. - *

      - * DiffUtil uses this method to check equality instead of {@link Object#equals(Object)} - * so that you can change its behavior depending on your UI. - * For example, if you are using DiffUtil with a - * {@link android.support.v7.widget.RecyclerView.Adapter RecyclerView.Adapter}, you should - * return whether the items' visual representations are the same. - *

      - * This method is called only if {@link #areItemsTheSame(int, int)} returns - * {@code true} for these items. - * - * @param oldItemPosition The position of the item in the old list - * @param newItemPosition The position of the item in the new list which replaces the - * oldItem - * @return True if the contents of the items are the same or false if they are different. - */ - public abstract boolean areContentsTheSame(int oldItemPosition, int newItemPosition); - - /** - * When {@link #areItemsTheSame(int, int)} returns {@code true} for two items and - * {@link #areContentsTheSame(int, int)} returns false for them, DiffUtil - * calls this method to get a payload about the change. - *

      - * For example, if you are using DiffUtil with {@link RecyclerView}, you can return the - * particular field that changed in the item and your - * {@link android.support.v7.widget.RecyclerView.ItemAnimator ItemAnimator} can use that - * information to run the correct animation. - *

      - * Default implementation returns {@code null}. - * - * @param oldItemPosition The position of the item in the old list - * @param newItemPosition The position of the item in the new list - * - * @return A payload object that represents the change between the two items. - */ - @Nullable - public Object getChangePayload(int oldItemPosition, int newItemPosition) { - return null; - } - } - - /** - * Snakes represent a match between two lists. It is optionally prefixed or postfixed with an - * add or remove operation. See the Myers' paper for details. - */ - static class Snake { - /** - * Position in the old list - */ - int x; - - /** - * Position in the new list - */ - int y; - - /** - * Number of matches. Might be 0. - */ - int size; - - /** - * If true, this is a removal from the original list followed by {@code size} matches. - * If false, this is an addition from the new list followed by {@code size} matches. - */ - boolean removal; - - /** - * If true, the addition or removal is at the end of the snake. - * If false, the addition or removal is at the beginning of the snake. - */ - boolean reverse; - } - - /** - * Represents a range in two lists that needs to be solved. - *

      - * This internal class is used when running Myers' algorithm without recursion. - */ - static class Range { - - int oldListStart, oldListEnd; - - int newListStart, newListEnd; - - public Range() { - } - - public Range(int oldListStart, int oldListEnd, int newListStart, int newListEnd) { - this.oldListStart = oldListStart; - this.oldListEnd = oldListEnd; - this.newListStart = newListStart; - this.newListEnd = newListEnd; - } - } - - /** - * This class holds the information about the result of a - * {@link DiffUtil#calculateDiff(Callback, boolean)} call. - *

      - * You can consume the updates in a DiffResult via - * {@link #dispatchUpdatesTo(ListUpdateCallback)} or directly stream the results into a - * {@link RecyclerView.Adapter} via {@link #dispatchUpdatesTo(RecyclerView.Adapter)}. - */ - public static class DiffResult { - /** - * While reading the flags below, keep in mind that when multiple items move in a list, - * Myers's may pick any of them as the anchor item and consider that one NOT_CHANGED while - * picking others as additions and removals. This is completely fine as we later detect - * all moves. - *

      - * Below, when an item is mentioned to stay in the same "location", it means we won't - * dispatch a move/add/remove for it, it DOES NOT mean the item is still in the same - * position. - */ - // item stayed the same. - private static final int FLAG_NOT_CHANGED = 1; - // item stayed in the same location but changed. - private static final int FLAG_CHANGED = FLAG_NOT_CHANGED << 1; - // Item has moved and also changed. - private static final int FLAG_MOVED_CHANGED = FLAG_CHANGED << 1; - // Item has moved but did not change. - private static final int FLAG_MOVED_NOT_CHANGED = FLAG_MOVED_CHANGED << 1; - // Ignore this update. - // If this is an addition from the new list, it means the item is actually removed from an - // earlier position and its move will be dispatched when we process the matching removal - // from the old list. - // If this is a removal from the old list, it means the item is actually added back to an - // earlier index in the new list and we'll dispatch its move when we are processing that - // addition. - private static final int FLAG_IGNORE = FLAG_MOVED_NOT_CHANGED << 1; - - // since we are re-using the int arrays that were created in the Myers' step, we mask - // change flags - private static final int FLAG_OFFSET = 5; - - private static final int FLAG_MASK = (1 << FLAG_OFFSET) - 1; - - // The Myers' snakes. At this point, we only care about their diagonal sections. - private final List mSnakes; - - // The list to keep oldItemStatuses. As we traverse old items, we assign flags to them - // which also includes whether they were a real removal or a move (and its new index). - private final int[] mOldItemStatuses; - // The list to keep newItemStatuses. As we traverse new items, we assign flags to them - // which also includes whether they were a real addition or a move(and its old index). - private final int[] mNewItemStatuses; - // The callback that was given to calcualte diff method. - private final Callback mCallback; - - private final int mOldListSize; - - private final int mNewListSize; - - private final boolean mDetectMoves; - - /** - * @param callback The callback that was used to calculate the diff - * @param snakes The list of Myers' snakes - * @param oldItemStatuses An int[] that can be re-purposed to keep metadata - * @param newItemStatuses An int[] that can be re-purposed to keep metadata - * @param detectMoves True if this DiffResult will try to detect moved items - */ - DiffResult(Callback callback, List snakes, int[] oldItemStatuses, - int[] newItemStatuses, boolean detectMoves) { - mSnakes = snakes; - mOldItemStatuses = oldItemStatuses; - mNewItemStatuses = newItemStatuses; - Arrays.fill(mOldItemStatuses, 0); - Arrays.fill(mNewItemStatuses, 0); - mCallback = callback; - mOldListSize = callback.getOldListSize(); - mNewListSize = callback.getNewListSize(); - mDetectMoves = detectMoves; - addRootSnake(); - findMatchingItems(); - } - - /** - * We always add a Snake to 0/0 so that we can run loops from end to beginning and be done - * when we run out of snakes. - */ - private void addRootSnake() { - Snake firstSnake = mSnakes.isEmpty() ? null : mSnakes.get(0); - if (firstSnake == null || firstSnake.x != 0 || firstSnake.y != 0) { - Snake root = new Snake(); - root.x = 0; - root.y = 0; - root.removal = false; - root.size = 0; - root.reverse = false; - mSnakes.add(0, root); - } - } - - /** - * This method traverses each addition / removal and tries to match it to a previous - * removal / addition. This is how we detect move operations. - *

      - * This class also flags whether an item has been changed or not. - *

      - * DiffUtil does this pre-processing so that if it is running on a big list, it can be moved - * to background thread where most of the expensive stuff will be calculated and kept in - * the statuses maps. DiffResult uses this pre-calculated information while dispatching - * the updates (which is probably being called on the main thread). - */ - private void findMatchingItems() { - int posOld = mOldListSize; - int posNew = mNewListSize; - // traverse the matrix from right bottom to 0,0. - for (int i = mSnakes.size() - 1; i >= 0; i--) { - final Snake snake = mSnakes.get(i); - final int endX = snake.x + snake.size; - final int endY = snake.y + snake.size; - if (mDetectMoves) { - while (posOld > endX) { - // this is a removal. Check remaining snakes to see if this was added before - findAddition(posOld, posNew, i); - posOld--; - } - while (posNew > endY) { - // this is an addition. Check remaining snakes to see if this was removed - // before - findRemoval(posOld, posNew, i); - posNew--; - } - } - for (int j = 0; j < snake.size; j++) { - // matching items. Check if it is changed or not - final int oldItemPos = snake.x + j; - final int newItemPos = snake.y + j; - final boolean theSame = mCallback - .areContentsTheSame(oldItemPos, newItemPos); - final int changeFlag = theSame ? FLAG_NOT_CHANGED : FLAG_CHANGED; - mOldItemStatuses[oldItemPos] = (newItemPos << FLAG_OFFSET) | changeFlag; - mNewItemStatuses[newItemPos] = (oldItemPos << FLAG_OFFSET) | changeFlag; - } - posOld = snake.x; - posNew = snake.y; - } - } - - private void findAddition(int x, int y, int snakeIndex) { - if (mOldItemStatuses[x - 1] != 0) { - return; // already set by a latter item - } - findMatchingItem(x, y, snakeIndex, false); - } - - private void findRemoval(int x, int y, int snakeIndex) { - if (mNewItemStatuses[y - 1] != 0) { - return; // already set by a latter item - } - findMatchingItem(x, y, snakeIndex, true); - } - - /** - * Finds a matching item that is before the given coordinates in the matrix - * (before : left and above). - * - * @param x The x position in the matrix (position in the old list) - * @param y The y position in the matrix (position in the new list) - * @param snakeIndex The current snake index - * @param removal True if we are looking for a removal, false otherwise - * - * @return True if such item is found. - */ - private boolean findMatchingItem(final int x, final int y, final int snakeIndex, - final boolean removal) { - final int myItemPos; - int curX; - int curY; - if (removal) { - myItemPos = y - 1; - curX = x; - curY = y - 1; - } else { - myItemPos = x - 1; - curX = x - 1; - curY = y; - } - for (int i = snakeIndex; i >= 0; i--) { - final Snake snake = mSnakes.get(i); - final int endX = snake.x + snake.size; - final int endY = snake.y + snake.size; - if (removal) { - // check removals for a match - for (int pos = curX - 1; pos >= endX; pos--) { - if (mCallback.areItemsTheSame(pos, myItemPos)) { - // found! - final boolean theSame = mCallback.areContentsTheSame(pos, myItemPos); - final int changeFlag = theSame ? FLAG_MOVED_NOT_CHANGED - : FLAG_MOVED_CHANGED; - mNewItemStatuses[myItemPos] = (pos << FLAG_OFFSET) | FLAG_IGNORE; - mOldItemStatuses[pos] = (myItemPos << FLAG_OFFSET) | changeFlag; - return true; - } - } - } else { - // check for additions for a match - for (int pos = curY - 1; pos >= endY; pos--) { - if (mCallback.areItemsTheSame(myItemPos, pos)) { - // found - final boolean theSame = mCallback.areContentsTheSame(myItemPos, pos); - final int changeFlag = theSame ? FLAG_MOVED_NOT_CHANGED - : FLAG_MOVED_CHANGED; - mOldItemStatuses[x - 1] = (pos << FLAG_OFFSET) | FLAG_IGNORE; - mNewItemStatuses[pos] = ((x - 1) << FLAG_OFFSET) | changeFlag; - return true; - } - } - } - curX = snake.x; - curY = snake.y; - } - return false; - } - - /** - * Dispatches the update events to the given adapter. - *

      - * For example, if you have an {@link android.support.v7.widget.RecyclerView.Adapter Adapter} - * that is backed by a {@link List}, you can swap the list with the new one then call this - * method to dispatch all updates to the RecyclerView. - *

      -         *     List oldList = mAdapter.getData();
      -         *     DiffResult result = DiffUtil.calculateDiff(new MyCallback(oldList, newList));
      -         *     mAdapter.setData(newList);
      -         *     result.dispatchUpdatesTo(mAdapter);
      -         * 
      - *

      - * Note that the RecyclerView requires you to dispatch adapter updates immediately when you - * change the data (you cannot defer {@code notify*} calls). The usage above adheres to this - * rule because updates are sent to the adapter right after the backing data is changed, - * before RecyclerView tries to read it. - *

      - * On the other hand, if you have another - * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver AdapterDataObserver} - * that tries to process events synchronously, this may confuse that observer because the - * list is instantly moved to its final state while the adapter updates are dispatched later - * on, one by one. If you have such an - * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver AdapterDataObserver}, - * you can use - * {@link #dispatchUpdatesTo(ListUpdateCallback)} to handle each modification - * manually. - * - * @param adapter A RecyclerView adapter which was displaying the old list and will start - * displaying the new list. - */ - public void dispatchUpdatesTo(final RecyclerView.Adapter adapter) { - dispatchUpdatesTo(new ListUpdateCallback() { - @Override - public void onInserted(int position, int count) { - adapter.notifyItemRangeInserted(position, count); - } - - @Override - public void onRemoved(int position, int count) { - adapter.notifyItemRangeRemoved(position, count); - } - - @Override - public void onMoved(int fromPosition, int toPosition) { - adapter.notifyItemMoved(fromPosition, toPosition); - } - - @Override - public void onChanged(int position, int count, Object payload) { - adapter.notifyItemRangeChanged(position, count, payload); - } - }); - } - - /** - * Dispatches update operations to the given Callback. - *

      - * These updates are atomic such that the first update call effects every update call that - * comes after it (the same as RecyclerView). - * - * @param updateCallback The callback to receive the update operations. - * @see #dispatchUpdatesTo(RecyclerView.Adapter) - */ - public void dispatchUpdatesTo(ListUpdateCallback updateCallback) { - final BatchingListUpdateCallback batchingCallback; - if (updateCallback instanceof BatchingListUpdateCallback) { - batchingCallback = (BatchingListUpdateCallback) updateCallback; - } else { - batchingCallback = new BatchingListUpdateCallback(updateCallback); - // replace updateCallback with a batching callback and override references to - // updateCallback so that we don't call it directly by mistake - //noinspection UnusedAssignment - updateCallback = batchingCallback; - } - // These are add/remove ops that are converted to moves. We track their positions until - // their respective update operations are processed. - final List postponedUpdates = new ArrayList<>(); - int posOld = mOldListSize; - int posNew = mNewListSize; - for (int snakeIndex = mSnakes.size() - 1; snakeIndex >= 0; snakeIndex--) { - final Snake snake = mSnakes.get(snakeIndex); - final int snakeSize = snake.size; - final int endX = snake.x + snakeSize; - final int endY = snake.y + snakeSize; - if (endX < posOld) { - dispatchRemovals(postponedUpdates, batchingCallback, endX, posOld - endX, endX); - } - - if (endY < posNew) { - dispatchAdditions(postponedUpdates, batchingCallback, endX, posNew - endY, - endY); - } - for (int i = snakeSize - 1; i >= 0; i--) { - if ((mOldItemStatuses[snake.x + i] & FLAG_MASK) == FLAG_CHANGED) { - batchingCallback.onChanged(snake.x + i, 1, - mCallback.getChangePayload(snake.x + i, snake.y + i)); - } - } - posOld = snake.x; - posNew = snake.y; - } - batchingCallback.dispatchLastEvent(); - } - - private static PostponedUpdate removePostponedUpdate(List updates, - int pos, boolean removal) { - for (int i = updates.size() - 1; i >= 0; i--) { - final PostponedUpdate update = updates.get(i); - if (update.posInOwnerList == pos && update.removal == removal) { - updates.remove(i); - for (int j = i; j < updates.size(); j++) { - // offset other ops since they swapped positions - updates.get(j).currentPos += removal ? 1 : -1; - } - return update; - } - } - return null; - } - - private void dispatchAdditions(List postponedUpdates, - ListUpdateCallback updateCallback, int start, int count, int globalIndex) { - if (!mDetectMoves) { - updateCallback.onInserted(start, count); - return; - } - for (int i = count - 1; i >= 0; i--) { - int status = mNewItemStatuses[globalIndex + i] & FLAG_MASK; - switch (status) { - case 0: // real addition - updateCallback.onInserted(start, 1); - for (PostponedUpdate update : postponedUpdates) { - update.currentPos += 1; - } - break; - case FLAG_MOVED_CHANGED: - case FLAG_MOVED_NOT_CHANGED: - final int pos = mNewItemStatuses[globalIndex + i] >> FLAG_OFFSET; - final PostponedUpdate update = removePostponedUpdate(postponedUpdates, pos, - true); - // the item was moved from that position - //noinspection ConstantConditions - updateCallback.onMoved(update.currentPos, start); - if (status == FLAG_MOVED_CHANGED) { - // also dispatch a change - updateCallback.onChanged(start, 1, - mCallback.getChangePayload(pos, globalIndex + i)); - } - break; - case FLAG_IGNORE: // ignoring this - postponedUpdates.add(new PostponedUpdate(globalIndex + i, start, false)); - break; - default: - throw new IllegalStateException( - "unknown flag for pos " + (globalIndex + i) + " " + Long - .toBinaryString(status)); - } - } - } - - private void dispatchRemovals(List postponedUpdates, - ListUpdateCallback updateCallback, int start, int count, int globalIndex) { - if (!mDetectMoves) { - updateCallback.onRemoved(start, count); - return; - } - for (int i = count - 1; i >= 0; i--) { - final int status = mOldItemStatuses[globalIndex + i] & FLAG_MASK; - switch (status) { - case 0: // real removal - updateCallback.onRemoved(start + i, 1); - for (PostponedUpdate update : postponedUpdates) { - update.currentPos -= 1; - } - break; - case FLAG_MOVED_CHANGED: - case FLAG_MOVED_NOT_CHANGED: - final int pos = mOldItemStatuses[globalIndex + i] >> FLAG_OFFSET; - final PostponedUpdate update = removePostponedUpdate(postponedUpdates, pos, - false); - // the item was moved to that position. we do -1 because this is a move not - // add and removing current item offsets the target move by 1 - //noinspection ConstantConditions - updateCallback.onMoved(start + i, update.currentPos - 1); - if (status == FLAG_MOVED_CHANGED) { - // also dispatch a change - updateCallback.onChanged(update.currentPos - 1, 1, - mCallback.getChangePayload(globalIndex + i, pos)); - } - break; - case FLAG_IGNORE: // ignoring this - postponedUpdates.add(new PostponedUpdate(globalIndex + i, start + i, true)); - break; - default: - throw new IllegalStateException( - "unknown flag for pos " + (globalIndex + i) + " " + Long - .toBinaryString(status)); - } - } - } - - @VisibleForTesting - List getSnakes() { - return mSnakes; - } - } - - /** - * Represents an update that we skipped because it was a move. - *

      - * When an update is skipped, it is tracked as other updates are dispatched until the matching - * add/remove operation is found at which point the tracked position is used to dispatch the - * update. - */ - private static class PostponedUpdate { - - int posInOwnerList; - - int currentPos; - - boolean removal; - - public PostponedUpdate(int posInOwnerList, int currentPos, boolean removal) { - this.posInOwnerList = posInOwnerList; - this.currentPos = currentPos; - this.removal = removal; - } - } -} diff --git a/app/src/main/java/android/support/v7/util/ListUpdateCallback.java b/app/src/main/java/android/support/v7/util/ListUpdateCallback.java deleted file mode 100644 index 2136202958..0000000000 --- a/app/src/main/java/android/support/v7/util/ListUpdateCallback.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package android.support.v7.util; - -/** - * An interface that can receive Update operations that are applied to a list. - *

      - * This class can be used together with DiffUtil to detect changes between two lists. - */ -public interface ListUpdateCallback { - /** - * Called when {@code count} number of items are inserted at the given position. - * - * @param position The position of the new item. - * @param count The number of items that have been added. - */ - void onInserted(int position, int count); - - /** - * Called when {@code count} number of items are removed from the given position. - * - * @param position The position of the item which has been removed. - * @param count The number of items which have been removed. - */ - void onRemoved(int position, int count); - - /** - * Called when an item changes its position in the list. - * - * @param fromPosition The previous position of the item before the move. - * @param toPosition The new position of the item. - */ - void onMoved(int fromPosition, int toPosition); - - /** - * Called when {@code count} number of items are updated at the given position. - * - * @param position The position of the item which has been updated. - * @param count The number of items which has changed. - */ - void onChanged(int position, int count, Object payload); -} diff --git a/app/src/main/java/android/support/v7/util/MessageThreadUtil.java b/app/src/main/java/android/support/v7/util/MessageThreadUtil.java deleted file mode 100644 index 8aa9eda764..0000000000 --- a/app/src/main/java/android/support/v7/util/MessageThreadUtil.java +++ /dev/null @@ -1,283 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import android.os.Handler; -import android.os.Looper; -import android.support.v4.content.ParallelExecutorCompat; -import android.util.Log; - -import java.util.concurrent.Executor; -import java.util.concurrent.atomic.AtomicBoolean; - -class MessageThreadUtil implements ThreadUtil { - - @Override - public MainThreadCallback getMainThreadProxy(final MainThreadCallback callback) { - return new MainThreadCallback() { - final private MessageQueue mQueue = new MessageQueue(); - final private Handler mMainThreadHandler = new Handler(Looper.getMainLooper()); - - private static final int UPDATE_ITEM_COUNT = 1; - private static final int ADD_TILE = 2; - private static final int REMOVE_TILE = 3; - - @Override - public void updateItemCount(int generation, int itemCount) { - sendMessage(SyncQueueItem.obtainMessage(UPDATE_ITEM_COUNT, generation, itemCount)); - } - - @Override - public void addTile(int generation, TileList.Tile tile) { - sendMessage(SyncQueueItem.obtainMessage(ADD_TILE, generation, tile)); - } - - @Override - public void removeTile(int generation, int position) { - sendMessage(SyncQueueItem.obtainMessage(REMOVE_TILE, generation, position)); - } - - private void sendMessage(SyncQueueItem msg) { - mQueue.sendMessage(msg); - mMainThreadHandler.post(mMainThreadRunnable); - } - - private Runnable mMainThreadRunnable = new Runnable() { - @Override - public void run() { - SyncQueueItem msg = mQueue.next(); - while (msg != null) { - switch (msg.what) { - case UPDATE_ITEM_COUNT: - callback.updateItemCount(msg.arg1, msg.arg2); - break; - case ADD_TILE: - //noinspection unchecked - callback.addTile(msg.arg1, (TileList.Tile) msg.data); - break; - case REMOVE_TILE: - callback.removeTile(msg.arg1, msg.arg2); - break; - default: - Log.e("ThreadUtil", "Unsupported message, what=" + msg.what); - } - msg = mQueue.next(); - } - } - }; - }; - } - - @Override - public BackgroundCallback getBackgroundProxy(final BackgroundCallback callback) { - return new BackgroundCallback() { - final private MessageQueue mQueue = new MessageQueue(); - final private Executor mExecutor = ParallelExecutorCompat.getParallelExecutor(); - AtomicBoolean mBackgroundRunning = new AtomicBoolean(false); - - private static final int REFRESH = 1; - private static final int UPDATE_RANGE = 2; - private static final int LOAD_TILE = 3; - private static final int RECYCLE_TILE = 4; - - @Override - public void refresh(int generation) { - sendMessageAtFrontOfQueue(SyncQueueItem.obtainMessage(REFRESH, generation, null)); - } - - @Override - public void updateRange(int rangeStart, int rangeEnd, - int extRangeStart, int extRangeEnd, int scrollHint) { - sendMessageAtFrontOfQueue(SyncQueueItem.obtainMessage(UPDATE_RANGE, - rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint, null)); - } - - @Override - public void loadTile(int position, int scrollHint) { - sendMessage(SyncQueueItem.obtainMessage(LOAD_TILE, position, scrollHint)); - } - - @Override - public void recycleTile(TileList.Tile tile) { - sendMessage(SyncQueueItem.obtainMessage(RECYCLE_TILE, 0, tile)); - } - - private void sendMessage(SyncQueueItem msg) { - mQueue.sendMessage(msg); - maybeExecuteBackgroundRunnable(); - } - - private void sendMessageAtFrontOfQueue(SyncQueueItem msg) { - mQueue.sendMessageAtFrontOfQueue(msg); - maybeExecuteBackgroundRunnable(); - } - - private void maybeExecuteBackgroundRunnable() { - if (mBackgroundRunning.compareAndSet(false, true)) { - mExecutor.execute(mBackgroundRunnable); - } - } - - private Runnable mBackgroundRunnable = new Runnable() { - @Override - public void run() { - while (true) { - SyncQueueItem msg = mQueue.next(); - if (msg == null) { - break; - } - switch (msg.what) { - case REFRESH: - mQueue.removeMessages(REFRESH); - callback.refresh(msg.arg1); - break; - case UPDATE_RANGE: - mQueue.removeMessages(UPDATE_RANGE); - mQueue.removeMessages(LOAD_TILE); - callback.updateRange( - msg.arg1, msg.arg2, msg.arg3, msg.arg4, msg.arg5); - break; - case LOAD_TILE: - callback.loadTile(msg.arg1, msg.arg2); - break; - case RECYCLE_TILE: - //noinspection unchecked - callback.recycleTile((TileList.Tile) msg.data); - break; - default: - Log.e("ThreadUtil", "Unsupported message, what=" + msg.what); - } - } - mBackgroundRunning.set(false); - } - }; - }; - } - - /** - * Replica of android.os.Message. Unfortunately, cannot use it without a Handler and don't want - * to create a thread just for this component. - */ - static class SyncQueueItem { - - private static SyncQueueItem sPool; - private static final Object sPoolLock = new Object(); - private SyncQueueItem next; - public int what; - public int arg1; - public int arg2; - public int arg3; - public int arg4; - public int arg5; - public Object data; - - void recycle() { - next = null; - what = arg1 = arg2 = arg3 = arg4 = arg5 = 0; - data = null; - synchronized (sPoolLock) { - if (sPool != null) { - next = sPool; - } - sPool = this; - } - } - - static SyncQueueItem obtainMessage(int what, int arg1, int arg2, int arg3, int arg4, - int arg5, Object data) { - synchronized (sPoolLock) { - final SyncQueueItem item; - if (sPool == null) { - item = new SyncQueueItem(); - } else { - item = sPool; - sPool = sPool.next; - item.next = null; - } - item.what = what; - item.arg1 = arg1; - item.arg2 = arg2; - item.arg3 = arg3; - item.arg4 = arg4; - item.arg5 = arg5; - item.data = data; - return item; - } - } - - static SyncQueueItem obtainMessage(int what, int arg1, int arg2) { - return obtainMessage(what, arg1, arg2, 0, 0, 0, null); - } - - static SyncQueueItem obtainMessage(int what, int arg1, Object data) { - return obtainMessage(what, arg1, 0, 0, 0, 0, data); - } - } - - static class MessageQueue { - - private SyncQueueItem mRoot; - - synchronized SyncQueueItem next() { - if (mRoot == null) { - return null; - } - final SyncQueueItem next = mRoot; - mRoot = mRoot.next; - return next; - } - - synchronized void sendMessageAtFrontOfQueue(SyncQueueItem item) { - item.next = mRoot; - mRoot = item; - } - - synchronized void sendMessage(SyncQueueItem item) { - if (mRoot == null) { - mRoot = item; - return; - } - SyncQueueItem last = mRoot; - while (last.next != null) { - last = last.next; - } - last.next = item; - } - - synchronized void removeMessages(int what) { - while (mRoot != null && mRoot.what == what) { - SyncQueueItem item = mRoot; - mRoot = mRoot.next; - item.recycle(); - } - if (mRoot != null) { - SyncQueueItem prev = mRoot; - SyncQueueItem item = prev.next; - while (item != null) { - SyncQueueItem next = item.next; - if (item.what == what) { - prev.next = next; - item.recycle(); - } else { - prev = item; - } - item = next; - } - } - } - } -} diff --git a/app/src/main/java/android/support/v7/util/SortedList.java b/app/src/main/java/android/support/v7/util/SortedList.java deleted file mode 100644 index 1d48468191..0000000000 --- a/app/src/main/java/android/support/v7/util/SortedList.java +++ /dev/null @@ -1,821 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.Collection; -import java.util.Comparator; - -/** - * A Sorted list implementation that can keep items in order and also notify for changes in the - * list - * such that it can be bound to a {@link android.support.v7.widget.RecyclerView.Adapter - * RecyclerView.Adapter}. - *

      - * It keeps items ordered using the {@link Callback#compare(Object, Object)} method and uses - * binary search to retrieve items. If the sorting criteria of your items may change, make sure you - * call appropriate methods while editing them to avoid data inconsistencies. - *

      - * You can control the order of items and change notifications via the {@link Callback} parameter. - */ -@SuppressWarnings("unchecked") -public class SortedList { - - /** - * Used by {@link #indexOf(Object)} when he item cannot be found in the list. - */ - public static final int INVALID_POSITION = -1; - - private static final int MIN_CAPACITY = 10; - private static final int CAPACITY_GROWTH = MIN_CAPACITY; - private static final int INSERTION = 1; - private static final int DELETION = 1 << 1; - private static final int LOOKUP = 1 << 2; - T[] mData; - - /** - * A copy of the previous list contents used during the merge phase of addAll. - */ - private T[] mOldData; - private int mOldDataStart; - private int mOldDataSize; - - /** - * The size of the valid portion of mData during the merge phase of addAll. - */ - private int mMergedSize; - - - /** - * The callback instance that controls the behavior of the SortedList and get notified when - * changes happen. - */ - private Callback mCallback; - - private BatchedCallback mBatchedCallback; - - private int mSize; - private final Class mTClass; - - /** - * Creates a new SortedList of type T. - * - * @param klass The class of the contents of the SortedList. - * @param callback The callback that controls the behavior of SortedList. - */ - public SortedList(Class klass, Callback callback) { - this(klass, callback, MIN_CAPACITY); - } - - /** - * Creates a new SortedList of type T. - * - * @param klass The class of the contents of the SortedList. - * @param callback The callback that controls the behavior of SortedList. - * @param initialCapacity The initial capacity to hold items. - */ - public SortedList(Class klass, Callback callback, int initialCapacity) { - mTClass = klass; - mData = (T[]) Array.newInstance(klass, initialCapacity); - mCallback = callback; - mSize = 0; - } - - /** - * The number of items in the list. - * - * @return The number of items in the list. - */ - public int size() { - return mSize; - } - - /** - * Adds the given item to the list. If this is a new item, SortedList calls - * {@link Callback#onInserted(int, int)}. - *

      - * If the item already exists in the list and its sorting criteria is not changed, it is - * replaced with the existing Item. SortedList uses - * {@link Callback#areItemsTheSame(Object, Object)} to check if two items are the same item - * and uses {@link Callback#areContentsTheSame(Object, Object)} to decide whether it should - * call {@link Callback#onChanged(int, int)} or not. In both cases, it always removes the - * reference to the old item and puts the new item into the backing array even if - * {@link Callback#areContentsTheSame(Object, Object)} returns false. - *

      - * If the sorting criteria of the item is changed, SortedList won't be able to find - * its duplicate in the list which will result in having a duplicate of the Item in the list. - * If you need to update sorting criteria of an item that already exists in the list, - * use {@link #updateItemAt(int, Object)}. You can find the index of the item using - * {@link #indexOf(Object)} before you update the object. - * - * @param item The item to be added into the list. - * - * @return The index of the newly added item. - * @see {@link Callback#compare(Object, Object)} - * @see {@link Callback#areItemsTheSame(Object, Object)} - * @see {@link Callback#areContentsTheSame(Object, Object)}} - */ - public int add(T item) { - throwIfMerging(); - return add(item, true); - } - - /** - * Adds the given items to the list. Equivalent to calling {@link SortedList#add} in a loop, - * except the callback events may be in a different order/granularity since addAll can batch - * them for better performance. - *

      - * If allowed, may modify the input array and even take the ownership over it in order - * to avoid extra memory allocation during sorting and deduplication. - *

      - * @param items Array of items to be added into the list. - * @param mayModifyInput If true, SortedList is allowed to modify the input. - * @see {@link SortedList#addAll(Object[] items)}. - */ - public void addAll(T[] items, boolean mayModifyInput) { - throwIfMerging(); - if (items.length == 0) { - return; - } - if (mayModifyInput) { - addAllInternal(items); - } else { - T[] copy = (T[]) Array.newInstance(mTClass, items.length); - System.arraycopy(items, 0, copy, 0, items.length); - addAllInternal(copy); - } - - } - - /** - * Adds the given items to the list. Does not modify the input. - * - * @see {@link SortedList#addAll(T[] items, boolean mayModifyInput)} - * - * @param items Array of items to be added into the list. - */ - public void addAll(T... items) { - addAll(items, false); - } - - /** - * Adds the given items to the list. Does not modify the input. - * - * @see {@link SortedList#addAll(T[] items, boolean mayModifyInput)} - * - * @param items Collection of items to be added into the list. - */ - public void addAll(Collection items) { - T[] copy = (T[]) Array.newInstance(mTClass, items.size()); - addAll(items.toArray(copy), true); - } - - private void addAllInternal(T[] newItems) { - final boolean forceBatchedUpdates = !(mCallback instanceof BatchedCallback); - if (forceBatchedUpdates) { - beginBatchedUpdates(); - } - - mOldData = mData; - mOldDataStart = 0; - mOldDataSize = mSize; - - Arrays.sort(newItems, mCallback); // Arrays.sort is stable. - - final int newSize = deduplicate(newItems); - if (mSize == 0) { - mData = newItems; - mSize = newSize; - mMergedSize = newSize; - mCallback.onInserted(0, newSize); - } else { - merge(newItems, newSize); - } - - mOldData = null; - - if (forceBatchedUpdates) { - endBatchedUpdates(); - } - } - - /** - * Remove duplicate items, leaving only the last item from each group of "same" items. - * Move the remaining items to the beginning of the array. - * - * @return Number of deduplicated items at the beginning of the array. - */ - private int deduplicate(T[] items) { - if (items.length == 0) { - throw new IllegalArgumentException("Input array must be non-empty"); - } - - // Keep track of the range of equal items at the end of the output. - // Start with the range containing just the first item. - int rangeStart = 0; - int rangeEnd = 1; - - for (int i = 1; i < items.length; ++i) { - T currentItem = items[i]; - - int compare = mCallback.compare(items[rangeStart], currentItem); - if (compare > 0) { - throw new IllegalArgumentException("Input must be sorted in ascending order."); - } - - if (compare == 0) { - // The range of equal items continues, update it. - final int sameItemPos = findSameItem(currentItem, items, rangeStart, rangeEnd); - if (sameItemPos != INVALID_POSITION) { - // Replace the duplicate item. - items[sameItemPos] = currentItem; - } else { - // Expand the range. - if (rangeEnd != i) { // Avoid redundant copy. - items[rangeEnd] = currentItem; - } - rangeEnd++; - } - } else { - // The range has ended. Reset it to contain just the current item. - if (rangeEnd != i) { // Avoid redundant copy. - items[rangeEnd] = currentItem; - } - rangeStart = rangeEnd++; - } - } - return rangeEnd; - } - - - private int findSameItem(T item, T[] items, int from, int to) { - for (int pos = from; pos < to; pos++) { - if (mCallback.areItemsTheSame(items[pos], item)) { - return pos; - } - } - return INVALID_POSITION; - } - - /** - * This method assumes that newItems are sorted and deduplicated. - */ - private void merge(T[] newData, int newDataSize) { - final int mergedCapacity = mSize + newDataSize + CAPACITY_GROWTH; - mData = (T[]) Array.newInstance(mTClass, mergedCapacity); - mMergedSize = 0; - - int newDataStart = 0; - while (mOldDataStart < mOldDataSize || newDataStart < newDataSize) { - if (mOldDataStart == mOldDataSize) { - // No more old items, copy the remaining new items. - int itemCount = newDataSize - newDataStart; - System.arraycopy(newData, newDataStart, mData, mMergedSize, itemCount); - mMergedSize += itemCount; - mSize += itemCount; - mCallback.onInserted(mMergedSize - itemCount, itemCount); - break; - } - - if (newDataStart == newDataSize) { - // No more new items, copy the remaining old items. - int itemCount = mOldDataSize - mOldDataStart; - System.arraycopy(mOldData, mOldDataStart, mData, mMergedSize, itemCount); - mMergedSize += itemCount; - break; - } - - T oldItem = mOldData[mOldDataStart]; - T newItem = newData[newDataStart]; - int compare = mCallback.compare(oldItem, newItem); - if (compare > 0) { - // New item is lower, output it. - mData[mMergedSize++] = newItem; - mSize++; - newDataStart++; - mCallback.onInserted(mMergedSize - 1, 1); - } else if (compare == 0 && mCallback.areItemsTheSame(oldItem, newItem)) { - // Items are the same. Output the new item, but consume both. - mData[mMergedSize++] = newItem; - newDataStart++; - mOldDataStart++; - if (!mCallback.areContentsTheSame(oldItem, newItem)) { - mCallback.onChanged(mMergedSize - 1, 1); - } - } else { - // Old item is lower than or equal to (but not the same as the new). Output it. - // New item with the same sort order will be inserted later. - mData[mMergedSize++] = oldItem; - mOldDataStart++; - } - } - } - - private void throwIfMerging() { - if (mOldData != null) { - throw new IllegalStateException("Cannot call this method from within addAll"); - } - } - - /** - * Batches adapter updates that happen between calling this method until calling - * {@link #endBatchedUpdates()}. For example, if you add multiple items in a loop - * and they are placed into consecutive indices, SortedList calls - * {@link Callback#onInserted(int, int)} only once with the proper item count. If an event - * cannot be merged with the previous event, the previous event is dispatched - * to the callback instantly. - *

      - * After running your data updates, you must call {@link #endBatchedUpdates()} - * which will dispatch any deferred data change event to the current callback. - *

      - * A sample implementation may look like this: - *

      -     *     mSortedList.beginBatchedUpdates();
      -     *     try {
      -     *         mSortedList.add(item1)
      -     *         mSortedList.add(item2)
      -     *         mSortedList.remove(item3)
      -     *         ...
      -     *     } finally {
      -     *         mSortedList.endBatchedUpdates();
      -     *     }
      -     * 
      - *

      - * Instead of using this method to batch calls, you can use a Callback that extends - * {@link BatchedCallback}. In that case, you must make sure that you are manually calling - * {@link BatchedCallback#dispatchLastEvent()} right after you complete your data changes. - * Failing to do so may create data inconsistencies with the Callback. - *

      - * If the current Callback in an instance of {@link BatchedCallback}, calling this method - * has no effect. - */ - public void beginBatchedUpdates() { - throwIfMerging(); - if (mCallback instanceof BatchedCallback) { - return; - } - if (mBatchedCallback == null) { - mBatchedCallback = new BatchedCallback(mCallback); - } - mCallback = mBatchedCallback; - } - - /** - * Ends the update transaction and dispatches any remaining event to the callback. - */ - public void endBatchedUpdates() { - throwIfMerging(); - if (mCallback instanceof BatchedCallback) { - ((BatchedCallback) mCallback).dispatchLastEvent(); - } - if (mCallback == mBatchedCallback) { - mCallback = mBatchedCallback.mWrappedCallback; - } - } - - private int add(T item, boolean notify) { - int index = findIndexOf(item, mData, 0, mSize, INSERTION); - if (index == INVALID_POSITION) { - index = 0; - } else if (index < mSize) { - T existing = mData[index]; - if (mCallback.areItemsTheSame(existing, item)) { - if (mCallback.areContentsTheSame(existing, item)) { - //no change but still replace the item - mData[index] = item; - return index; - } else { - mData[index] = item; - mCallback.onChanged(index, 1); - return index; - } - } - } - addToData(index, item); - if (notify) { - mCallback.onInserted(index, 1); - } - return index; - } - - /** - * Removes the provided item from the list and calls {@link Callback#onRemoved(int, int)}. - * - * @param item The item to be removed from the list. - * - * @return True if item is removed, false if item cannot be found in the list. - */ - public boolean remove(T item) { - throwIfMerging(); - return remove(item, true); - } - - /** - * Removes the item at the given index and calls {@link Callback#onRemoved(int, int)}. - * - * @param index The index of the item to be removed. - * - * @return The removed item. - */ - public T removeItemAt(int index) { - throwIfMerging(); - T item = get(index); - removeItemAtIndex(index, true); - return item; - } - - private boolean remove(T item, boolean notify) { - int index = findIndexOf(item, mData, 0, mSize, DELETION); - if (index == INVALID_POSITION) { - return false; - } - removeItemAtIndex(index, notify); - return true; - } - - private void removeItemAtIndex(int index, boolean notify) { - System.arraycopy(mData, index + 1, mData, index, mSize - index - 1); - mSize--; - mData[mSize] = null; - if (notify) { - mCallback.onRemoved(index, 1); - } - } - - /** - * Updates the item at the given index and calls {@link Callback#onChanged(int, int)} and/or - * {@link Callback#onMoved(int, int)} if necessary. - *

      - * You can use this method if you need to change an existing Item such that its position in the - * list may change. - *

      - * If the new object is a different object (get(index) != item) and - * {@link Callback#areContentsTheSame(Object, Object)} returns true, SortedList - * avoids calling {@link Callback#onChanged(int, int)} otherwise it calls - * {@link Callback#onChanged(int, int)}. - *

      - * If the new position of the item is different than the provided index, - * SortedList - * calls {@link Callback#onMoved(int, int)}. - * - * @param index The index of the item to replace - * @param item The item to replace the item at the given Index. - * @see #add(Object) - */ - public void updateItemAt(int index, T item) { - throwIfMerging(); - final T existing = get(index); - // assume changed if the same object is given back - boolean contentsChanged = existing == item || !mCallback.areContentsTheSame(existing, item); - if (existing != item) { - // different items, we can use comparison and may avoid lookup - final int cmp = mCallback.compare(existing, item); - if (cmp == 0) { - mData[index] = item; - if (contentsChanged) { - mCallback.onChanged(index, 1); - } - return; - } - } - if (contentsChanged) { - mCallback.onChanged(index, 1); - } - // TODO this done in 1 pass to avoid shifting twice. - removeItemAtIndex(index, false); - int newIndex = add(item, false); - if (index != newIndex) { - mCallback.onMoved(index, newIndex); - } - } - - /** - * This method can be used to recalculate the position of the item at the given index, without - * triggering an {@link Callback#onChanged(int, int)} callback. - *

      - * If you are editing objects in the list such that their position in the list may change but - * you don't want to trigger an onChange animation, you can use this method to re-position it. - * If the item changes position, SortedList will call {@link Callback#onMoved(int, int)} - * without - * calling {@link Callback#onChanged(int, int)}. - *

      - * A sample usage may look like: - * - *

      -     *     final int position = mSortedList.indexOf(item);
      -     *     item.incrementPriority(); // assume items are sorted by priority
      -     *     mSortedList.recalculatePositionOfItemAt(position);
      -     * 
      - * In the example above, because the sorting criteria of the item has been changed, - * mSortedList.indexOf(item) will not be able to find the item. This is why the code above - * first - * gets the position before editing the item, edits it and informs the SortedList that item - * should be repositioned. - * - * @param index The current index of the Item whose position should be re-calculated. - * @see #updateItemAt(int, Object) - * @see #add(Object) - */ - public void recalculatePositionOfItemAt(int index) { - throwIfMerging(); - // TODO can be improved - final T item = get(index); - removeItemAtIndex(index, false); - int newIndex = add(item, false); - if (index != newIndex) { - mCallback.onMoved(index, newIndex); - } - } - - /** - * Returns the item at the given index. - * - * @param index The index of the item to retrieve. - * - * @return The item at the given index. - * @throws java.lang.IndexOutOfBoundsException if provided index is negative or larger than the - * size of the list. - */ - public T get(int index) throws IndexOutOfBoundsException { - if (index >= mSize || index < 0) { - throw new IndexOutOfBoundsException("Asked to get item at " + index + " but size is " - + mSize); - } - if (mOldData != null) { - // The call is made from a callback during addAll execution. The data is split - // between mData and mOldData. - if (index >= mMergedSize) { - return mOldData[index - mMergedSize + mOldDataStart]; - } - } - return mData[index]; - } - - /** - * Returns the position of the provided item. - * - * @param item The item to query for position. - * - * @return The position of the provided item or {@link #INVALID_POSITION} if item is not in the - * list. - */ - public int indexOf(T item) { - if (mOldData != null) { - int index = findIndexOf(item, mData, 0, mMergedSize, LOOKUP); - if (index != INVALID_POSITION) { - return index; - } - index = findIndexOf(item, mOldData, mOldDataStart, mOldDataSize, LOOKUP); - if (index != INVALID_POSITION) { - return index - mOldDataStart + mMergedSize; - } - return INVALID_POSITION; - } - return findIndexOf(item, mData, 0, mSize, LOOKUP); - } - - private int findIndexOf(T item, T[] mData, int left, int right, int reason) { - while (left < right) { - final int middle = (left + right) / 2; - T myItem = mData[middle]; - final int cmp = mCallback.compare(myItem, item); - if (cmp < 0) { - left = middle + 1; - } else if (cmp == 0) { - if (mCallback.areItemsTheSame(myItem, item)) { - return middle; - } else { - int exact = linearEqualitySearch(item, middle, left, right); - if (reason == INSERTION) { - return exact == INVALID_POSITION ? middle : exact; - } else { - return exact; - } - } - } else { - right = middle; - } - } - return reason == INSERTION ? left : INVALID_POSITION; - } - - private int linearEqualitySearch(T item, int middle, int left, int right) { - // go left - for (int next = middle - 1; next >= left; next--) { - T nextItem = mData[next]; - int cmp = mCallback.compare(nextItem, item); - if (cmp != 0) { - break; - } - if (mCallback.areItemsTheSame(nextItem, item)) { - return next; - } - } - for (int next = middle + 1; next < right; next++) { - T nextItem = mData[next]; - int cmp = mCallback.compare(nextItem, item); - if (cmp != 0) { - break; - } - if (mCallback.areItemsTheSame(nextItem, item)) { - return next; - } - } - return INVALID_POSITION; - } - - private void addToData(int index, T item) { - if (index > mSize) { - throw new IndexOutOfBoundsException( - "cannot add item to " + index + " because size is " + mSize); - } - if (mSize == mData.length) { - // we are at the limit enlarge - T[] newData = (T[]) Array.newInstance(mTClass, mData.length + CAPACITY_GROWTH); - System.arraycopy(mData, 0, newData, 0, index); - newData[index] = item; - System.arraycopy(mData, index, newData, index + 1, mSize - index); - mData = newData; - } else { - // just shift, we fit - System.arraycopy(mData, index, mData, index + 1, mSize - index); - mData[index] = item; - } - mSize++; - } - - /** - * Removes all items from the SortedList. - */ - public void clear() { - throwIfMerging(); - if (mSize == 0) { - return; - } - final int prevSize = mSize; - Arrays.fill(mData, 0, prevSize, null); - mSize = 0; - mCallback.onRemoved(0, prevSize); - } - - /** - * The class that controls the behavior of the {@link SortedList}. - *

      - * It defines how items should be sorted and how duplicates should be handled. - *

      - * SortedList calls the callback methods on this class to notify changes about the underlying - * data. - */ - public static abstract class Callback implements Comparator, ListUpdateCallback { - - /** - * Similar to {@link java.util.Comparator#compare(Object, Object)}, should compare two and - * return how they should be ordered. - * - * @param o1 The first object to compare. - * @param o2 The second object to compare. - * - * @return a negative integer, zero, or a positive integer as the - * first argument is less than, equal to, or greater than the - * second. - */ - @Override - abstract public int compare(T2 o1, T2 o2); - - /** - * Called by the SortedList when the item at the given position is updated. - * - * @param position The position of the item which has been updated. - * @param count The number of items which has changed. - */ - abstract public void onChanged(int position, int count); - - @Override - public void onChanged(int position, int count, Object payload) { - onChanged(position, count); - } - - /** - * Called by the SortedList when it wants to check whether two items have the same data - * or not. SortedList uses this information to decide whether it should call - * {@link #onChanged(int, int)} or not. - *

      - * SortedList uses this method to check equality instead of {@link Object#equals(Object)} - * so - * that you can change its behavior depending on your UI. - *

      - * For example, if you are using SortedList with a {@link android.support.v7.widget.RecyclerView.Adapter - * RecyclerView.Adapter}, you should - * return whether the items' visual representations are the same or not. - * - * @param oldItem The previous representation of the object. - * @param newItem The new object that replaces the previous one. - * - * @return True if the contents of the items are the same or false if they are different. - */ - abstract public boolean areContentsTheSame(T2 oldItem, T2 newItem); - - /** - * Called by the SortedList to decide whether two object represent the same Item or not. - *

      - * For example, if your items have unique ids, this method should check their equality. - * - * @param item1 The first item to check. - * @param item2 The second item to check. - * - * @return True if the two items represent the same object or false if they are different. - */ - abstract public boolean areItemsTheSame(T2 item1, T2 item2); - } - - /** - * A callback implementation that can batch notify events dispatched by the SortedList. - *

      - * This class can be useful if you want to do multiple operations on a SortedList but don't - * want to dispatch each event one by one, which may result in a performance issue. - *

      - * For example, if you are going to add multiple items to a SortedList, BatchedCallback call - * convert individual onInserted(index, 1) calls into one - * onInserted(index, N) if items are added into consecutive indices. This change - * can help RecyclerView resolve changes much more easily. - *

      - * If consecutive changes in the SortedList are not suitable for batching, BatchingCallback - * dispatches them as soon as such case is detected. After your edits on the SortedList is - * complete, you must always call {@link BatchedCallback#dispatchLastEvent()} to flush - * all changes to the Callback. - */ - public static class BatchedCallback extends Callback { - - private final Callback mWrappedCallback; - private final BatchingListUpdateCallback mBatchingListUpdateCallback; - /** - * Creates a new BatchedCallback that wraps the provided Callback. - * - * @param wrappedCallback The Callback which should received the data change callbacks. - * Other method calls (e.g. {@link #compare(Object, Object)} from - * the SortedList are directly forwarded to this Callback. - */ - public BatchedCallback(Callback wrappedCallback) { - mWrappedCallback = wrappedCallback; - mBatchingListUpdateCallback = new BatchingListUpdateCallback(mWrappedCallback); - } - - @Override - public int compare(T2 o1, T2 o2) { - return mWrappedCallback.compare(o1, o2); - } - - @Override - public void onInserted(int position, int count) { - mBatchingListUpdateCallback.onInserted(position, count); - } - - @Override - public void onRemoved(int position, int count) { - mBatchingListUpdateCallback.onRemoved(position, count); - } - - @Override - public void onMoved(int fromPosition, int toPosition) { - mBatchingListUpdateCallback.onInserted(fromPosition, toPosition); - } - - @Override - public void onChanged(int position, int count) { - mBatchingListUpdateCallback.onChanged(position, count, null); - } - - @Override - public boolean areContentsTheSame(T2 oldItem, T2 newItem) { - return mWrappedCallback.areContentsTheSame(oldItem, newItem); - } - - @Override - public boolean areItemsTheSame(T2 item1, T2 item2) { - return mWrappedCallback.areItemsTheSame(item1, item2); - } - - /** - * This method dispatches any pending event notifications to the wrapped Callback. - * You must always call this method after you are done with editing the SortedList. - */ - public void dispatchLastEvent() { - mBatchingListUpdateCallback.dispatchLastEvent(); - } - } -} diff --git a/app/src/main/java/android/support/v7/util/ThreadUtil.java b/app/src/main/java/android/support/v7/util/ThreadUtil.java deleted file mode 100644 index 05db034259..0000000000 --- a/app/src/main/java/android/support/v7/util/ThreadUtil.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -interface ThreadUtil { - - interface MainThreadCallback { - - void updateItemCount(int generation, int itemCount); - - void addTile(int generation, TileList.Tile tile); - - void removeTile(int generation, int position); - } - - interface BackgroundCallback { - - void refresh(int generation); - - void updateRange(int rangeStart, int rangeEnd, int extRangeStart, int extRangeEnd, - int scrollHint); - - void loadTile(int position, int scrollHint); - - void recycleTile(TileList.Tile tile); - } - - MainThreadCallback getMainThreadProxy(MainThreadCallback callback); - - BackgroundCallback getBackgroundProxy(BackgroundCallback callback); -} diff --git a/app/src/main/java/android/support/v7/util/TileList.java b/app/src/main/java/android/support/v7/util/TileList.java deleted file mode 100644 index f686a31c24..0000000000 --- a/app/src/main/java/android/support/v7/util/TileList.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.util; - -import android.util.SparseArray; - -import java.lang.reflect.Array; - -/** - * A sparse collection of tiles sorted for efficient access. - */ -class TileList { - - final int mTileSize; - - // Keyed by start position. - private final SparseArray> mTiles = new SparseArray>(10); - - Tile mLastAccessedTile; - - public TileList(int tileSize) { - mTileSize = tileSize; - } - - public T getItemAt(int pos) { - if (mLastAccessedTile == null || !mLastAccessedTile.containsPosition(pos)) { - final int startPosition = pos - (pos % mTileSize); - final int index = mTiles.indexOfKey(startPosition); - if (index < 0) { - return null; - } - mLastAccessedTile = mTiles.valueAt(index); - } - return mLastAccessedTile.getByPosition(pos); - } - - public int size() { - return mTiles.size(); - } - - public void clear() { - mTiles.clear(); - } - - public Tile getAtIndex(int index) { - return mTiles.valueAt(index); - } - - public Tile addOrReplace(Tile newTile) { - final int index = mTiles.indexOfKey(newTile.mStartPosition); - if (index < 0) { - mTiles.put(newTile.mStartPosition, newTile); - return null; - } - Tile oldTile = mTiles.valueAt(index); - mTiles.setValueAt(index, newTile); - if (mLastAccessedTile == oldTile) { - mLastAccessedTile = newTile; - } - return oldTile; - } - - public Tile removeAtPos(int startPosition) { - Tile tile = mTiles.get(startPosition); - if (mLastAccessedTile == tile) { - mLastAccessedTile = null; - } - mTiles.delete(startPosition); - return tile; - } - - public static class Tile { - public final T[] mItems; - public int mStartPosition; - public int mItemCount; - Tile mNext; // Used only for pooling recycled tiles. - - public Tile(Class klass, int size) { - //noinspection unchecked - mItems = (T[]) Array.newInstance(klass, size); - } - - boolean containsPosition(int pos) { - return mStartPosition <= pos && pos < mStartPosition + mItemCount; - } - - T getByPosition(int pos) { - return mItems[pos - mStartPosition]; - } - } -} diff --git a/app/src/main/java/android/support/v7/widget/AdapterHelper.java b/app/src/main/java/android/support/v7/widget/AdapterHelper.java index 47e972225e..ecf0c294d4 100644 --- a/app/src/main/java/android/support/v7/widget/AdapterHelper.java +++ b/app/src/main/java/android/support/v7/widget/AdapterHelper.java @@ -23,7 +23,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import static android.support.v7.widget.RecyclerView.*; +import static android.support.v7.widget.RecyclerView.ViewHolder; /** * Helper class that can enqueue and process adapter update operations. @@ -67,8 +67,6 @@ class AdapterHelper implements OpReorderer.Callback { final OpReorderer mOpReorderer; - private int mExistingUpdateTypes = 0; - AdapterHelper(Callback callback) { this(callback, false); } @@ -87,7 +85,6 @@ class AdapterHelper implements OpReorderer.Callback { void reset() { recycleUpdateOpsAndClearList(mPendingUpdates); recycleUpdateOpsAndClearList(mPostponedList); - mExistingUpdateTypes = 0; } void preProcess() { @@ -122,7 +119,6 @@ class AdapterHelper implements OpReorderer.Callback { mCallback.onDispatchSecondPass(mPostponedList.get(i)); } recycleUpdateOpsAndClearList(mPostponedList); - mExistingUpdateTypes = 0; } private void applyMove(UpdateOp op) { @@ -149,7 +145,7 @@ class AdapterHelper implements OpReorderer.Callback { if (type == POSITION_TYPE_INVISIBLE) { // Looks like we have other updates that we cannot merge with this one. // Create an UpdateOp and dispatch it to LayoutManager. - UpdateOp newOp = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount, null); + UpdateOp newOp = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount); dispatchAndUpdateViewHolders(newOp); typeChanged = true; } @@ -160,7 +156,7 @@ class AdapterHelper implements OpReorderer.Callback { if (type == POSITION_TYPE_NEW_OR_LAID_OUT) { // Looks like we have other updates that we cannot merge with this one. // Create UpdateOp op and dispatch it to LayoutManager. - UpdateOp newOp = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount, null); + UpdateOp newOp = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount); postponeAndUpdateViewHolders(newOp); typeChanged = true; } @@ -176,7 +172,7 @@ class AdapterHelper implements OpReorderer.Callback { } if (tmpCount != op.itemCount) { // all 1 effect recycleUpdateOp(op); - op = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount, null); + op = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount); } if (type == POSITION_TYPE_INVISIBLE) { dispatchAndUpdateViewHolders(op); @@ -194,8 +190,7 @@ class AdapterHelper implements OpReorderer.Callback { ViewHolder vh = mCallback.findViewHolder(position); if (vh != null || canFindInPreLayout(position)) { // deferred if (type == POSITION_TYPE_INVISIBLE) { - UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount, - op.payload); + UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount); dispatchAndUpdateViewHolders(newOp); tmpCount = 0; tmpStart = position; @@ -203,8 +198,7 @@ class AdapterHelper implements OpReorderer.Callback { type = POSITION_TYPE_NEW_OR_LAID_OUT; } else { // applied if (type == POSITION_TYPE_NEW_OR_LAID_OUT) { - UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount, - op.payload); + UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount); postponeAndUpdateViewHolders(newOp); tmpCount = 0; tmpStart = position; @@ -214,9 +208,8 @@ class AdapterHelper implements OpReorderer.Callback { tmpCount++; } if (tmpCount != op.itemCount) { // all 1 effect - Object payload = op.payload; recycleUpdateOp(op); - op = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount, payload); + op = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount); } if (type == POSITION_TYPE_INVISIBLE) { dispatchAndUpdateViewHolders(op); @@ -279,7 +272,7 @@ class AdapterHelper implements OpReorderer.Callback { tmpCnt++; } else { // need to dispatch this separately - UpdateOp tmp = obtainUpdateOp(op.cmd, tmpStart, tmpCnt, op.payload); + UpdateOp tmp = obtainUpdateOp(op.cmd, tmpStart, tmpCnt); if (DEBUG) { Log.d(TAG, "need to dispatch separately " + tmp); } @@ -292,10 +285,9 @@ class AdapterHelper implements OpReorderer.Callback { tmpCnt = 1; } } - Object payload = op.payload; recycleUpdateOp(op); if (tmpCnt > 0) { - UpdateOp tmp = obtainUpdateOp(op.cmd, tmpStart, tmpCnt, payload); + UpdateOp tmp = obtainUpdateOp(op.cmd, tmpStart, tmpCnt); if (DEBUG) { Log.d(TAG, "dispatching:" + tmp); } @@ -319,7 +311,7 @@ class AdapterHelper implements OpReorderer.Callback { mCallback.offsetPositionsForRemovingInvisible(offsetStart, op.itemCount); break; case UpdateOp.UPDATE: - mCallback.markViewHoldersUpdated(offsetStart, op.itemCount, op.payload); + mCallback.markViewHoldersUpdated(offsetStart, op.itemCount); break; default: throw new IllegalArgumentException("only remove and update ops can be dispatched" @@ -437,7 +429,9 @@ class AdapterHelper implements OpReorderer.Callback { if (DEBUG) { Log.d(TAG, "postponing " + op); } +// Utils.log("add UpdateOp to PostponedList"); mPostponedList.add(op); +// Utils.log("op" + op.positionStart + "=" + op.itemCount); switch (op.cmd) { case UpdateOp.ADD: mCallback.offsetPositionsForAdd(op.positionStart, op.itemCount); @@ -450,7 +444,7 @@ class AdapterHelper implements OpReorderer.Callback { op.itemCount); break; case UpdateOp.UPDATE: - mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount, op.payload); + mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount); break; default: throw new IllegalArgumentException("Unknown update op type for " + op); @@ -461,10 +455,6 @@ class AdapterHelper implements OpReorderer.Callback { return mPendingUpdates.size() > 0; } - boolean hasAnyUpdateTypes(int updateTypes) { - return (mExistingUpdateTypes & updateTypes) != 0; - } - int findPositionOffset(int position) { return findPositionOffset(position, 0); } @@ -501,12 +491,8 @@ class AdapterHelper implements OpReorderer.Callback { /** * @return True if updates should be processed. */ - boolean onItemRangeChanged(int positionStart, int itemCount, Object payload) { - if (itemCount < 1) { - return false; - } - mPendingUpdates.add(obtainUpdateOp(UpdateOp.UPDATE, positionStart, itemCount, payload)); - mExistingUpdateTypes |= UpdateOp.UPDATE; + boolean onItemRangeChanged(int positionStart, int itemCount) { + mPendingUpdates.add(obtainUpdateOp(UpdateOp.UPDATE, positionStart, itemCount)); return mPendingUpdates.size() == 1; } @@ -514,11 +500,7 @@ class AdapterHelper implements OpReorderer.Callback { * @return True if updates should be processed. */ boolean onItemRangeInserted(int positionStart, int itemCount) { - if (itemCount < 1) { - return false; - } - mPendingUpdates.add(obtainUpdateOp(UpdateOp.ADD, positionStart, itemCount, null)); - mExistingUpdateTypes |= UpdateOp.ADD; + mPendingUpdates.add(obtainUpdateOp(UpdateOp.ADD, positionStart, itemCount)); return mPendingUpdates.size() == 1; } @@ -526,11 +508,7 @@ class AdapterHelper implements OpReorderer.Callback { * @return True if updates should be processed. */ boolean onItemRangeRemoved(int positionStart, int itemCount) { - if (itemCount < 1) { - return false; - } - mPendingUpdates.add(obtainUpdateOp(UpdateOp.REMOVE, positionStart, itemCount, null)); - mExistingUpdateTypes |= UpdateOp.REMOVE; + mPendingUpdates.add(obtainUpdateOp(UpdateOp.REMOVE, positionStart, itemCount)); return mPendingUpdates.size() == 1; } @@ -539,13 +517,12 @@ class AdapterHelper implements OpReorderer.Callback { */ boolean onItemRangeMoved(int from, int to, int itemCount) { if (from == to) { - return false; // no-op + return false;//no-op } if (itemCount != 1) { throw new IllegalArgumentException("Moving more than 1 item is not supported yet"); } - mPendingUpdates.add(obtainUpdateOp(UpdateOp.MOVE, from, to, null)); - mExistingUpdateTypes |= UpdateOp.MOVE; + mPendingUpdates.add(obtainUpdateOp(UpdateOp.MOVE, from, to)); return mPendingUpdates.size() == 1; } @@ -570,7 +547,7 @@ class AdapterHelper implements OpReorderer.Callback { break; case UpdateOp.UPDATE: mCallback.onDispatchSecondPass(op); - mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount, op.payload); + mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount); break; case UpdateOp.MOVE: mCallback.onDispatchSecondPass(op); @@ -582,47 +559,6 @@ class AdapterHelper implements OpReorderer.Callback { } } recycleUpdateOpsAndClearList(mPendingUpdates); - mExistingUpdateTypes = 0; - } - - public int applyPendingUpdatesToPosition(int position) { - final int size = mPendingUpdates.size(); - for (int i = 0; i < size; i ++) { - UpdateOp op = mPendingUpdates.get(i); - switch (op.cmd) { - case UpdateOp.ADD: - if (op.positionStart <= position) { - position += op.itemCount; - } - break; - case UpdateOp.REMOVE: - if (op.positionStart <= position) { - final int end = op.positionStart + op.itemCount; - if (end > position) { - return RecyclerView.NO_POSITION; - } - position -= op.itemCount; - } - break; - case UpdateOp.MOVE: - if (op.positionStart == position) { - position = op.itemCount;//position end - } else { - if (op.positionStart < position) { - position -= 1; - } - if (op.itemCount <= position) { - position += 1; - } - } - break; - } - } - return position; - } - - boolean hasUpdates() { - return !mPostponedList.isEmpty() && !mPendingUpdates.isEmpty(); } /** @@ -630,13 +566,13 @@ class AdapterHelper implements OpReorderer.Callback { */ static class UpdateOp { - static final int ADD = 1; + static final int ADD = 0; - static final int REMOVE = 1 << 1; + static final int REMOVE = 1; - static final int UPDATE = 1 << 2; + static final int UPDATE = 2; - static final int MOVE = 1 << 3; + static final int MOVE = 3; static final int POOL_SIZE = 30; @@ -644,16 +580,13 @@ class AdapterHelper implements OpReorderer.Callback { int positionStart; - Object payload; - // holds the target position if this is a MOVE int itemCount; - UpdateOp(int cmd, int positionStart, int itemCount, Object payload) { + UpdateOp(int cmd, int positionStart, int itemCount) { this.cmd = cmd; this.positionStart = positionStart; this.itemCount = itemCount; - this.payload = payload; } String cmdToString() { @@ -672,9 +605,7 @@ class AdapterHelper implements OpReorderer.Callback { @Override public String toString() { - return Integer.toHexString(System.identityHashCode(this)) - + "[" + cmdToString() + ",s:" + positionStart + "c:" + itemCount - +",p:"+payload + "]"; + return "[" + cmdToString() + ",s:" + positionStart + "c:" + itemCount + "]"; } @Override @@ -703,13 +634,6 @@ class AdapterHelper implements OpReorderer.Callback { if (positionStart != op.positionStart) { return false; } - if (payload != null) { - if (!payload.equals(op.payload)) { - return false; - } - } else if (op.payload != null) { - return false; - } return true; } @@ -724,15 +648,14 @@ class AdapterHelper implements OpReorderer.Callback { } @Override - public UpdateOp obtainUpdateOp(int cmd, int positionStart, int itemCount, Object payload) { + public UpdateOp obtainUpdateOp(int cmd, int positionStart, int itemCount) { UpdateOp op = mUpdateOpPool.acquire(); if (op == null) { - op = new UpdateOp(cmd, positionStart, itemCount, payload); + op = new UpdateOp(cmd, positionStart, itemCount); } else { op.cmd = cmd; op.positionStart = positionStart; op.itemCount = itemCount; - op.payload = payload; } return op; } @@ -740,7 +663,6 @@ class AdapterHelper implements OpReorderer.Callback { @Override public void recycleUpdateOp(UpdateOp op) { if (!mDisableRecycler) { - op.payload = null; mUpdateOpPool.release(op); } } @@ -764,7 +686,7 @@ class AdapterHelper implements OpReorderer.Callback { void offsetPositionsForRemovingLaidOutOrNewView(int positionStart, int itemCount); - void markViewHoldersUpdated(int positionStart, int itemCount, Object payloads); + void markViewHoldersUpdated(int positionStart, int itemCount); void onDispatchFirstPass(UpdateOp updateOp); diff --git a/app/src/main/java/android/support/v7/widget/ChildHelper.java b/app/src/main/java/android/support/v7/widget/ChildHelper.java index 0afa4056ff..e5556796f1 100644 --- a/app/src/main/java/android/support/v7/widget/ChildHelper.java +++ b/app/src/main/java/android/support/v7/widget/ChildHelper.java @@ -51,30 +51,6 @@ class ChildHelper { mHiddenViews = new ArrayList(); } - /** - * Marks a child view as hidden - * - * @param child View to hide. - */ - private void hideViewInternal(View child) { - mHiddenViews.add(child); - mCallback.onEnteredHiddenState(child); - } - - /** - * Unmarks a child view as hidden. - * - * @param child View to hide. - */ - private boolean unhideViewInternal(View child) { - if (mHiddenViews.remove(child)) { - mCallback.onLeftHiddenState(child); - return true; - } else { - return false; - } - } - /** * Adds a view to the ViewGroup * @@ -100,11 +76,11 @@ class ChildHelper { } else { offset = getOffset(index); } + mCallback.addView(child, offset); mBucket.insert(offset, hidden); if (hidden) { - hideViewInternal(child); + mHiddenViews.add(child); } - mCallback.addView(child, offset); if (DEBUG) { Log.d(TAG, "addViewAt " + index + ",h:" + hidden + ", " + this); } @@ -141,10 +117,10 @@ class ChildHelper { if (index < 0) { return; } - if (mBucket.remove(index)) { - unhideViewInternal(view); - } mCallback.removeViewAt(index); + if (mBucket.remove(index)) { + mHiddenViews.remove(view); + } if (DEBUG) { Log.d(TAG, "remove View off:" + index + "," + this); } @@ -162,10 +138,10 @@ class ChildHelper { if (view == null) { return; } - if (mBucket.remove(offset)) { - unhideViewInternal(view); - } mCallback.removeViewAt(offset); + if (mBucket.remove(offset)) { + mHiddenViews.remove(view); + } if (DEBUG) { Log.d(TAG, "removeViewAt " + index + ", off:" + offset + ", " + this); } @@ -185,12 +161,9 @@ class ChildHelper { * Removes all views from the ViewGroup including the hidden ones. */ void removeAllViewsUnfiltered() { - mBucket.reset(); - for (int i = mHiddenViews.size() - 1; i >= 0; i--) { - mCallback.onLeftHiddenState(mHiddenViews.get(i)); - mHiddenViews.remove(i); - } mCallback.removeAllViews(); + mBucket.reset(); + mHiddenViews.clear(); if (DEBUG) { Log.d(TAG, "removeAllViewsUnfiltered"); } @@ -208,8 +181,8 @@ class ChildHelper { for (int i = 0; i < count; i++) { final View view = mHiddenViews.get(i); RecyclerView.ViewHolder holder = mCallback.getChildViewHolder(view); - if (holder.getLayoutPosition() == position && !holder.isInvalid() && !holder.isRemoved() - && (type == RecyclerView.INVALID_TYPE || holder.getItemViewType() == type)) { + if (holder.getPosition() == position && !holder.isInvalid() && + (type == RecyclerView.INVALID_TYPE || holder.getItemViewType() == type)) { return view; } } @@ -232,11 +205,8 @@ class ChildHelper { } else { offset = getOffset(index); } - mBucket.insert(offset, hidden); - if (hidden) { - hideViewInternal(child); - } mCallback.attachViewToParent(child, offset, layoutParams); + mBucket.insert(offset, hidden); if (DEBUG) { Log.d(TAG, "attach view to parent index:" + index + ",off:" + offset + "," + "h:" + hidden + ", " + this); @@ -280,8 +250,8 @@ class ChildHelper { */ void detachViewFromParent(int index) { final int offset = getOffset(index); - mBucket.remove(offset); mCallback.detachViewFromParent(offset); + mBucket.remove(offset); if (DEBUG) { Log.d(TAG, "detach view from parent " + index + ", off:" + offset); } @@ -333,34 +303,15 @@ class ChildHelper { throw new RuntimeException("trying to hide same view twice, how come ? " + view); } mBucket.set(offset); - hideViewInternal(view); + mHiddenViews.add(view); if (DEBUG) { Log.d(TAG, "hiding child " + view + " at offset " + offset+ ", " + this); } } - /** - * Moves a child view from hidden list to regular list. - * Calling this method should probably be followed by a detach, otherwise, it will suddenly - * show up in LayoutManager's children list. - * - * @param view The hidden View to unhide - */ - void unhide(View view) { - final int offset = mCallback.indexOfChild(view); - if (offset < 0) { - throw new IllegalArgumentException("view is not a child, cannot hide " + view); - } - if (!mBucket.get(offset)) { - throw new RuntimeException("trying to unhide a view that was not hidden" + view); - } - mBucket.clear(offset); - unhideViewInternal(view); - } - @Override public String toString() { - return mBucket.toString() + ", hidden list:" + mHiddenViews.size(); + return mBucket.toString(); } /** @@ -372,18 +323,18 @@ class ChildHelper { boolean removeViewIfHidden(View view) { final int index = mCallback.indexOfChild(view); if (index == -1) { - if (unhideViewInternal(view) && DEBUG) { + if (mHiddenViews.remove(view) && DEBUG) { throw new IllegalStateException("view is in hidden list but not in view group"); } return true; } if (mBucket.get(index)) { mBucket.remove(index); - if (!unhideViewInternal(view) && DEBUG) { + mCallback.removeViewAt(index); + if (!mHiddenViews.remove(view) && DEBUG) { throw new IllegalStateException( "removed a hidden view but it is not in hidden views list"); } - mCallback.removeViewAt(index); return true; } return false; @@ -529,9 +480,5 @@ class ChildHelper { void attachViewToParent(View child, int index, ViewGroup.LayoutParams layoutParams); void detachViewFromParent(int offset); - - void onEnteredHiddenState(View child); - - void onLeftHiddenState(View child); } } diff --git a/app/src/main/java/android/support/v7/widget/DefaultItemAnimator.java b/app/src/main/java/android/support/v7/widget/DefaultItemAnimator.java index d64621ade9..2a27d65ab8 100644 --- a/app/src/main/java/android/support/v7/widget/DefaultItemAnimator.java +++ b/app/src/main/java/android/support/v7/widget/DefaultItemAnimator.java @@ -15,8 +15,6 @@ */ package android.support.v7.widget; -import android.support.annotation.NonNull; -import android.support.v4.animation.AnimatorCompatHelper; import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewPropertyAnimatorCompat; import android.support.v4.view.ViewPropertyAnimatorListener; @@ -33,22 +31,23 @@ import java.util.List; * * @see RecyclerView#setItemAnimator(RecyclerView.ItemAnimator) */ -public class DefaultItemAnimator extends SimpleItemAnimator { +public class DefaultItemAnimator extends RecyclerView.ItemAnimator { private static final boolean DEBUG = false; - private ArrayList mPendingRemovals = new ArrayList<>(); - private ArrayList mPendingAdditions = new ArrayList<>(); - private ArrayList mPendingMoves = new ArrayList<>(); - private ArrayList mPendingChanges = new ArrayList<>(); + private ArrayList mPendingRemovals = new ArrayList(); + private ArrayList mPendingAdditions = new ArrayList(); + private ArrayList mPendingMoves = new ArrayList(); + private ArrayList mPendingChanges = new ArrayList(); - private ArrayList> mAdditionsList = new ArrayList<>(); - private ArrayList> mMovesList = new ArrayList<>(); - private ArrayList> mChangesList = new ArrayList<>(); + private ArrayList> mAdditionsList = + new ArrayList>(); + private ArrayList> mMovesList = new ArrayList>(); + private ArrayList> mChangesList = new ArrayList>(); - private ArrayList mAddAnimations = new ArrayList<>(); - private ArrayList mMoveAnimations = new ArrayList<>(); - private ArrayList mRemoveAnimations = new ArrayList<>(); - private ArrayList mChangeAnimations = new ArrayList<>(); + private ArrayList mAddAnimations = new ArrayList(); + private ArrayList mMoveAnimations = new ArrayList(); + private ArrayList mRemoveAnimations = new ArrayList(); + private ArrayList mChangeAnimations = new ArrayList(); private static class MoveInfo { public ViewHolder holder; @@ -110,7 +109,7 @@ public class DefaultItemAnimator extends SimpleItemAnimator { mPendingRemovals.clear(); // Next, move stuff if (movesPending) { - final ArrayList moves = new ArrayList<>(); + final ArrayList moves = new ArrayList(); moves.addAll(mPendingMoves); mMovesList.add(moves); mPendingMoves.clear(); @@ -134,7 +133,7 @@ public class DefaultItemAnimator extends SimpleItemAnimator { } // Next, change stuff, to run in parallel with move animations if (changesPending) { - final ArrayList changes = new ArrayList<>(); + final ArrayList changes = new ArrayList(); changes.addAll(mPendingChanges); mChangesList.add(changes); mPendingChanges.clear(); @@ -157,12 +156,11 @@ public class DefaultItemAnimator extends SimpleItemAnimator { } // Next, add stuff if (additionsPending) { - final ArrayList additions = new ArrayList<>(); + final ArrayList additions = new ArrayList(); additions.addAll(mPendingAdditions); mAdditionsList.add(additions); mPendingAdditions.clear(); Runnable adder = new Runnable() { - @Override public void run() { for (ViewHolder holder : additions) { animateAddImpl(holder); @@ -186,7 +184,7 @@ public class DefaultItemAnimator extends SimpleItemAnimator { @Override public boolean animateRemove(final ViewHolder holder) { - resetAnimation(holder); + endAnimation(holder); mPendingRemovals.add(holder); return true; } @@ -194,14 +192,12 @@ public class DefaultItemAnimator extends SimpleItemAnimator { private void animateRemoveImpl(final ViewHolder holder) { final View view = holder.itemView; final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view); - mRemoveAnimations.add(holder); animation.setDuration(getRemoveDuration()) .alpha(0).setListener(new VpaListenerAdapter() { @Override public void onAnimationStart(View view) { dispatchRemoveStarting(holder); } - @Override public void onAnimationEnd(View view) { animation.setListener(null); @@ -211,11 +207,12 @@ public class DefaultItemAnimator extends SimpleItemAnimator { dispatchFinishedWhenDone(); } }).start(); + mRemoveAnimations.add(holder); } @Override public boolean animateAdd(final ViewHolder holder) { - resetAnimation(holder); + endAnimation(holder); ViewCompat.setAlpha(holder.itemView, 0); mPendingAdditions.add(holder); return true; @@ -223,8 +220,8 @@ public class DefaultItemAnimator extends SimpleItemAnimator { private void animateAddImpl(final ViewHolder holder) { final View view = holder.itemView; - final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view); mAddAnimations.add(holder); + final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view); animation.alpha(1).setDuration(getAddDuration()). setListener(new VpaListenerAdapter() { @Override @@ -252,7 +249,7 @@ public class DefaultItemAnimator extends SimpleItemAnimator { final View view = holder.itemView; fromX += ViewCompat.getTranslationX(holder.itemView); fromY += ViewCompat.getTranslationY(holder.itemView); - resetAnimation(holder); + endAnimation(holder); int deltaX = toX - fromX; int deltaY = toY - fromY; if (deltaX == 0 && deltaY == 0) { @@ -282,8 +279,8 @@ public class DefaultItemAnimator extends SimpleItemAnimator { // TODO: make EndActions end listeners instead, since end actions aren't called when // vpas are canceled (and can't end them. why?) // need listener functionality in VPACompat for this. Ick. - final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view); mMoveAnimations.add(holder); + final ViewPropertyAnimatorCompat animation = ViewCompat.animate(view); animation.setDuration(getMoveDuration()).setListener(new VpaListenerAdapter() { @Override public void onAnimationStart(View view) { @@ -311,24 +308,19 @@ public class DefaultItemAnimator extends SimpleItemAnimator { @Override public boolean animateChange(ViewHolder oldHolder, ViewHolder newHolder, int fromX, int fromY, int toX, int toY) { - if (oldHolder == newHolder) { - // Don't know how to run change animations when the same view holder is re-used. - // run a move animation to handle position changes. - return animateMove(oldHolder, fromX, fromY, toX, toY); - } final float prevTranslationX = ViewCompat.getTranslationX(oldHolder.itemView); final float prevTranslationY = ViewCompat.getTranslationY(oldHolder.itemView); final float prevAlpha = ViewCompat.getAlpha(oldHolder.itemView); - resetAnimation(oldHolder); + endAnimation(oldHolder); int deltaX = (int) (toX - fromX - prevTranslationX); int deltaY = (int) (toY - fromY - prevTranslationY); // recover prev translation state after ending animation ViewCompat.setTranslationX(oldHolder.itemView, prevTranslationX); ViewCompat.setTranslationY(oldHolder.itemView, prevTranslationY); ViewCompat.setAlpha(oldHolder.itemView, prevAlpha); - if (newHolder != null) { + if (newHolder != null && newHolder.itemView != null) { // carry over translation values - resetAnimation(newHolder); + endAnimation(newHolder); ViewCompat.setTranslationX(newHolder.itemView, -deltaX); ViewCompat.setTranslationY(newHolder.itemView, -deltaY); ViewCompat.setAlpha(newHolder.itemView, 0); @@ -339,36 +331,34 @@ public class DefaultItemAnimator extends SimpleItemAnimator { private void animateChangeImpl(final ChangeInfo changeInfo) { final ViewHolder holder = changeInfo.oldHolder; - final View view = holder == null ? null : holder.itemView; + final View view = holder.itemView; final ViewHolder newHolder = changeInfo.newHolder; final View newView = newHolder != null ? newHolder.itemView : null; - if (view != null) { - final ViewPropertyAnimatorCompat oldViewAnim = ViewCompat.animate(view).setDuration( - getChangeDuration()); - mChangeAnimations.add(changeInfo.oldHolder); - oldViewAnim.translationX(changeInfo.toX - changeInfo.fromX); - oldViewAnim.translationY(changeInfo.toY - changeInfo.fromY); - oldViewAnim.alpha(0).setListener(new VpaListenerAdapter() { - @Override - public void onAnimationStart(View view) { - dispatchChangeStarting(changeInfo.oldHolder, true); - } + mChangeAnimations.add(changeInfo.oldHolder); - @Override - public void onAnimationEnd(View view) { - oldViewAnim.setListener(null); - ViewCompat.setAlpha(view, 1); - ViewCompat.setTranslationX(view, 0); - ViewCompat.setTranslationY(view, 0); - dispatchChangeFinished(changeInfo.oldHolder, true); - mChangeAnimations.remove(changeInfo.oldHolder); - dispatchFinishedWhenDone(); - } - }).start(); - } + final ViewPropertyAnimatorCompat oldViewAnim = ViewCompat.animate(view).setDuration( + getChangeDuration()); + oldViewAnim.translationX(changeInfo.toX - changeInfo.fromX); + oldViewAnim.translationY(changeInfo.toY - changeInfo.fromY); + oldViewAnim.alpha(0).setListener(new VpaListenerAdapter() { + @Override + public void onAnimationStart(View view) { + dispatchChangeStarting(changeInfo.oldHolder, true); + } + @Override + public void onAnimationEnd(View view) { + oldViewAnim.setListener(null); + ViewCompat.setAlpha(view, 1); + ViewCompat.setTranslationX(view, 0); + ViewCompat.setTranslationY(view, 0); + dispatchChangeFinished(changeInfo.oldHolder, true); + mChangeAnimations.remove(changeInfo.oldHolder); + dispatchFinishedWhenDone(); + } + }).start(); if (newView != null) { - final ViewPropertyAnimatorCompat newViewAnimation = ViewCompat.animate(newView); mChangeAnimations.add(changeInfo.newHolder); + final ViewPropertyAnimatorCompat newViewAnimation = ViewCompat.animate(newView); newViewAnimation.translationX(0).translationY(0).setDuration(getChangeDuration()). alpha(1).setListener(new VpaListenerAdapter() { @Override @@ -437,7 +427,7 @@ public class DefaultItemAnimator extends SimpleItemAnimator { ViewCompat.setTranslationY(view, 0); ViewCompat.setTranslationX(view, 0); dispatchMoveFinished(item); - mPendingMoves.remove(i); + mPendingMoves.remove(item); } } endChangeAnimation(mPendingChanges, item); @@ -454,7 +444,7 @@ public class DefaultItemAnimator extends SimpleItemAnimator { ArrayList changes = mChangesList.get(i); endChangeAnimation(changes, item); if (changes.isEmpty()) { - mChangesList.remove(i); + mChangesList.remove(changes); } } for (int i = mMovesList.size() - 1; i >= 0; i--) { @@ -467,7 +457,7 @@ public class DefaultItemAnimator extends SimpleItemAnimator { dispatchMoveFinished(item); moves.remove(j); if (moves.isEmpty()) { - mMovesList.remove(i); + mMovesList.remove(moves); } break; } @@ -479,31 +469,27 @@ public class DefaultItemAnimator extends SimpleItemAnimator { ViewCompat.setAlpha(view, 1); dispatchAddFinished(item); if (additions.isEmpty()) { - mAdditionsList.remove(i); + mAdditionsList.remove(additions); } } } // animations should be ended by the cancel above. - //noinspection PointlessBooleanExpression,ConstantConditions if (mRemoveAnimations.remove(item) && DEBUG) { throw new IllegalStateException("after animation is cancelled, item should not be in " + "mRemoveAnimations list"); } - //noinspection PointlessBooleanExpression,ConstantConditions if (mAddAnimations.remove(item) && DEBUG) { throw new IllegalStateException("after animation is cancelled, item should not be in " + "mAddAnimations list"); } - //noinspection PointlessBooleanExpression,ConstantConditions if (mChangeAnimations.remove(item) && DEBUG) { throw new IllegalStateException("after animation is cancelled, item should not be in " + "mChangeAnimations list"); } - //noinspection PointlessBooleanExpression,ConstantConditions if (mMoveAnimations.remove(item) && DEBUG) { throw new IllegalStateException("after animation is cancelled, item should not be in " + "mMoveAnimations list"); @@ -511,11 +497,6 @@ public class DefaultItemAnimator extends SimpleItemAnimator { dispatchFinishedWhenDone(); } - private void resetAnimation(ViewHolder holder) { - AnimatorCompatHelper.clearInterpolator(holder.itemView); - endAnimation(holder); - } - @Override public boolean isRunning() { return (!mPendingAdditions.isEmpty() || @@ -634,28 +615,6 @@ public class DefaultItemAnimator extends SimpleItemAnimator { } } - /** - * {@inheritDoc} - *

      - * If the payload list is not empty, DefaultItemAnimator returns true. - * When this is the case: - *

        - *
      • If you override {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}, both - * ViewHolder arguments will be the same instance. - *
      • - *
      • - * If you are not overriding {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}, - * then DefaultItemAnimator will call {@link #animateMove(ViewHolder, int, int, int, int)} and - * run a move animation instead. - *
      • - *
      - */ - @Override - public boolean canReuseUpdatedViewHolder(@NonNull ViewHolder viewHolder, - @NonNull List payloads) { - return !payloads.isEmpty() || super.canReuseUpdatedViewHolder(viewHolder, payloads); - } - private static class VpaListenerAdapter implements ViewPropertyAnimatorListener { @Override public void onAnimationStart(View view) {} @@ -665,5 +624,5 @@ public class DefaultItemAnimator extends SimpleItemAnimator { @Override public void onAnimationCancel(View view) {} - } + }; } diff --git a/app/src/main/java/android/support/v7/widget/GridLayoutManager.java b/app/src/main/java/android/support/v7/widget/GridLayoutManager.java index 4d19163614..a05ac473e4 100644 --- a/app/src/main/java/android/support/v7/widget/GridLayoutManager.java +++ b/app/src/main/java/android/support/v7/widget/GridLayoutManager.java @@ -10,7 +10,7 @@ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and + * See the License for the specific languag`e governing permissions and * limitations under the License. */ package android.support.v7.widget; @@ -38,16 +38,16 @@ public class GridLayoutManager extends LinearLayoutManager { private static final String TAG = "GridLayoutManager"; public static final int DEFAULT_SPAN_COUNT = -1; /** - * Span size have been changed but we've not done a new layout calculation. + * The measure spec for the scroll direction. */ - boolean mPendingSpanCountChange = false; + static final int MAIN_DIR_SPEC = + View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); + int mSpanCount = DEFAULT_SPAN_COUNT; /** - * Right borders for each span. - *

      For i-th item start is {@link #mCachedBorders}[i-1] + 1 - * and end is {@link #mCachedBorders}[i]. + * The size of each span */ - int [] mCachedBorders; + int mSizePerSpan; /** * Temporary array to keep views in layoutChunk method */ @@ -58,21 +58,6 @@ public class GridLayoutManager extends LinearLayoutManager { // re-used variable to acquire decor insets from RecyclerView final Rect mDecorInsets = new Rect(); - - /** - * Constructor used when layout manager is set in XML by RecyclerView attribute - * "layoutManager". If spanCount is not specified in the XML, it defaults to a - * single column. - * - * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_spanCount - */ - public GridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, - int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - Properties properties = getProperties(context, attrs, defStyleAttr, defStyleRes); - setSpanCount(properties.spanCount); - } - /** * Creates a vertical GridLayoutManager * @@ -120,9 +105,7 @@ public class GridLayoutManager extends LinearLayoutManager { if (state.getItemCount() < 1) { return 0; } - - // Row count is one more than the last item's row index. - return getSpanGroupIndex(recycler, state, state.getItemCount() - 1) + 1; + return getSpanGroupIndex(recycler, state, state.getItemCount() - 1); } @Override @@ -134,9 +117,7 @@ public class GridLayoutManager extends LinearLayoutManager { if (state.getItemCount() < 1) { return 0; } - - // Column count is one more than the last item's column index. - return getSpanGroupIndex(recycler, state, state.getItemCount() - 1) + 1; + return getSpanGroupIndex(recycler, state, state.getItemCount() - 1); } @Override @@ -148,7 +129,7 @@ public class GridLayoutManager extends LinearLayoutManager { return; } LayoutParams glp = (LayoutParams) lp; - int spanGroupIndex = getSpanGroupIndex(recycler, state, glp.getViewLayoutPosition()); + int spanGroupIndex = getSpanGroupIndex(recycler, state, glp.getViewPosition()); if (mOrientation == HORIZONTAL) { info.setCollectionItemInfo(AccessibilityNodeInfoCompat.CollectionItemInfoCompat.obtain( glp.getSpanIndex(), glp.getSpanSize(), @@ -174,12 +155,6 @@ public class GridLayoutManager extends LinearLayoutManager { clearPreLayoutSpanMappingCache(); } - @Override - public void onLayoutCompleted(RecyclerView.State state) { - super.onLayoutCompleted(state); - mPendingSpanCountChange = false; - } - private void clearPreLayoutSpanMappingCache() { mPreLayoutSpanSizeCache.clear(); mPreLayoutSpanIndexCache.clear(); @@ -189,7 +164,7 @@ public class GridLayoutManager extends LinearLayoutManager { final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { final LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams(); - final int viewPosition = lp.getViewLayoutPosition(); + final int viewPosition = lp.getViewPosition(); mPreLayoutSpanSizeCache.put(viewPosition, lp.getSpanSize()); mPreLayoutSpanIndexCache.put(viewPosition, lp.getSpanIndex()); } @@ -211,8 +186,7 @@ public class GridLayoutManager extends LinearLayoutManager { } @Override - public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount, - Object payload) { + public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount) { mSpanSizeLookup.invalidateSpanIndexCache(); } @@ -223,13 +197,8 @@ public class GridLayoutManager extends LinearLayoutManager { @Override public RecyclerView.LayoutParams generateDefaultLayoutParams() { - if (mOrientation == HORIZONTAL) { - return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, - ViewGroup.LayoutParams.MATCH_PARENT); - } else { - return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT); - } + return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT); } @Override @@ -277,174 +246,29 @@ public class GridLayoutManager extends LinearLayoutManager { } else { totalSpace = getHeight() - getPaddingBottom() - getPaddingTop(); } - calculateItemBorders(totalSpace); + mSizePerSpan = totalSpace / mSpanCount; } @Override - public void setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec) { - if (mCachedBorders == null) { - super.setMeasuredDimension(childrenBounds, wSpec, hSpec); - } - final int width, height; - final int horizontalPadding = getPaddingLeft() + getPaddingRight(); - final int verticalPadding = getPaddingTop() + getPaddingBottom(); - if (mOrientation == VERTICAL) { - final int usedHeight = childrenBounds.height() + verticalPadding; - height = chooseSize(hSpec, usedHeight, getMinimumHeight()); - width = chooseSize(wSpec, mCachedBorders[mCachedBorders.length - 1] + horizontalPadding, - getMinimumWidth()); - } else { - final int usedWidth = childrenBounds.width() + horizontalPadding; - width = chooseSize(wSpec, usedWidth, getMinimumWidth()); - height = chooseSize(hSpec, mCachedBorders[mCachedBorders.length - 1] + verticalPadding, - getMinimumHeight()); - } - setMeasuredDimension(width, height); - } - - /** - * @param totalSpace Total available space after padding is removed - */ - private void calculateItemBorders(int totalSpace) { - mCachedBorders = calculateItemBorders(mCachedBorders, mSpanCount, totalSpace); - } - - /** - * @param cachedBorders The out array - * @param spanCount number of spans - * @param totalSpace total available space after padding is removed - * @return The updated array. Might be the same instance as the provided array if its size - * has not changed. - */ - static int[] calculateItemBorders(int[] cachedBorders, int spanCount, int totalSpace) { - if (cachedBorders == null || cachedBorders.length != spanCount + 1 - || cachedBorders[cachedBorders.length - 1] != totalSpace) { - cachedBorders = new int[spanCount + 1]; - } - cachedBorders[0] = 0; - int sizePerSpan = totalSpace / spanCount; - int sizePerSpanRemainder = totalSpace % spanCount; - int consumedPixels = 0; - int additionalSize = 0; - for (int i = 1; i <= spanCount; i++) { - int itemSize = sizePerSpan; - additionalSize += sizePerSpanRemainder; - if (additionalSize > 0 && (spanCount - additionalSize) < sizePerSpanRemainder) { - itemSize += 1; - additionalSize -= spanCount; - } - consumedPixels += itemSize; - cachedBorders[i] = consumedPixels; - } - return cachedBorders; - } - - int getSpaceForSpanRange(int startSpan, int spanSize) { - if (mOrientation == VERTICAL && isLayoutRTL()) { - return mCachedBorders[mSpanCount - startSpan] - - mCachedBorders[mSpanCount - startSpan - spanSize]; - } else { - return mCachedBorders[startSpan + spanSize] - mCachedBorders[startSpan]; - } - } - - @Override - void onAnchorReady(RecyclerView.Recycler recycler, RecyclerView.State state, - AnchorInfo anchorInfo, int itemDirection) { - super.onAnchorReady(recycler, state, anchorInfo, itemDirection); + void onAnchorReady(RecyclerView.State state, AnchorInfo anchorInfo) { + super.onAnchorReady(state, anchorInfo); updateMeasurements(); if (state.getItemCount() > 0 && !state.isPreLayout()) { - ensureAnchorIsInCorrectSpan(recycler, state, anchorInfo, itemDirection); + ensureAnchorIsInFirstSpan(anchorInfo); } - ensureViewSet(); - } - - private void ensureViewSet() { if (mSet == null || mSet.length != mSpanCount) { mSet = new View[mSpanCount]; } } - @Override - public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, - RecyclerView.State state) { - updateMeasurements(); - ensureViewSet(); - return super.scrollHorizontallyBy(dx, recycler, state); - } - - @Override - public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, - RecyclerView.State state) { - updateMeasurements(); - ensureViewSet(); - return super.scrollVerticallyBy(dy, recycler, state); - } - - private void ensureAnchorIsInCorrectSpan(RecyclerView.Recycler recycler, - RecyclerView.State state, AnchorInfo anchorInfo, int itemDirection) { - final boolean layingOutInPrimaryDirection = - itemDirection == LayoutState.ITEM_DIRECTION_TAIL; - int span = getSpanIndex(recycler, state, anchorInfo.mPosition); - if (layingOutInPrimaryDirection) { - // choose span 0 - while (span > 0 && anchorInfo.mPosition > 0) { - anchorInfo.mPosition--; - span = getSpanIndex(recycler, state, anchorInfo.mPosition); - } - } else { - // choose the max span we can get. hopefully last one - final int indexLimit = state.getItemCount() - 1; - int pos = anchorInfo.mPosition; - int bestSpan = span; - while (pos < indexLimit) { - int next = getSpanIndex(recycler, state, pos + 1); - if (next > bestSpan) { - pos += 1; - bestSpan = next; - } else { - break; - } - } - anchorInfo.mPosition = pos; + private void ensureAnchorIsInFirstSpan(AnchorInfo anchorInfo) { + int span = mSpanSizeLookup.getCachedSpanIndex(anchorInfo.mPosition, mSpanCount); + while (span > 0 && anchorInfo.mPosition > 0) { + anchorInfo.mPosition--; + span = mSpanSizeLookup.getCachedSpanIndex(anchorInfo.mPosition, mSpanCount); } } - @Override - View findReferenceChild(RecyclerView.Recycler recycler, RecyclerView.State state, - int start, int end, int itemCount) { - ensureLayoutState(); - View invalidMatch = null; - View outOfBoundsMatch = null; - final int boundsStart = mOrientationHelper.getStartAfterPadding(); - final int boundsEnd = mOrientationHelper.getEndAfterPadding(); - final int diff = end > start ? 1 : -1; - - for (int i = start; i != end; i += diff) { - final View view = getChildAt(i); - final int position = getPosition(view); - if (position >= 0 && position < itemCount) { - final int span = getSpanIndex(recycler, state, position); - if (span != 0) { - continue; - } - if (((RecyclerView.LayoutParams) view.getLayoutParams()).isItemRemoved()) { - if (invalidMatch == null) { - invalidMatch = view; // removed item, least preferred - } - } else if (mOrientationHelper.getDecoratedStart(view) >= boundsEnd || - mOrientationHelper.getDecoratedEnd(view) < boundsStart) { - if (outOfBoundsMatch == null) { - outOfBoundsMatch = view; // item is not visible, less preferred - } - } else { - return view; - } - } - } - return outOfBoundsMatch != null ? outOfBoundsMatch : invalidMatch; - } - private int getSpanGroupIndex(RecyclerView.Recycler recycler, RecyclerView.State state, int viewPosition) { if (!state.isPreLayout()) { @@ -507,15 +331,6 @@ public class GridLayoutManager extends LinearLayoutManager { @Override void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State state, LayoutState layoutState, LayoutChunkResult result) { - final int otherDirSpecMode = mOrientationHelper.getModeInOther(); - final boolean flexibleInOtherDir = otherDirSpecMode != View.MeasureSpec.EXACTLY; - final int currentOtherDirSize = getChildCount() > 0 ? mCachedBorders[mSpanCount] : 0; - // if grid layout's dimensions are not specified, let the new row change the measurements - // This is not perfect since we not covering all rows but still solves an important case - // where they may have a header row which should be laid out according to children. - if (flexibleInOtherDir) { - updateMeasurements(); // reset measurements - } final boolean layingOutInPrimaryDirection = layoutState.mItemDirection == LayoutState.ITEM_DIRECTION_TAIL; int count = 0; @@ -553,7 +368,6 @@ public class GridLayoutManager extends LinearLayoutManager { } int maxSize = 0; - float maxSizeInOther = 0; // use a float to get size per span // we should assign spans before item decor offsets are calculated assignSpans(recycler, state, count, consumedSpanCount, layingOutInPrimaryDirection); @@ -572,61 +386,35 @@ public class GridLayoutManager extends LinearLayoutManager { addDisappearingView(view, 0); } } - calculateItemDecorationsForChild(view, mDecorInsets); - measureChild(view, otherDirSpecMode, false); + int spanSize = getSpanSize(recycler, state, getPosition(view)); + final int spec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan * spanSize, + View.MeasureSpec.EXACTLY); + final LayoutParams lp = (LayoutParams) view.getLayoutParams(); + if (mOrientation == VERTICAL) { + measureChildWithDecorationsAndMargin(view, spec, getMainDirSpec(lp.height)); + } else { + measureChildWithDecorationsAndMargin(view, getMainDirSpec(lp.width), spec); + } final int size = mOrientationHelper.getDecoratedMeasurement(view); if (size > maxSize) { maxSize = size; } - final LayoutParams lp = (LayoutParams) view.getLayoutParams(); - final float otherSize = 1f * mOrientationHelper.getDecoratedMeasurementInOther(view) / - lp.mSpanSize; - if (otherSize > maxSizeInOther) { - maxSizeInOther = otherSize; - } - } - if (flexibleInOtherDir) { - // re-distribute columns - guessMeasurement(maxSizeInOther, currentOtherDirSize); - // now we should re-measure any item that was match parent. - maxSize = 0; - for (int i = 0; i < count; i++) { - View view = mSet[i]; - measureChild(view, View.MeasureSpec.EXACTLY, true); - final int size = mOrientationHelper.getDecoratedMeasurement(view); - if (size > maxSize) { - maxSize = size; - } - } } - // Views that did not measure the maxSize has to be re-measured - // We will stop doing this once we introduce Gravity in the GLM layout params + // views that did not measure the maxSize has to be re-measured + final int maxMeasureSpec = getMainDirSpec(maxSize); for (int i = 0; i < count; i ++) { final View view = mSet[i]; if (mOrientationHelper.getDecoratedMeasurement(view) != maxSize) { - final LayoutParams lp = (LayoutParams) view.getLayoutParams(); - final Rect decorInsets = lp.mDecorInsets; - final int verticalInsets = decorInsets.top + decorInsets.bottom - + lp.topMargin + lp.bottomMargin; - final int horizontalInsets = decorInsets.left + decorInsets.right - + lp.leftMargin + lp.rightMargin; - final int totalSpaceInOther = getSpaceForSpanRange(lp.mSpanIndex, lp.mSpanSize); - final int wSpec; - final int hSpec; + int spanSize = getSpanSize(recycler, state, getPosition(view)); + final int spec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan * spanSize, + View.MeasureSpec.EXACTLY); if (mOrientation == VERTICAL) { - wSpec = getChildMeasureSpec(totalSpaceInOther, View.MeasureSpec.EXACTLY, - horizontalInsets, lp.width, false); - hSpec = View.MeasureSpec.makeMeasureSpec(maxSize - verticalInsets, - View.MeasureSpec.EXACTLY); + measureChildWithDecorationsAndMargin(view, spec, maxMeasureSpec); } else { - wSpec = View.MeasureSpec.makeMeasureSpec(maxSize - horizontalInsets, - View.MeasureSpec.EXACTLY); - hSpec = getChildMeasureSpec(totalSpaceInOther, View.MeasureSpec.EXACTLY, - verticalInsets, lp.height, false); + measureChildWithDecorationsAndMargin(view, maxMeasureSpec, spec); } - measureChildWithDecorationsAndMargin(view, wSpec, hSpec, true); } } @@ -654,20 +442,16 @@ public class GridLayoutManager extends LinearLayoutManager { View view = mSet[i]; LayoutParams params = (LayoutParams) view.getLayoutParams(); if (mOrientation == VERTICAL) { - if (isLayoutRTL()) { - right = getPaddingLeft() + mCachedBorders[mSpanCount - params.mSpanIndex]; - left = right - mOrientationHelper.getDecoratedMeasurementInOther(view); - } else { - left = getPaddingLeft() + mCachedBorders[params.mSpanIndex]; - right = left + mOrientationHelper.getDecoratedMeasurementInOther(view); - } + left = getPaddingLeft() + mSizePerSpan * params.mSpanIndex; + right = left + mOrientationHelper.getDecoratedMeasurementInOther(view); } else { - top = getPaddingTop() + mCachedBorders[params.mSpanIndex]; + top = getPaddingTop() + mSizePerSpan * params.mSpanIndex; bottom = top + mOrientationHelper.getDecoratedMeasurementInOther(view); } // We calculate everything with View's bounding box (which includes decor and margins) // To calculate correct layout position, we subtract margins. - layoutDecoratedWithMargins(view, left, top, right, bottom); + layoutDecorated(view, left + params.leftMargin, top + params.topMargin, + right - params.rightMargin, bottom - params.bottomMargin); if (DEBUG) { Log.d(TAG, "laid out child at position " + getPosition(view) + ", with l:" + (left + params.leftMargin) + ", t:" + (top + params.topMargin) + ", r:" @@ -683,74 +467,39 @@ public class GridLayoutManager extends LinearLayoutManager { Arrays.fill(mSet, null); } - /** - * Measures a child with currently known information. This is not necessarily the child's final - * measurement. (see fillChunk for details). - * - * @param view The child view to be measured - * @param otherDirParentSpecMode The RV measure spec that should be used in the secondary - * orientation - * @param alreadyMeasured True if we've already measured this view once - */ - private void measureChild(View view, int otherDirParentSpecMode, boolean alreadyMeasured) { - final LayoutParams lp = (LayoutParams) view.getLayoutParams(); - final Rect decorInsets = lp.mDecorInsets; - final int verticalInsets = decorInsets.top + decorInsets.bottom - + lp.topMargin + lp.bottomMargin; - final int horizontalInsets = decorInsets.left + decorInsets.right - + lp.leftMargin + lp.rightMargin; - final int availableSpaceInOther = getSpaceForSpanRange(lp.mSpanIndex, lp.mSpanSize); - final int wSpec; - final int hSpec; - if (mOrientation == VERTICAL) { - wSpec = getChildMeasureSpec(availableSpaceInOther, otherDirParentSpecMode, - horizontalInsets, lp.width, false); - hSpec = getChildMeasureSpec(mOrientationHelper.getTotalSpace(), getHeightMode(), - verticalInsets, lp.height, true); + private int getMainDirSpec(int dim) { + if (dim < 0) { + return MAIN_DIR_SPEC; } else { - hSpec = getChildMeasureSpec(availableSpaceInOther, otherDirParentSpecMode, - verticalInsets, lp.height, false); - wSpec = getChildMeasureSpec(mOrientationHelper.getTotalSpace(), getWidthMode(), - horizontalInsets, lp.width, true); + return View.MeasureSpec.makeMeasureSpec(dim, View.MeasureSpec.EXACTLY); } - measureChildWithDecorationsAndMargin(view, wSpec, hSpec, alreadyMeasured); } - /** - * This is called after laying out a row (if vertical) or a column (if horizontal) when the - * RecyclerView does not have exact measurement specs. - *

      - * Here we try to assign a best guess width or height and re-do the layout to update other - * views that wanted to MATCH_PARENT in the non-scroll orientation. - * - * @param maxSizeInOther The maximum size per span ratio from the measurement of the children. - * @param currentOtherDirSize The size before this layout chunk. There is no reason to go below. - */ - private void guessMeasurement(float maxSizeInOther, int currentOtherDirSize) { - final int contentSize = Math.round(maxSizeInOther * mSpanCount); - // always re-calculate because borders were stretched during the fill - calculateItemBorders(Math.max(contentSize, currentOtherDirSize)); - } - - private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int heightSpec, - boolean alreadyMeasured) { + private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int heightSpec) { + calculateItemDecorationsForChild(child, mDecorInsets); RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams(); - final boolean measure; - if (alreadyMeasured) { - measure = shouldReMeasureChild(child, widthSpec, heightSpec, lp); - } else { - measure = shouldMeasureChild(child, widthSpec, heightSpec, lp); + widthSpec = updateSpecWithExtra(widthSpec, lp.leftMargin + mDecorInsets.left, + lp.rightMargin + mDecorInsets.right); + heightSpec = updateSpecWithExtra(heightSpec, lp.topMargin + mDecorInsets.top, + lp.bottomMargin + mDecorInsets.bottom); + child.measure(widthSpec, heightSpec); + } + + private int updateSpecWithExtra(int spec, int startInset, int endInset) { + if (startInset == 0 && endInset == 0) { + return spec; } - if (measure) { - child.measure(widthSpec, heightSpec); + final int mode = View.MeasureSpec.getMode(spec); + if (mode == View.MeasureSpec.AT_MOST || mode == View.MeasureSpec.EXACTLY) { + return View.MeasureSpec.makeMeasureSpec( + View.MeasureSpec.getSize(spec) - startInset - endInset, mode); } + return spec; } private void assignSpans(RecyclerView.Recycler recycler, RecyclerView.State state, int count, int consumedSpanCount, boolean layingOutInPrimaryDirection) { - // spans are always assigned from 0 to N no matter if it is RTL or not. - // RTL is used only when positioning the view. - int span, start, end, diff; + int span, spanDiff, start, end, diff; // make sure we traverse from min position to max position if (layingOutInPrimaryDirection) { start = 0; @@ -761,13 +510,23 @@ public class GridLayoutManager extends LinearLayoutManager { end = -1; diff = -1; } - span = 0; + if (mOrientation == VERTICAL && isLayoutRTL()) { // start from last span + span = consumedSpanCount - 1; + spanDiff = -1; + } else { + span = 0; + spanDiff = 1; + } for (int i = start; i != end; i += diff) { View view = mSet[i]; LayoutParams params = (LayoutParams) view.getLayoutParams(); params.mSpanSize = getSpanSize(recycler, state, getPosition(view)); - params.mSpanIndex = span; - span += params.mSpanSize; + if (spanDiff == -1 && params.mSpanSize > 1) { + params.mSpanIndex = span - (params.mSpanSize - 1); + } else { + params.mSpanIndex = span; + } + span += spanDiff * params.mSpanSize; } } @@ -794,14 +553,12 @@ public class GridLayoutManager extends LinearLayoutManager { if (spanCount == mSpanCount) { return; } - mPendingSpanCountChange = true; if (spanCount < 1) { throw new IllegalArgumentException("Span count should be at least 1. Provided " + spanCount); } mSpanCount = spanCount; mSpanSizeLookup.invalidateSpanIndexCache(); - requestLayout(); } /** @@ -973,81 +730,9 @@ public class GridLayoutManager extends LinearLayoutManager { } } - @Override - public View onFocusSearchFailed(View focused, int focusDirection, - RecyclerView.Recycler recycler, RecyclerView.State state) { - View prevFocusedChild = findContainingItemView(focused); - if (prevFocusedChild == null) { - return null; - } - LayoutParams lp = (LayoutParams) prevFocusedChild.getLayoutParams(); - final int prevSpanStart = lp.mSpanIndex; - final int prevSpanEnd = lp.mSpanIndex + lp.mSpanSize; - View view = super.onFocusSearchFailed(focused, focusDirection, recycler, state); - if (view == null) { - return null; - } - // LinearLayoutManager finds the last child. What we want is the child which has the same - // spanIndex. - final int layoutDir = convertFocusDirectionToLayoutDirection(focusDirection); - final boolean ascend = (layoutDir == LayoutState.LAYOUT_END) != mShouldReverseLayout; - final int start, inc, limit; - if (ascend) { - start = getChildCount() - 1; - inc = -1; - limit = -1; - } else { - start = 0; - inc = 1; - limit = getChildCount(); - } - final boolean preferLastSpan = mOrientation == VERTICAL && isLayoutRTL(); - View weakCandidate = null; // somewhat matches but not strong - int weakCandidateSpanIndex = -1; - int weakCandidateOverlap = 0; // how many spans overlap - - for (int i = start; i != limit; i += inc) { - View candidate = getChildAt(i); - if (candidate == prevFocusedChild) { - break; - } - if (!candidate.isFocusable()) { - continue; - } - final LayoutParams candidateLp = (LayoutParams) candidate.getLayoutParams(); - final int candidateStart = candidateLp.mSpanIndex; - final int candidateEnd = candidateLp.mSpanIndex + candidateLp.mSpanSize; - if (candidateStart == prevSpanStart && candidateEnd == prevSpanEnd) { - return candidate; // perfect match - } - boolean assignAsWeek = false; - if (weakCandidate == null) { - assignAsWeek = true; - } else { - int maxStart = Math.max(candidateStart, prevSpanStart); - int minEnd = Math.min(candidateEnd, prevSpanEnd); - int overlap = minEnd - maxStart; - if (overlap > weakCandidateOverlap) { - assignAsWeek = true; - } else if (overlap == weakCandidateOverlap && - preferLastSpan == (candidateStart > weakCandidateSpanIndex)) { - assignAsWeek = true; - } - } - - if (assignAsWeek) { - weakCandidate = candidate; - weakCandidateSpanIndex = candidateLp.mSpanIndex; - weakCandidateOverlap = Math.min(candidateEnd, prevSpanEnd) - - Math.max(candidateStart, prevSpanStart); - } - } - return weakCandidate; - } - @Override public boolean supportsPredictiveItemAnimations() { - return mPendingSavedState == null && !mPendingSpanCountChange; + return mPendingSavedState == null; } /** @@ -1068,10 +753,6 @@ public class GridLayoutManager extends LinearLayoutManager { /** * LayoutParams used by GridLayoutManager. - *

      - * Note that if the orientation is {@link #VERTICAL}, the width parameter is ignored and if the - * orientation is {@link #HORIZONTAL} the height parameter is ignored because child view is - * expected to fill all of the space given to it. */ public static class LayoutParams extends RecyclerView.LayoutParams { @@ -1108,11 +789,10 @@ public class GridLayoutManager extends LinearLayoutManager { * Returns the current span index of this View. If the View is not laid out yet, the return * value is undefined. *

      - * Starting with RecyclerView 24.2.0, span indices are always indexed from position 0 - * even if the layout is RTL. In a vertical GridLayoutManager, leftmost span is span - * 0 if the layout is LTR and rightmost span is span 0 if the layout is - * RTL. Prior to 24.2.0, it was the opposite which was conflicting with - * {@link SpanSizeLookup#getSpanIndex(int, int)}. + * Note that span index may change by whether the RecyclerView is RTL or not. For + * example, if the number of spans is 3 and layout is RTL, the rightmost item will have + * span index of 2. If the layout changes back to LTR, span index for this view will be 0. + * If the item was occupying 2 spans, span indices would be 1 and 0 respectively. *

      * If the View occupies multiple spans, span with the minimum index is returned. * diff --git a/app/src/main/java/android/support/v7/widget/LayoutState.java b/app/src/main/java/android/support/v7/widget/LayoutState.java index 23d8ee8940..e62a80a289 100644 --- a/app/src/main/java/android/support/v7/widget/LayoutState.java +++ b/app/src/main/java/android/support/v7/widget/LayoutState.java @@ -10,12 +10,11 @@ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and + * See the License for the specific languag`e governing permissions and * limitations under the License. */ package android.support.v7.widget; - import android.view.View; /** @@ -36,11 +35,8 @@ class LayoutState { final static int ITEM_DIRECTION_TAIL = 1; - /** - * We may not want to recycle children in some cases (e.g. layout) - */ - boolean mRecycle = true; - + final static int SCOLLING_OFFSET_NaN = Integer.MIN_VALUE; + /** * Number of pixels that we should fill, in the layout direction. */ @@ -64,24 +60,11 @@ class LayoutState { int mLayoutDirection; /** - * This is the target pixel closest to the start of the layout that we are trying to fill + * Used if you want to pre-layout items that are not yet visible. + * The difference with {@link #mAvailable} is that, when recycling, distance rendered for + * {@link #mExtra} is not considered not to recycle visible children. */ - int mStartLine = 0; - - /** - * This is the target pixel closest to the end of the layout that we are trying to fill - */ - int mEndLine = 0; - - /** - * If true, layout should stop if a focusable view is added - */ - boolean mStopInFocusable; - - /** - * If the content is not wrapped with any value - */ - boolean mInfinite; + int mExtra = 0; /** * @return true if there are more items in the data adapter @@ -101,16 +84,4 @@ class LayoutState { mCurrentPosition += mItemDirection; return view; } - - @Override - public String toString() { - return "LayoutState{" + - "mAvailable=" + mAvailable + - ", mCurrentPosition=" + mCurrentPosition + - ", mItemDirection=" + mItemDirection + - ", mLayoutDirection=" + mLayoutDirection + - ", mStartLine=" + mStartLine + - ", mEndLine=" + mEndLine + - '}'; - } } diff --git a/app/src/main/java/android/support/v7/widget/LinearLayoutManager.java b/app/src/main/java/android/support/v7/widget/LinearLayoutManager.java index 5ee2537ffb..230db34523 100644 --- a/app/src/main/java/android/support/v7/widget/LinearLayoutManager.java +++ b/app/src/main/java/android/support/v7/widget/LinearLayoutManager.java @@ -10,14 +10,12 @@ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and + * See the License for the specific languag`e governing permissions and * limitations under the License. */ package android.support.v7.widget; -import static android.support.v7.widget.RecyclerView.NO_POSITION; - import android.content.Context; import android.graphics.PointF; import android.os.Parcel; @@ -25,9 +23,6 @@ import android.os.Parcelable; import android.support.v4.view.ViewCompat; import android.support.v4.view.accessibility.AccessibilityEventCompat; import android.support.v4.view.accessibility.AccessibilityRecordCompat; -import android.support.v7.widget.RecyclerView.LayoutParams; -import android.support.v7.widget.helper.ItemTouchHelper; -import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; @@ -35,12 +30,13 @@ import android.view.accessibility.AccessibilityEvent; import java.util.List; +import static android.support.v7.widget.RecyclerView.NO_POSITION; + /** - * A {@link android.support.v7.widget.RecyclerView.LayoutManager} implementation which provides + * A {@link RecyclerView.LayoutManager} implementation which provides * similar functionality to {@link android.widget.ListView}. */ -public class LinearLayoutManager extends RecyclerView.LayoutManager implements - ItemTouchHelper.ViewDropHandler, RecyclerView.SmoothScroller.ScrollVectorProvider { +public class LinearLayoutManager extends RecyclerView.LayoutManager { private static final String TAG = "LinearLayoutManager"; @@ -58,7 +54,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * than this factor times the total space of the list. If layout is vertical, total space is the * height minus padding, if layout is horizontal, total space is the width minus padding. */ - private static final float MAX_SCROLL_FACTOR = 1 / 3f; + private static final float MAX_SCROLL_FACTOR = 0.33f; /** @@ -134,12 +130,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * Re-used variable to keep anchor information on re-layout. * Anchor position and coordinate defines the reference point for LLM while doing a layout. * */ - final AnchorInfo mAnchorInfo = new AnchorInfo(); - - /** - * Stashed to avoid allocation, currently only used in #fill() - */ - private final LayoutChunkResult mLayoutChunkResult = new LayoutChunkResult(); + final AnchorInfo mAnchorInfo; /** * Creates a vertical LinearLayoutManager @@ -157,34 +148,17 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * @param reverseLayout When set to true, layouts from end to start. */ public LinearLayoutManager(Context context, int orientation, boolean reverseLayout) { + mAnchorInfo = new AnchorInfo(); setOrientation(orientation); setReverseLayout(reverseLayout); - setAutoMeasureEnabled(true); - } - - /** - * Constructor used when layout manager is set in XML by RecyclerView attribute - * "layoutManager". Defaults to vertical orientation. - * - * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_android_orientation - * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_reverseLayout - * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_stackFromEnd - */ - public LinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, - int defStyleRes) { - Properties properties = getProperties(context, attrs, defStyleAttr, defStyleRes); - setOrientation(properties.orientation); - setReverseLayout(properties.reverseLayout); - setStackFromEnd(properties.stackFromEnd); - setAutoMeasureEnabled(true); } /** * {@inheritDoc} */ @Override - public LayoutParams generateDefaultLayoutParams() { - return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + public RecyclerView.LayoutParams generateDefaultLayoutParams() { + return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); } @@ -204,7 +178,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * RecyclerView. *

      * If you are using a {@link RecyclerView.RecycledViewPool}, it might be a good idea to set - * this flag to true so that views will be available to other RecyclerViews + * this flag to true so that views will be avilable to other RecyclerViews * immediately. *

      * Note that, setting this flag will result in a performance drop if RecyclerView @@ -243,7 +217,6 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } SavedState state = new SavedState(); if (getChildCount() > 0) { - ensureLayoutState(); boolean didLayoutFromEnd = mLastStackFromEnd ^ mShouldReverseLayout; state.mAnchorLayoutFromEnd = didLayoutFromEnd; if (didLayoutFromEnd) { @@ -309,9 +282,10 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } /** - * Returns the current orientation of the layout. + * Returns the current orientaion of the layout. * - * @return Current orientation, either {@link #HORIZONTAL} or {@link #VERTICAL} + * @return Current orientation. + * @see #mOrientation * @see #setOrientation(int) */ public int getOrientation() { @@ -319,7 +293,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } /** - * Sets the orientation of the layout. {@link android.support.v7.widget.LinearLayoutManager} + * Sets the orientation of the layout. {@link LinearLayoutManager} * will do its best to keep scroll position. * * @param orientation {@link #HORIZONTAL} or {@link #VERTICAL} @@ -355,7 +329,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * Returns if views are laid out from the opposite direction of the layout. * * @return If layout is reversed or not. - * @see #setReverseLayout(boolean) + * @see {@link #setReverseLayout(boolean)} */ public boolean getReverseLayout() { return mReverseLayout; @@ -367,8 +341,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * laid out at the end of the UI, second item is laid out before it etc. * * For horizontal layouts, it depends on the layout direction. - * When set to true, If {@link android.support.v7.widget.RecyclerView} is LTR, than it will - * layout from RTL, if {@link android.support.v7.widget.RecyclerView}} is RTL, it will layout + * When set to true, If {@link RecyclerView} is LTR, than it will + * layout from RTL, if {@link RecyclerView}} is RTL, it will layout * from LTR. * * If you are looking for the exact same behavior of @@ -396,18 +370,14 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements final int firstChild = getPosition(getChildAt(0)); final int viewPosition = position - firstChild; if (viewPosition >= 0 && viewPosition < childCount) { - final View child = getChildAt(viewPosition); - if (getPosition(child) == position) { - return child; // in pre-layout, this may not match - } + return getChildAt(viewPosition); } - // fallback to traversal. This might be necessary in pre-layout. - return super.findViewByPosition(position); + return null; } /** *

      Returns the amount of extra space that should be laid out by LayoutManager. - * By default, {@link android.support.v7.widget.LinearLayoutManager} lays out 1 extra page of + * By default, {@link LinearLayoutManager} lays out 1 extra page of * items while smooth scrolling and 0 otherwise. You can override this method to implement your * custom layout pre-cache logic.

      *

      Laying out invisible elements will eventually come with performance cost. On the other @@ -429,12 +399,17 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { LinearSmoothScroller linearSmoothScroller = - new LinearSmoothScroller(recyclerView.getContext()); + new LinearSmoothScroller(recyclerView.getContext()) { + @Override + public PointF computeScrollVectorForPosition(int targetPosition) { + return LinearLayoutManager.this + .computeScrollVectorForPosition(targetPosition); + } + }; linearSmoothScroller.setTargetPosition(position); startSmoothScroll(linearSmoothScroller); } - @Override public PointF computeScrollVectorForPosition(int targetPosition) { if (getChildCount() == 0) { return null; @@ -463,12 +438,6 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements if (DEBUG) { Log.d(TAG, "is pre layout:" + state.isPreLayout()); } - if (mPendingSavedState != null || mPendingScrollPosition != NO_POSITION) { - if (state.getItemCount() == 0) { - removeAndRecycleAllViews(recycler); - return; - } - } if (mPendingSavedState != null && mPendingSavedState.hasValidAnchor()) { mPendingScrollPosition = mPendingSavedState.mAnchorPosition; } @@ -478,14 +447,10 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements // resolve layout direction resolveShouldLayoutReverse(); - if (!mAnchorInfo.mValid || mPendingScrollPosition != NO_POSITION || - mPendingSavedState != null) { - mAnchorInfo.reset(); - mAnchorInfo.mLayoutFromEnd = mShouldReverseLayout ^ mStackFromEnd; - // calculate anchor position and coordinate - updateAnchorInfoForLayout(recycler, state, mAnchorInfo); - mAnchorInfo.mValid = true; - } + mAnchorInfo.reset(); + mAnchorInfo.mLayoutFromEnd = mShouldReverseLayout ^ mStackFromEnd; + // calculate anchor position and coordinate + updateAnchorInfoForLayout(state, mAnchorInfo); if (DEBUG) { Log.d(TAG, "Anchor info:" + mAnchorInfo); } @@ -495,9 +460,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements int extraForStart; int extraForEnd; final int extra = getExtraLayoutSpace(state); - // If the previous scroll delta was less than zero, the extra space should be laid out - // at the start. Otherwise, it should be at the end. - if (mLayoutState.mLastScrollDelta >= 0) { + boolean before = state.getTargetScrollPosition() < mAnchorInfo.mPosition; + if (before == mShouldReverseLayout) { extraForEnd = extra; extraForStart = 0; } else { @@ -533,18 +497,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } int startOffset; int endOffset; - final int firstLayoutDirection; - if (mAnchorInfo.mLayoutFromEnd) { - firstLayoutDirection = mShouldReverseLayout ? LayoutState.ITEM_DIRECTION_TAIL : - LayoutState.ITEM_DIRECTION_HEAD; - } else { - firstLayoutDirection = mShouldReverseLayout ? LayoutState.ITEM_DIRECTION_HEAD : - LayoutState.ITEM_DIRECTION_TAIL; - } - - onAnchorReady(recycler, state, mAnchorInfo, firstLayoutDirection); + onAnchorReady(state, mAnchorInfo); detachAndScrapAttachedViews(recycler); - mLayoutState.mInfinite = resolveIsInfinite(); mLayoutState.mIsPreLayout = state.isPreLayout(); if (mAnchorInfo.mLayoutFromEnd) { // fill towards start @@ -552,7 +506,6 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements mLayoutState.mExtra = extraForStart; fill(recycler, mLayoutState, state, false); startOffset = mLayoutState.mOffset; - final int firstElement = mLayoutState.mCurrentPosition; if (mLayoutState.mAvailable > 0) { extraForEnd += mLayoutState.mAvailable; } @@ -562,22 +515,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements mLayoutState.mCurrentPosition += mLayoutState.mItemDirection; fill(recycler, mLayoutState, state, false); endOffset = mLayoutState.mOffset; - - if (mLayoutState.mAvailable > 0) { - // end could not consume all. add more items towards start - extraForStart = mLayoutState.mAvailable; - updateLayoutStateToFillStart(firstElement, startOffset); - mLayoutState.mExtra = extraForStart; - fill(recycler, mLayoutState, state, false); - startOffset = mLayoutState.mOffset; - } } else { // fill towards end updateLayoutStateToFillEnd(mAnchorInfo); mLayoutState.mExtra = extraForEnd; fill(recycler, mLayoutState, state, false); endOffset = mLayoutState.mOffset; - final int lastElement = mLayoutState.mCurrentPosition; if (mLayoutState.mAvailable > 0) { extraForStart += mLayoutState.mAvailable; } @@ -587,15 +530,6 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements mLayoutState.mCurrentPosition += mLayoutState.mItemDirection; fill(recycler, mLayoutState, state, false); startOffset = mLayoutState.mOffset; - - if (mLayoutState.mAvailable > 0) { - extraForEnd = mLayoutState.mAvailable; - // start could not consume all it should. add more items towards end - updateLayoutStateToFillEnd(lastElement, endOffset); - mLayoutState.mExtra = extraForEnd; - fill(recycler, mLayoutState, state, false); - endOffset = mLayoutState.mOffset; - } } // changes may cause gaps on the UI, try to fix them. @@ -623,36 +557,25 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } layoutForPredictiveAnimations(recycler, state, startOffset, endOffset); if (!state.isPreLayout()) { + mPendingScrollPosition = NO_POSITION; + mPendingScrollPositionOffset = INVALID_OFFSET; mOrientationHelper.onLayoutComplete(); - } else { - mAnchorInfo.reset(); } mLastStackFromEnd = mStackFromEnd; + mPendingSavedState = null; // we don't need this anymore if (DEBUG) { validateChildOrder(); } } - @Override - public void onLayoutCompleted(RecyclerView.State state) { - super.onLayoutCompleted(state); - mPendingSavedState = null; // we don't need this anymore - mPendingScrollPosition = NO_POSITION; - mPendingScrollPositionOffset = INVALID_OFFSET; - mAnchorInfo.reset(); - } - /** * Method called when Anchor position is decided. Extending class can setup accordingly or * even update anchor info if necessary. - * @param recycler The recycler for the layout - * @param state The layout state - * @param anchorInfo The mutable POJO that keeps the position and offset. - * @param firstLayoutItemDirection The direction of the first layout filling in terms of adapter - * indices. + * + * @param state + * @param anchorInfo Simple data structure to keep anchor point information for the next layout */ - void onAnchorReady(RecyclerView.Recycler recycler, RecyclerView.State state, - AnchorInfo anchorInfo, int firstLayoutItemDirection) { + void onAnchorReady(RecyclerView.State state, AnchorInfo anchorInfo) { } /** @@ -668,6 +591,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements || !supportsPredictiveItemAnimations()) { return; } + // to make the logic simpler, we calculate the size of children and call fill. int scrapExtraStart = 0, scrapExtraEnd = 0; final List scrapList = recycler.getScrapList(); @@ -675,10 +599,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements final int firstChildPos = getPosition(getChildAt(0)); for (int i = 0; i < scrapSize; i++) { RecyclerView.ViewHolder scrap = scrapList.get(i); - if (scrap.isRemoved()) { - continue; - } - final int position = scrap.getLayoutPosition(); + final int position = scrap.getPosition(); final int direction = position < firstChildPos != mShouldReverseLayout ? LayoutState.LAYOUT_START : LayoutState.LAYOUT_END; if (direction == LayoutState.LAYOUT_START) { @@ -698,7 +619,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements updateLayoutStateToFillStart(getPosition(anchor), startOffset); mLayoutState.mExtra = scrapExtraStart; mLayoutState.mAvailable = 0; - mLayoutState.assignPositionFromScrapList(); + mLayoutState.mCurrentPosition += mShouldReverseLayout ? 1 : -1; fill(recycler, mLayoutState, state, false); } @@ -707,14 +628,13 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements updateLayoutStateToFillEnd(getPosition(anchor), endOffset); mLayoutState.mExtra = scrapExtraEnd; mLayoutState.mAvailable = 0; - mLayoutState.assignPositionFromScrapList(); + mLayoutState.mCurrentPosition += mShouldReverseLayout ? -1 : 1; fill(recycler, mLayoutState, state, false); } mLayoutState.mScrapList = null; } - private void updateAnchorInfoForLayout(RecyclerView.Recycler recycler, RecyclerView.State state, - AnchorInfo anchorInfo) { + private void updateAnchorInfoForLayout(RecyclerView.State state, AnchorInfo anchorInfo) { if (updateAnchorFromPendingData(state, anchorInfo)) { if (DEBUG) { Log.d(TAG, "updated anchor info from pending information"); @@ -722,7 +642,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements return; } - if (updateAnchorFromChildren(recycler, state, anchorInfo)) { + if (updateAnchorFromChildren(state, anchorInfo)) { if (DEBUG) { Log.d(TAG, "updated anchor info from existing children"); } @@ -741,22 +661,24 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements *

      * If a child has focus, it is given priority. */ - private boolean updateAnchorFromChildren(RecyclerView.Recycler recycler, - RecyclerView.State state, AnchorInfo anchorInfo) { + private boolean updateAnchorFromChildren(RecyclerView.State state, AnchorInfo anchorInfo) { if (getChildCount() == 0) { return false; } - final View focused = getFocusedChild(); - if (focused != null && anchorInfo.isViewValidAsAnchor(focused, state)) { - anchorInfo.assignFromViewAndKeepVisibleRect(focused); + View focused = getFocusedChild(); + if (focused != null && anchorInfo.assignFromViewIfValid(focused, state)) { + if (DEBUG) { + Log.d(TAG, "decided anchor child from focused view"); + } return true; } + if (mLastStackFromEnd != mStackFromEnd) { return false; } - View referenceChild = anchorInfo.mLayoutFromEnd - ? findReferenceChildClosestToEnd(recycler, state) - : findReferenceChildClosestToStart(recycler, state); + + View referenceChild = anchorInfo.mLayoutFromEnd ? findReferenceChildClosestToEnd(state) + : findReferenceChildClosestToStart(state); if (referenceChild != null) { anchorInfo.assignFromView(referenceChild); // If all visible views are removed in 1 pass, reference child might be out of bounds. @@ -854,7 +776,6 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } // override layout from end values for consistency anchorInfo.mLayoutFromEnd = mShouldReverseLayout; - // if this changes, we should update prepareForDrop as well if (mShouldReverseLayout) { anchorInfo.mCoordinate = mOrientationHelper.getEndAfterPadding() - mPendingScrollPositionOffset; @@ -926,7 +847,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements mLayoutState.mCurrentPosition = itemPosition; mLayoutState.mLayoutDirection = LayoutState.LAYOUT_END; mLayoutState.mOffset = offset; - mLayoutState.mScrollingOffset = LayoutState.SCROLLING_OFFSET_NaN; + mLayoutState.mScrollingOffset = LayoutState.SCOLLING_OFFSET_NaN; } private void updateLayoutStateToFillStart(AnchorInfo anchorInfo) { @@ -940,7 +861,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements LayoutState.ITEM_DIRECTION_HEAD; mLayoutState.mLayoutDirection = LayoutState.LAYOUT_START; mLayoutState.mOffset = offset; - mLayoutState.mScrollingOffset = LayoutState.SCROLLING_OFFSET_NaN; + mLayoutState.mScrollingOffset = LayoutState.SCOLLING_OFFSET_NaN; } @@ -950,22 +871,13 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements void ensureLayoutState() { if (mLayoutState == null) { - mLayoutState = createLayoutState(); + mLayoutState = new LayoutState(); } if (mOrientationHelper == null) { mOrientationHelper = OrientationHelper.createOrientationHelper(this, mOrientation); } } - /** - * Test overrides this to plug some tracking and verification. - * - * @return A new LayoutState - */ - LayoutState createLayoutState() { - return new LayoutState(); - } - /** *

      Scroll the RecyclerView to make the position visible.

      * @@ -993,13 +905,14 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements /** * Scroll to the specified adapter position with the given offset from resolved layout * start. Resolved layout start depends on {@link #getReverseLayout()}, - * {@link ViewCompat#getLayoutDirection(android.view.View)} and {@link #getStackFromEnd()}. + * {@link ViewCompat#getLayoutDirection(View)} and {@link #getStackFromEnd()}. *

      * For example, if layout is {@link #VERTICAL} and {@link #getStackFromEnd()} is true, calling * scrollToPositionWithOffset(10, 20) will layout such that * item[10]'s bottom is 20 pixels above the RecyclerView's bottom. *

      * Note that scroll position change will not be reflected until the next layout call. + * *

      * If you are just trying to make a position visible, use {@link #scrollToPosition(int)}. * @@ -1077,33 +990,27 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements if (getChildCount() == 0) { return 0; } - ensureLayoutState(); return ScrollbarHelper.computeScrollOffset(state, mOrientationHelper, - findFirstVisibleChildClosestToStart(!mSmoothScrollbarEnabled, true), - findFirstVisibleChildClosestToEnd(!mSmoothScrollbarEnabled, true), - this, mSmoothScrollbarEnabled, mShouldReverseLayout); + getChildClosestToStart(), getChildClosestToEnd(), this, + mSmoothScrollbarEnabled, mShouldReverseLayout); } private int computeScrollExtent(RecyclerView.State state) { if (getChildCount() == 0) { return 0; } - ensureLayoutState(); return ScrollbarHelper.computeScrollExtent(state, mOrientationHelper, - findFirstVisibleChildClosestToStart(!mSmoothScrollbarEnabled, true), - findFirstVisibleChildClosestToEnd(!mSmoothScrollbarEnabled, true), - this, mSmoothScrollbarEnabled); + getChildClosestToStart(), getChildClosestToEnd(), this, + mSmoothScrollbarEnabled); } private int computeScrollRange(RecyclerView.State state) { if (getChildCount() == 0) { return 0; } - ensureLayoutState(); return ScrollbarHelper.computeScrollRange(state, mOrientationHelper, - findFirstVisibleChildClosestToStart(!mSmoothScrollbarEnabled, true), - findFirstVisibleChildClosestToEnd(!mSmoothScrollbarEnabled, true), - this, mSmoothScrollbarEnabled); + getChildClosestToStart(), getChildClosestToEnd(), this, + mSmoothScrollbarEnabled); } /** @@ -1140,11 +1047,9 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements private void updateLayoutState(int layoutDirection, int requiredSpace, boolean canUseExistingSpace, RecyclerView.State state) { - // If parent provides a hint, don't measure unlimited. - mLayoutState.mInfinite = resolveIsInfinite(); mLayoutState.mExtra = getExtraLayoutSpace(state); mLayoutState.mLayoutDirection = layoutDirection; - int scrollingOffset; + int fastScrollSpace; if (layoutDirection == LayoutState.LAYOUT_END) { mLayoutState.mExtra += mOrientationHelper.getEndPadding(); // get the first child in the direction we are going @@ -1155,7 +1060,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements mLayoutState.mCurrentPosition = getPosition(child) + mLayoutState.mItemDirection; mLayoutState.mOffset = mOrientationHelper.getDecoratedEnd(child); // calculate how much we can scroll without adding new children (independent of layout) - scrollingOffset = mOrientationHelper.getDecoratedEnd(child) + fastScrollSpace = mOrientationHelper.getDecoratedEnd(child) - mOrientationHelper.getEndAfterPadding(); } else { @@ -1165,19 +1070,14 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements : LayoutState.ITEM_DIRECTION_HEAD; mLayoutState.mCurrentPosition = getPosition(child) + mLayoutState.mItemDirection; mLayoutState.mOffset = mOrientationHelper.getDecoratedStart(child); - scrollingOffset = -mOrientationHelper.getDecoratedStart(child) + fastScrollSpace = -mOrientationHelper.getDecoratedStart(child) + mOrientationHelper.getStartAfterPadding(); } mLayoutState.mAvailable = requiredSpace; if (canUseExistingSpace) { - mLayoutState.mAvailable -= scrollingOffset; + mLayoutState.mAvailable -= fastScrollSpace; } - mLayoutState.mScrollingOffset = scrollingOffset; - } - - boolean resolveIsInfinite() { - return mOrientationHelper.getMode() == View.MeasureSpec.UNSPECIFIED - && mOrientationHelper.getEnd() == 0; + mLayoutState.mScrollingOffset = fastScrollSpace; } int scrollBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { @@ -1189,8 +1089,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements final int layoutDirection = dy > 0 ? LayoutState.LAYOUT_END : LayoutState.LAYOUT_START; final int absDy = Math.abs(dy); updateLayoutState(layoutDirection, absDy, true, state); - final int consumed = mLayoutState.mScrollingOffset - + fill(recycler, mLayoutState, state, false); + final int freeScroll = mLayoutState.mScrollingOffset; + final int consumed = freeScroll + fill(recycler, mLayoutState, state, false); if (consumed < 0) { if (DEBUG) { Log.d(TAG, "Don't have any more elements to scroll"); @@ -1202,7 +1102,6 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements if (DEBUG) { Log.d(TAG, "scroll req: " + dy + " scrolled: " + scrolled); } - mLayoutState.mLastScrollDelta = scrolled; return scrolled; } @@ -1239,13 +1138,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements /** * Recycles views that went out of bounds after scrolling towards the end of the layout. - *

      - * Checks both layout position and visible position to guarantee that the view is not visible. * - * @param recycler Recycler instance of {@link android.support.v7.widget.RecyclerView} + * @param recycler Recycler instance of {@link RecyclerView} * @param dt This can be used to add additional padding to the visible area. This is used - * to detect children that will go out of bounds after scrolling, without - * actually moving them. + * to + * detect children that will go out of bounds after scrolling, without actually + * moving them. */ private void recycleViewsFromStart(RecyclerView.Recycler recycler, int dt) { if (dt < 0) { @@ -1261,9 +1159,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements if (mShouldReverseLayout) { for (int i = childCount - 1; i >= 0; i--) { View child = getChildAt(i); - if (mOrientationHelper.getDecoratedEnd(child) > limit - || mOrientationHelper.getTransformedEndWithDecoration(child) > limit) { - // stop here + if (mOrientationHelper.getDecoratedEnd(child) > limit) {// stop here recycleChildren(recycler, childCount - 1, i); return; } @@ -1271,9 +1167,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } else { for (int i = 0; i < childCount; i++) { View child = getChildAt(i); - if (mOrientationHelper.getDecoratedEnd(child) > limit - || mOrientationHelper.getTransformedEndWithDecoration(child) > limit) { - // stop here + if (mOrientationHelper.getDecoratedEnd(child) > limit) {// stop here recycleChildren(recycler, 0, i); return; } @@ -1284,10 +1178,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements /** * Recycles views that went out of bounds after scrolling towards the start of the layout. - *

      - * Checks both layout position and visible position to guarantee that the view is not visible. * - * @param recycler Recycler instance of {@link android.support.v7.widget.RecyclerView} + * @param recycler Recycler instance of {@link RecyclerView} * @param dt This can be used to add additional padding to the visible area. This is used * to detect children that will go out of bounds after scrolling, without * actually moving them. @@ -1305,9 +1197,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements if (mShouldReverseLayout) { for (int i = 0; i < childCount; i++) { View child = getChildAt(i); - if (mOrientationHelper.getDecoratedStart(child) < limit - || mOrientationHelper.getTransformedStartWithDecoration(child) < limit) { - // stop here + if (mOrientationHelper.getDecoratedStart(child) < limit) {// stop here recycleChildren(recycler, 0, i); return; } @@ -1315,9 +1205,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } else { for (int i = childCount - 1; i >= 0; i--) { View child = getChildAt(i); - if (mOrientationHelper.getDecoratedStart(child) < limit - || mOrientationHelper.getTransformedStartWithDecoration(child) < limit) { - // stop here + if (mOrientationHelper.getDecoratedStart(child) < limit) {// stop here recycleChildren(recycler, childCount - 1, i); return; } @@ -1333,12 +1221,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * @param layoutState Current layout state. Right now, this object does not change but * we may consider moving it out of this view so passing around as a * parameter for now, rather than accessing {@link #mLayoutState} - * @see #recycleViewsFromStart(android.support.v7.widget.RecyclerView.Recycler, int) - * @see #recycleViewsFromEnd(android.support.v7.widget.RecyclerView.Recycler, int) - * @see android.support.v7.widget.LinearLayoutManager.LayoutState#mLayoutDirection + * @see #recycleViewsFromStart(RecyclerView.Recycler, int) + * @see #recycleViewsFromEnd(RecyclerView.Recycler, int) + * @see LayoutState#mLayoutDirection */ private void recycleByLayoutState(RecyclerView.Recycler recycler, LayoutState layoutState) { - if (!layoutState.mRecycle || layoutState.mInfinite) { + if (!layoutState.mRecycle) { return; } if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) { @@ -1350,20 +1238,20 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements /** * The magic functions :). Fills the given layout, defined by the layoutState. This is fairly - * independent from the rest of the {@link android.support.v7.widget.LinearLayoutManager} + * independent from the rest of the {@link LinearLayoutManager} * and with little change, can be made publicly available as a helper class. * * @param recycler Current recycler that is attached to RecyclerView * @param layoutState Configuration on how we should fill out the available space. * @param state Context passed by the RecyclerView to control scroll steps. * @param stopOnFocusable If true, filling stops in the first focusable new child - * @return Number of pixels that it added. Useful for scroll functions. + * @return Number of pixels that it added. Useful for scoll functions. */ int fill(RecyclerView.Recycler recycler, LayoutState layoutState, RecyclerView.State state, boolean stopOnFocusable) { // max offset we should set is mFastScroll + available final int start = layoutState.mAvailable; - if (layoutState.mScrollingOffset != LayoutState.SCROLLING_OFFSET_NaN) { + if (layoutState.mScrollingOffset != LayoutState.SCOLLING_OFFSET_NaN) { // TODO ugly bug fix. should not happen if (layoutState.mAvailable < 0) { layoutState.mScrollingOffset += layoutState.mAvailable; @@ -1371,8 +1259,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements recycleByLayoutState(recycler, layoutState); } int remainingSpace = layoutState.mAvailable + layoutState.mExtra; - LayoutChunkResult layoutChunkResult = mLayoutChunkResult; - while ((layoutState.mInfinite || remainingSpace > 0) && layoutState.hasMore(state)) { + LayoutChunkResult layoutChunkResult = new LayoutChunkResult(); + while (remainingSpace > 0 && layoutState.hasMore(state)) { layoutChunkResult.resetInternal(); layoutChunk(recycler, state, layoutState, layoutChunkResult); if (layoutChunkResult.mFinished) { @@ -1392,7 +1280,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements remainingSpace -= layoutChunkResult.mConsumed; } - if (layoutState.mScrollingOffset != LayoutState.SCROLLING_OFFSET_NaN) { + if (layoutState.mScrollingOffset != LayoutState.SCOLLING_OFFSET_NaN) { layoutState.mScrollingOffset += layoutChunkResult.mConsumed; if (layoutState.mAvailable < 0) { layoutState.mScrollingOffset += layoutState.mAvailable; @@ -1421,7 +1309,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements result.mFinished = true; return; } - LayoutParams params = (LayoutParams) view.getLayoutParams(); + RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams(); if (layoutState.mScrapList == null) { if (mShouldReverseLayout == (layoutState.mLayoutDirection == LayoutState.LAYOUT_START)) { @@ -1469,7 +1357,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } // We calculate everything with View's bounding box (which includes decor and margins) // To calculate correct layout position, we subtract margins. - layoutDecoratedWithMargins(view, left, top, right, bottom); + layoutDecorated(view, left + params.leftMargin, top + params.topMargin, + right - params.rightMargin, bottom - params.bottomMargin); if (DEBUG) { Log.d(TAG, "laid out child at position " + getPosition(view) + ", with l:" + (left + params.leftMargin) + ", t:" + (top + params.topMargin) + ", r:" @@ -1482,13 +1371,6 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements result.mFocusable = view.isFocusable(); } - @Override - boolean shouldMeasureTwice() { - return getHeightMode() != View.MeasureSpec.EXACTLY - && getWidthMode() != View.MeasureSpec.EXACTLY - && hasFlexibleChildInBothOrientations(); - } - /** * Converts a focusDirection to orientation. * @@ -1499,24 +1381,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * @return {@link LayoutState#LAYOUT_START} or {@link LayoutState#LAYOUT_END} if focus direction * is applicable to current state, {@link LayoutState#INVALID_LAYOUT} otherwise. */ - int convertFocusDirectionToLayoutDirection(int focusDirection) { + private int convertFocusDirectionToLayoutDirection(int focusDirection) { switch (focusDirection) { case View.FOCUS_BACKWARD: - if (mOrientation == VERTICAL) { - return LayoutState.LAYOUT_START; - } else if (isLayoutRTL()) { - return LayoutState.LAYOUT_END; - } else { - return LayoutState.LAYOUT_START; - } + return LayoutState.LAYOUT_START; case View.FOCUS_FORWARD: - if (mOrientation == VERTICAL) { - return LayoutState.LAYOUT_END; - } else if (isLayoutRTL()) { - return LayoutState.LAYOUT_START; - } else { - return LayoutState.LAYOUT_END; - } + return LayoutState.LAYOUT_END; case View.FOCUS_UP: return mOrientation == VERTICAL ? LayoutState.LAYOUT_START : LayoutState.INVALID_LAYOUT; @@ -1558,42 +1428,6 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements return getChildAt(mShouldReverseLayout ? 0 : getChildCount() - 1); } - /** - * Convenience method to find the visible child closes to start. Caller should check if it has - * enough children. - * - * @param completelyVisible Whether child should be completely visible or not - * @return The first visible child closest to start of the layout from user's perspective. - */ - private View findFirstVisibleChildClosestToStart(boolean completelyVisible, - boolean acceptPartiallyVisible) { - if (mShouldReverseLayout) { - return findOneVisibleChild(getChildCount() - 1, -1, completelyVisible, - acceptPartiallyVisible); - } else { - return findOneVisibleChild(0, getChildCount(), completelyVisible, - acceptPartiallyVisible); - } - } - - /** - * Convenience method to find the visible child closes to end. Caller should check if it has - * enough children. - * - * @param completelyVisible Whether child should be completely visible or not - * @return The first visible child closest to end of the layout from user's perspective. - */ - private View findFirstVisibleChildClosestToEnd(boolean completelyVisible, - boolean acceptPartiallyVisible) { - if (mShouldReverseLayout) { - return findOneVisibleChild(0, getChildCount(), completelyVisible, - acceptPartiallyVisible); - } else { - return findOneVisibleChild(getChildCount() - 1, -1, completelyVisible, - acceptPartiallyVisible); - } - } - /** * Among the children that are suitable to be considered as an anchor child, returns the one @@ -1605,10 +1439,9 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * It also prioritizes children that are within the visible bounds. * @return A View that can be used an an anchor View. */ - private View findReferenceChildClosestToEnd(RecyclerView.Recycler recycler, - RecyclerView.State state) { - return mShouldReverseLayout ? findFirstReferenceChild(recycler, state) : - findLastReferenceChild(recycler, state); + private View findReferenceChildClosestToEnd(RecyclerView.State state) { + return mShouldReverseLayout ? findFirstReferenceChild(state.getItemCount()) : + findLastReferenceChild(state.getItemCount()); } /** @@ -1622,24 +1455,20 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * * @return A View that can be used an an anchor View. */ - private View findReferenceChildClosestToStart(RecyclerView.Recycler recycler, - RecyclerView.State state) { - return mShouldReverseLayout ? findLastReferenceChild(recycler, state) : - findFirstReferenceChild(recycler, state); + private View findReferenceChildClosestToStart(RecyclerView.State state) { + return mShouldReverseLayout ? findLastReferenceChild(state.getItemCount()) : + findFirstReferenceChild(state.getItemCount()); } - private View findFirstReferenceChild(RecyclerView.Recycler recycler, RecyclerView.State state) { - return findReferenceChild(recycler, state, 0, getChildCount(), state.getItemCount()); + private View findFirstReferenceChild(int itemCount) { + return findReferenceChild(0, getChildCount(), itemCount); } - private View findLastReferenceChild(RecyclerView.Recycler recycler, RecyclerView.State state) { - return findReferenceChild(recycler, state, getChildCount() - 1, -1, state.getItemCount()); + private View findLastReferenceChild(int itemCount) { + return findReferenceChild(getChildCount() - 1, -1, itemCount); } - // overridden by GridLayoutManager - View findReferenceChild(RecyclerView.Recycler recycler, RecyclerView.State state, - int start, int end, int itemCount) { - ensureLayoutState(); + private View findReferenceChild(int start, int end, int itemCount) { View invalidMatch = null; View outOfBoundsMatch = null; final int boundsStart = mOrientationHelper.getStartAfterPadding(); @@ -1649,7 +1478,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements final View view = getChildAt(i); final int position = getPosition(view); if (position >= 0 && position < itemCount) { - if (((LayoutParams) view.getLayoutParams()).isItemRemoved()) { + if (((RecyclerView.LayoutParams) view.getLayoutParams()).isItemRemoved()) { if (invalidMatch == null) { invalidMatch = view; // removed item, least preferred } @@ -1667,8 +1496,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } /** - * Returns the adapter position of the first visible view. This position does not include - * adapter changes that were dispatched after the last layout pass. + * Returns the adapter position of the first visible view. *

      * Note that, this value is not affected by layout orientation or item order traversal. * ({@link #setReverseLayout(boolean)}). Views are sorted by their positions in the adapter, @@ -1685,13 +1513,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * @see #findLastVisibleItemPosition() */ public int findFirstVisibleItemPosition() { - final View child = findOneVisibleChild(0, getChildCount(), false, true); + final View child = findOneVisibleChild(0, getChildCount(), false); return child == null ? NO_POSITION : getPosition(child); } /** - * Returns the adapter position of the first fully visible view. This position does not include - * adapter changes that were dispatched after the last layout pass. + * Returns the adapter position of the first fully visible view. *

      * Note that bounds check is only performed in the current orientation. That means, if * LayoutManager is horizontal, it will only check the view's left and right edges. @@ -1702,13 +1529,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * @see #findLastCompletelyVisibleItemPosition() */ public int findFirstCompletelyVisibleItemPosition() { - final View child = findOneVisibleChild(0, getChildCount(), true, false); + final View child = findOneVisibleChild(0, getChildCount(), true); return child == null ? NO_POSITION : getPosition(child); } /** - * Returns the adapter position of the last visible view. This position does not include - * adapter changes that were dispatched after the last layout pass. + * Returns the adapter position of the last visible view. *

      * Note that, this value is not affected by layout orientation or item order traversal. * ({@link #setReverseLayout(boolean)}). Views are sorted by their positions in the adapter, @@ -1725,13 +1551,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * @see #findFirstVisibleItemPosition() */ public int findLastVisibleItemPosition() { - final View child = findOneVisibleChild(getChildCount() - 1, -1, false, true); + final View child = findOneVisibleChild(getChildCount() - 1, -1, false); return child == null ? NO_POSITION : getPosition(child); } /** - * Returns the adapter position of the last fully visible view. This position does not include - * adapter changes that were dispatched after the last layout pass. + * Returns the adapter position of the last fully visible view. *

      * Note that bounds check is only performed in the current orientation. That means, if * LayoutManager is horizontal, it will only check the view's left and right edges. @@ -1742,17 +1567,14 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements * @see #findFirstCompletelyVisibleItemPosition() */ public int findLastCompletelyVisibleItemPosition() { - final View child = findOneVisibleChild(getChildCount() - 1, -1, true, false); + final View child = findOneVisibleChild(getChildCount() - 1, -1, true); return child == null ? NO_POSITION : getPosition(child); } - View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible, - boolean acceptPartiallyVisible) { - ensureLayoutState(); + View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible) { final int start = mOrientationHelper.getStartAfterPadding(); final int end = mOrientationHelper.getEndAfterPadding(); final int next = toIndex > fromIndex ? 1 : -1; - View partiallyVisible = null; for (int i = fromIndex; i != toIndex; i+=next) { final View child = getChildAt(i); final int childStart = mOrientationHelper.getDecoratedStart(child); @@ -1761,15 +1583,13 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements if (completelyVisible) { if (childStart >= start && childEnd <= end) { return child; - } else if (acceptPartiallyVisible && partiallyVisible == null) { - partiallyVisible = child; } } else { return child; } } } - return partiallyVisible; + return null; } @Override @@ -1784,12 +1604,11 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements if (layoutDir == LayoutState.INVALID_LAYOUT) { return null; } - ensureLayoutState(); final View referenceChild; if (layoutDir == LayoutState.LAYOUT_START) { - referenceChild = findReferenceChildClosestToStart(recycler, state); + referenceChild = findReferenceChildClosestToStart(state); } else { - referenceChild = findReferenceChildClosestToEnd(recycler, state); + referenceChild = findReferenceChildClosestToEnd(state); } if (referenceChild == null) { if (DEBUG) { @@ -1801,7 +1620,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements ensureLayoutState(); final int maxScroll = (int) (MAX_SCROLL_FACTOR * mOrientationHelper.getTotalSpace()); updateLayoutState(layoutDir, maxScroll, false, state); - mLayoutState.mScrollingOffset = LayoutState.SCROLLING_OFFSET_NaN; + mLayoutState.mScrollingOffset = LayoutState.SCOLLING_OFFSET_NaN; mLayoutState.mRecycle = false; fill(recycler, mLayoutState, state, true); final View nextFocus; @@ -1885,47 +1704,13 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements return mPendingSavedState == null && mLastStackFromEnd == mStackFromEnd; } - /** - * @hide This method should be called by ItemTouchHelper only. - */ - @Override - public void prepareForDrop(View view, View target, int x, int y) { - assertNotInLayoutOrScroll("Cannot drop a view during a scroll or layout calculation"); - ensureLayoutState(); - resolveShouldLayoutReverse(); - final int myPos = getPosition(view); - final int targetPos = getPosition(target); - final int dropDirection = myPos < targetPos ? LayoutState.ITEM_DIRECTION_TAIL : - LayoutState.ITEM_DIRECTION_HEAD; - if (mShouldReverseLayout) { - if (dropDirection == LayoutState.ITEM_DIRECTION_TAIL) { - scrollToPositionWithOffset(targetPos, - mOrientationHelper.getEndAfterPadding() - - (mOrientationHelper.getDecoratedStart(target) + - mOrientationHelper.getDecoratedMeasurement(view))); - } else { - scrollToPositionWithOffset(targetPos, - mOrientationHelper.getEndAfterPadding() - - mOrientationHelper.getDecoratedEnd(target)); - } - } else { - if (dropDirection == LayoutState.ITEM_DIRECTION_HEAD) { - scrollToPositionWithOffset(targetPos, mOrientationHelper.getDecoratedStart(target)); - } else { - scrollToPositionWithOffset(targetPos, - mOrientationHelper.getDecoratedEnd(target) - - mOrientationHelper.getDecoratedMeasurement(view)); - } - } - } - /** * Helper class that keeps temporary state while {LayoutManager} is filling out the empty * space. */ static class LayoutState { - final static String TAG = "LLM#LayoutState"; + final static String TAG = "LinearLayoutManager#LayoutState"; final static int LAYOUT_START = -1; @@ -1937,7 +1722,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements final static int ITEM_DIRECTION_TAIL = 1; - final static int SCROLLING_OFFSET_NaN = Integer.MIN_VALUE; + final static int SCOLLING_OFFSET_NaN = Integer.MIN_VALUE; /** * We may not want to recycle children in some cases (e.g. layout) @@ -1992,23 +1777,12 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements */ boolean mIsPreLayout = false; - /** - * The most recent {@link #scrollBy(int, RecyclerView.Recycler, RecyclerView.State)} - * amount. - */ - int mLastScrollDelta; - /** * When LLM needs to layout particular views, it sets this list in which case, LayoutState * will only return views from this list and return null if it cannot find an item. */ List mScrapList = null; - /** - * Used when there is no limit in how many views can be laid out. - */ - boolean mInfinite; - /** * @return true if there are more items in the data adapter */ @@ -2024,7 +1798,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements */ View next(RecyclerView.Recycler recycler) { if (mScrapList != null) { - return nextViewFromScrapList(); + return nextFromLimitedList(); } final View view = recycler.getViewForPosition(mCurrentPosition); mCurrentPosition += mItemDirection; @@ -2032,69 +1806,41 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } /** - * Returns the next item from the scrap list. + * Returns next item from limited list. *

      * Upon finding a valid VH, sets current item position to VH.itemPosition + mItemDirection * * @return View if an item in the current position or direction exists if not null. */ - private View nextViewFromScrapList() { - final int size = mScrapList.size(); - for (int i = 0; i < size; i++) { - final View view = mScrapList.get(i).itemView; - final LayoutParams lp = (LayoutParams) view.getLayoutParams(); - if (lp.isItemRemoved()) { - continue; - } - if (mCurrentPosition == lp.getViewLayoutPosition()) { - assignPositionFromScrapList(view); - return view; - } - } - return null; - } - - public void assignPositionFromScrapList() { - assignPositionFromScrapList(null); - } - - public void assignPositionFromScrapList(View ignore) { - final View closest = nextViewInLimitedList(ignore); - if (closest == null) { - mCurrentPosition = NO_POSITION; - } else { - mCurrentPosition = ((LayoutParams) closest.getLayoutParams()) - .getViewLayoutPosition(); - } - } - - public View nextViewInLimitedList(View ignore) { + private View nextFromLimitedList() { int size = mScrapList.size(); - View closest = null; + RecyclerView.ViewHolder closest = null; int closestDistance = Integer.MAX_VALUE; - if (DEBUG && mIsPreLayout) { - throw new IllegalStateException("Scrap list cannot be used in pre layout"); - } for (int i = 0; i < size; i++) { - View view = mScrapList.get(i).itemView; - final LayoutParams lp = (LayoutParams) view.getLayoutParams(); - if (view == ignore || lp.isItemRemoved()) { + RecyclerView.ViewHolder viewHolder = mScrapList.get(i); + if (!mIsPreLayout && viewHolder.isRemoved()) { continue; } - final int distance = (lp.getViewLayoutPosition() - mCurrentPosition) * - mItemDirection; + final int distance = (viewHolder.getPosition() - mCurrentPosition) * mItemDirection; if (distance < 0) { continue; // item is not in current direction } if (distance < closestDistance) { - closest = view; + closest = viewHolder; closestDistance = distance; if (distance == 0) { break; } } } - return closest; + if (DEBUG) { + Log.d(TAG, "layout from scrap. found view:?" + (closest != null)); + } + if (closest != null) { + mCurrentPosition = closest.getPosition() + mItemDirection; + return closest.itemView; + } + return null; } void log() { @@ -2103,10 +1849,7 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements } } - /** - * @hide - */ - public static class SavedState implements Parcelable { + static class SavedState implements Parcelable { int mAnchorPosition; @@ -2150,8 +1893,8 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements dest.writeInt(mAnchorLayoutFromEnd ? 1 : 0); } - public static final Parcelable.Creator CREATOR - = new Parcelable.Creator() { + public static final Creator CREATOR + = new Creator() { @Override public SavedState createFromParcel(Parcel in) { return new SavedState(in); @@ -2171,17 +1914,10 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements int mPosition; int mCoordinate; boolean mLayoutFromEnd; - boolean mValid; - - AnchorInfo() { - reset(); - } - void reset() { mPosition = NO_POSITION; mCoordinate = INVALID_OFFSET; mLayoutFromEnd = false; - mValid = false; } /** @@ -2200,61 +1936,21 @@ public class LinearLayoutManager extends RecyclerView.LayoutManager implements "mPosition=" + mPosition + ", mCoordinate=" + mCoordinate + ", mLayoutFromEnd=" + mLayoutFromEnd + - ", mValid=" + mValid + '}'; } - private boolean isViewValidAsAnchor(View child, RecyclerView.State state) { - LayoutParams lp = (LayoutParams) child.getLayoutParams(); - return !lp.isItemRemoved() && lp.getViewLayoutPosition() >= 0 - && lp.getViewLayoutPosition() < state.getItemCount(); - } - - public void assignFromViewAndKeepVisibleRect(View child) { - final int spaceChange = mOrientationHelper.getTotalSpaceChange(); - if (spaceChange >= 0) { + /** + * Assign anchor position information from the provided view if it is valid as a reference + * child. + */ + public boolean assignFromViewIfValid(View child, RecyclerView.State state) { + RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams(); + if (!lp.isItemRemoved() && lp.getViewPosition() >= 0 + && lp.getViewPosition() < state.getItemCount()) { assignFromView(child); - return; - } - mPosition = getPosition(child); - if (mLayoutFromEnd) { - final int prevLayoutEnd = mOrientationHelper.getEndAfterPadding() - spaceChange; - final int childEnd = mOrientationHelper.getDecoratedEnd(child); - final int previousEndMargin = prevLayoutEnd - childEnd; - mCoordinate = mOrientationHelper.getEndAfterPadding() - previousEndMargin; - // ensure we did not push child's top out of bounds because of this - if (previousEndMargin > 0) {// we have room to shift bottom if necessary - final int childSize = mOrientationHelper.getDecoratedMeasurement(child); - final int estimatedChildStart = mCoordinate - childSize; - final int layoutStart = mOrientationHelper.getStartAfterPadding(); - final int previousStartMargin = mOrientationHelper.getDecoratedStart(child) - - layoutStart; - final int startReference = layoutStart + Math.min(previousStartMargin, 0); - final int startMargin = estimatedChildStart - startReference; - if (startMargin < 0) { - // offset to make top visible but not too much - mCoordinate += Math.min(previousEndMargin, -startMargin); - } - } - } else { - final int childStart = mOrientationHelper.getDecoratedStart(child); - final int startMargin = childStart - mOrientationHelper.getStartAfterPadding(); - mCoordinate = childStart; - if (startMargin > 0) { // we have room to fix end as well - final int estimatedEnd = childStart + - mOrientationHelper.getDecoratedMeasurement(child); - final int previousLayoutEnd = mOrientationHelper.getEndAfterPadding() - - spaceChange; - final int previousEndMargin = previousLayoutEnd - - mOrientationHelper.getDecoratedEnd(child); - final int endReference = mOrientationHelper.getEndAfterPadding() - - Math.min(0, previousEndMargin); - final int endMargin = endReference - estimatedEnd; - if (endMargin < 0) { - mCoordinate -= Math.min(startMargin, -endMargin); - } - } + return true; } + return false; } public void assignFromView(View child) { diff --git a/app/src/main/java/android/support/v7/widget/LinearSmoothScroller.java b/app/src/main/java/android/support/v7/widget/LinearSmoothScroller.java index 78250c1496..bdf2803d73 100644 --- a/app/src/main/java/android/support/v7/widget/LinearSmoothScroller.java +++ b/app/src/main/java/android/support/v7/widget/LinearSmoothScroller.java @@ -18,7 +18,6 @@ package android.support.v7.widget; import android.content.Context; import android.graphics.PointF; -import android.support.annotation.Nullable; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; @@ -26,16 +25,12 @@ import android.view.animation.DecelerateInterpolator; import android.view.animation.LinearInterpolator; /** - * {@link RecyclerView.SmoothScroller} implementation which uses a {@link LinearInterpolator} until - * the target position becomes a child of the RecyclerView and then uses a + * {@link RecyclerView.SmoothScroller} implementation which uses + * {@link LinearInterpolator} until the target position becames a child of + * the RecyclerView and then uses * {@link DecelerateInterpolator} to slowly approach to target position. - *

      - * If the {@link RecyclerView.LayoutManager} you are using does not implement the - * {@link RecyclerView.SmoothScroller.ScrollVectorProvider} interface, then you must override the - * {@link #computeScrollVectorForPosition(int)} method. All the LayoutManagers bundled with - * the support library implement this interface. */ -public class LinearSmoothScroller extends RecyclerView.SmoothScroller { +abstract public class LinearSmoothScroller extends RecyclerView.SmoothScroller { private static final String TAG = "LinearSmoothScroller"; @@ -49,8 +44,8 @@ public class LinearSmoothScroller extends RecyclerView.SmoothScroller { * Align child view's left or top with parent view's left or top * * @see #calculateDtToFit(int, int, int, int, int) - * @see #calculateDxToMakeVisible(android.view.View, int) - * @see #calculateDyToMakeVisible(android.view.View, int) + * @see #calculateDxToMakeVisible(View, int) + * @see #calculateDyToMakeVisible(View, int) */ public static final int SNAP_TO_START = -1; @@ -58,8 +53,8 @@ public class LinearSmoothScroller extends RecyclerView.SmoothScroller { * Align child view's right or bottom with parent view's right or bottom * * @see #calculateDtToFit(int, int, int, int, int) - * @see #calculateDxToMakeVisible(android.view.View, int) - * @see #calculateDyToMakeVisible(android.view.View, int) + * @see #calculateDxToMakeVisible(View, int) + * @see #calculateDyToMakeVisible(View, int) */ public static final int SNAP_TO_END = 1; @@ -70,8 +65,8 @@ public class LinearSmoothScroller extends RecyclerView.SmoothScroller { * {@code SNAP_TO_ANY} is the same as using {@code SNAP_TO_START}

      * * @see #calculateDtToFit(int, int, int, int, int) - * @see #calculateDxToMakeVisible(android.view.View, int) - * @see #calculateDyToMakeVisible(android.view.View, int) + * @see #calculateDxToMakeVisible(View, int) + * @see #calculateDyToMakeVisible(View, int) */ public static final int SNAP_TO_ANY = 0; @@ -127,7 +122,6 @@ public class LinearSmoothScroller extends RecyclerView.SmoothScroller { stop(); return; } - //noinspection PointlessBooleanExpression if (DEBUG && mTargetVector != null && ((mTargetVector.x * dx < 0 || mTargetVector.y * dy < 0))) { throw new IllegalStateException("Scroll happened in the opposite direction" @@ -184,7 +178,7 @@ public class LinearSmoothScroller extends RecyclerView.SmoothScroller { * * @param dx Distance in pixels that we want to scroll * @return Time in milliseconds - * @see #calculateSpeedPerPixel(android.util.DisplayMetrics) + * @see #calculateSpeedPerPixel(DisplayMetrics) */ protected int calculateTimeForScrolling(int dx) { // In a case where dx is very small, rounding may return 0 although dx > 0. @@ -231,9 +225,12 @@ public class LinearSmoothScroller extends RecyclerView.SmoothScroller { // find an interim target position PointF scrollVector = computeScrollVectorForPosition(getTargetPosition()); if (scrollVector == null || (scrollVector.x == 0 && scrollVector.y == 0)) { + Log.e(TAG, "To support smooth scrolling, you should override \n" + + "LayoutManager#computeScrollVectorForPosition.\n" + + "Falling back to instant scroll"); final int target = getTargetPosition(); - action.jumpTo(target); stop(); + instantScrollToPosition(target); return; } normalize(scrollVector); @@ -260,8 +257,8 @@ public class LinearSmoothScroller extends RecyclerView.SmoothScroller { } /** - * Helper method for {@link #calculateDxToMakeVisible(android.view.View, int)} and - * {@link #calculateDyToMakeVisible(android.view.View, int)} + * Helper method for {@link #calculateDxToMakeVisible(View, int)} and + * {@link #calculateDyToMakeVisible(View, int)} */ public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) { @@ -294,13 +291,13 @@ public class LinearSmoothScroller extends RecyclerView.SmoothScroller { * @param view The view which we want to make fully visible * @param snapPreference The edge which the view should snap to when entering the visible * area. One of {@link #SNAP_TO_START}, {@link #SNAP_TO_END} or - * {@link #SNAP_TO_ANY}. + * {@link #SNAP_TO_END}. * @return The vertical scroll amount necessary to make the view visible with the given * snap preference. */ public int calculateDyToMakeVisible(View view, int snapPreference) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); - if (layoutManager == null || !layoutManager.canScrollVertically()) { + if (!layoutManager.canScrollVertically()) { return 0; } final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) @@ -325,7 +322,7 @@ public class LinearSmoothScroller extends RecyclerView.SmoothScroller { */ public int calculateDxToMakeVisible(View view, int snapPreference) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); - if (layoutManager == null || !layoutManager.canScrollHorizontally()) { + if (!layoutManager.canScrollHorizontally()) { return 0; } final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) @@ -337,25 +334,5 @@ public class LinearSmoothScroller extends RecyclerView.SmoothScroller { return calculateDtToFit(left, right, start, end, snapPreference); } - /** - * Compute the scroll vector for a given target position. - *

      - * This method can return null if the layout manager cannot calculate a scroll vector - * for the given position (e.g. it has no current scroll position). - * - * @param targetPosition the position to which the scroller is scrolling - * - * @return the scroll vector for a given target position - */ - @Nullable - public PointF computeScrollVectorForPosition(int targetPosition) { - RecyclerView.LayoutManager layoutManager = getLayoutManager(); - if (layoutManager instanceof ScrollVectorProvider) { - return ((ScrollVectorProvider) layoutManager) - .computeScrollVectorForPosition(targetPosition); - } - Log.w(TAG, "You should override computeScrollVectorForPosition when the LayoutManager" + - " does not implement " + ScrollVectorProvider.class.getCanonicalName()); - return null; - } + abstract public PointF computeScrollVectorForPosition(int targetPosition); } diff --git a/app/src/main/java/android/support/v7/widget/LinearSnapHelper.java b/app/src/main/java/android/support/v7/widget/LinearSnapHelper.java deleted file mode 100644 index 4b37c685bb..0000000000 --- a/app/src/main/java/android/support/v7/widget/LinearSnapHelper.java +++ /dev/null @@ -1,286 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific languag`e governing permissions and - * limitations under the License. - */ - -package android.support.v7.widget; - -import android.graphics.PointF; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.view.View; - -/** - * Implementation of the {@link SnapHelper} supporting snapping in either vertical or horizontal - * orientation. - *

      - * The implementation will snap the center of the target child view to the center of - * the attached {@link RecyclerView}. If you intend to change this behavior then override - * {@link SnapHelper#calculateDistanceToFinalSnap}. - */ -public class LinearSnapHelper extends SnapHelper { - - private static final float INVALID_DISTANCE = 1f; - - // Orientation helpers are lazily created per LayoutManager. - @Nullable - private OrientationHelper mVerticalHelper; - @Nullable - private OrientationHelper mHorizontalHelper; - - @Override - public int[] calculateDistanceToFinalSnap( - @NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) { - int[] out = new int[2]; - if (layoutManager.canScrollHorizontally()) { - out[0] = distanceToCenter(layoutManager, targetView, - getHorizontalHelper(layoutManager)); - } else { - out[0] = 0; - } - - if (layoutManager.canScrollVertically()) { - out[1] = distanceToCenter(layoutManager, targetView, - getVerticalHelper(layoutManager)); - } else { - out[1] = 0; - } - return out; - } - - @Override - public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, - int velocityY) { - if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) { - return RecyclerView.NO_POSITION; - } - - final int itemCount = layoutManager.getItemCount(); - if (itemCount == 0) { - return RecyclerView.NO_POSITION; - } - - final View currentView = findSnapView(layoutManager); - if (currentView == null) { - return RecyclerView.NO_POSITION; - } - - final int currentPosition = layoutManager.getPosition(currentView); - if (currentPosition == RecyclerView.NO_POSITION) { - return RecyclerView.NO_POSITION; - } - - RecyclerView.SmoothScroller.ScrollVectorProvider vectorProvider = - (RecyclerView.SmoothScroller.ScrollVectorProvider) layoutManager; - // deltaJumps sign comes from the velocity which may not match the order of children in - // the LayoutManager. To overcome this, we ask for a vector from the LayoutManager to - // get the direction. - PointF vectorForEnd = vectorProvider.computeScrollVectorForPosition(itemCount - 1); - if (vectorForEnd == null) { - // cannot get a vector for the given position. - return RecyclerView.NO_POSITION; - } - - int vDeltaJump, hDeltaJump; - if (layoutManager.canScrollHorizontally()) { - hDeltaJump = estimateNextPositionDiffForFling(layoutManager, - getHorizontalHelper(layoutManager), velocityX, 0); - if (vectorForEnd.x < 0) { - hDeltaJump = -hDeltaJump; - } - } else { - hDeltaJump = 0; - } - if (layoutManager.canScrollVertically()) { - vDeltaJump = estimateNextPositionDiffForFling(layoutManager, - getVerticalHelper(layoutManager), 0, velocityY); - if (vectorForEnd.y < 0) { - vDeltaJump = -vDeltaJump; - } - } else { - vDeltaJump = 0; - } - - int deltaJump = layoutManager.canScrollVertically() ? vDeltaJump : hDeltaJump; - if (deltaJump == 0) { - return RecyclerView.NO_POSITION; - } - - int targetPos = currentPosition + deltaJump; - if (targetPos < 0) { - targetPos = 0; - } - if (targetPos >= itemCount) { - targetPos = itemCount - 1; - } - return targetPos; - } - - @Override - public View findSnapView(RecyclerView.LayoutManager layoutManager) { - if (layoutManager.canScrollVertically()) { - return findCenterView(layoutManager, getVerticalHelper(layoutManager)); - } else if (layoutManager.canScrollHorizontally()) { - return findCenterView(layoutManager, getHorizontalHelper(layoutManager)); - } - return null; - } - - private int distanceToCenter(@NonNull RecyclerView.LayoutManager layoutManager, - @NonNull View targetView, OrientationHelper helper) { - final int childCenter = helper.getDecoratedStart(targetView) + - (helper.getDecoratedMeasurement(targetView) / 2); - final int containerCenter; - if (layoutManager.getClipToPadding()) { - containerCenter = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; - } else { - containerCenter = helper.getEnd() / 2; - } - return childCenter - containerCenter; - } - - /** - * Estimates a position to which SnapHelper will try to scroll to in response to a fling. - * - * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached - * {@link RecyclerView}. - * @param helper The {@link OrientationHelper} that is created from the LayoutManager. - * @param velocityX The velocity on the x axis. - * @param velocityY The velocity on the y axis. - * - * @return The diff between the target scroll position and the current position. - */ - private int estimateNextPositionDiffForFling(RecyclerView.LayoutManager layoutManager, - OrientationHelper helper, int velocityX, int velocityY) { - int[] distances = calculateScrollDistance(velocityX, velocityY); - float distancePerChild = computeDistancePerChild(layoutManager, helper); - if (distancePerChild <= 0) { - return 0; - } - int distance = - Math.abs(distances[0]) > Math.abs(distances[1]) ? distances[0] : distances[1]; - return (int) Math.floor(distance / distancePerChild); - } - - /** - * Return the child view that is currently closest to the center of this parent. - * - * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached - * {@link RecyclerView}. - * @param helper The relevant {@link OrientationHelper} for the attached {@link RecyclerView}. - * - * @return the child view that is currently closest to the center of this parent. - */ - @Nullable - private View findCenterView(RecyclerView.LayoutManager layoutManager, - OrientationHelper helper) { - int childCount = layoutManager.getChildCount(); - if (childCount == 0) { - return null; - } - - View closestChild = null; - final int center; - if (layoutManager.getClipToPadding()) { - center = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; - } else { - center = helper.getEnd() / 2; - } - int absClosest = Integer.MAX_VALUE; - - for (int i = 0; i < childCount; i++) { - final View child = layoutManager.getChildAt(i); - int childCenter = helper.getDecoratedStart(child) + - (helper.getDecoratedMeasurement(child) / 2); - int absDistance = Math.abs(childCenter - center); - - /** if child center is closer than previous closest, set it as closest **/ - if (absDistance < absClosest) { - absClosest = absDistance; - closestChild = child; - } - } - return closestChild; - } - - /** - * Computes an average pixel value to pass a single child. - *

      - * Returns a negative value if it cannot be calculated. - * - * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached - * {@link RecyclerView}. - * @param helper The relevant {@link OrientationHelper} for the attached - * {@link RecyclerView.LayoutManager}. - * - * @return A float value that is the average number of pixels needed to scroll by one view in - * the relevant direction. - */ - private float computeDistancePerChild(RecyclerView.LayoutManager layoutManager, - OrientationHelper helper) { - View minPosView = null; - View maxPosView = null; - int minPos = Integer.MAX_VALUE; - int maxPos = Integer.MIN_VALUE; - int childCount = layoutManager.getChildCount(); - if (childCount == 0) { - return INVALID_DISTANCE; - } - - for (int i = 0; i < childCount; i++) { - View child = layoutManager.getChildAt(i); - final int pos = layoutManager.getPosition(child); - if (pos == RecyclerView.NO_POSITION) { - continue; - } - if (pos < minPos) { - minPos = pos; - minPosView = child; - } - if (pos > maxPos) { - maxPos = pos; - maxPosView = child; - } - } - if (minPosView == null || maxPosView == null) { - return INVALID_DISTANCE; - } - int start = Math.min(helper.getDecoratedStart(minPosView), - helper.getDecoratedStart(maxPosView)); - int end = Math.max(helper.getDecoratedEnd(minPosView), - helper.getDecoratedEnd(maxPosView)); - int distance = end - start; - if (distance == 0) { - return INVALID_DISTANCE; - } - return 1f * distance / ((maxPos - minPos) + 1); - } - - @NonNull - private OrientationHelper getVerticalHelper(@NonNull RecyclerView.LayoutManager layoutManager) { - if (mVerticalHelper == null || mVerticalHelper.mLayoutManager != layoutManager) { - mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager); - } - return mVerticalHelper; - } - - @NonNull - private OrientationHelper getHorizontalHelper( - @NonNull RecyclerView.LayoutManager layoutManager) { - if (mHorizontalHelper == null || mHorizontalHelper.mLayoutManager != layoutManager) { - mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager); - } - return mHorizontalHelper; - } -} diff --git a/app/src/main/java/android/support/v7/widget/OpReorderer.java b/app/src/main/java/android/support/v7/widget/OpReorderer.java index db01a0cffc..1a34cba3e2 100644 --- a/app/src/main/java/android/support/v7/widget/OpReorderer.java +++ b/app/src/main/java/android/support/v7/widget/OpReorderer.java @@ -16,9 +16,10 @@ package android.support.v7.widget; +import android.support.v7.widget.AdapterHelper.UpdateOp; + import java.util.List; -import android.support.v7.widget.AdapterHelper.UpdateOp; import static android.support.v7.widget.AdapterHelper.UpdateOp.ADD; import static android.support.v7.widget.AdapterHelper.UpdateOp.MOVE; import static android.support.v7.widget.AdapterHelper.UpdateOp.REMOVE; @@ -100,7 +101,7 @@ class OpReorderer { } else if (moveOp.positionStart < removeOp.positionStart + removeOp.itemCount) { final int remaining = removeOp.positionStart + removeOp.itemCount - moveOp.positionStart; - extraRm = mCallback.obtainUpdateOp(REMOVE, moveOp.positionStart + 1, remaining, null); + extraRm = mCallback.obtainUpdateOp(REMOVE, moveOp.positionStart + 1, remaining); removeOp.itemCount = moveOp.positionStart - removeOp.positionStart; } @@ -187,7 +188,7 @@ class OpReorderer { } else if (moveOp.itemCount < updateOp.positionStart + updateOp.itemCount) { // moved item is updated. add an update for it updateOp.itemCount--; - extraUp1 = mCallback.obtainUpdateOp(UPDATE, moveOp.positionStart, 1, updateOp.payload); + extraUp1 = mCallback.obtainUpdateOp(UPDATE, moveOp.positionStart, 1); } // now affect of add is consumed. now apply effect of first remove if (moveOp.positionStart <= updateOp.positionStart) { @@ -195,8 +196,7 @@ class OpReorderer { } else if (moveOp.positionStart < updateOp.positionStart + updateOp.itemCount) { final int remaining = updateOp.positionStart + updateOp.itemCount - moveOp.positionStart; - extraUp2 = mCallback.obtainUpdateOp(UPDATE, moveOp.positionStart + 1, remaining, - updateOp.payload); + extraUp2 = mCallback.obtainUpdateOp(UPDATE, moveOp.positionStart + 1, remaining); updateOp.itemCount -= remaining; } list.set(update, moveOp); @@ -231,7 +231,7 @@ class OpReorderer { static interface Callback { - UpdateOp obtainUpdateOp(int cmd, int startPosition, int itemCount, Object payload); + UpdateOp obtainUpdateOp(int cmd, int startPosition, int itemCount); void recycleUpdateOp(UpdateOp op); } diff --git a/app/src/main/java/android/support/v7/widget/OrientationHelper.java b/app/src/main/java/android/support/v7/widget/OrientationHelper.java index 8987b9cc8b..a678d86de0 100644 --- a/app/src/main/java/android/support/v7/widget/OrientationHelper.java +++ b/app/src/main/java/android/support/v7/widget/OrientationHelper.java @@ -16,7 +16,6 @@ package android.support.v7.widget; -import android.graphics.Rect; import android.view.View; import android.widget.LinearLayout; @@ -42,8 +41,6 @@ public abstract class OrientationHelper { private int mLastTotalSpace = INVALID_SIZE; - final Rect mTmpRect = new Rect(); - private OrientationHelper(RecyclerView.LayoutManager layoutManager) { mLayoutManager = layoutManager; } @@ -79,7 +76,7 @@ public abstract class OrientationHelper { * * @param view The view element to check * @return The first pixel of the element - * @see #getDecoratedEnd(android.view.View) + * @see #getDecoratedEnd(View) */ public abstract int getDecoratedStart(View view); @@ -91,42 +88,10 @@ public abstract class OrientationHelper { * * @param view The view element to check * @return The last pixel of the element - * @see #getDecoratedStart(android.view.View) + * @see #getDecoratedStart(View) */ public abstract int getDecoratedEnd(View view); - /** - * Returns the end of the View after its matrix transformations are applied to its layout - * position. - *

      - * This method is useful when trying to detect the visible edge of a View. - *

      - * It includes the decorations but does not include the margins. - * - * @param view The view whose transformed end will be returned - * @return The end of the View after its decor insets and transformation matrix is applied to - * its position - * - * @see RecyclerView.LayoutManager#getTransformedBoundingBox(View, boolean, Rect) - */ - public abstract int getTransformedEndWithDecoration(View view); - - /** - * Returns the start of the View after its matrix transformations are applied to its layout - * position. - *

      - * This method is useful when trying to detect the visible edge of a View. - *

      - * It includes the decorations but does not include the margins. - * - * @param view The view whose transformed start will be returned - * @return The start of the View after its decor insets and transformation matrix is applied to - * its position - * - * @see RecyclerView.LayoutManager#getTransformedBoundingBox(View, boolean, Rect) - */ - public abstract int getTransformedStartWithDecoration(View view); - /** * Returns the space occupied by this View in the current orientation including decorations and * margins. @@ -200,28 +165,6 @@ public abstract class OrientationHelper { */ public abstract int getEndPadding(); - /** - * Returns the MeasureSpec mode for the current orientation from the LayoutManager. - * - * @return The current measure spec mode. - * - * @see View.MeasureSpec - * @see RecyclerView.LayoutManager#getWidthMode() - * @see RecyclerView.LayoutManager#getHeightMode() - */ - public abstract int getMode(); - - /** - * Returns the MeasureSpec mode for the perpendicular orientation from the LayoutManager. - * - * @return The current measure spec mode. - * - * @see View.MeasureSpec - * @see RecyclerView.LayoutManager#getWidthMode() - * @see RecyclerView.LayoutManager#getHeightMode() - */ - public abstract int getModeInOther(); - /** * Creates an OrientationHelper for the given LayoutManager and orientation. * @@ -299,18 +242,6 @@ public abstract class OrientationHelper { return mLayoutManager.getDecoratedLeft(view) - params.leftMargin; } - @Override - public int getTransformedEndWithDecoration(View view) { - mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect); - return mTmpRect.right; - } - - @Override - public int getTransformedStartWithDecoration(View view) { - mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect); - return mTmpRect.left; - } - @Override public int getTotalSpace() { return mLayoutManager.getWidth() - mLayoutManager.getPaddingLeft() @@ -326,16 +257,6 @@ public abstract class OrientationHelper { public int getEndPadding() { return mLayoutManager.getPaddingRight(); } - - @Override - public int getMode() { - return mLayoutManager.getWidthMode(); - } - - @Override - public int getModeInOther() { - return mLayoutManager.getHeightMode(); - } }; } @@ -397,18 +318,6 @@ public abstract class OrientationHelper { return mLayoutManager.getDecoratedTop(view) - params.topMargin; } - @Override - public int getTransformedEndWithDecoration(View view) { - mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect); - return mTmpRect.bottom; - } - - @Override - public int getTransformedStartWithDecoration(View view) { - mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect); - return mTmpRect.top; - } - @Override public int getTotalSpace() { return mLayoutManager.getHeight() - mLayoutManager.getPaddingTop() @@ -424,16 +333,6 @@ public abstract class OrientationHelper { public int getEndPadding() { return mLayoutManager.getPaddingBottom(); } - - @Override - public int getMode() { - return mLayoutManager.getHeightMode(); - } - - @Override - public int getModeInOther() { - return mLayoutManager.getWidthMode(); - } }; } } \ No newline at end of file diff --git a/app/src/main/java/android/support/v7/widget/RecyclerView.java b/app/src/main/java/android/support/v7/widget/RecyclerView.java index 87ebb16489..2bfa3cee6d 100644 --- a/app/src/main/java/android/support/v7/widget/RecyclerView.java +++ b/app/src/main/java/android/support/v7/widget/RecyclerView.java @@ -18,32 +18,17 @@ package android.support.v7.widget; import android.content.Context; -import android.content.res.TypedArray; import android.database.Observable; import android.graphics.Canvas; -import android.graphics.Matrix; import android.graphics.PointF; import android.graphics.Rect; -import android.graphics.RectF; import android.os.Build; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; -import android.os.SystemClock; -import android.support.annotation.CallSuper; -import android.support.annotation.IntDef; -import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.annotation.VisibleForTesting; -import android.support.v4.os.ParcelableCompat; -import android.support.v4.os.ParcelableCompatCreatorCallbacks; -import android.support.v4.os.TraceCompat; -import android.support.v4.view.AbsSavedState; -import android.support.v4.view.InputDeviceCompat; +import android.support.v4.util.ArrayMap; import android.support.v4.view.MotionEventCompat; -import android.support.v4.view.NestedScrollingChild; -import android.support.v4.view.NestedScrollingChildHelper; -import android.support.v4.view.ScrollingView; import android.support.v4.view.VelocityTrackerCompat; import android.support.v4.view.ViewCompat; import android.support.v4.view.accessibility.AccessibilityEventCompat; @@ -51,13 +36,10 @@ import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; import android.support.v4.view.accessibility.AccessibilityRecordCompat; import android.support.v4.widget.EdgeEffectCompat; import android.support.v4.widget.ScrollerCompat; -import android.support.v7.recyclerview.R; -import android.support.v7.widget.RecyclerView.ItemAnimator.ItemHolderInfo; import android.util.AttributeSet; import android.util.Log; import android.util.SparseArray; import android.util.SparseIntArray; -import android.util.TypedValue; import android.view.FocusFinder; import android.view.MotionEvent; import android.view.VelocityTracker; @@ -69,10 +51,6 @@ import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityManager; import android.view.animation.Interpolator; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -104,77 +82,23 @@ import static android.support.v7.widget.AdapterHelper.UpdateOp; *

    • Dirty (view): A child view that must be rebound by the adapter before * being displayed.
    • * - * - *

      Positions in RecyclerView:

      - *

      - * RecyclerView introduces an additional level of abstraction between the {@link Adapter} and - * {@link LayoutManager} to be able to detect data set changes in batches during a layout - * calculation. This saves LayoutManager from tracking adapter changes to calculate animations. - * It also helps with performance because all view bindings happen at the same time and unnecessary - * bindings are avoided. - *

      - * For this reason, there are two types of position related methods in RecyclerView: - *

        - *
      • layout position: Position of an item in the latest layout calculation. This is the - * position from the LayoutManager's perspective.
      • - *
      • adapter position: Position of an item in the adapter. This is the position from - * the Adapter's perspective.
      • - *
      - *

      - * These two positions are the same except the time between dispatching adapter.notify* - * events and calculating the updated layout. - *

      - * Methods that return or receive *LayoutPosition* use position as of the latest - * layout calculation (e.g. {@link ViewHolder#getLayoutPosition()}, - * {@link #findViewHolderForLayoutPosition(int)}). These positions include all changes until the - * last layout calculation. You can rely on these positions to be consistent with what user is - * currently seeing on the screen. For example, if you have a list of items on the screen and user - * asks for the 5th element, you should use these methods as they'll match what user - * is seeing. - *

      - * The other set of position related methods are in the form of - * *AdapterPosition*. (e.g. {@link ViewHolder#getAdapterPosition()}, - * {@link #findViewHolderForAdapterPosition(int)}) You should use these methods when you need to - * work with up-to-date adapter positions even if they may not have been reflected to layout yet. - * For example, if you want to access the item in the adapter on a ViewHolder click, you should use - * {@link ViewHolder#getAdapterPosition()}. Beware that these methods may not be able to calculate - * adapter positions if {@link Adapter#notifyDataSetChanged()} has been called and new layout has - * not yet been calculated. For this reasons, you should carefully handle {@link #NO_POSITION} or - * null results from these methods. - *

      - * When writing a {@link LayoutManager} you almost always want to use layout positions whereas when - * writing an {@link Adapter}, you probably want to use adapter positions. - * - * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_layoutManager */ -public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild { - +public class RecyclerView extends ViewGroup { private static final String TAG = "RecyclerView"; private static final boolean DEBUG = false; - private static final int[] NESTED_SCROLLING_ATTRS - = {16843830 /* android.R.attr.nestedScrollingEnabled */}; - - private static final int[] CLIP_TO_PADDING_ATTR = {android.R.attr.clipToPadding}; - /** - * On Kitkat and JB MR2, there is a bug which prevents DisplayList from being invalidated if - * a View is two levels deep(wrt to ViewHolder.itemView). DisplayList can be invalidated by - * setting View's visibility to INVISIBLE when View is detached. On Kitkat and JB MR2, Recycler - * recursively traverses itemView and invalidates display list for each ViewGroup that matches - * this criteria. + * On Kitkat, there is a bug which prevents DisplayList from being invalidated if a View is two + * levels deep(wrt to ViewHolder.itemView). DisplayList can be invalidated by setting + * View's visibility to INVISIBLE when View is detached. On Kitkat, Recycler recursively + * traverses itemView and invalidates display list for each ViewGroup that matches this + * criteria. */ - private static final boolean FORCE_INVALIDATE_DISPLAY_LIST = Build.VERSION.SDK_INT == 18 - || Build.VERSION.SDK_INT == 19 || Build.VERSION.SDK_INT == 20; - /** - * On M+, an unspecified measure spec may include a hint which we can use. On older platforms, - * this value might be garbage. To save LayoutManagers from it, RecyclerView sets the size to - * 0 when mode is unspecified. - */ - static final boolean ALLOW_SIZE_IN_UNSPECIFIED_SPEC = Build.VERSION.SDK_INT >= 23; + private static final boolean FORCE_INVALIDATE_DISPLAY_LIST = Build.VERSION.SDK_INT == 19 || + Build.VERSION.SDK_INT == 20; - static final boolean DISPATCH_TEMP_DETACH = false; + private static final boolean DISPATCH_TEMP_DETACH = false; public static final int HORIZONTAL = 0; public static final int VERTICAL = 1; @@ -182,97 +106,20 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro public static final long NO_ID = -1; public static final int INVALID_TYPE = -1; - /** - * Constant for use with {@link #setScrollingTouchSlop(int)}. Indicates - * that the RecyclerView should use the standard touch slop for smooth, - * continuous scrolling. - */ - public static final int TOUCH_SLOP_DEFAULT = 0; - - /** - * Constant for use with {@link #setScrollingTouchSlop(int)}. Indicates - * that the RecyclerView should use the standard touch slop for scrolling - * widgets that snap to a page or other coarse-grained barrier. - */ - public static final int TOUCH_SLOP_PAGING = 1; - private static final int MAX_SCROLL_DURATION = 2000; - /** - * RecyclerView is calculating a scroll. - * If there are too many of these in Systrace, some Views inside RecyclerView might be causing - * it. Try to avoid using EditText, focusable views or handle them with care. - */ - private static final String TRACE_SCROLL_TAG = "RV Scroll"; - - /** - * OnLayout has been called by the View system. - * If this shows up too many times in Systrace, make sure the children of RecyclerView do not - * update themselves directly. This will cause a full re-layout but when it happens via the - * Adapter notifyItemChanged, RecyclerView can avoid full layout calculation. - */ - private static final String TRACE_ON_LAYOUT_TAG = "RV OnLayout"; - - /** - * NotifyDataSetChanged or equal has been called. - * If this is taking a long time, try sending granular notify adapter changes instead of just - * calling notifyDataSetChanged or setAdapter / swapAdapter. Adding stable ids to your adapter - * might help. - */ - private static final String TRACE_ON_DATA_SET_CHANGE_LAYOUT_TAG = "RV FullInvalidate"; - - /** - * RecyclerView is doing a layout for partial adapter updates (we know what has changed) - * If this is taking a long time, you may have dispatched too many Adapter updates causing too - * many Views being rebind. Make sure all are necessary and also prefer using notify*Range - * methods. - */ - private static final String TRACE_HANDLE_ADAPTER_UPDATES_TAG = "RV PartialInvalidate"; - - /** - * RecyclerView is rebinding a View. - * If this is taking a lot of time, consider optimizing your layout or make sure you are not - * doing extra operations in onBindViewHolder call. - */ - private static final String TRACE_BIND_VIEW_TAG = "RV OnBindView"; - - /** - * RecyclerView is creating a new View. - * If too many of these present in Systrace: - * - There might be a problem in Recycling (e.g. custom Animations that set transient state and - * prevent recycling or ItemAnimator not implementing the contract properly. ({@link - * > Adapter#onFailedToRecycleView(ViewHolder)}) - * - * - There might be too many item view types. - * > Try merging them - * - * - There might be too many itemChange animations and not enough space in RecyclerPool. - * >Try increasing your pool size and item cache size. - */ - private static final String TRACE_CREATE_VIEW_TAG = "RV CreateView"; - private static final Class[] LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE = - new Class[]{Context.class, AttributeSet.class, int.class, int.class}; - private final RecyclerViewDataObserver mObserver = new RecyclerViewDataObserver(); final Recycler mRecycler = new Recycler(); private SavedState mPendingSavedState; - /** - * Handles adapter updates - */ AdapterHelper mAdapterHelper; - /** - * Handles abstraction between LayoutManager children and RecyclerView children - */ ChildHelper mChildHelper; - /** - * Keeps data about views to be used for animations - */ - final ViewInfoStore mViewInfoStore = new ViewInfoStore(); + // we use this like a set + final List mDisappearingViewsInLayoutPass = new ArrayList(); /** * Prior to L, there is no way to query this variable which is why we override the setter and @@ -287,52 +134,45 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * 3) We're attached */ private final Runnable mUpdateChildViewsRunnable = new Runnable() { - @Override public void run() { - if (!mFirstLayoutComplete || isLayoutRequested()) { + if (!mAdapterHelper.hasPendingUpdates()) { + return; + } + if (!mFirstLayoutComplete) { // a layout request will happen, we should not do layout here. return; } - if (!mIsAttached) { - requestLayout(); - // if we are not attached yet, mark us as requiring layout and skip - return; + if (mDataSetHasChangedAfterLayout) { + dispatchLayout(); + } else { + eatRequestLayout(); + mAdapterHelper.preProcess(); + if (!mLayoutRequestEaten) { + // We run this after pre-processing is complete so that ViewHolders have their + // final adapter positions. No need to run it if a layout is already requested. + rebindUpdatedViewHolders(); + } + resumeRequestLayout(true); } - if (mLayoutFrozen) { - mLayoutRequestEaten = true; - return; //we'll process updates when ice age ends. - } - consumePendingUpdateOperations(); } }; private final Rect mTempRect = new Rect(); - private final Rect mTempRect2 = new Rect(); - private final RectF mTempRectF = new RectF(); private Adapter mAdapter; - @VisibleForTesting LayoutManager mLayout; + private LayoutManager mLayout; private RecyclerListener mRecyclerListener; - private final ArrayList mItemDecorations = new ArrayList<>(); + private final ArrayList mItemDecorations = new ArrayList(); private final ArrayList mOnItemTouchListeners = - new ArrayList<>(); + new ArrayList(); private OnItemTouchListener mActiveOnItemTouchListener; private boolean mIsAttached; private boolean mHasFixedSize; - @VisibleForTesting boolean mFirstLayoutComplete; - - // Counting lock to control whether we should ignore requestLayout calls from children or not. - private int mEatRequestLayout = 0; - + private boolean mFirstLayoutComplete; + private boolean mEatRequestLayout; private boolean mLayoutRequestEaten; - private boolean mLayoutFrozen; - private boolean mIgnoreMotionEventTillDown; - - // binary OR of change events that were eaten during a layout or scroll. - private int mEatenAccessibilityChangeFlags; private boolean mAdapterUpdateDuringMeasure; private final boolean mPostUpdatesOnAnimation; private final AccessibilityManager mAccessibilityManager; - private List mOnChildAttachStateListeners; /** * Set to true when an adapter data set changed notification is received. @@ -341,23 +181,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro private boolean mDataSetHasChangedAfterLayout = false; /** - * This variable is incremented during a dispatchLayout and/or scroll. + * This variable is set to true during a dispatchLayout and/or scroll. * Some methods should not be called during these periods (e.g. adapter data change). * Doing so will create hard to find bugs so we better check it and throw an exception. * * @see #assertInLayoutOrScroll(String) * @see #assertNotInLayoutOrScroll(String) */ - private int mLayoutOrScrollCounter = 0; - - /** - * Similar to mLayoutOrScrollCounter but logs a warning instead of throwing an exception - * (for API compatibility). - *

      - * It is a bad practice for a developer to update the data in a scroll callback since it is - * potentially called during a layout. - */ - private int mDispatchScrollCounter = 0; + private boolean mRunningLayoutOrScroll = false; private EdgeEffectCompat mLeftGlow, mTopGlow, mRightGlow, mBottomGlow; @@ -393,20 +224,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro private int mInitialTouchY; private int mLastTouchX; private int mLastTouchY; - private int mTouchSlop; - private OnFlingListener mOnFlingListener; + private final int mTouchSlop; private final int mMinFlingVelocity; private final int mMaxFlingVelocity; - // This value is used when handling generic motion events. - private float mScrollFactor = Float.MIN_VALUE; - private boolean mPreserveFocusAfterLayout = true; private final ViewFlinger mViewFlinger = new ViewFlinger(); final State mState = new State(); private OnScrollListener mScrollListener; - private List mScrollListeners; // For use in item animations boolean mItemsAddedOrRemoved = false; @@ -415,17 +241,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro new ItemAnimatorRestoreListener(); private boolean mPostedAnimatorRunner = false; private RecyclerViewAccessibilityDelegate mAccessibilityDelegate; - private ChildDrawingOrderCallback mChildDrawingOrderCallback; - - // simple array to keep min and max child position during a layout calculation - // preserved not to create a new one in each layout pass - private final int[] mMinMaxLayoutPositions = new int[2]; - - private NestedScrollingChildHelper mScrollingChildHelper; - private final int[] mScrollOffset = new int[2]; - private final int[] mScrollConsumed = new int[2]; - private final int[] mNestedOffsets = new int[2]; - private Runnable mItemAnimatorRunner = new Runnable() { @Override public void run() { @@ -437,69 +252,22 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro }; private static final Interpolator sQuinticInterpolator = new Interpolator() { - @Override public float getInterpolation(float t) { t -= 1.0f; return t * t * t * t * t + 1.0f; } }; - /** - * The callback to convert view info diffs into animations. - */ - private final ViewInfoStore.ProcessCallback mViewInfoProcessCallback = - new ViewInfoStore.ProcessCallback() { - @Override - public void processDisappeared(ViewHolder viewHolder, @NonNull ItemHolderInfo info, - @Nullable ItemHolderInfo postInfo) { - mRecycler.unscrapView(viewHolder); - animateDisappearance(viewHolder, info, postInfo); - } - @Override - public void processAppeared(ViewHolder viewHolder, - ItemHolderInfo preInfo, ItemHolderInfo info) { - animateAppearance(viewHolder, preInfo, info); - } - - @Override - public void processPersistent(ViewHolder viewHolder, - @NonNull ItemHolderInfo preInfo, @NonNull ItemHolderInfo postInfo) { - viewHolder.setIsRecyclable(false); - if (mDataSetHasChangedAfterLayout) { - // since it was rebound, use change instead as we'll be mapping them from - // stable ids. If stable ids were false, we would not be running any - // animations - if (mItemAnimator.animateChange(viewHolder, viewHolder, preInfo, postInfo)) { - postAnimationRunner(); - } - } else if (mItemAnimator.animatePersistence(viewHolder, preInfo, postInfo)) { - postAnimationRunner(); - } - } - @Override - public void unused(ViewHolder viewHolder) { - mLayout.removeAndRecycleView(viewHolder.itemView, mRecycler); - } - }; - public RecyclerView(Context context) { this(context, null); } - public RecyclerView(Context context, @Nullable AttributeSet attrs) { + public RecyclerView(Context context, AttributeSet attrs) { this(context, attrs, 0); } - public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { + public RecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); - if (attrs != null) { - TypedArray a = context.obtainStyledAttributes(attrs, CLIP_TO_PADDING_ATTR, defStyle, 0); - mClipToPadding = a.getBoolean(0, true); - a.recycle(); - } else { - mClipToPadding = true; - } - setScrollContainer(true); setFocusableInTouchMode(true); final int version = Build.VERSION.SDK_INT; mPostUpdatesOnAnimation = version >= 16; @@ -508,7 +276,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mTouchSlop = vc.getScaledTouchSlop(); mMinFlingVelocity = vc.getScaledMinimumFlingVelocity(); mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity(); - setWillNotDraw(getOverScrollMode() == View.OVER_SCROLL_NEVER); + setWillNotDraw(ViewCompat.getOverScrollMode(this) == ViewCompat.OVER_SCROLL_NEVER); mItemAnimator.setListener(mItemAnimatorListener); initAdapterManager(); @@ -522,35 +290,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mAccessibilityManager = (AccessibilityManager) getContext() .getSystemService(Context.ACCESSIBILITY_SERVICE); setAccessibilityDelegateCompat(new RecyclerViewAccessibilityDelegate(this)); - // Create the layoutManager if specified. - - boolean nestedScrollingEnabled = true; - - if (attrs != null) { - int defStyleRes = 0; - TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecyclerView, - defStyle, defStyleRes); - String layoutManagerName = a.getString(R.styleable.RecyclerView_layoutManager); - int descendantFocusability = a.getInt( - R.styleable.RecyclerView_android_descendantFocusability, -1); - if (descendantFocusability == -1) { - setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); - } - a.recycle(); - createLayoutManager(context, layoutManagerName, attrs, defStyle, defStyleRes); - - if (Build.VERSION.SDK_INT >= 21) { - a = context.obtainStyledAttributes(attrs, NESTED_SCROLLING_ATTRS, - defStyle, defStyleRes); - nestedScrollingEnabled = a.getBoolean(0, true); - a.recycle(); - } - } else { - setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); - } - - // Re-set whether nested scrolling is enabled so that it is set on all API levels - setNestedScrollingEnabled(nestedScrollingEnabled); } /** @@ -571,72 +310,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro ViewCompat.setAccessibilityDelegate(this, mAccessibilityDelegate); } - /** - * Instantiate and set a LayoutManager, if specified in the attributes. - */ - private void createLayoutManager(Context context, String className, AttributeSet attrs, - int defStyleAttr, int defStyleRes) { - if (className != null) { - className = className.trim(); - if (className.length() != 0) { // Can't use isEmpty since it was added in API 9. - className = getFullClassName(context, className); - try { - ClassLoader classLoader; - if (isInEditMode()) { - // Stupid layoutlib cannot handle simple class loaders. - classLoader = this.getClass().getClassLoader(); - } else { - classLoader = context.getClassLoader(); - } - Class layoutManagerClass = - classLoader.loadClass(className).asSubclass(LayoutManager.class); - Constructor constructor; - Object[] constructorArgs = null; - try { - constructor = layoutManagerClass - .getConstructor(LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE); - constructorArgs = new Object[]{context, attrs, defStyleAttr, defStyleRes}; - } catch (NoSuchMethodException e) { - try { - constructor = layoutManagerClass.getConstructor(); - } catch (NoSuchMethodException e1) { - e1.initCause(e); - throw new IllegalStateException(attrs.getPositionDescription() + - ": Error creating LayoutManager " + className, e1); - } - } - constructor.setAccessible(true); - setLayoutManager(constructor.newInstance(constructorArgs)); - } catch (ClassNotFoundException e) { - throw new IllegalStateException(attrs.getPositionDescription() - + ": Unable to find LayoutManager " + className, e); - } catch (InvocationTargetException e) { - throw new IllegalStateException(attrs.getPositionDescription() - + ": Could not instantiate the LayoutManager: " + className, e); - } catch (InstantiationException e) { - throw new IllegalStateException(attrs.getPositionDescription() - + ": Could not instantiate the LayoutManager: " + className, e); - } catch (IllegalAccessException e) { - throw new IllegalStateException(attrs.getPositionDescription() - + ": Cannot access non-public constructor " + className, e); - } catch (ClassCastException e) { - throw new IllegalStateException(attrs.getPositionDescription() - + ": Class is not a LayoutManager " + className, e); - } - } - } - } - - private String getFullClassName(Context context, String className) { - if (className.charAt(0) == '.') { - return context.getPackageName() + className; - } - if (className.contains(".")) { - return className; - } - return RecyclerView.class.getPackage().getName() + '.' + className; - } - private void initChildrenHelper() { mChildHelper = new ChildHelper(new ChildHelper.Callback() { @Override @@ -686,54 +359,13 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro @Override public void attachViewToParent(View child, int index, ViewGroup.LayoutParams layoutParams) { - final ViewHolder vh = getChildViewHolderInt(child); - if (vh != null) { - if (!vh.isTmpDetached() && !vh.shouldIgnore()) { - throw new IllegalArgumentException("Called attach on a child which is not" - + " detached: " + vh); - } - if (DEBUG) { - Log.d(TAG, "reAttach " + vh); - } - vh.clearTmpDetachFlag(); - } RecyclerView.this.attachViewToParent(child, index, layoutParams); } @Override public void detachViewFromParent(int offset) { - final View view = getChildAt(offset); - if (view != null) { - final ViewHolder vh = getChildViewHolderInt(view); - if (vh != null) { - if (vh.isTmpDetached() && !vh.shouldIgnore()) { - throw new IllegalArgumentException("called detach on an already" - + " detached child " + vh); - } - if (DEBUG) { - Log.d(TAG, "tmpDetach " + vh); - } - vh.addFlags(ViewHolder.FLAG_TMP_DETACHED); - } - } RecyclerView.this.detachViewFromParent(offset); } - - @Override - public void onEnteredHiddenState(View child) { - final ViewHolder vh = getChildViewHolderInt(child); - if (vh != null) { - vh.onEnteredHiddenState(); - } - } - - @Override - public void onLeftHiddenState(View child) { - final ViewHolder vh = getChildViewHolderInt(child); - if (vh != null) { - vh.onLeftHiddenState(); - } - } }); } @@ -741,19 +373,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mAdapterHelper = new AdapterHelper(new Callback() { @Override public ViewHolder findViewHolder(int position) { - final ViewHolder vh = findViewHolderForPosition(position, true); - if (vh == null) { - return null; - } - // ensure it is not hidden because for adapter helper, the only thing matter is that - // LM thinks view is a child. - if (mChildHelper.isHidden(vh.itemView)) { - if (DEBUG) { - Log.d(TAG, "assuming view holder cannot be find because it is hidden"); - } - return null; - } - return vh; + return findViewHolderForPosition(position, true); } @Override @@ -770,8 +390,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } @Override - public void markViewHoldersUpdated(int positionStart, int itemCount, Object payload) { - viewRangeUpdate(positionStart, itemCount, payload); + public void markViewHoldersUpdated(int positionStart, int itemCount) { + viewRangeUpdate(positionStart, itemCount); mItemsChanged = true; } @@ -789,8 +409,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mLayout.onItemsRemoved(RecyclerView.this, op.positionStart, op.itemCount); break; case UpdateOp.UPDATE: - mLayout.onItemsUpdated(RecyclerView.this, op.positionStart, op.itemCount, - op.payload); + mLayout.onItemsUpdated(RecyclerView.this, op.positionStart, op.itemCount); break; case UpdateOp.MOVE: mLayout.onItemsMoved(RecyclerView.this, op.positionStart, op.itemCount, 1); @@ -819,13 +438,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * RecyclerView can perform several optimizations if it can know in advance that RecyclerView's - * size is not affected by the adapter contents. RecyclerView can still change its size based - * on other factors (e.g. its parent's size) but this size calculation cannot depend on the - * size of its children or contents of its adapter (except the number of items in the adapter). - *

      - * If your use of RecyclerView falls into this category, set this to {@code true}. It will allow - * RecyclerView to avoid invalidating the whole layout when its adapter contents change. + * RecyclerView can perform several optimizations if it can know in advance that changes in + * adapter content cannot change the size of the RecyclerView itself. + * If your use of RecyclerView falls into this category, set this to true. * * @param hasFixedSize true if adapter changes cannot affect the size of the RecyclerView. */ @@ -853,32 +468,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } } - /** - * Configure the scrolling touch slop for a specific use case. - * - * Set up the RecyclerView's scrolling motion threshold based on common usages. - * Valid arguments are {@link #TOUCH_SLOP_DEFAULT} and {@link #TOUCH_SLOP_PAGING}. - * - * @param slopConstant One of the TOUCH_SLOP_ constants representing - * the intended usage of this RecyclerView - */ - public void setScrollingTouchSlop(int slopConstant) { - final ViewConfiguration vc = ViewConfiguration.get(getContext()); - switch (slopConstant) { - default: - Log.w(TAG, "setScrollingTouchSlop(): bad argument constant " - + slopConstant + "; using default value"); - // fall-through - case TOUCH_SLOP_DEFAULT: - mTouchSlop = vc.getScaledTouchSlop(); - break; - - case TOUCH_SLOP_PAGING: - mTouchSlop = vc.getScaledPagingTouchSlop(); - break; - } - } - /** * Swaps the current adapter with the provided one. It is similar to * {@link #setAdapter(Adapter)} but assumes existing adapter and the new adapter uses the same @@ -894,10 +483,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @see #setAdapter(Adapter) */ public void swapAdapter(Adapter adapter, boolean removeAndRecycleExistingViews) { - // bail out if layout is frozen - setLayoutFrozen(false); setAdapterInternal(adapter, true, removeAndRecycleExistingViews); - setDataSetChangedAfterLayout(); + mDataSetHasChangedAfterLayout = true; requestLayout(); } /** @@ -910,8 +497,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @see #swapAdapter(Adapter, boolean) */ public void setAdapter(Adapter adapter) { - // bail out if layout is frozen - setLayoutFrozen(false); setAdapterInternal(adapter, false, true); requestLayout(); } @@ -929,7 +514,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro boolean removeAndRecycleViews) { if (mAdapter != null) { mAdapter.unregisterAdapterDataObserver(mObserver); - mAdapter.onDetachedFromRecyclerView(this); } if (!compatibleWithPrevious || removeAndRecycleViews) { // end all running animations @@ -942,17 +526,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // count. if (mLayout != null) { mLayout.removeAndRecycleAllViews(mRecycler); - mLayout.removeAndRecycleScrapInt(mRecycler); + mLayout.removeAndRecycleScrapInt(mRecycler, true); } - // we should clear it here before adapters are swapped to ensure correct callbacks. - mRecycler.clear(); } mAdapterHelper.reset(); final Adapter oldAdapter = mAdapter; mAdapter = adapter; if (adapter != null) { adapter.registerAdapterDataObserver(mObserver); - adapter.onAttachedToRecyclerView(this); } if (mLayout != null) { mLayout.onAdapterChanged(oldAdapter, mAdapter); @@ -986,63 +567,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mRecyclerListener = listener; } - /** - *

      Return the offset of the RecyclerView's text baseline from the its top - * boundary. If the LayoutManager of this RecyclerView does not support baseline alignment, - * this method returns -1.

      - * - * @return the offset of the baseline within the RecyclerView's bounds or -1 - * if baseline alignment is not supported - */ - @Override - public int getBaseline() { - if (mLayout != null) { - return mLayout.getBaseline(); - } else { - return super.getBaseline(); - } - } - - /** - * Register a listener that will be notified whenever a child view is attached to or detached - * from RecyclerView. - * - *

      This listener will be called when a LayoutManager or the RecyclerView decides - * that a child view is no longer needed. If an application associates expensive - * or heavyweight data with item views, this may be a good place to release - * or free those resources.

      - * - * @param listener Listener to register - */ - public void addOnChildAttachStateChangeListener(OnChildAttachStateChangeListener listener) { - if (mOnChildAttachStateListeners == null) { - mOnChildAttachStateListeners = new ArrayList<>(); - } - mOnChildAttachStateListeners.add(listener); - } - - /** - * Removes the provided listener from child attached state listeners list. - * - * @param listener Listener to unregister - */ - public void removeOnChildAttachStateChangeListener(OnChildAttachStateChangeListener listener) { - if (mOnChildAttachStateListeners == null) { - return; - } - mOnChildAttachStateListeners.remove(listener); - } - - /** - * Removes all listeners that were added via - * {@link #addOnChildAttachStateChangeListener(OnChildAttachStateChangeListener)}. - */ - public void clearOnChildAttachStateChangeListeners() { - if (mOnChildAttachStateListeners != null) { - mOnChildAttachStateListeners.clear(); - } - } - /** * Set the {@link LayoutManager} that this RecyclerView will use. * @@ -1059,27 +583,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro if (layout == mLayout) { return; } - stopScroll(); - // TODO We should do this switch a dispatchLayout pass and animate children. There is a good + // TODO We should do this switch a dispachLayout pass and animate children. There is a good // chance that LayoutManagers will re-use views. if (mLayout != null) { - // end all running animations - if (mItemAnimator != null) { - mItemAnimator.endAnimations(); - } - mLayout.removeAndRecycleAllViews(mRecycler); - mLayout.removeAndRecycleScrapInt(mRecycler); - mRecycler.clear(); - if (mIsAttached) { - mLayout.dispatchDetachedFromWindow(this, mRecycler); + mLayout.onDetachedFromWindow(this, mRecycler); } mLayout.setRecyclerView(null); - mLayout = null; - } else { - mRecycler.clear(); } - // this is just a defensive measure for faulty item animators. + mRecycler.clear(); mChildHelper.removeAllViewsUnfiltered(); mLayout = layout; if (layout != null) { @@ -1089,34 +601,12 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } mLayout.setRecyclerView(this); if (mIsAttached) { - mLayout.dispatchAttachedToWindow(this); + mLayout.onAttachedToWindow(this); } } requestLayout(); } - /** - * Set a {@link OnFlingListener} for this {@link RecyclerView}. - *

      - * If the {@link OnFlingListener} is set then it will receive - * calls to {@link #fling(int,int)} and will be able to intercept them. - * - * @param onFlingListener The {@link OnFlingListener} instance. - */ - public void setOnFlingListener(@Nullable OnFlingListener onFlingListener) { - mOnFlingListener = onFlingListener; - } - - /** - * Get the current {@link OnFlingListener} from this {@link RecyclerView}. - * - * @return The {@link OnFlingListener} instance currently set (can be null). - */ - @Nullable - public OnFlingListener getOnFlingListener() { - return mOnFlingListener; - } - @Override protected Parcelable onSaveInstanceState() { SavedState state = new SavedState(super.onSaveInstanceState()); @@ -1133,11 +623,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro @Override protected void onRestoreInstanceState(Parcelable state) { - if (!(state instanceof SavedState)) { - super.onRestoreInstanceState(state); - return; - } - mPendingSavedState = (SavedState) state; super.onRestoreInstanceState(mPendingSavedState.getSuperState()); if (mLayout != null && mPendingSavedState.mLayoutState != null) { @@ -1145,38 +630,18 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } } - /** - * Override to prevent freezing of any views created by the adapter. - */ - @Override - protected void dispatchSaveInstanceState(SparseArray container) { - dispatchFreezeSelfOnly(container); - } - - /** - * Override to prevent thawing of any views created by the adapter. - */ - @Override - protected void dispatchRestoreInstanceState(SparseArray container) { - dispatchThawSelfOnly(container); - } - /** * Adds a view to the animatingViews list. * mAnimatingViews holds the child views that are currently being kept around * purely for the purpose of being animated out of view. They are drawn as a regular * part of the child list of the RecyclerView, but they are invisible to the LayoutManager * as they are managed separately from the regular child views. - * @param viewHolder The ViewHolder to be removed + * @param view The view to be removed */ - private void addAnimatingView(ViewHolder viewHolder) { - final View view = viewHolder.itemView; + private void addAnimatingView(View view) { final boolean alreadyParented = view.getParent() == this; mRecycler.unscrapView(getChildViewHolder(view)); - if (viewHolder.isTmpDetached()) { - // re-attach - mChildHelper.attachViewToParent(view, -1, view.getLayoutParams(), true); - } else if(!alreadyParented) { + if (!alreadyParented) { mChildHelper.addView(view, true); } else { mChildHelper.hide(view); @@ -1186,13 +651,11 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Removes a view from the animatingViews list. * @param view The view to be removed - * @see #addAnimatingView(RecyclerView.ViewHolder) - * @return true if an animating view is removed + * @see #addAnimatingView(View) */ - private boolean removeAnimatingView(View view) { + private void removeAnimatingView(View view) { eatRequestLayout(); - final boolean removed = mChildHelper.removeViewIfHidden(view); - if (removed) { + if (mChildHelper.removeViewIfHidden(view)) { final ViewHolder viewHolder = getChildViewHolderInt(view); mRecycler.unscrapView(viewHolder); mRecycler.recycleViewHolderInternal(viewHolder); @@ -1200,9 +663,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro Log.d(TAG, "after removing animated view: " + view + ", " + this); } } - // only clear request eaten flag if we removed the view. - resumeRequestLayout(!removed); - return removed; + resumeRequestLayout(false); } /** @@ -1280,14 +741,16 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return; } if (DEBUG) { - Log.d(TAG, "setting scroll state to " + state + " from " + mScrollState, - new Exception()); + Log.d(TAG, "setting scroll state to " + state + " from " + mScrollState, new Exception()); } mScrollState = state; if (state != SCROLL_STATE_SETTLING) { stopScrollersInternal(); } - dispatchOnScrollStateChanged(state); + if (mScrollListener != null) { + mScrollListener.onScrollStateChanged(this, state); + } + mLayout.onScrollStateChanged(state); } /** @@ -1353,107 +816,31 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } mItemDecorations.remove(decor); if (mItemDecorations.isEmpty()) { - setWillNotDraw(getOverScrollMode() == View.OVER_SCROLL_NEVER); + setWillNotDraw(ViewCompat.getOverScrollMode(this) == ViewCompat.OVER_SCROLL_NEVER); } markItemDecorInsetsDirty(); requestLayout(); } - /** - * Sets the {@link ChildDrawingOrderCallback} to be used for drawing children. - *

      - * See {@link ViewGroup#getChildDrawingOrder(int, int)} for details. Calling this method will - * always call {@link ViewGroup#setChildrenDrawingOrderEnabled(boolean)}. The parameter will be - * true if childDrawingOrderCallback is not null, false otherwise. - *

      - * Note that child drawing order may be overridden by View's elevation. - * - * @param childDrawingOrderCallback The ChildDrawingOrderCallback to be used by the drawing - * system. - */ - public void setChildDrawingOrderCallback(ChildDrawingOrderCallback childDrawingOrderCallback) { - if (childDrawingOrderCallback == mChildDrawingOrderCallback) { - return; - } - mChildDrawingOrderCallback = childDrawingOrderCallback; - setChildrenDrawingOrderEnabled(mChildDrawingOrderCallback != null); - } - /** * Set a listener that will be notified of any changes in scroll state or position. * * @param listener Listener to set or null to clear - * - * @deprecated Use {@link #addOnScrollListener(OnScrollListener)} and - * {@link #removeOnScrollListener(OnScrollListener)} */ - @Deprecated public void setOnScrollListener(OnScrollListener listener) { mScrollListener = listener; } - /** - * Add a listener that will be notified of any changes in scroll state or position. - * - *

      Components that add a listener should take care to remove it when finished. - * Other components that take ownership of a view may call {@link #clearOnScrollListeners()} - * to remove all attached listeners.

      - * - * @param listener listener to set or null to clear - */ - public void addOnScrollListener(OnScrollListener listener) { - if (mScrollListeners == null) { - mScrollListeners = new ArrayList<>(); - } - mScrollListeners.add(listener); - } - - /** - * Remove a listener that was notified of any changes in scroll state or position. - * - * @param listener listener to set or null to clear - */ - public void removeOnScrollListener(OnScrollListener listener) { - if (mScrollListeners != null) { - mScrollListeners.remove(listener); - } - } - - /** - * Remove all secondary listener that were notified of any changes in scroll state or position. - */ - public void clearOnScrollListeners() { - if (mScrollListeners != null) { - mScrollListeners.clear(); - } - } - /** * Convenience method to scroll to a certain position. * * RecyclerView does not implement scrolling logic, rather forwards the call to - * {@link android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition(int)} + * {@link LayoutManager#scrollToPosition(int)} * @param position Scroll to this adapter position - * @see android.support.v7.widget.RecyclerView.LayoutManager#scrollToPosition(int) + * @see LayoutManager#scrollToPosition(int) */ public void scrollToPosition(int position) { - if (mLayoutFrozen) { - return; - } stopScroll(); - if (mLayout == null) { - Log.e(TAG, "Cannot scroll to position a LayoutManager set. " + - "Call setLayoutManager with a non-null argument."); - return; - } - mLayout.scrollToPosition(position); - awakenScrollBars(); - } - - private void jumpToPositionForSmoothScroller(int position) { - if (mLayout == null) { - return; - } mLayout.scrollToPosition(position); awakenScrollBars(); } @@ -1474,37 +861,25 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @see LayoutManager#smoothScrollToPosition(RecyclerView, State, int) */ public void smoothScrollToPosition(int position) { - if (mLayoutFrozen) { - return; - } - if (mLayout == null) { - Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. " + - "Call setLayoutManager with a non-null argument."); - return; - } mLayout.smoothScrollToPosition(this, mState, position); } @Override public void scrollTo(int x, int y) { - Log.w(TAG, "RecyclerView does not support scrolling to an absolute position. " - + "Use scrollToPosition instead"); + throw new UnsupportedOperationException( + "RecyclerView does not support scrolling to an absolute position."); } @Override public void scrollBy(int x, int y) { if (mLayout == null) { - Log.e(TAG, "Cannot scroll without a LayoutManager set. " + + throw new IllegalStateException("Cannot scroll without a LayoutManager set. " + "Call setLayoutManager with a non-null argument."); - return; - } - if (mLayoutFrozen) { - return; } final boolean canScrollHorizontal = mLayout.canScrollHorizontally(); final boolean canScrollVertical = mLayout.canScrollVertically(); if (canScrollHorizontal || canScrollVertical) { - scrollByInternal(canScrollHorizontal ? x : 0, canScrollVertical ? y : 0, null); + scrollByInternal(canScrollHorizontal ? x : 0, canScrollVertical ? y : 0); } } @@ -1517,116 +892,69 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * This method consumes all deferred changes to avoid that case. */ private void consumePendingUpdateOperations() { - if (!mFirstLayoutComplete || mDataSetHasChangedAfterLayout) { - TraceCompat.beginSection(TRACE_ON_DATA_SET_CHANGE_LAYOUT_TAG); - dispatchLayout(); - TraceCompat.endSection(); - return; + if (mAdapterHelper.hasPendingUpdates()) { + mUpdateChildViewsRunnable.run(); } - if (!mAdapterHelper.hasPendingUpdates()) { - return; - } - - // if it is only an item change (no add-remove-notifyDataSetChanged) we can check if any - // of the visible items is affected and if not, just ignore the change. - if (mAdapterHelper.hasAnyUpdateTypes(UpdateOp.UPDATE) && !mAdapterHelper - .hasAnyUpdateTypes(UpdateOp.ADD | UpdateOp.REMOVE | UpdateOp.MOVE)) { - TraceCompat.beginSection(TRACE_HANDLE_ADAPTER_UPDATES_TAG); - eatRequestLayout(); - mAdapterHelper.preProcess(); - if (!mLayoutRequestEaten) { - if (hasUpdatedView()) { - dispatchLayout(); - } else { - // no need to layout, clean state - mAdapterHelper.consumePostponedUpdates(); - } - } - resumeRequestLayout(true); - TraceCompat.endSection(); - } else if (mAdapterHelper.hasPendingUpdates()) { - TraceCompat.beginSection(TRACE_ON_DATA_SET_CHANGE_LAYOUT_TAG); - dispatchLayout(); - TraceCompat.endSection(); - } - } - - /** - * @return True if an existing view holder needs to be updated - */ - private boolean hasUpdatedView() { - final int childCount = mChildHelper.getChildCount(); - for (int i = 0; i < childCount; i++) { - final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); - if (holder == null || holder.shouldIgnore()) { - continue; - } - if (holder.isUpdated()) { - return true; - } - } - return false; } /** * Does not perform bounds checking. Used by internal methods that have already validated input. - *

      - * It also reports any unused scroll request to the related EdgeEffect. - * - * @param x The amount of horizontal scroll request - * @param y The amount of vertical scroll request - * @param ev The originating MotionEvent, or null if not from a touch event. - * - * @return Whether any scroll was consumed in either direction. */ - boolean scrollByInternal(int x, int y, MotionEvent ev) { - int unconsumedX = 0, unconsumedY = 0; - int consumedX = 0, consumedY = 0; - + void scrollByInternal(int x, int y) { + int overscrollX = 0, overscrollY = 0; + int hresult = 0, vresult = 0; consumePendingUpdateOperations(); if (mAdapter != null) { eatRequestLayout(); - onEnterLayoutOrScroll(); - TraceCompat.beginSection(TRACE_SCROLL_TAG); + mRunningLayoutOrScroll = true; if (x != 0) { - consumedX = mLayout.scrollHorizontallyBy(x, mRecycler, mState); - unconsumedX = x - consumedX; + hresult = mLayout.scrollHorizontallyBy(x, mRecycler, mState); + overscrollX = x - hresult; } if (y != 0) { - consumedY = mLayout.scrollVerticallyBy(y, mRecycler, mState); - unconsumedY = y - consumedY; + vresult = mLayout.scrollVerticallyBy(y, mRecycler, mState); + overscrollY = y - vresult; } - TraceCompat.endSection(); - repositionShadowingViews(); - onExitLayoutOrScroll(); + if (supportsChangeAnimations()) { + // Fix up shadow views used by changing animations + int count = mChildHelper.getChildCount(); + for (int i = 0; i < count; i++) { + View view = mChildHelper.getChildAt(i); + ViewHolder holder = getChildViewHolder(view); + if (holder != null && holder.mShadowingHolder != null) { + ViewHolder shadowingHolder = holder.mShadowingHolder; + View shadowingView = shadowingHolder != null ? shadowingHolder.itemView : null; + if (shadowingView != null) { + int left = view.getLeft(); + int top = view.getTop(); + if (left != shadowingView.getLeft() || top != shadowingView.getTop()) { + shadowingView.layout(left, top, + left + shadowingView.getWidth(), + top + shadowingView.getHeight()); + } + } + } + } + } + mRunningLayoutOrScroll = false; resumeRequestLayout(false); } if (!mItemDecorations.isEmpty()) { invalidate(); } - - if (dispatchNestedScroll(consumedX, consumedY, unconsumedX, unconsumedY, mScrollOffset)) { - // Update the last touch co-ords, taking any scroll offset into account - mLastTouchX -= mScrollOffset[0]; - mLastTouchY -= mScrollOffset[1]; - if (ev != null) { - ev.offsetLocation(mScrollOffset[0], mScrollOffset[1]); - } - mNestedOffsets[0] += mScrollOffset[0]; - mNestedOffsets[1] += mScrollOffset[1]; - } else if (getOverScrollMode() != View.OVER_SCROLL_NEVER) { - if (ev != null) { - pullGlows(ev.getX(), unconsumedX, ev.getY(), unconsumedY); - } + if (ViewCompat.getOverScrollMode(this) != ViewCompat.OVER_SCROLL_NEVER) { considerReleasingGlowsOnScroll(x, y); + pullGlows(overscrollX, overscrollY); } - if (consumedX != 0 || consumedY != 0) { - dispatchOnScrolled(consumedX, consumedY); + if (hresult != 0 || vresult != 0) { + onScrollChanged(0, 0, 0, 0); // dummy values, View's implementation does not use these. + if (mScrollListener != null) { + mScrollListener.onScrolled(this, hresult, vresult); + } } if (!awakenScrollBars()) { invalidate(); } - return consumedX != 0 || consumedY != 0; } /** @@ -1640,19 +968,17 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro *

      Default implementation returns 0.

      * *

      If you want to support scroll bars, override - * {@link RecyclerView.LayoutManager#computeHorizontalScrollOffset(RecyclerView.State)} in your + * {@link LayoutManager#computeHorizontalScrollOffset(State)} in your * LayoutManager.

      * * @return The horizontal offset of the scrollbar's thumb - * @see android.support.v7.widget.RecyclerView.LayoutManager#computeHorizontalScrollOffset - * (RecyclerView.State) + * @see LayoutManager#computeHorizontalScrollOffset + * (RecyclerView.Adapter) */ @Override - public int computeHorizontalScrollOffset() { - if (mLayout == null) { - return 0; - } - return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollOffset(mState) : 0; + protected int computeHorizontalScrollOffset() { + return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollOffset(mState) + : 0; } /** @@ -1666,17 +992,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro *

      Default implementation returns 0.

      * *

      If you want to support scroll bars, override - * {@link RecyclerView.LayoutManager#computeHorizontalScrollExtent(RecyclerView.State)} in your + * {@link LayoutManager#computeHorizontalScrollExtent(State)} in your * LayoutManager.

      * * @return The horizontal extent of the scrollbar's thumb - * @see RecyclerView.LayoutManager#computeHorizontalScrollExtent(RecyclerView.State) + * @see LayoutManager#computeHorizontalScrollExtent(State) */ @Override - public int computeHorizontalScrollExtent() { - if (mLayout == null) { - return 0; - } + protected int computeHorizontalScrollExtent() { return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollExtent(mState) : 0; } @@ -1689,17 +1012,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro *

      Default implementation returns 0.

      * *

      If you want to support scroll bars, override - * {@link RecyclerView.LayoutManager#computeHorizontalScrollRange(RecyclerView.State)} in your + * {@link LayoutManager#computeHorizontalScrollRange(State)} in your * LayoutManager.

      * * @return The total horizontal range represented by the vertical scrollbar - * @see RecyclerView.LayoutManager#computeHorizontalScrollRange(RecyclerView.State) + * @see LayoutManager#computeHorizontalScrollRange(State) */ @Override - public int computeHorizontalScrollRange() { - if (mLayout == null) { - return 0; - } + protected int computeHorizontalScrollRange() { return mLayout.canScrollHorizontally() ? mLayout.computeHorizontalScrollRange(mState) : 0; } @@ -1713,18 +1033,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro *

      Default implementation returns 0.

      * *

      If you want to support scroll bars, override - * {@link RecyclerView.LayoutManager#computeVerticalScrollOffset(RecyclerView.State)} in your + * {@link LayoutManager#computeVerticalScrollOffset(State)} in your * LayoutManager.

      * * @return The vertical offset of the scrollbar's thumb - * @see android.support.v7.widget.RecyclerView.LayoutManager#computeVerticalScrollOffset - * (RecyclerView.State) + * @see LayoutManager#computeVerticalScrollOffset + * (RecyclerView.Adapter) */ @Override - public int computeVerticalScrollOffset() { - if (mLayout == null) { - return 0; - } + protected int computeVerticalScrollOffset() { return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollOffset(mState) : 0; } @@ -1738,17 +1055,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro *

      Default implementation returns 0.

      * *

      If you want to support scroll bars, override - * {@link RecyclerView.LayoutManager#computeVerticalScrollExtent(RecyclerView.State)} in your + * {@link LayoutManager#computeVerticalScrollExtent(State)} in your * LayoutManager.

      * * @return The vertical extent of the scrollbar's thumb - * @see RecyclerView.LayoutManager#computeVerticalScrollExtent(RecyclerView.State) + * @see LayoutManager#computeVerticalScrollExtent(State) */ @Override - public int computeVerticalScrollExtent() { - if (mLayout == null) { - return 0; - } + protected int computeVerticalScrollExtent() { return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollExtent(mState) : 0; } @@ -1761,111 +1075,34 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro *

      Default implementation returns 0.

      * *

      If you want to support scroll bars, override - * {@link RecyclerView.LayoutManager#computeVerticalScrollRange(RecyclerView.State)} in your + * {@link LayoutManager#computeVerticalScrollRange(State)} in your * LayoutManager.

      * * @return The total vertical range represented by the vertical scrollbar - * @see RecyclerView.LayoutManager#computeVerticalScrollRange(RecyclerView.State) + * @see LayoutManager#computeVerticalScrollRange(State) */ @Override - public int computeVerticalScrollRange() { - if (mLayout == null) { - return 0; - } + protected int computeVerticalScrollRange() { return mLayout.canScrollVertically() ? mLayout.computeVerticalScrollRange(mState) : 0; } void eatRequestLayout() { - mEatRequestLayout++; - if (mEatRequestLayout == 1 && !mLayoutFrozen) { + if (!mEatRequestLayout) { + mEatRequestLayout = true; mLayoutRequestEaten = false; } } void resumeRequestLayout(boolean performLayoutChildren) { - if (mEatRequestLayout < 1) { - //noinspection PointlessBooleanExpression - if (DEBUG) { - throw new IllegalStateException("invalid eat request layout count"); - } - mEatRequestLayout = 1; - } - if (!performLayoutChildren) { - // Reset the layout request eaten counter. - // This is necessary since eatRequest calls can be nested in which case the other - // call will override the inner one. - // for instance: - // eat layout for process adapter updates - // eat layout for dispatchLayout - // a bunch of req layout calls arrive - - mLayoutRequestEaten = false; - } - if (mEatRequestLayout == 1) { - // when layout is frozen we should delay dispatchLayout() - if (performLayoutChildren && mLayoutRequestEaten && !mLayoutFrozen && + if (mEatRequestLayout) { + if (performLayoutChildren && mLayoutRequestEaten && mLayout != null && mAdapter != null) { dispatchLayout(); } - if (!mLayoutFrozen) { - mLayoutRequestEaten = false; - } + mEatRequestLayout = false; + mLayoutRequestEaten = false; } - mEatRequestLayout--; - } - - /** - * Enable or disable layout and scroll. After setLayoutFrozen(true) is called, - * Layout requests will be postponed until setLayoutFrozen(false) is called; - * child views are not updated when RecyclerView is frozen, {@link #smoothScrollBy(int, int)}, - * {@link #scrollBy(int, int)}, {@link #scrollToPosition(int)} and - * {@link #smoothScrollToPosition(int)} are dropped; TouchEvents and GenericMotionEvents are - * dropped; {@link LayoutManager#onFocusSearchFailed(View, int, Recycler, State)} will not be - * called. - * - *

      - * setLayoutFrozen(true) does not prevent app from directly calling {@link - * LayoutManager#scrollToPosition(int)}, {@link LayoutManager#smoothScrollToPosition( - * RecyclerView, State, int)}. - *

      - * {@link #setAdapter(Adapter)} and {@link #swapAdapter(Adapter, boolean)} will automatically - * stop frozen. - *

      - * Note: Running ItemAnimator is not stopped automatically, it's caller's - * responsibility to call ItemAnimator.end(). - * - * @param frozen true to freeze layout and scroll, false to re-enable. - */ - public void setLayoutFrozen(boolean frozen) { - if (frozen != mLayoutFrozen) { - assertNotInLayoutOrScroll("Do not setLayoutFrozen in layout or scroll"); - if (!frozen) { - mLayoutFrozen = false; - if (mLayoutRequestEaten && mLayout != null && mAdapter != null) { - requestLayout(); - } - mLayoutRequestEaten = false; - } else { - final long now = SystemClock.uptimeMillis(); - MotionEvent cancelEvent = MotionEvent.obtain(now, now, - MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); - onTouchEvent(cancelEvent); - mLayoutFrozen = true; - mIgnoreMotionEventTillDown = true; - stopScroll(); - } - } - } - - /** - * Returns true if layout and scroll are frozen. - * - * @return true if layout and scroll are frozen - * @see #setLayoutFrozen(boolean) - */ - public boolean isLayoutFrozen() { - return mLayoutFrozen; } /** @@ -1875,20 +1112,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param dy Pixels to scroll vertically */ public void smoothScrollBy(int dx, int dy) { - if (mLayout == null) { - Log.e(TAG, "Cannot smooth scroll without a LayoutManager set. " + - "Call setLayoutManager with a non-null argument."); - return; - } - if (mLayoutFrozen) { - return; - } - if (!mLayout.canScrollHorizontally()) { - dx = 0; - } - if (!mLayout.canScrollVertically()) { - dy = 0; - } if (dx != 0 || dy != 0) { mViewFlinger.smoothScrollBy(dx, dy); } @@ -1901,50 +1124,20 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * * @param velocityX Initial horizontal velocity in pixels per second * @param velocityY Initial vertical velocity in pixels per second - * @return true if the fling was started, false if the velocity was too low to fling or - * LayoutManager does not support scrolling in the axis fling is issued. - * - * @see LayoutManager#canScrollVertically() - * @see LayoutManager#canScrollHorizontally() + * @return true if the fling was started, false if the velocity was too low to fling */ public boolean fling(int velocityX, int velocityY) { - if (mLayout == null) { - Log.e(TAG, "Cannot fling without a LayoutManager set. " + - "Call setLayoutManager with a non-null argument."); - return false; - } - if (mLayoutFrozen) { - return false; - } - - final boolean canScrollHorizontal = mLayout.canScrollHorizontally(); - final boolean canScrollVertical = mLayout.canScrollVertically(); - - if (!canScrollHorizontal || Math.abs(velocityX) < mMinFlingVelocity) { + if (Math.abs(velocityX) < mMinFlingVelocity) { velocityX = 0; } - if (!canScrollVertical || Math.abs(velocityY) < mMinFlingVelocity) { + if (Math.abs(velocityY) < mMinFlingVelocity) { velocityY = 0; } - if (velocityX == 0 && velocityY == 0) { - // If we don't have any velocity, return false - return false; - } - - if (!dispatchNestedPreFling(velocityX, velocityY)) { - final boolean canScroll = canScrollHorizontal || canScrollVertical; - dispatchNestedFling(velocityX, velocityY, canScroll); - - if (mOnFlingListener != null && mOnFlingListener.onFling(velocityX, velocityY)) { - return true; - } - - if (canScroll) { - velocityX = Math.max(-mMaxFlingVelocity, Math.min(velocityX, mMaxFlingVelocity)); - velocityY = Math.max(-mMaxFlingVelocity, Math.min(velocityY, mMaxFlingVelocity)); - mViewFlinger.fling(velocityX, velocityY); - return true; - } + velocityX = Math.max(-mMaxFlingVelocity, Math.min(velocityX, mMaxFlingVelocity)); + velocityY = Math.max(-mMaxFlingVelocity, Math.min(velocityY, mMaxFlingVelocity)); + if (velocityX != 0 || velocityY != 0) { + mViewFlinger.fling(velocityX, velocityY); + return true; } return false; } @@ -1963,60 +1156,30 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro */ private void stopScrollersInternal() { mViewFlinger.stop(); - if (mLayout != null) { - mLayout.stopSmoothScroller(); - } - } - - /** - * Returns the minimum velocity to start a fling. - * - * @return The minimum velocity to start a fling - */ - public int getMinFlingVelocity() { - return mMinFlingVelocity; - } - - - /** - * Returns the maximum fling velocity used by this RecyclerView. - * - * @return The maximum fling velocity used by this RecyclerView. - */ - public int getMaxFlingVelocity() { - return mMaxFlingVelocity; + mLayout.stopSmoothScroller(); } /** * Apply a pull to relevant overscroll glow effects */ - private void pullGlows(float x, float overscrollX, float y, float overscrollY) { - boolean invalidate = false; + private void pullGlows(int overscrollX, int overscrollY) { if (overscrollX < 0) { ensureLeftGlow(); - if (mLeftGlow.onPull(-overscrollX / getWidth(), 1f - y / getHeight())) { - invalidate = true; - } + mLeftGlow.onPull(-overscrollX / (float) getWidth()); } else if (overscrollX > 0) { ensureRightGlow(); - if (mRightGlow.onPull(overscrollX / getWidth(), y / getHeight())) { - invalidate = true; - } + mRightGlow.onPull(overscrollX / (float) getWidth()); } if (overscrollY < 0) { ensureTopGlow(); - if (mTopGlow.onPull(-overscrollY / getHeight(), x / getWidth())) { - invalidate = true; - } + mTopGlow.onPull(-overscrollY / (float) getHeight()); } else if (overscrollY > 0) { ensureBottomGlow(); - if (mBottomGlow.onPull(overscrollY / getHeight(), 1f - x / getWidth())) { - invalidate = true; - } + mBottomGlow.onPull(overscrollY / (float) getHeight()); } - if (invalidate || overscrollX != 0 || overscrollY != 0) { + if (overscrollX != 0 || overscrollY != 0) { ViewCompat.postInvalidateOnAnimation(this); } } @@ -2130,178 +1293,28 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mLeftGlow = mRightGlow = mTopGlow = mBottomGlow = null; } - /** - * Since RecyclerView is a collection ViewGroup that includes virtual children (items that are - * in the Adapter but not visible in the UI), it employs a more involved focus search strategy - * that differs from other ViewGroups. - *

      - * It first does a focus search within the RecyclerView. If this search finds a View that is in - * the focus direction with respect to the currently focused View, RecyclerView returns that - * child as the next focus target. When it cannot find such child, it calls - * {@link LayoutManager#onFocusSearchFailed(View, int, Recycler, State)} to layout more Views - * in the focus search direction. If LayoutManager adds a View that matches the - * focus search criteria, it will be returned as the focus search result. Otherwise, - * RecyclerView will call parent to handle the focus search like a regular ViewGroup. - *

      - * When the direction is {@link View#FOCUS_FORWARD} or {@link View#FOCUS_BACKWARD}, a View that - * is not in the focus direction is still valid focus target which may not be the desired - * behavior if the Adapter has more children in the focus direction. To handle this case, - * RecyclerView converts the focus direction to an absolute direction and makes a preliminary - * focus search in that direction. If there are no Views to gain focus, it will call - * {@link LayoutManager#onFocusSearchFailed(View, int, Recycler, State)} before running a - * focus search with the original (relative) direction. This allows RecyclerView to provide - * better candidates to the focus search while still allowing the view system to take focus from - * the RecyclerView and give it to a more suitable child if such child exists. - * - * @param focused The view that currently has focus - * @param direction One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN}, - * {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT}, {@link View#FOCUS_FORWARD}, - * {@link View#FOCUS_BACKWARD} or 0 for not applicable. - * - * @return A new View that can be the next focus after the focused View - */ + // Focus handling + @Override public View focusSearch(View focused, int direction) { View result = mLayout.onInterceptFocusSearch(focused, direction); if (result != null) { return result; } - final boolean canRunFocusFailure = mAdapter != null && mLayout != null - && !isComputingLayout() && !mLayoutFrozen; - final FocusFinder ff = FocusFinder.getInstance(); - if (canRunFocusFailure - && (direction == View.FOCUS_FORWARD || direction == View.FOCUS_BACKWARD)) { - // convert direction to absolute direction and see if we have a view there and if not - // tell LayoutManager to add if it can. - boolean needsFocusFailureLayout = false; - if (mLayout.canScrollVertically()) { - final int absDir = - direction == View.FOCUS_FORWARD ? View.FOCUS_DOWN : View.FOCUS_UP; - final View found = ff.findNextFocus(this, focused, absDir); - needsFocusFailureLayout = found == null; - } - if (!needsFocusFailureLayout && mLayout.canScrollHorizontally()) { - boolean rtl = mLayout.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL; - final int absDir = (direction == View.FOCUS_FORWARD) ^ rtl - ? View.FOCUS_RIGHT : View.FOCUS_LEFT; - final View found = ff.findNextFocus(this, focused, absDir); - needsFocusFailureLayout = found == null; - } - if (needsFocusFailureLayout) { - consumePendingUpdateOperations(); - final View focusedItemView = findContainingItemView(focused); - if (focusedItemView == null) { - // panic, focused view is not a child anymore, cannot call super. - return null; - } - eatRequestLayout(); - mLayout.onFocusSearchFailed(focused, direction, mRecycler, mState); - resumeRequestLayout(false); - } - result = ff.findNextFocus(this, focused, direction); - } else { - result = ff.findNextFocus(this, focused, direction); - if (result == null && canRunFocusFailure) { - consumePendingUpdateOperations(); - final View focusedItemView = findContainingItemView(focused); - if (focusedItemView == null) { - // panic, focused view is not a child anymore, cannot call super. - return null; - } - eatRequestLayout(); - result = mLayout.onFocusSearchFailed(focused, direction, mRecycler, mState); - resumeRequestLayout(false); - } + result = ff.findNextFocus(this, focused, direction); + if (result == null && mAdapter != null) { + eatRequestLayout(); + result = mLayout.onFocusSearchFailed(focused, direction, mRecycler, mState); + resumeRequestLayout(false); } - return isPreferredNextFocus(focused, result, direction) - ? result : super.focusSearch(focused, direction); - } - - /** - * Checks if the new focus candidate is a good enough candidate such that RecyclerView will - * assign it as the next focus View instead of letting view hierarchy decide. - * A good candidate means a View that is aligned in the focus direction wrt the focused View - * and is not the RecyclerView itself. - * When this method returns false, RecyclerView will let the parent make the decision so the - * same View may still get the focus as a result of that search. - */ - private boolean isPreferredNextFocus(View focused, View next, int direction) { - if (next == null || next == this) { - return false; - } - if (focused == null) { - return true; - } - - if(direction == View.FOCUS_FORWARD || direction == View.FOCUS_BACKWARD) { - final boolean rtl = mLayout.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL; - final int absHorizontal = (direction == View.FOCUS_FORWARD) ^ rtl - ? View.FOCUS_RIGHT : View.FOCUS_LEFT; - if (isPreferredNextFocusAbsolute(focused, next, absHorizontal)) { - return true; - } - if (direction == View.FOCUS_FORWARD) { - return isPreferredNextFocusAbsolute(focused, next, View.FOCUS_DOWN); - } else { - return isPreferredNextFocusAbsolute(focused, next, View.FOCUS_UP); - } - } else { - return isPreferredNextFocusAbsolute(focused, next, direction); - } - - } - - /** - * Logic taken from FocusSearch#isCandidate - */ - private boolean isPreferredNextFocusAbsolute(View focused, View next, int direction) { - mTempRect.set(0, 0, focused.getWidth(), focused.getHeight()); - mTempRect2.set(0, 0, next.getWidth(), next.getHeight()); - offsetDescendantRectToMyCoords(focused, mTempRect); - offsetDescendantRectToMyCoords(next, mTempRect2); - switch (direction) { - case View.FOCUS_LEFT: - return (mTempRect.right > mTempRect2.right - || mTempRect.left >= mTempRect2.right) - && mTempRect.left > mTempRect2.left; - case View.FOCUS_RIGHT: - return (mTempRect.left < mTempRect2.left - || mTempRect.right <= mTempRect2.left) - && mTempRect.right < mTempRect2.right; - case View.FOCUS_UP: - return (mTempRect.bottom > mTempRect2.bottom - || mTempRect.top >= mTempRect2.bottom) - && mTempRect.top > mTempRect2.top; - case View.FOCUS_DOWN: - return (mTempRect.top < mTempRect2.top - || mTempRect.bottom <= mTempRect2.top) - && mTempRect.bottom < mTempRect2.bottom; - } - throw new IllegalArgumentException("direction must be absolute. received:" + direction); + return result != null ? result : super.focusSearch(focused, direction); } @Override public void requestChildFocus(View child, View focused) { if (!mLayout.onRequestChildFocus(this, mState, child, focused) && focused != null) { mTempRect.set(0, 0, focused.getWidth(), focused.getHeight()); - - // get item decor offsets w/o refreshing. If they are invalid, there will be another - // layout pass to fix them, then it is LayoutManager's responsibility to keep focused - // View in viewport. - final ViewGroup.LayoutParams focusedLayoutParams = focused.getLayoutParams(); - if (focusedLayoutParams instanceof LayoutParams) { - // if focused child has item decors, use them. Otherwise, ignore. - final LayoutParams lp = (LayoutParams) focusedLayoutParams; - if (!lp.mInsetsDirty) { - final Rect insets = lp.mDecorInsets; - mTempRect.left -= insets.left; - mTempRect.right += insets.right; - mTempRect.top -= insets.top; - mTempRect.bottom += insets.bottom; - } - } - offsetDescendantRectToMyCoords(focused, mTempRect); offsetRectIntoDescendantCoords(child, mTempRect); requestChildRectangleOnScreen(child, mTempRect, !mFirstLayoutComplete); @@ -2316,29 +1329,18 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro @Override public void addFocusables(ArrayList views, int direction, int focusableMode) { - if (mLayout == null || !mLayout.onAddFocusables(this, views, direction, focusableMode)) { + if (!mLayout.onAddFocusables(this, views, direction, focusableMode)) { super.addFocusables(views, direction, focusableMode); } } - @Override - protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { - if (isComputingLayout()) { - // if we are in the middle of a layout calculation, don't let any child take focus. - // RV will handle it after layout calculation is finished. - return false; - } - return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); - } - @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); - mLayoutOrScrollCounter = 0; mIsAttached = true; - mFirstLayoutComplete = mFirstLayoutComplete && !isLayoutRequested(); + mFirstLayoutComplete = false; if (mLayout != null) { - mLayout.dispatchAttachedToWindow(this); + mLayout.onAttachedToWindow(this); } mPostedAnimatorRunner = false; } @@ -2349,21 +1351,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro if (mItemAnimator != null) { mItemAnimator.endAnimations(); } + mFirstLayoutComplete = false; + stopScroll(); mIsAttached = false; if (mLayout != null) { - mLayout.dispatchDetachedFromWindow(this, mRecycler); + mLayout.onDetachedFromWindow(this, mRecycler); } removeCallbacks(mItemAnimatorRunner); - mViewInfoStore.onDetach(); - } - - /** - * Returns true if RecyclerView is attached to window. - */ - // @override - public boolean isAttachedToWindow() { - return mIsAttached; } /** @@ -2374,7 +1369,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @see #assertNotInLayoutOrScroll(String) */ void assertInLayoutOrScroll(String message) { - if (!isComputingLayout()) { + if (!mRunningLayoutOrScroll) { if (message == null) { throw new IllegalStateException("Cannot call this method unless RecyclerView is " + "computing a layout or scrolling"); @@ -2392,20 +1387,13 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @see #assertInLayoutOrScroll(String) */ void assertNotInLayoutOrScroll(String message) { - if (isComputingLayout()) { + if (mRunningLayoutOrScroll) { if (message == null) { throw new IllegalStateException("Cannot call this method while RecyclerView is " + "computing a layout or scrolling"); } throw new IllegalStateException(message); } - if (mDispatchScrollCounter > 0) { - Log.w(TAG, "Cannot call this method in a scroll callback. Scroll callbacks might be run" - + " during a measure & layout pass where you cannot change the RecyclerView" - + " data. Any method call that might change the structure of the RecyclerView" - + " or the adapter contents should be postponed to the next frame.", - new IllegalStateException("")); - } } /** @@ -2419,7 +1407,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * for each incoming MotionEvent until the end of the gesture.

      * * @param listener Listener to add - * @see SimpleOnItemTouchListener */ public void addOnItemTouchListener(OnItemTouchListener listener) { mOnItemTouchListeners.add(listener); @@ -2487,20 +1474,11 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro @Override public boolean onInterceptTouchEvent(MotionEvent e) { - if (mLayoutFrozen) { - // When layout is frozen, RV does not intercept the motion event. - // A child view e.g. a button may still get the click. - return false; - } if (dispatchOnItemTouchIntercept(e)) { cancelTouch(); return true; } - if (mLayout == null) { - return false; - } - final boolean canScrollHorizontally = mLayout.canScrollHorizontally(); final boolean canScrollVertically = mLayout.canScrollVertically(); @@ -2514,10 +1492,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro switch (action) { case MotionEvent.ACTION_DOWN: - if (mIgnoreMotionEventTillDown) { - mIgnoreMotionEventTillDown = false; - } - mScrollPointerId = e.getPointerId(0); + mScrollPointerId = MotionEventCompat.getPointerId(e, 0); mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f); mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f); @@ -2525,36 +1500,24 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro getParent().requestDisallowInterceptTouchEvent(true); setScrollState(SCROLL_STATE_DRAGGING); } - - // Clear the nested offsets - mNestedOffsets[0] = mNestedOffsets[1] = 0; - - int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE; - if (canScrollHorizontally) { - nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL; - } - if (canScrollVertically) { - nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL; - } - startNestedScroll(nestedScrollAxis); break; case MotionEventCompat.ACTION_POINTER_DOWN: - mScrollPointerId = e.getPointerId(actionIndex); - mInitialTouchX = mLastTouchX = (int) (e.getX(actionIndex) + 0.5f); - mInitialTouchY = mLastTouchY = (int) (e.getY(actionIndex) + 0.5f); + mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex); + mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f); + mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f); break; case MotionEvent.ACTION_MOVE: { - final int index = e.findPointerIndex(mScrollPointerId); + final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId); if (index < 0) { Log.e(TAG, "Error processing scroll; pointer index for id " + mScrollPointerId + " not found. Did any MotionEvents get skipped?"); return false; } - final int x = (int) (e.getX(index) + 0.5f); - final int y = (int) (e.getY(index) + 0.5f); + final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f); + final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f); if (mScrollState != SCROLL_STATE_DRAGGING) { final int dx = x - mInitialTouchX; final int dy = y - mInitialTouchY; @@ -2568,6 +1531,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro startScroll = true; } if (startScroll) { + getParent().requestDisallowInterceptTouchEvent(true); setScrollState(SCROLL_STATE_DRAGGING); } } @@ -2579,7 +1543,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro case MotionEvent.ACTION_UP: { mVelocityTracker.clear(); - stopNestedScroll(); } break; case MotionEvent.ACTION_CANCEL: { @@ -2589,125 +1552,72 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return mScrollState == SCROLL_STATE_DRAGGING; } - @Override - public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { - final int listenerCount = mOnItemTouchListeners.size(); - for (int i = 0; i < listenerCount; i++) { - final OnItemTouchListener listener = mOnItemTouchListeners.get(i); - listener.onRequestDisallowInterceptTouchEvent(disallowIntercept); - } - super.requestDisallowInterceptTouchEvent(disallowIntercept); - } - @Override public boolean onTouchEvent(MotionEvent e) { - if (mLayoutFrozen || mIgnoreMotionEventTillDown) { - return false; - } if (dispatchOnItemTouch(e)) { cancelTouch(); return true; } - if (mLayout == null) { - return false; - } - final boolean canScrollHorizontally = mLayout.canScrollHorizontally(); final boolean canScrollVertically = mLayout.canScrollVertically(); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } - boolean eventAddedToVelocityTracker = false; + mVelocityTracker.addMovement(e); - final MotionEvent vtev = MotionEvent.obtain(e); final int action = MotionEventCompat.getActionMasked(e); final int actionIndex = MotionEventCompat.getActionIndex(e); - if (action == MotionEvent.ACTION_DOWN) { - mNestedOffsets[0] = mNestedOffsets[1] = 0; - } - vtev.offsetLocation(mNestedOffsets[0], mNestedOffsets[1]); - switch (action) { case MotionEvent.ACTION_DOWN: { - mScrollPointerId = e.getPointerId(0); + mScrollPointerId = MotionEventCompat.getPointerId(e, 0); mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f); mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f); - - int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE; - if (canScrollHorizontally) { - nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL; - } - if (canScrollVertically) { - nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL; - } - startNestedScroll(nestedScrollAxis); } break; case MotionEventCompat.ACTION_POINTER_DOWN: { - mScrollPointerId = e.getPointerId(actionIndex); - mInitialTouchX = mLastTouchX = (int) (e.getX(actionIndex) + 0.5f); - mInitialTouchY = mLastTouchY = (int) (e.getY(actionIndex) + 0.5f); + mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex); + mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f); + mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f); } break; case MotionEvent.ACTION_MOVE: { - final int index = e.findPointerIndex(mScrollPointerId); + final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId); if (index < 0) { Log.e(TAG, "Error processing scroll; pointer index for id " + mScrollPointerId + " not found. Did any MotionEvents get skipped?"); return false; } - final int x = (int) (e.getX(index) + 0.5f); - final int y = (int) (e.getY(index) + 0.5f); - int dx = mLastTouchX - x; - int dy = mLastTouchY - y; - - if (dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset)) { - dx -= mScrollConsumed[0]; - dy -= mScrollConsumed[1]; - vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]); - // Updated the nested offsets - mNestedOffsets[0] += mScrollOffset[0]; - mNestedOffsets[1] += mScrollOffset[1]; - } - + final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f); + final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f); if (mScrollState != SCROLL_STATE_DRAGGING) { + final int dx = x - mInitialTouchX; + final int dy = y - mInitialTouchY; boolean startScroll = false; if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) { - if (dx > 0) { - dx -= mTouchSlop; - } else { - dx += mTouchSlop; - } + mLastTouchX = mInitialTouchX + mTouchSlop * (dx < 0 ? -1 : 1); startScroll = true; } if (canScrollVertically && Math.abs(dy) > mTouchSlop) { - if (dy > 0) { - dy -= mTouchSlop; - } else { - dy += mTouchSlop; - } + mLastTouchY = mInitialTouchY + mTouchSlop * (dy < 0 ? -1 : 1); startScroll = true; } if (startScroll) { + getParent().requestDisallowInterceptTouchEvent(true); setScrollState(SCROLL_STATE_DRAGGING); } } - if (mScrollState == SCROLL_STATE_DRAGGING) { - mLastTouchX = x - mScrollOffset[0]; - mLastTouchY = y - mScrollOffset[1]; - - if (scrollByInternal( - canScrollHorizontally ? dx : 0, - canScrollVertically ? dy : 0, - vtev)) { - getParent().requestDisallowInterceptTouchEvent(true); - } + final int dx = x - mLastTouchX; + final int dy = y - mLastTouchY; + scrollByInternal(canScrollHorizontally ? -dx : 0, + canScrollVertically ? -dy : 0); } + mLastTouchX = x; + mLastTouchY = y; } break; case MotionEventCompat.ACTION_POINTER_UP: { @@ -2715,8 +1625,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } break; case MotionEvent.ACTION_UP: { - mVelocityTracker.addMovement(vtev); - eventAddedToVelocityTracker = true; mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity); final float xvel = canScrollHorizontally ? -VelocityTrackerCompat.getXVelocity(mVelocityTracker, mScrollPointerId) : 0; @@ -2725,7 +1633,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro if (!((xvel != 0 || yvel != 0) && fling((int) xvel, (int) yvel))) { setScrollState(SCROLL_STATE_IDLE); } - resetTouch(); + mVelocityTracker.clear(); + releaseGlows(); } break; case MotionEvent.ACTION_CANCEL: { @@ -2733,176 +1642,57 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } break; } - if (!eventAddedToVelocityTracker) { - mVelocityTracker.addMovement(vtev); - } - vtev.recycle(); - return true; } - private void resetTouch() { + private void cancelTouch() { if (mVelocityTracker != null) { mVelocityTracker.clear(); } - stopNestedScroll(); releaseGlows(); - } - - private void cancelTouch() { - resetTouch(); setScrollState(SCROLL_STATE_IDLE); } private void onPointerUp(MotionEvent e) { final int actionIndex = MotionEventCompat.getActionIndex(e); - if (e.getPointerId(actionIndex) == mScrollPointerId) { + if (MotionEventCompat.getPointerId(e, actionIndex) == mScrollPointerId) { // Pick a new pointer to pick up the slack. final int newIndex = actionIndex == 0 ? 1 : 0; - mScrollPointerId = e.getPointerId(newIndex); - mInitialTouchX = mLastTouchX = (int) (e.getX(newIndex) + 0.5f); - mInitialTouchY = mLastTouchY = (int) (e.getY(newIndex) + 0.5f); + mScrollPointerId = MotionEventCompat.getPointerId(e, newIndex); + mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, newIndex) + 0.5f); + mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, newIndex) + 0.5f); } } - // @Override - public boolean onGenericMotionEvent(MotionEvent event) { - if (mLayout == null) { - return false; - } - if (mLayoutFrozen) { - return false; - } - if ((event.getSource() & InputDeviceCompat.SOURCE_CLASS_POINTER) != 0) { - if (event.getAction() == MotionEventCompat.ACTION_SCROLL) { - final float vScroll, hScroll; - if (mLayout.canScrollVertically()) { - // Inverse the sign of the vertical scroll to align the scroll orientation - // with AbsListView. - vScroll = -MotionEventCompat - .getAxisValue(event, MotionEventCompat.AXIS_VSCROLL); - } else { - vScroll = 0f; - } - if (mLayout.canScrollHorizontally()) { - hScroll = MotionEventCompat - .getAxisValue(event, MotionEventCompat.AXIS_HSCROLL); - } else { - hScroll = 0f; - } - - if (vScroll != 0 || hScroll != 0) { - final float scrollFactor = getScrollFactor(); - scrollByInternal((int) (hScroll * scrollFactor), - (int) (vScroll * scrollFactor), event); - } - } - } - return false; - } - - /** - * Ported from View.getVerticalScrollFactor. - */ - private float getScrollFactor() { - if (mScrollFactor == Float.MIN_VALUE) { - TypedValue outValue = new TypedValue(); - if (getContext().getTheme().resolveAttribute( - android.R.attr.listPreferredItemHeight, outValue, true)) { - mScrollFactor = outValue.getDimension( - getContext().getResources().getDisplayMetrics()); - } else { - return 0; //listPreferredItemHeight is not defined, no generic scrolling - } - } - return mScrollFactor; - } - @Override protected void onMeasure(int widthSpec, int heightSpec) { - if (mLayout == null) { - defaultOnMeasure(widthSpec, heightSpec); - return; - } - if (mLayout.mAutoMeasure) { - final int widthMode = MeasureSpec.getMode(widthSpec); - final int heightMode = MeasureSpec.getMode(heightSpec); - final boolean skipMeasure = widthMode == MeasureSpec.EXACTLY - && heightMode == MeasureSpec.EXACTLY; - mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec); - if (skipMeasure || mAdapter == null) { - return; - } - if (mState.mLayoutStep == State.STEP_START) { - dispatchLayoutStep1(); - } - // set dimensions in 2nd step. Pre-layout should happen with old dimensions for - // consistency - mLayout.setMeasureSpecs(widthSpec, heightSpec); - mState.mIsMeasuring = true; - dispatchLayoutStep2(); - - // now we can get the width and height from the children. - mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec); - - // if RecyclerView has non-exact width and height and if there is at least one child - // which also has non-exact width & height, we have to re-measure. - if (mLayout.shouldMeasureTwice()) { - mLayout.setMeasureSpecs( - MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); - mState.mIsMeasuring = true; - dispatchLayoutStep2(); - // now we can get the width and height from the children. - mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec); - } - } else { - if (mHasFixedSize) { - mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec); - return; - } - // custom onMeasure - if (mAdapterUpdateDuringMeasure) { - eatRequestLayout(); - processAdapterUpdatesAndSetAnimationFlags(); - - if (mState.mRunPredictiveAnimations) { - mState.mInPreLayout = true; - } else { - // consume remaining updates to provide a consistent state with the layout pass. - mAdapterHelper.consumeUpdatesInOnePass(); - mState.mInPreLayout = false; - } - mAdapterUpdateDuringMeasure = false; - resumeRequestLayout(false); - } - - if (mAdapter != null) { - mState.mItemCount = mAdapter.getItemCount(); - } else { - mState.mItemCount = 0; - } + if (mAdapterUpdateDuringMeasure) { eatRequestLayout(); - mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec); + processAdapterUpdatesAndSetAnimationFlags(); + + if (mState.mRunPredictiveAnimations) { + // TODO: try to provide a better approach. + // When RV decides to run predictive animations, we need to measure in pre-layout + // state so that pre-layout pass results in correct layout. + // On the other hand, this will prevent the layout manager from resizing properly. + mState.mInPreLayout = true; + } else { + // consume remaining updates to provide a consistent state with the layout pass. + mAdapterHelper.consumeUpdatesInOnePass(); + mState.mInPreLayout = false; + } + mAdapterUpdateDuringMeasure = false; resumeRequestLayout(false); - mState.mInPreLayout = false; // clear } - } - /** - * Used when onMeasure is called before layout manager is set - */ - void defaultOnMeasure(int widthSpec, int heightSpec) { - // calling LayoutManager here is not pretty but that API is already public and it is better - // than creating another method since this is internal. - final int width = LayoutManager.chooseSize(widthSpec, - getPaddingLeft() + getPaddingRight(), - ViewCompat.getMinimumWidth(this)); - final int height = LayoutManager.chooseSize(heightSpec, - getPaddingTop() + getPaddingBottom(), - ViewCompat.getMinimumHeight(this)); + if (mAdapter != null) { + mState.mItemCount = mAdapter.getItemCount(); + } else { + mState.mItemCount = 0; + } - setMeasuredDimension(width, height); + mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec); + mState.mInPreLayout = false; // clear } @Override @@ -2910,7 +1700,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro super.onSizeChanged(w, h, oldw, oldh); if (w != oldw || h != oldh) { invalidateGlows(); - // layout's w/h are updated during measure/layout steps. } } @@ -2936,91 +1725,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } } - private void onEnterLayoutOrScroll() { - mLayoutOrScrollCounter ++; - } - - private void onExitLayoutOrScroll() { - mLayoutOrScrollCounter --; - if (mLayoutOrScrollCounter < 1) { - if (DEBUG && mLayoutOrScrollCounter < 0) { - throw new IllegalStateException("layout or scroll counter cannot go below zero." - + "Some calls are not matching"); - } - mLayoutOrScrollCounter = 0; - dispatchContentChangedIfNecessary(); - } - } - - boolean isAccessibilityEnabled() { - return mAccessibilityManager != null && mAccessibilityManager.isEnabled(); - } - - private void dispatchContentChangedIfNecessary() { - final int flags = mEatenAccessibilityChangeFlags; - mEatenAccessibilityChangeFlags = 0; - if (flags != 0 && isAccessibilityEnabled()) { - final AccessibilityEvent event = AccessibilityEvent.obtain(); - event.setEventType(AccessibilityEventCompat.TYPE_WINDOW_CONTENT_CHANGED); - AccessibilityEventCompat.setContentChangeTypes(event, flags); - sendAccessibilityEventUnchecked(event); - } - } - - /** - * Returns whether RecyclerView is currently computing a layout. - *

      - * If this method returns true, it means that RecyclerView is in a lockdown state and any - * attempt to update adapter contents will result in an exception because adapter contents - * cannot be changed while RecyclerView is trying to compute the layout. - *

      - * It is very unlikely that your code will be running during this state as it is - * called by the framework when a layout traversal happens or RecyclerView starts to scroll - * in response to system events (touch, accessibility etc). - *

      - * This case may happen if you have some custom logic to change adapter contents in - * response to a View callback (e.g. focus change callback) which might be triggered during a - * layout calculation. In these cases, you should just postpone the change using a Handler or a - * similar mechanism. - * - * @return true if RecyclerView is currently computing a layout, false - * otherwise - */ - public boolean isComputingLayout() { - return mLayoutOrScrollCounter > 0; - } - - /** - * Returns true if an accessibility event should not be dispatched now. This happens when an - * accessibility request arrives while RecyclerView does not have a stable state which is very - * hard to handle for a LayoutManager. Instead, this method records necessary information about - * the event and dispatches a window change event after the critical section is finished. - * - * @return True if the accessibility event should be postponed. - */ - boolean shouldDeferAccessibilityEvent(AccessibilityEvent event) { - if (isComputingLayout()) { - int type = 0; - if (event != null) { - type = AccessibilityEventCompat.getContentChangeTypes(event); - } - if (type == 0) { - type = AccessibilityEventCompat.CONTENT_CHANGE_TYPE_UNDEFINED; - } - mEatenAccessibilityChangeFlags |= type; - return true; - } - return false; - } - - @Override - public void sendAccessibilityEventUnchecked(AccessibilityEvent event) { - if (shouldDeferAccessibilityEvent(event)) { - return; - } - super.sendAccessibilityEventUnchecked(event); - } - /** * Gets the current ItemAnimator for this RecyclerView. A null return value * indicates that there is no animator and that item changes will happen without @@ -3034,6 +1738,10 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return mItemAnimator; } + private boolean supportsChangeAnimations() { + return mItemAnimator != null && mItemAnimator.getSupportsChangeAnimations(); + } + /** * Post a runnable to the next frame to run pending item animations. Only the first such * request will be posted, governed by the mPostedAnimatorRunner flag. @@ -3066,12 +1774,13 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // simple animations are a subset of advanced animations (which will cause a // pre-layout step) // If layout supports predictive animations, pre-process to decide if we want to run them - if (predictiveItemAnimationsEnabled()) { + if (mItemAnimator != null && mLayout.supportsPredictiveItemAnimations()) { mAdapterHelper.preProcess(); } else { mAdapterHelper.consumeUpdatesInOnePass(); } - boolean animationTypeSupported = mItemsAddedOrRemoved || mItemsChanged; + boolean animationTypeSupported = (mItemsAddedOrRemoved && !mItemsChanged) || + (mItemsAddedOrRemoved || (mItemsChanged && supportsChangeAnimations())); mState.mRunSimpleAnimations = mFirstLayoutComplete && mItemAnimator != null && (mDataSetHasChangedAfterLayout || animationTypeSupported || mLayout.mRequestedSimpleAnimations) && @@ -3097,159 +1806,43 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * The overall approach figures out what items exist before/after layout and * infers one of the five above states for each of the items. Then the animations * are set up accordingly: - * PERSISTENT views are animated via - * {@link ItemAnimator#animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo)} - * DISAPPEARING views are animated via - * {@link ItemAnimator#animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo)} - * APPEARING views are animated via - * {@link ItemAnimator#animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo)} - * and changed views are animated via - * {@link ItemAnimator#animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo)}. + * PERSISTENT views are moved ({@link ItemAnimator#animateMove(ViewHolder, int, int, int, int)}) + * REMOVED views are removed ({@link ItemAnimator#animateRemove(ViewHolder)}) + * ADDED views are added ({@link ItemAnimator#animateAdd(ViewHolder)}) + * DISAPPEARING views are moved off screen + * APPEARING views are moved on screen */ void dispatchLayout() { if (mAdapter == null) { Log.e(TAG, "No adapter attached; skipping layout"); - // leave the state in START return; } - if (mLayout == null) { - Log.e(TAG, "No layout manager attached; skipping layout"); - // leave the state in START - return; - } - mState.mIsMeasuring = false; - if (mState.mLayoutStep == State.STEP_START) { - dispatchLayoutStep1(); - mLayout.setExactMeasureSpecsFrom(this); - dispatchLayoutStep2(); - } else if (mAdapterHelper.hasUpdates() || mLayout.getWidth() != getWidth() || - mLayout.getHeight() != getHeight()) { - // First 2 steps are done in onMeasure but looks like we have to run again due to - // changed size. - mLayout.setExactMeasureSpecsFrom(this); - dispatchLayoutStep2(); - } else { - // always make sure we sync them (to ensure mode is exact) - mLayout.setExactMeasureSpecsFrom(this); - } - dispatchLayoutStep3(); - } - - private void saveFocusInfo() { - View child = null; - if (mPreserveFocusAfterLayout && hasFocus() && mAdapter != null) { - child = getFocusedChild(); - } - - final ViewHolder focusedVh = child == null ? null : findContainingViewHolder(child); - if (focusedVh == null) { - resetFocusInfo(); - } else { - mState.mFocusedItemId = mAdapter.hasStableIds() ? focusedVh.getItemId() : NO_ID; - mState.mFocusedItemPosition = mDataSetHasChangedAfterLayout ? NO_POSITION : - focusedVh.getAdapterPosition(); - mState.mFocusedSubChildId = getDeepestFocusedViewWithId(focusedVh.itemView); - } - } - - private void resetFocusInfo() { - mState.mFocusedItemId = NO_ID; - mState.mFocusedItemPosition = NO_POSITION; - mState.mFocusedSubChildId = View.NO_ID; - } - - private void recoverFocusFromState() { - if (!mPreserveFocusAfterLayout || mAdapter == null || !hasFocus()) { - return; - } - // only recover focus if RV itself has the focus or the focused view is hidden - if (!isFocused()) { - final View focusedChild = getFocusedChild(); - if (focusedChild == null || !mChildHelper.isHidden(focusedChild)) { - return; - } - } - ViewHolder focusTarget = null; - if (mState.mFocusedItemPosition != NO_POSITION) { - focusTarget = findViewHolderForAdapterPosition(mState.mFocusedItemPosition); - } - if (focusTarget == null && mState.mFocusedItemId != NO_ID && mAdapter.hasStableIds()) { - focusTarget = findViewHolderForItemId(mState.mFocusedItemId); - } - if (focusTarget == null || focusTarget.itemView.hasFocus() || - !focusTarget.itemView.hasFocusable()) { - return; - } - // looks like the focused item has been replaced with another view that represents the - // same item in the adapter. Request focus on that. - View viewToFocus = focusTarget.itemView; - if (mState.mFocusedSubChildId != NO_ID) { - View child = focusTarget.itemView.findViewById(mState.mFocusedSubChildId); - if (child != null && child.isFocusable()) { - viewToFocus = child; - } - } - viewToFocus.requestFocus(); - } - - private int getDeepestFocusedViewWithId(View view) { - int lastKnownId = view.getId(); - while (!view.isFocused() && view instanceof ViewGroup && view.hasFocus()) { - view = ((ViewGroup) view).getFocusedChild(); - final int id = view.getId(); - if (id != View.NO_ID) { - lastKnownId = view.getId(); - } - } - return lastKnownId; - } - - /** - * The first step of a layout where we; - * - process adapter updates - * - decide which animation should run - * - save information about current views - * - If necessary, run predictive layout and save its information - */ - private void dispatchLayoutStep1() { - mState.assertLayoutStep(State.STEP_START); - mState.mIsMeasuring = false; + mDisappearingViewsInLayoutPass.clear(); eatRequestLayout(); - mViewInfoStore.clear(); - onEnterLayoutOrScroll(); - saveFocusInfo(); + mRunningLayoutOrScroll = true; + processAdapterUpdatesAndSetAnimationFlags(); - mState.mTrackOldChangeHolders = mState.mRunSimpleAnimations && mItemsChanged; + + mState.mOldChangedHolders = mState.mRunSimpleAnimations && mItemsChanged + && supportsChangeAnimations() ? new ArrayMap() : null; mItemsAddedOrRemoved = mItemsChanged = false; + ArrayMap appearingViewInitialBounds = null; mState.mInPreLayout = mState.mRunPredictiveAnimations; mState.mItemCount = mAdapter.getItemCount(); - findMinMaxChildLayoutPositions(mMinMaxLayoutPositions); if (mState.mRunSimpleAnimations) { // Step 0: Find out where all non-removed items are, pre-layout + mState.mPreLayoutHolderMap.clear(); + mState.mPostLayoutHolderMap.clear(); int count = mChildHelper.getChildCount(); for (int i = 0; i < count; ++i) { final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); if (holder.shouldIgnore() || (holder.isInvalid() && !mAdapter.hasStableIds())) { continue; } - final ItemHolderInfo animationInfo = mItemAnimator - .recordPreLayoutInformation(mState, holder, - ItemAnimator.buildAdapterChangeFlagsForAnimations(holder), - holder.getUnmodifiedPayloads()); - mViewInfoStore.addToPreLayout(holder, animationInfo); - if (mState.mTrackOldChangeHolders && holder.isUpdated() && !holder.isRemoved() - && !holder.shouldIgnore() && !holder.isInvalid()) { - long key = getChangedHolderKey(holder); - // This is NOT the only place where a ViewHolder is added to old change holders - // list. There is another case where: - // * A VH is currently hidden but not deleted - // * The hidden item is changed in the adapter - // * Layout manager decides to layout the item in the pre-Layout pass (step1) - // When this case is detected, RV will un-hide that view and add to the old - // change holders list. - mViewInfoStore.addToOldChangeHolders(key, holder); - } + final View view = holder.itemView; + mState.mPreLayoutHolderMap.put(holder, new ItemHolderInfo(holder, + view.getLeft(), view.getTop(), view.getRight(), view.getBottom())); } } if (mState.mRunPredictiveAnimations) { @@ -3260,53 +1853,63 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // Save old positions so that LayoutManager can run its mapping logic. saveOldPositions(); + // processAdapterUpdatesAndSetAnimationFlags already run pre-layout animations. + if (mState.mOldChangedHolders != null) { + int count = mChildHelper.getChildCount(); + for (int i = 0; i < count; ++i) { + final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); + if (holder.isChanged() && !holder.isRemoved() && !holder.shouldIgnore()) { + long key = getChangedHolderKey(holder); + mState.mOldChangedHolders.put(key, holder); + mState.mPreLayoutHolderMap.remove(holder); + } + } + } + final boolean didStructureChange = mState.mStructureChanged; mState.mStructureChanged = false; // temporarily disable flag because we are asking for previous layout mLayout.onLayoutChildren(mRecycler, mState); mState.mStructureChanged = didStructureChange; + appearingViewInitialBounds = new ArrayMap(); for (int i = 0; i < mChildHelper.getChildCount(); ++i) { - final View child = mChildHelper.getChildAt(i); - final ViewHolder viewHolder = getChildViewHolderInt(child); - if (viewHolder.shouldIgnore()) { + boolean found = false; + View child = mChildHelper.getChildAt(i); + if (getChildViewHolderInt(child).shouldIgnore()) { continue; } - if (!mViewInfoStore.isInPreLayout(viewHolder)) { - int flags = ItemAnimator.buildAdapterChangeFlagsForAnimations(viewHolder); - boolean wasHidden = viewHolder - .hasAnyOfTheFlags(ViewHolder.FLAG_BOUNCED_FROM_HIDDEN_LIST); - if (!wasHidden) { - flags |= ItemAnimator.FLAG_APPEARED_IN_PRE_LAYOUT; - } - final ItemHolderInfo animationInfo = mItemAnimator.recordPreLayoutInformation( - mState, viewHolder, flags, viewHolder.getUnmodifiedPayloads()); - if (wasHidden) { - recordAnimationInfoIfBouncedHiddenView(viewHolder, animationInfo); - } else { - mViewInfoStore.addToAppearedInPreLayoutHolders(viewHolder, animationInfo); + for (int j = 0; j < mState.mPreLayoutHolderMap.size(); ++j) { + ViewHolder holder = mState.mPreLayoutHolderMap.keyAt(j); + if (holder.itemView == child) { + found = true; + break; } } + if (!found) { + appearingViewInitialBounds.put(child, new Rect(child.getLeft(), child.getTop(), + child.getRight(), child.getBottom())); + } } // we don't process disappearing list because they may re-appear in post layout pass. clearOldPositions(); + mAdapterHelper.consumePostponedUpdates(); } else { clearOldPositions(); + // in case pre layout did run but we decided not to run predictive animations. + mAdapterHelper.consumeUpdatesInOnePass(); + if (mState.mOldChangedHolders != null) { + int count = mChildHelper.getChildCount(); + for (int i = 0; i < count; ++i) { + final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); + if (holder.isChanged() && !holder.isRemoved() && !holder.shouldIgnore()) { + long key = getChangedHolderKey(holder); + mState.mOldChangedHolders.put(key, holder); + mState.mPreLayoutHolderMap.remove(holder); + } + } + } } - onExitLayoutOrScroll(); - resumeRequestLayout(false); - mState.mLayoutStep = State.STEP_LAYOUT; - } - - /** - * The second layout step where we do the actual layout of the views for the final state. - * This step might be run multiple times if necessary (e.g. measure). - */ - private void dispatchLayoutStep2() { - eatRequestLayout(); - onEnterLayoutOrScroll(); - mState.assertLayoutStep(State.STEP_LAYOUT | State.STEP_ANIMATIONS); - mAdapterHelper.consumeUpdatesInOnePass(); mState.mItemCount = mAdapter.getItemCount(); mState.mDeletedInvisibleItemCountSincePreviousLayout = 0; @@ -3319,199 +1922,110 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // onLayoutChildren may have caused client code to disable item animations; re-check mState.mRunSimpleAnimations = mState.mRunSimpleAnimations && mItemAnimator != null; - mState.mLayoutStep = State.STEP_ANIMATIONS; - onExitLayoutOrScroll(); - resumeRequestLayout(false); - } - /** - * The final step of the layout where we save the information about views for animations, - * trigger animations and do any necessary cleanup. - */ - private void dispatchLayoutStep3() { - mState.assertLayoutStep(State.STEP_ANIMATIONS); - eatRequestLayout(); - onEnterLayoutOrScroll(); - mState.mLayoutStep = State.STEP_START; if (mState.mRunSimpleAnimations) { - // Step 3: Find out where things are now, and process change animations. - // traverse list in reverse because we may call animateChange in the loop which may - // remove the target view holder. - for (int i = mChildHelper.getChildCount() - 1; i >= 0; i--) { + // Step 3: Find out where things are now, post-layout + ArrayMap newChangedHolders = mState.mOldChangedHolders != null ? + new ArrayMap() : null; + int count = mChildHelper.getChildCount(); + for (int i = 0; i < count; ++i) { ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); if (holder.shouldIgnore()) { continue; } + final View view = holder.itemView; long key = getChangedHolderKey(holder); - final ItemHolderInfo animationInfo = mItemAnimator - .recordPostLayoutInformation(mState, holder); - ViewHolder oldChangeViewHolder = mViewInfoStore.getFromOldChangeHolders(key); - if (oldChangeViewHolder != null && !oldChangeViewHolder.shouldIgnore()) { - // run a change animation - - // If an Item is CHANGED but the updated version is disappearing, it creates - // a conflicting case. - // Since a view that is marked as disappearing is likely to be going out of - // bounds, we run a change animation. Both views will be cleaned automatically - // once their animations finish. - // On the other hand, if it is the same view holder instance, we run a - // disappearing animation instead because we are not going to rebind the updated - // VH unless it is enforced by the layout manager. - final boolean oldDisappearing = mViewInfoStore.isDisappearing( - oldChangeViewHolder); - final boolean newDisappearing = mViewInfoStore.isDisappearing(holder); - if (oldDisappearing && oldChangeViewHolder == holder) { - // run disappear animation instead of change - mViewInfoStore.addToPostLayout(holder, animationInfo); - } else { - final ItemHolderInfo preInfo = mViewInfoStore.popFromPreLayout( - oldChangeViewHolder); - // we add and remove so that any post info is merged. - mViewInfoStore.addToPostLayout(holder, animationInfo); - ItemHolderInfo postInfo = mViewInfoStore.popFromPostLayout(holder); - if (preInfo == null) { - handleMissingPreInfoForChangeError(key, holder, oldChangeViewHolder); - } else { - animateChange(oldChangeViewHolder, holder, preInfo, postInfo, - oldDisappearing, newDisappearing); - } - } + if (newChangedHolders != null && mState.mOldChangedHolders.get(key) != null) { + newChangedHolders.put(key, holder); } else { - mViewInfoStore.addToPostLayout(holder, animationInfo); + mState.mPostLayoutHolderMap.put(holder, new ItemHolderInfo(holder, + view.getLeft(), view.getTop(), view.getRight(), view.getBottom())); } } + processDisappearingList(appearingViewInitialBounds); + // Step 4: Animate DISAPPEARING and REMOVED items + int preLayoutCount = mState.mPreLayoutHolderMap.size(); + for (int i = preLayoutCount - 1; i >= 0; i--) { + ViewHolder itemHolder = mState.mPreLayoutHolderMap.keyAt(i); + if (!mState.mPostLayoutHolderMap.containsKey(itemHolder)) { + ItemHolderInfo disappearingItem = mState.mPreLayoutHolderMap.valueAt(i); + mState.mPreLayoutHolderMap.removeAt(i); - // Step 4: Process view info lists and trigger animations - mViewInfoStore.process(mViewInfoProcessCallback); + View disappearingItemView = disappearingItem.holder.itemView; + removeDetachedView(disappearingItemView, false); + mRecycler.unscrapView(disappearingItem.holder); + + animateDisappearance(disappearingItem); + } + } + // Step 5: Animate APPEARING and ADDED items + int postLayoutCount = mState.mPostLayoutHolderMap.size(); + if (postLayoutCount > 0) { + for (int i = postLayoutCount - 1; i >= 0; i--) { + ViewHolder itemHolder = mState.mPostLayoutHolderMap.keyAt(i); + ItemHolderInfo info = mState.mPostLayoutHolderMap.valueAt(i); + if ((mState.mPreLayoutHolderMap.isEmpty() || + !mState.mPreLayoutHolderMap.containsKey(itemHolder))) { + mState.mPostLayoutHolderMap.removeAt(i); + Rect initialBounds = (appearingViewInitialBounds != null) ? + appearingViewInitialBounds.get(itemHolder.itemView) : null; + animateAppearance(itemHolder, initialBounds, + info.left, info.top); + } + } + } + // Step 6: Animate PERSISTENT items + count = mState.mPostLayoutHolderMap.size(); + for (int i = 0; i < count; ++i) { + ViewHolder postHolder = mState.mPostLayoutHolderMap.keyAt(i); + ItemHolderInfo postInfo = mState.mPostLayoutHolderMap.valueAt(i); + ItemHolderInfo preInfo = mState.mPreLayoutHolderMap.get(postHolder); + if (preInfo != null && postInfo != null) { + if (preInfo.left != postInfo.left || preInfo.top != postInfo.top) { + postHolder.setIsRecyclable(false); + if (DEBUG) { + Log.d(TAG, "PERSISTENT: " + postHolder + + " with view " + postHolder.itemView); + } + if (mItemAnimator.animateMove(postHolder, + preInfo.left, preInfo.top, postInfo.left, postInfo.top)) { + postAnimationRunner(); + } + } + } + } + // Step 7: Animate CHANGING items + count = mState.mOldChangedHolders != null ? mState.mOldChangedHolders.size() : 0; + // traverse reverse in case view gets recycled while we are traversing the list. + for (int i = count - 1; i >= 0; i--) { + long key = mState.mOldChangedHolders.keyAt(i); + ViewHolder oldHolder = mState.mOldChangedHolders.get(key); + View oldView = oldHolder.itemView; + if (oldHolder.shouldIgnore()) { + continue; + } + // We probably don't need this check anymore since these views are removed from + // the list if they are recycled. + if (mRecycler.mChangedScrap != null && + mRecycler.mChangedScrap.contains(oldHolder)) { + animateChange(oldHolder, newChangedHolders.get(key)); + } else if (DEBUG) { + Log.e(TAG, "cannot find old changed holder in changed scrap :/" + oldHolder); + } + } } - - mLayout.removeAndRecycleScrapInt(mRecycler); + resumeRequestLayout(false); + mLayout.removeAndRecycleScrapInt(mRecycler, !mState.mRunPredictiveAnimations); mState.mPreviousLayoutItemCount = mState.mItemCount; mDataSetHasChangedAfterLayout = false; mState.mRunSimpleAnimations = false; - mState.mRunPredictiveAnimations = false; + mRunningLayoutOrScroll = false; mLayout.mRequestedSimpleAnimations = false; if (mRecycler.mChangedScrap != null) { mRecycler.mChangedScrap.clear(); } - mLayout.onLayoutCompleted(mState); - onExitLayoutOrScroll(); - resumeRequestLayout(false); - mViewInfoStore.clear(); - if (didChildRangeChange(mMinMaxLayoutPositions[0], mMinMaxLayoutPositions[1])) { - dispatchOnScrolled(0, 0); - } - recoverFocusFromState(); - resetFocusInfo(); - } - - /** - * This handles the case where there is an unexpected VH missing in the pre-layout map. - *

      - * We might be able to detect the error in the application which will help the developer to - * resolve the issue. - *

      - * If it is not an expected error, we at least print an error to notify the developer and ignore - * the animation. - * - * https://code.google.com/p/android/issues/detail?id=193958 - * - * @param key The change key - * @param holder Current ViewHolder - * @param oldChangeViewHolder Changed ViewHolder - */ - private void handleMissingPreInfoForChangeError(long key, - ViewHolder holder, ViewHolder oldChangeViewHolder) { - // check if two VH have the same key, if so, print that as an error - final int childCount = mChildHelper.getChildCount(); - for (int i = 0; i < childCount; i++) { - View view = mChildHelper.getChildAt(i); - ViewHolder other = getChildViewHolderInt(view); - if (other == holder) { - continue; - } - final long otherKey = getChangedHolderKey(other); - if (otherKey == key) { - if (mAdapter != null && mAdapter.hasStableIds()) { - throw new IllegalStateException("Two different ViewHolders have the same stable" - + " ID. Stable IDs in your adapter MUST BE unique and SHOULD NOT" - + " change.\n ViewHolder 1:" + other + " \n View Holder 2:" + holder); - } else { - throw new IllegalStateException("Two different ViewHolders have the same change" - + " ID. This might happen due to inconsistent Adapter update events or" - + " if the LayoutManager lays out the same View multiple times." - + "\n ViewHolder 1:" + other + " \n View Holder 2:" + holder); - } - } - } - // Very unlikely to happen but if it does, notify the developer. - Log.e(TAG, "Problem while matching changed view holders with the new" - + "ones. The pre-layout information for the change holder " + oldChangeViewHolder - + " cannot be found but it is necessary for " + holder); - } - - /** - * Records the animation information for a view holder that was bounced from hidden list. It - * also clears the bounce back flag. - */ - private void recordAnimationInfoIfBouncedHiddenView(ViewHolder viewHolder, - ItemHolderInfo animationInfo) { - // looks like this view bounced back from hidden list! - viewHolder.setFlags(0, ViewHolder.FLAG_BOUNCED_FROM_HIDDEN_LIST); - if (mState.mTrackOldChangeHolders && viewHolder.isUpdated() - && !viewHolder.isRemoved() && !viewHolder.shouldIgnore()) { - long key = getChangedHolderKey(viewHolder); - mViewInfoStore.addToOldChangeHolders(key, viewHolder); - } - mViewInfoStore.addToPreLayout(viewHolder, animationInfo); - } - - private void findMinMaxChildLayoutPositions(int[] into) { - final int count = mChildHelper.getChildCount(); - if (count == 0) { - into[0] = NO_POSITION; - into[1] = NO_POSITION; - return; - } - int minPositionPreLayout = Integer.MAX_VALUE; - int maxPositionPreLayout = Integer.MIN_VALUE; - for (int i = 0; i < count; ++i) { - final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); - if (holder.shouldIgnore()) { - continue; - } - final int pos = holder.getLayoutPosition(); - if (pos < minPositionPreLayout) { - minPositionPreLayout = pos; - } - if (pos > maxPositionPreLayout) { - maxPositionPreLayout = pos; - } - } - into[0] = minPositionPreLayout; - into[1] = maxPositionPreLayout; - } - - private boolean didChildRangeChange(int minPositionPreLayout, int maxPositionPreLayout) { - findMinMaxChildLayoutPositions(mMinMaxLayoutPositions); - return mMinMaxLayoutPositions[0] != minPositionPreLayout || - mMinMaxLayoutPositions[1] != maxPositionPreLayout; - } - - @Override - protected void removeDetachedView(View child, boolean animate) { - ViewHolder vh = getChildViewHolderInt(child); - if (vh != null) { - if (vh.isTmpDetached()) { - vh.clearTmpDetachFlag(); - } else if (!vh.shouldIgnore()) { - throw new IllegalArgumentException("Called removeDetachedView with a view which" - + " is not flagged as tmp detached." + vh); - } - } - dispatchChildDetached(child); - super.removeDetachedView(child, animate); + mState.mOldChangedHolders = null; } /** @@ -3522,57 +2036,130 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return mAdapter.hasStableIds() ? holder.getItemId() : holder.mPosition; } - private void animateAppearance(@NonNull ViewHolder itemHolder, - @Nullable ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) { - itemHolder.setIsRecyclable(false); - if (mItemAnimator.animateAppearance(itemHolder, preLayoutInfo, postLayoutInfo)) { - postAnimationRunner(); - } - } - - private void animateDisappearance(@NonNull ViewHolder holder, - @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo) { - addAnimatingView(holder); - holder.setIsRecyclable(false); - if (mItemAnimator.animateDisappearance(holder, preLayoutInfo, postLayoutInfo)) { - postAnimationRunner(); - } - } - - private void animateChange(@NonNull ViewHolder oldHolder, @NonNull ViewHolder newHolder, - @NonNull ItemHolderInfo preInfo, @NonNull ItemHolderInfo postInfo, - boolean oldHolderDisappearing, boolean newHolderDisappearing) { - oldHolder.setIsRecyclable(false); - if (oldHolderDisappearing) { - addAnimatingView(oldHolder); - } - if (oldHolder != newHolder) { - if (newHolderDisappearing) { - addAnimatingView(newHolder); + /** + * A LayoutManager may want to layout a view just to animate disappearance. + * This method handles those views and triggers remove animation on them. + */ + private void processDisappearingList(ArrayMap appearingViews) { + final int count = mDisappearingViewsInLayoutPass.size(); + for (int i = 0; i < count; i ++) { + View view = mDisappearingViewsInLayoutPass.get(i); + ViewHolder vh = getChildViewHolderInt(view); + final ItemHolderInfo info = mState.mPreLayoutHolderMap.remove(vh); + if (!mState.isPreLayout()) { + mState.mPostLayoutHolderMap.remove(vh); } - oldHolder.mShadowedHolder = newHolder; - // old holder should disappear after animation ends - addAnimatingView(oldHolder); - mRecycler.unscrapView(oldHolder); + if (appearingViews.remove(view) != null) { + mLayout.removeAndRecycleView(view, mRecycler); + continue; + } + if (info != null) { + animateDisappearance(info); + } else { + // let it disappear from the position it becomes visible + animateDisappearance(new ItemHolderInfo(vh, view.getLeft(), view.getTop(), + view.getRight(), view.getBottom())); + } + } + mDisappearingViewsInLayoutPass.clear(); + } + + private void animateAppearance(ViewHolder itemHolder, Rect beforeBounds, int afterLeft, + int afterTop) { + View newItemView = itemHolder.itemView; + if (beforeBounds != null && + (beforeBounds.left != afterLeft || beforeBounds.top != afterTop)) { + // slide items in if before/after locations differ + itemHolder.setIsRecyclable(false); + if (DEBUG) { + Log.d(TAG, "APPEARING: " + itemHolder + " with view " + newItemView); + } + if (mItemAnimator.animateMove(itemHolder, + beforeBounds.left, beforeBounds.top, + afterLeft, afterTop)) { + postAnimationRunner(); + } + } else { + if (DEBUG) { + Log.d(TAG, "ADDED: " + itemHolder + " with view " + newItemView); + } + itemHolder.setIsRecyclable(false); + if (mItemAnimator.animateAdd(itemHolder)) { + postAnimationRunner(); + } + } + } + + private void animateDisappearance(ItemHolderInfo disappearingItem) { + View disappearingItemView = disappearingItem.holder.itemView; + addAnimatingView(disappearingItemView); + int oldLeft = disappearingItem.left; + int oldTop = disappearingItem.top; + int newLeft = disappearingItemView.getLeft(); + int newTop = disappearingItemView.getTop(); + if (oldLeft != newLeft || oldTop != newTop) { + disappearingItem.holder.setIsRecyclable(false); + disappearingItemView.layout(newLeft, newTop, + newLeft + disappearingItemView.getWidth(), + newTop + disappearingItemView.getHeight()); + if (DEBUG) { + Log.d(TAG, "DISAPPEARING: " + disappearingItem.holder + + " with view " + disappearingItemView); + } + if (mItemAnimator.animateMove(disappearingItem.holder, oldLeft, oldTop, + newLeft, newTop)) { + postAnimationRunner(); + } + } else { + if (DEBUG) { + Log.d(TAG, "REMOVED: " + disappearingItem.holder + + " with view " + disappearingItemView); + } + disappearingItem.holder.setIsRecyclable(false); + if (mItemAnimator.animateRemove(disappearingItem.holder)) { + postAnimationRunner(); + } + } + } + + private void animateChange(ViewHolder oldHolder, ViewHolder newHolder) { + oldHolder.setIsRecyclable(false); + removeDetachedView(oldHolder.itemView, false); + addAnimatingView(oldHolder.itemView); + oldHolder.mShadowedHolder = newHolder; + mRecycler.unscrapView(oldHolder); + if (DEBUG) { + Log.d(TAG, "CHANGED: " + oldHolder + " with view " + oldHolder.itemView); + } + final int fromLeft = oldHolder.itemView.getLeft(); + final int fromTop = oldHolder.itemView.getTop(); + final int toLeft, toTop; + if (newHolder == null || newHolder.shouldIgnore()) { + toLeft = fromLeft; + toTop = fromTop; + } else { + toLeft = newHolder.itemView.getLeft(); + toTop = newHolder.itemView.getTop(); newHolder.setIsRecyclable(false); newHolder.mShadowingHolder = oldHolder; } - if (mItemAnimator.animateChange(oldHolder, newHolder, preInfo, postInfo)) { + if(mItemAnimator.animateChange(oldHolder, newHolder, + fromLeft, fromTop, toLeft, toTop)) { postAnimationRunner(); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { - TraceCompat.beginSection(TRACE_ON_LAYOUT_TAG); + eatRequestLayout(); dispatchLayout(); - TraceCompat.endSection(); + resumeRequestLayout(false); mFirstLayoutComplete = true; } @Override public void requestLayout() { - if (mEatRequestLayout == 0 && !mLayoutFrozen) { + if (!mEatRequestLayout) { super.requestLayout(); } else { mLayoutRequestEaten = true; @@ -3596,7 +2183,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro for (int i = 0; i < count; i++) { mItemDecorations.get(i).onDrawOver(c, this, mState); } - // TODO If padding is not 0 and clipChildrenToPadding is false, to draw glows properly, we + // TODO If padding is not 0 and chilChildrenToPadding is false, to draw glows properly, we // need find children closest to edges. Not sure if it is worth the effort. boolean needsInvalidate = false; if (mLeftGlow != null && !mLeftGlow.isFinished()) { @@ -3688,18 +2275,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return mLayout.generateLayoutParams(p); } - /** - * Returns true if RecyclerView is currently running some animations. - *

      - * If you want to be notified when animations are finished, use - * {@link ItemAnimator#isRunning(ItemAnimator.ItemAnimatorFinishedListener)}. - * - * @return True if there are some item animations currently running or waiting to be started. - */ - public boolean isAnimating() { - return mItemAnimator != null && mItemAnimator.isRunning(); - } - void saveOldPositions() { final int childCount = mChildHelper.getUnfilteredChildCount(); for (int i = 0; i < childCount; i++) { @@ -3812,7 +2387,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param positionStart Adapter position to start at * @param itemCount Number of views that must explicitly be rebound */ - void viewRangeUpdate(int positionStart, int itemCount, Object payload) { + void viewRangeUpdate(int positionStart, int itemCount) { final int childCount = mChildHelper.getUnfilteredChildCount(); final int positionEnd = positionStart + itemCount; @@ -3826,7 +2401,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // We re-bind these view holders after pre-processing is complete so that // ViewHolders have their final positions assigned. holder.addFlags(ViewHolder.FLAG_UPDATE); - holder.addChangePayload(payload); + if (supportsChangeAnimations()) { + holder.addFlags(ViewHolder.FLAG_CHANGED); + } // lp cannot be null since we get ViewHolder from it. ((LayoutParams) child.getLayoutParams()).mInsetsDirty = true; } @@ -3834,24 +2411,36 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mRecycler.viewRangeUpdate(positionStart, itemCount); } - private boolean canReuseUpdatedViewHolder(ViewHolder viewHolder) { - return mItemAnimator == null || mItemAnimator.canReuseUpdatedViewHolder(viewHolder, - viewHolder.getUnmodifiedPayloads()); - } - - private void setDataSetChangedAfterLayout() { - if (mDataSetHasChangedAfterLayout) { - return; - } - mDataSetHasChangedAfterLayout = true; - final int childCount = mChildHelper.getUnfilteredChildCount(); + void rebindUpdatedViewHolders() { + final int childCount = mChildHelper.getChildCount(); for (int i = 0; i < childCount; i++) { - final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i)); - if (holder != null && !holder.shouldIgnore()) { - holder.addFlags(ViewHolder.FLAG_ADAPTER_POSITION_UNKNOWN); + final ViewHolder holder = getChildViewHolderInt(mChildHelper.getChildAt(i)); + // validate type is correct + if (holder == null || holder.shouldIgnore()) { + continue; + } + if (holder.isRemoved() || holder.isInvalid()) { + requestLayout(); + } else if (holder.needsUpdate()) { + final int type = mAdapter.getItemViewType(holder.mPosition); + if (holder.getItemViewType() == type) { + // Binding an attached view will request a layout if needed. + if (!holder.isChanged() || !supportsChangeAnimations()) { + mAdapter.bindViewHolder(holder, holder.mPosition); + } else { + // Don't rebind changed holders if change animations are enabled. + // We want the old contents for the animation and will get a new + // holder for the new contents. + requestLayout(); + } + } else { + // binding to a new view will need re-layout anyways. We can as well trigger + // it here so that it happens during layout + holder.addFlags(ViewHolder.FLAG_INVALID); + requestLayout(); + } } } - mRecycler.setAdapterPositionsAsUnknown(); } /** @@ -3886,39 +2475,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro requestLayout(); } - /** - * Returns true if the RecyclerView should attempt to preserve currently focused Adapter Item's - * focus even if the View representing the Item is replaced during a layout calculation. - *

      - * By default, this value is {@code true}. - * - * @return True if the RecyclerView will try to preserve focused Item after a layout if it loses - * focus. - * - * @see #setPreserveFocusAfterLayout(boolean) - */ - public boolean getPreserveFocusAfterLayout() { - return mPreserveFocusAfterLayout; - } - - /** - * Set whether the RecyclerView should try to keep the same Item focused after a layout - * calculation or not. - *

      - * Usually, LayoutManagers keep focused views visible before and after layout but sometimes, - * views may lose focus during a layout calculation as their state changes or they are replaced - * with another view due to type change or animation. In these cases, RecyclerView can request - * focus on the new view automatically. - * - * @param preserveFocusAfterLayout Whether RecyclerView should preserve focused Item during a - * layout calculations. Defaults to true. - * - * @see #getPreserveFocusAfterLayout() - */ - public void setPreserveFocusAfterLayout(boolean preserveFocusAfterLayout) { - mPreserveFocusAfterLayout = preserveFocusAfterLayout; - } - /** * Retrieve the {@link ViewHolder} for the given child view. * @@ -3934,44 +2490,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return getChildViewHolderInt(child); } - /** - * Traverses the ancestors of the given view and returns the item view that contains it and - * also a direct child of the RecyclerView. This returned view can be used to get the - * ViewHolder by calling {@link #getChildViewHolder(View)}. - * - * @param view The view that is a descendant of the RecyclerView. - * - * @return The direct child of the RecyclerView which contains the given view or null if the - * provided view is not a descendant of this RecyclerView. - * - * @see #getChildViewHolder(View) - * @see #findContainingViewHolder(View) - */ - @Nullable - public View findContainingItemView(View view) { - ViewParent parent = view.getParent(); - while (parent != null && parent != this && parent instanceof View) { - view = (View) parent; - parent = view.getParent(); - } - return parent == this ? view : null; - } - - /** - * Returns the ViewHolder that contains the given view. - * - * @param view The view that is a descendant of the RecyclerView. - * - * @return The ViewHolder that contains the given view or null if the provided view is not a - * descendant of this RecyclerView. - */ - @Nullable - public ViewHolder findContainingViewHolder(View view) { - View itemView = findContainingItemView(view); - return itemView == null ? null : getChildViewHolder(itemView); - } - - static ViewHolder getChildViewHolderInt(View child) { if (child == null) { return null; @@ -3979,39 +2497,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return ((LayoutParams) child.getLayoutParams()).mViewHolder; } - /** - * @deprecated use {@link #getChildAdapterPosition(View)} or - * {@link #getChildLayoutPosition(View)}. - */ - @Deprecated - public int getChildPosition(View child) { - return getChildAdapterPosition(child); - } - /** * Return the adapter position that the given child view corresponds to. * * @param child Child View to query * @return Adapter position corresponding to the given view or {@link #NO_POSITION} */ - public int getChildAdapterPosition(View child) { + public int getChildPosition(View child) { final ViewHolder holder = getChildViewHolderInt(child); - return holder != null ? holder.getAdapterPosition() : NO_POSITION; - } - - /** - * Return the adapter position of the given child view as of the latest completed layout pass. - *

      - * This position may not be equal to Item's adapter position if there are pending changes - * in the adapter which have not been reflected to the layout yet. - * - * @param child Child View to query - * @return Adapter position of the given View as of last layout pass or {@link #NO_POSITION} if - * the View is representing a removed item. - */ - public int getChildLayoutPosition(View child) { - final ViewHolder holder = getChildViewHolderInt(child); - return holder != null ? holder.getLayoutPosition() : NO_POSITION; + return holder != null ? holder.getPosition() : NO_POSITION; } /** @@ -4029,90 +2523,25 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * @deprecated use {@link #findViewHolderForLayoutPosition(int)} or - * {@link #findViewHolderForAdapterPosition(int)} + * Return the ViewHolder for the item in the given position of the data set. + * + * @param position The position of the item in the data set of the adapter + * @return The ViewHolder at position */ - @Deprecated public ViewHolder findViewHolderForPosition(int position) { return findViewHolderForPosition(position, false); } - /** - * Return the ViewHolder for the item in the given position of the data set as of the latest - * layout pass. - *

      - * This method checks only the children of RecyclerView. If the item at the given - * position is not laid out, it will not create a new one. - *

      - * Note that when Adapter contents change, ViewHolder positions are not updated until the - * next layout calculation. If there are pending adapter updates, the return value of this - * method may not match your adapter contents. You can use - * #{@link ViewHolder#getAdapterPosition()} to get the current adapter position of a ViewHolder. - *

      - * When the ItemAnimator is running a change animation, there might be 2 ViewHolders - * with the same layout position representing the same Item. In this case, the updated - * ViewHolder will be returned. - * - * @param position The position of the item in the data set of the adapter - * @return The ViewHolder at position or null if there is no such item - */ - public ViewHolder findViewHolderForLayoutPosition(int position) { - return findViewHolderForPosition(position, false); - } - - /** - * Return the ViewHolder for the item in the given position of the data set. Unlike - * {@link #findViewHolderForLayoutPosition(int)} this method takes into account any pending - * adapter changes that may not be reflected to the layout yet. On the other hand, if - * {@link Adapter#notifyDataSetChanged()} has been called but the new layout has not been - * calculated yet, this method will return null since the new positions of views - * are unknown until the layout is calculated. - *

      - * This method checks only the children of RecyclerView. If the item at the given - * position is not laid out, it will not create a new one. - *

      - * When the ItemAnimator is running a change animation, there might be 2 ViewHolders - * representing the same Item. In this case, the updated ViewHolder will be returned. - * - * @param position The position of the item in the data set of the adapter - * @return The ViewHolder at position or null if there is no such item - */ - public ViewHolder findViewHolderForAdapterPosition(int position) { - if (mDataSetHasChangedAfterLayout) { - return null; - } - final int childCount = mChildHelper.getUnfilteredChildCount(); - // hidden VHs are not preferred but if that is the only one we find, we rather return it - ViewHolder hidden = null; - for (int i = 0; i < childCount; i++) { - final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i)); - if (holder != null && !holder.isRemoved() && getAdapterPositionFor(holder) == position) { - if (mChildHelper.isHidden(holder.itemView)) { - hidden = holder; - } else { - return holder; - } - } - } - return hidden; - } - ViewHolder findViewHolderForPosition(int position, boolean checkNewPosition) { final int childCount = mChildHelper.getUnfilteredChildCount(); - ViewHolder hidden = null; for (int i = 0; i < childCount; i++) { final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i)); if (holder != null && !holder.isRemoved()) { if (checkNewPosition) { - if (holder.mPosition != position) { - continue; + if (holder.mPosition == position) { + return holder; } - } else if (holder.getLayoutPosition() != position) { - continue; - } - if (mChildHelper.isHidden(holder.itemView)) { - hidden = holder; - } else { + } else if (holder.getPosition() == position) { return holder; } } @@ -4120,40 +2549,29 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // This method should not query cached views. It creates a problem during adapter updates // when we are dealing with already laid out views. Also, for the public method, it is more // reasonable to return null if position is not laid out. - return hidden; + return null; } /** * Return the ViewHolder for the item with the given id. The RecyclerView must * use an Adapter with {@link Adapter#setHasStableIds(boolean) stableIds} to * return a non-null value. - *

      - * This method checks only the children of RecyclerView. If the item with the given - * id is not laid out, it will not create a new one. - * - * When the ItemAnimator is running a change animation, there might be 2 ViewHolders with the - * same id. In this case, the updated ViewHolder will be returned. * * @param id The id for the requested item - * @return The ViewHolder with the given id or null if there is no such item + * @return The ViewHolder with the given id, of null if there + * is no such item. */ public ViewHolder findViewHolderForItemId(long id) { - if (mAdapter == null || !mAdapter.hasStableIds()) { - return null; - } final int childCount = mChildHelper.getUnfilteredChildCount(); - ViewHolder hidden = null; for (int i = 0; i < childCount; i++) { final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i)); - if (holder != null && !holder.isRemoved() && holder.getItemId() == id) { - if (mChildHelper.isHidden(holder.itemView)) { - hidden = holder; - } else { - return holder; - } + if (holder != null && holder.getItemId() == id) { + return holder; } } - return hidden; + // this method should not query cached views. They are not children so they + // should not be returned in this public method + return null; } /** @@ -4179,11 +2597,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return null; } - @Override - public boolean drawChild(Canvas canvas, View child, long drawingTime) { - return super.drawChild(canvas, child, drawingTime); - } - /** * Offset the bounds of all child views by dy pixels. * Useful for implementing simple scrolling in {@link LayoutManager LayoutManagers}. @@ -4241,10 +2654,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return lp.mDecorInsets; } - if (mState.isPreLayout() && (lp.isItemChanged() || lp.isViewInvalid())) { - // changed/invalid items should not be updated until they are rebound. - return lp.mDecorInsets; - } final Rect insets = lp.mDecorInsets; insets.set(0, 0, 0, 0); final int decorCount = mItemDecorations.size(); @@ -4260,108 +2669,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return insets; } - /** - * Called when the scroll position of this RecyclerView changes. Subclasses should use - * this method to respond to scrolling within the adapter's data set instead of an explicit - * listener. - * - *

      This method will always be invoked before listeners. If a subclass needs to perform - * any additional upkeep or bookkeeping after scrolling but before listeners run, - * this is a good place to do so.

      - * - *

      This differs from {@link View#onScrollChanged(int, int, int, int)} in that it receives - * the distance scrolled in either direction within the adapter's data set instead of absolute - * scroll coordinates. Since RecyclerView cannot compute the absolute scroll position from - * any arbitrary point in the data set, onScrollChanged will always receive - * the current {@link View#getScrollX()} and {@link View#getScrollY()} values which - * do not correspond to the data set scroll position. However, some subclasses may choose - * to use these fields as special offsets.

      - * - * @param dx horizontal distance scrolled in pixels - * @param dy vertical distance scrolled in pixels - */ - public void onScrolled(int dx, int dy) { - // Do nothing - } - - void dispatchOnScrolled(int hresult, int vresult) { - mDispatchScrollCounter ++; - // Pass the current scrollX/scrollY values; no actual change in these properties occurred - // but some general-purpose code may choose to respond to changes this way. - final int scrollX = getScrollX(); - final int scrollY = getScrollY(); - onScrollChanged(scrollX, scrollY, scrollX, scrollY); - - // Pass the real deltas to onScrolled, the RecyclerView-specific method. - onScrolled(hresult, vresult); - - // Invoke listeners last. Subclassed view methods always handle the event first. - // All internal state is consistent by the time listeners are invoked. - if (mScrollListener != null) { - mScrollListener.onScrolled(this, hresult, vresult); - } - if (mScrollListeners != null) { - for (int i = mScrollListeners.size() - 1; i >= 0; i--) { - mScrollListeners.get(i).onScrolled(this, hresult, vresult); - } - } - mDispatchScrollCounter --; - } - - /** - * Called when the scroll state of this RecyclerView changes. Subclasses should use this - * method to respond to state changes instead of an explicit listener. - * - *

      This method will always be invoked before listeners, but after the LayoutManager - * responds to the scroll state change.

      - * - * @param state the new scroll state, one of {@link #SCROLL_STATE_IDLE}, - * {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING} - */ - public void onScrollStateChanged(int state) { - // Do nothing - } - - void dispatchOnScrollStateChanged(int state) { - // Let the LayoutManager go first; this allows it to bring any properties into - // a consistent state before the RecyclerView subclass responds. - if (mLayout != null) { - mLayout.onScrollStateChanged(state); - } - - // Let the RecyclerView subclass handle this event next; any LayoutManager property - // changes will be reflected by this time. - onScrollStateChanged(state); - - // Listeners go last. All other internal state is consistent by this point. - if (mScrollListener != null) { - mScrollListener.onScrollStateChanged(this, state); - } - if (mScrollListeners != null) { - for (int i = mScrollListeners.size() - 1; i >= 0; i--) { - mScrollListeners.get(i).onScrollStateChanged(this, state); - } - } - } - - /** - * Returns whether there are pending adapter updates which are not yet applied to the layout. - *

      - * If this method returns true, it means that what user is currently seeing may not - * reflect them adapter contents (depending on what has changed). - * You may use this information to defer or cancel some operations. - *

      - * This method returns true if RecyclerView has not yet calculated the first layout after it is - * attached to the Window or the Adapter has been replaced. - * - * @return True if there are some adapter updates which are not yet reflected to layout or false - * if layout is up to date. - */ - public boolean hasPendingAdapterUpdates() { - return !mFirstLayoutComplete || mDataSetHasChangedAfterLayout - || mAdapterHelper.hasPendingUpdates(); - } - private class ViewFlinger implements Runnable { private int mLastFlingX; private int mLastFlingY; @@ -4381,10 +2688,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro @Override public void run() { - if (mLayout == null) { - stop(); - return; // no layout, cannot scroll. - } disableRunOnAnimationRequests(); consumePendingUpdateOperations(); // keep a local reference so that if it is changed during onAnimation method, it won't @@ -4403,8 +2706,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro int overscrollX = 0, overscrollY = 0; if (mAdapter != null) { eatRequestLayout(); - onEnterLayoutOrScroll(); - TraceCompat.beginSection(TRACE_SCROLL_TAG); + mRunningLayoutOrScroll = true; if (dx != 0) { hresult = mLayout.scrollHorizontallyBy(dx, mRecycler, mState); overscrollX = dx - hresult; @@ -4413,11 +2715,28 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro vresult = mLayout.scrollVerticallyBy(dy, mRecycler, mState); overscrollY = dy - vresult; } - TraceCompat.endSection(); - repositionShadowingViews(); - - onExitLayoutOrScroll(); - resumeRequestLayout(false); + if (supportsChangeAnimations()) { + // Fix up shadow views used by changing animations + int count = mChildHelper.getChildCount(); + for (int i = 0; i < count; i++) { + View view = mChildHelper.getChildAt(i); + ViewHolder holder = getChildViewHolder(view); + if (holder != null && holder.mShadowingHolder != null) { + View shadowingView = holder.mShadowingHolder != null ? + holder.mShadowingHolder.itemView : null; + if (shadowingView != null) { + int left = view.getLeft(); + int top = view.getTop(); + if (left != shadowingView.getLeft() || + top != shadowingView.getTop()) { + shadowingView.layout(left, top, + left + shadowingView.getWidth(), + top + shadowingView.getHeight()); + } + } + } + } + } if (smoothScroller != null && !smoothScroller.isPendingInitialRun() && smoothScroller.isRunning()) { @@ -4431,11 +2750,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro smoothScroller.onAnimation(dx - overscrollX, dy - overscrollY); } } + mRunningLayoutOrScroll = false; + resumeRequestLayout(false); } + final boolean fullyConsumedScroll = dx == hresult && dy == vresult; if (!mItemDecorations.isEmpty()) { invalidate(); } - if (getOverScrollMode() != View.OVER_SCROLL_NEVER) { + if (ViewCompat.getOverScrollMode(RecyclerView.this) != + ViewCompat.OVER_SCROLL_NEVER) { considerReleasingGlowsOnScroll(dx, dy); } if (overscrollX != 0 || overscrollY != 0) { @@ -4451,7 +2774,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro velY = overscrollY < 0 ? -vel : overscrollY > 0 ? vel : 0; } - if (getOverScrollMode() != View.OVER_SCROLL_NEVER) { + if (ViewCompat.getOverScrollMode(RecyclerView.this) != + ViewCompat.OVER_SCROLL_NEVER) { absorbGlows(velX, velY); } if ((velX != 0 || overscrollX == x || scroller.getFinalX() == 0) && @@ -4460,34 +2784,26 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } } if (hresult != 0 || vresult != 0) { - dispatchOnScrolled(hresult, vresult); + // dummy values, View's implementation does not use these. + onScrollChanged(0, 0, 0, 0); + if (mScrollListener != null) { + mScrollListener.onScrolled(RecyclerView.this, hresult, vresult); + } } if (!awakenScrollBars()) { invalidate(); } - final boolean fullyConsumedVertical = dy != 0 && mLayout.canScrollVertically() - && vresult == dy; - final boolean fullyConsumedHorizontal = dx != 0 && mLayout.canScrollHorizontally() - && hresult == dx; - final boolean fullyConsumedAny = (dx == 0 && dy == 0) || fullyConsumedHorizontal - || fullyConsumedVertical; - - if (scroller.isFinished() || !fullyConsumedAny) { + if (scroller.isFinished() || !fullyConsumedScroll) { setScrollState(SCROLL_STATE_IDLE); // setting state to idle will stop this. } else { postOnAnimation(); } } // call this after the onAnimation is complete not to have inconsistent callbacks etc. - if (smoothScroller != null) { - if (smoothScroller.isPendingInitialRun()) { - smoothScroller.onAnimation(0, 0); - } - if (!mReSchedulePostAnimationCallback) { - smoothScroller.stop(); //stop if it does not trigger any scroll - } + if (smoothScroller != null && smoothScroller.isPendingInitialRun()) { + smoothScroller.onAnimation(0, 0); } enableRunOnAnimationRequests(); } @@ -4508,7 +2824,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro if (mEatRunOnAnimationRequest) { mReSchedulePostAnimationCallback = true; } else { - removeCallbacks(this); ViewCompat.postOnAnimation(RecyclerView.this, this); } } @@ -4579,26 +2894,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } - private void repositionShadowingViews() { - // Fix up shadow views used by change animations - int count = mChildHelper.getChildCount(); - for (int i = 0; i < count; i++) { - View view = mChildHelper.getChildAt(i); - ViewHolder holder = getChildViewHolder(view); - if (holder != null && holder.mShadowingHolder != null) { - View shadowingView = holder.mShadowingHolder.itemView; - int left = view.getLeft(); - int top = view.getTop(); - if (left != shadowingView.getLeft() || - top != shadowingView.getTop()) { - shadowingView.layout(left, top, - left + shadowingView.getWidth(), - top + shadowingView.getHeight()); - } - } - } - } - private class RecyclerViewDataObserver extends AdapterDataObserver { @Override public void onChanged() { @@ -4608,10 +2903,10 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // This is more important to implement now since this callback will disable all // animations because we cannot rely on positions. mState.mStructureChanged = true; - setDataSetChangedAfterLayout(); + mDataSetHasChangedAfterLayout = true; } else { mState.mStructureChanged = true; - setDataSetChangedAfterLayout(); + mDataSetHasChangedAfterLayout = true; } if (!mAdapterHelper.hasPendingUpdates()) { requestLayout(); @@ -4619,9 +2914,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } @Override - public void onItemRangeChanged(int positionStart, int itemCount, Object payload) { + public void onItemRangeChanged(int positionStart, int itemCount) { assertNotInLayoutOrScroll(null); - if (mAdapterHelper.onItemRangeChanged(positionStart, itemCount, payload)) { + if (mAdapterHelper.onItemRangeChanged(positionStart, itemCount)) { triggerUpdateProcessor(); } } @@ -4719,9 +3014,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro if (mMaxScrap.get(viewType) <= scrapHeap.size()) { return; } - if (DEBUG && scrapHeap.contains(scrap)) { - throw new IllegalArgumentException("this scrap item already exists"); - } scrap.resetInternal(); scrapHeap.add(scrap); } @@ -4762,7 +3054,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro private ArrayList getScrapHeapForType(int viewType) { ArrayList scrap = mScrap.get(viewType); if (scrap == null) { - scrap = new ArrayList<>(); + scrap = new ArrayList(); mScrap.put(viewType, scrap); if (mMaxScrap.indexOfKey(viewType) < 0) { mMaxScrap.put(viewType, DEFAULT_MAX_SCRAP); @@ -4782,11 +3074,11 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * an adapter's data set representing the data at a given position or item ID. * If the view to be reused is considered "dirty" the adapter will be asked to rebind it. * If not, the view can be quickly reused by the LayoutManager with no further work. - * Clean views that have not {@link android.view.View#isLayoutRequested() requested layout} + * Clean views that have not {@link View#isLayoutRequested() requested layout} * may be repositioned by a LayoutManager without remeasurement.

      */ public final class Recycler { - final ArrayList mAttachedScrap = new ArrayList<>(); + final ArrayList mAttachedScrap = new ArrayList(); private ArrayList mChangedScrap = null; final ArrayList mCachedViews = new ArrayList(); @@ -4820,7 +3112,11 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mViewCacheMax = viewCount; // first, try the views that can be recycled for (int i = mCachedViews.size() - 1; i >= 0 && mCachedViews.size() > viewCount; i--) { - recycleCachedViewAt(i); + tryToRecycleCachedViewAt(i); + } + // if we could not recycle enough of them, remove some. + while (mCachedViews.size() > viewCount) { + mCachedViews.remove(mCachedViews.size() - 1); } } @@ -4845,11 +3141,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // if it is a removed holder, nothing to verify since we cannot ask adapter anymore // if it is not removed, verify the type and id. if (holder.isRemoved()) { - if (DEBUG && !mState.isPreLayout()) { - throw new IllegalStateException("should not receive a removed view unless it" - + " is pre layout"); - } - return mState.isPreLayout(); + return true; } if (holder.mPosition < 0 || holder.mPosition >= mAdapter.getItemCount()) { throw new IndexOutOfBoundsException("Inconsistency detected. Invalid view holder " @@ -4896,7 +3188,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro + "position " + position + "(offset:" + offsetPosition + ")." + "state:" + mState.getItemCount()); } - holder.mOwnerRecyclerView = RecyclerView.this; mAdapter.bindViewHolder(holder, offsetPosition); attachAccessibilityDelegate(view); if (mState.isPreLayout()) { @@ -5005,10 +3296,16 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } if (holder == null) { final int offsetPosition = mAdapterHelper.findPositionOffset(position); +// final int offsetPosition = position; +// Utils.log("offsetPosition position = " + position); +// Utils.log("offsetPosition = " + offsetPosition); +// Utils.log("offsetPosition count = " + mAdapter.getItemCount()); +// Utils.log("offsetPosition count = " + mState.getItemCount()); if (offsetPosition < 0 || offsetPosition >= mAdapter.getItemCount()) { throw new IndexOutOfBoundsException("Inconsistency detected. Invalid item " + "position " + position + "(offset:" + offsetPosition + ")." - + "state:" + mState.getItemCount()); + + "state:" + mState.getItemCount() + + "adpter:" + mAdapter.getClass().getName()); } final int type = mAdapter.getItemViewType(offsetPosition); @@ -5045,7 +3342,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro Log.d(TAG, "getViewForPosition(" + position + ") fetching from shared " + "pool"); } - holder = getRecycledViewPool().getRecycledView(type); + holder = getRecycledViewPool() + .getRecycledView(mAdapter.getItemViewType(offsetPosition)); if (holder != null) { holder.resetInternal(); if (FORCE_INVALIDATE_DISPLAY_LIST) { @@ -5054,29 +3352,13 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } } if (holder == null) { - holder = mAdapter.createViewHolder(RecyclerView.this, type); + holder = mAdapter.createViewHolder(RecyclerView.this, + mAdapter.getItemViewType(offsetPosition)); if (DEBUG) { Log.d(TAG, "getViewForPosition created new ViewHolder"); } } } - - // This is very ugly but the only place we can grab this information - // before the View is rebound and returned to the LayoutManager for post layout ops. - // We don't need this in pre-layout since the VH is not updated by the LM. - if (fromScrap && !mState.isPreLayout() && holder - .hasAnyOfTheFlags(ViewHolder.FLAG_BOUNCED_FROM_HIDDEN_LIST)) { - holder.setFlags(0, ViewHolder.FLAG_BOUNCED_FROM_HIDDEN_LIST); - if (mState.mRunSimpleAnimations) { - int changeFlags = ItemAnimator - .buildAdapterChangeFlagsForAnimations(holder); - changeFlags |= ItemAnimator.FLAG_APPEARED_IN_PRE_LAYOUT; - final ItemHolderInfo info = mItemAnimator.recordPreLayoutInformation(mState, - holder, changeFlags, holder.getUnmodifiedPayloads()); - recordAnimationInfoIfBouncedHiddenView(holder, info); - } - } - boolean bound = false; if (mState.isPreLayout() && holder.isBound()) { // do not update unless we absolutely have to. @@ -5087,7 +3369,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro + " come here only in pre-layout. Holder: " + holder); } final int offsetPosition = mAdapterHelper.findPositionOffset(position); - holder.mOwnerRecyclerView = RecyclerView.this; mAdapter.bindViewHolder(holder, offsetPosition); attachAccessibilityDelegate(holder.itemView); bound = true; @@ -5113,7 +3394,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } private void attachAccessibilityDelegate(View itemView) { - if (isAccessibilityEnabled()) { + if (mAccessibilityManager != null && mAccessibilityManager.isEnabled()) { if (ViewCompat.getImportantForAccessibility(itemView) == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) { ViewCompat.setImportantForAccessibility(itemView, @@ -5157,8 +3438,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * Recycle a detached view. The specified view will be added to a pool of views * for later rebinding and reuse. * - *

      A view must be fully detached (removed from parent) before it may be recycled. If the - * View is scrapped, it will be removed from scrap list.

      + *

      A view must be fully detached before it may be recycled. If the View is scrapped, + * it will be removed from scrap list.

      * * @param view Removed view for recycling * @see LayoutManager#removeAndRecycleView(View, Recycler) @@ -5167,9 +3448,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // This public recycle method tries to make view recycle-able since layout manager // intended to recycle this view (e.g. even if it is in scrap or change cache) ViewHolder holder = getChildViewHolderInt(view); - if (holder.isTmpDetached()) { - removeDetachedView(view, false); - } if (holder.isScrap()) { holder.unScrap(); } else if (holder.wasReturnedFromScrap()){ @@ -5179,7 +3457,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * Internally, use this method instead of {@link #recycleView(android.view.View)} to + * Internally, use this method instead of {@link #recycleView(View)} to * catch potential bugs. * @param view */ @@ -5190,32 +3468,33 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro void recycleAndClearCachedViews() { final int count = mCachedViews.size(); for (int i = count - 1; i >= 0; i--) { - recycleCachedViewAt(i); + tryToRecycleCachedViewAt(i); } mCachedViews.clear(); } /** - * Recycles a cached view and removes the view from the list. Views are added to cache - * if and only if they are recyclable, so this method does not check it again. - *

      - * A small exception to this rule is when the view does not have an animator reference - * but transient state is true (due to animations created outside ItemAnimator). In that - * case, adapter may choose to recycle it. From RecyclerView's perspective, the view is - * still recyclable since Adapter wants to do so. + * Tries to recyle a cached view and removes the view from the list if and only if it + * is recycled. * * @param cachedViewIndex The index of the view in cached views list + * @return True if item is recycled */ - void recycleCachedViewAt(int cachedViewIndex) { + boolean tryToRecycleCachedViewAt(int cachedViewIndex) { if (DEBUG) { Log.d(TAG, "Recycling cached view at index " + cachedViewIndex); } ViewHolder viewHolder = mCachedViews.get(cachedViewIndex); if (DEBUG) { - Log.d(TAG, "CachedViewHolder to be recycled: " + viewHolder); + Log.d(TAG, "CachedViewHolder to be recycled(if recycleable): " + viewHolder); } - addViewHolderToRecycledViewPool(viewHolder); - mCachedViews.remove(cachedViewIndex); + if (viewHolder.isRecyclable()) { + getRecycledViewPool().putRecycledView(viewHolder); + dispatchViewRecycled(viewHolder); + mCachedViews.remove(cachedViewIndex); + return true; + } + return false; } /** @@ -5231,62 +3510,38 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro + (holder.itemView.getParent() != null)); } - if (holder.isTmpDetached()) { - throw new IllegalArgumentException("Tmp detached view should be removed " - + "from RecyclerView before it can be recycled: " + holder); - } - if (holder.shouldIgnore()) { throw new IllegalArgumentException("Trying to recycle an ignored view holder. You" + " should first call stopIgnoringView(view) before calling recycle."); } - //noinspection unchecked - final boolean transientStatePreventsRecycling = holder - .doesTransientStatePreventRecycling(); - final boolean forceRecycle = mAdapter != null - && transientStatePreventsRecycling - && mAdapter.onFailedToRecycleView(holder); - boolean cached = false; - boolean recycled = false; - if (DEBUG && mCachedViews.contains(holder)) { - throw new IllegalArgumentException("cached view received recycle internal? " + - holder); - } - if (forceRecycle || holder.isRecyclable()) { - if (!holder.hasAnyOfTheFlags(ViewHolder.FLAG_INVALID | ViewHolder.FLAG_REMOVED - | ViewHolder.FLAG_UPDATE)) { - // Retire oldest cached view - int cachedViewSize = mCachedViews.size(); - if (cachedViewSize >= mViewCacheMax && cachedViewSize > 0) { - recycleCachedViewAt(0); - cachedViewSize --; + if (holder.isRecyclable()) { + boolean cached = false; + if (!holder.isInvalid() && (mState.mInPreLayout || !holder.isRemoved()) && + !holder.isChanged()) { + // Retire oldest cached views first + if (mCachedViews.size() == mViewCacheMax && !mCachedViews.isEmpty()) { + for (int i = 0; i < mCachedViews.size(); i++) { + if (tryToRecycleCachedViewAt(i)) { + break; + } + } } - if (cachedViewSize < mViewCacheMax) { + if (mCachedViews.size() < mViewCacheMax) { mCachedViews.add(holder); cached = true; } } if (!cached) { - addViewHolderToRecycledViewPool(holder); - recycled = true; + getRecycledViewPool().putRecycledView(holder); + dispatchViewRecycled(holder); } } else if (DEBUG) { Log.d(TAG, "trying to recycle a non-recycleable holder. Hopefully, it will " - + "re-visit here. We are still removing it from animation lists"); + + "re-visit here. We are stil removing it from animation lists"); } // even if the holder is not removed, we still call this method so that it is removed // from view holder lists. - mViewInfoStore.removeViewHolder(holder); - if (!cached && !recycled && transientStatePreventsRecycling) { - holder.mOwnerRecyclerView = null; - } - } - - void addViewHolderToRecycledViewPool(ViewHolder holder) { - ViewCompat.setAccessibilityDelegate(holder.itemView, null); - dispatchViewRecycled(holder); - holder.mOwnerRecyclerView = null; - getRecycledViewPool().putRecycledView(holder); + mState.onViewRecycled(holder); } /** @@ -5297,7 +3552,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro void quickRecycleScrapView(View view) { final ViewHolder holder = getChildViewHolderInt(view); holder.mScrapContainer = null; - holder.mInChangeScrap = false; holder.clearReturnedFromScrapFlag(); recycleViewHolderInternal(holder); } @@ -5313,20 +3567,18 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro */ void scrapView(View view) { final ViewHolder holder = getChildViewHolderInt(view); - if (holder.hasAnyOfTheFlags(ViewHolder.FLAG_REMOVED | ViewHolder.FLAG_INVALID) - || !holder.isUpdated() || canReuseUpdatedViewHolder(holder)) { + holder.setScrapContainer(this); + if (!holder.isChanged() || !supportsChangeAnimations()) { if (holder.isInvalid() && !holder.isRemoved() && !mAdapter.hasStableIds()) { throw new IllegalArgumentException("Called scrap view with an invalid view." + " Invalid views cannot be reused from scrap, they should rebound from" + " recycler pool."); } - holder.setScrapContainer(this, false); mAttachedScrap.add(holder); } else { if (mChangedScrap == null) { mChangedScrap = new ArrayList(); } - holder.setScrapContainer(this, true); mChangedScrap.add(holder); } } @@ -5338,13 +3590,12 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * until it is explicitly removed and recycled.

      */ void unscrapView(ViewHolder holder) { - if (holder.mInChangeScrap) { - mChangedScrap.remove(holder); - } else { + if (!holder.isChanged() || !supportsChangeAnimations() || mChangedScrap == null) { mAttachedScrap.remove(holder); + } else { + mChangedScrap.remove(holder); } holder.mScrapContainer = null; - holder.mInChangeScrap = false; holder.clearReturnedFromScrapFlag(); } @@ -5358,9 +3609,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro void clearScrap() { mAttachedScrap.clear(); - if (mChangedScrap != null) { - mChangedScrap.clear(); - } } ViewHolder getChangedScrapViewForPosition(int position) { @@ -5372,7 +3620,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // find by position for (int i = 0; i < changedScrapSize; i++) { final ViewHolder holder = mChangedScrap.get(i); - if (!holder.wasReturnedFromScrap() && holder.getLayoutPosition() == position) { + if (!holder.wasReturnedFromScrap() && holder.getPosition() == position) { holder.addFlags(ViewHolder.FLAG_RETURNED_FROM_SCRAP); return holder; } @@ -5409,7 +3657,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // Try first for an exact, non-invalid match from scrap. for (int i = 0; i < scrapCount; i++) { final ViewHolder holder = mAttachedScrap.get(i); - if (!holder.wasReturnedFromScrap() && holder.getLayoutPosition() == position + if (!holder.wasReturnedFromScrap() && holder.getPosition() == position && !holder.isInvalid() && (mState.mInPreLayout || !holder.isRemoved())) { if (type != INVALID_TYPE && holder.getItemViewType() != type) { Log.e(TAG, "Scrap view for position " + position + " isn't dirty but has" + @@ -5425,20 +3673,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro if (!dryRun) { View view = mChildHelper.findHiddenNonRemovedView(position, type); if (view != null) { - // This View is good to be used. We just need to unhide, detach and move to the - // scrap list. - final ViewHolder vh = getChildViewHolderInt(view); - mChildHelper.unhide(view); - int layoutIndex = mChildHelper.indexOfChild(view); - if (layoutIndex == RecyclerView.NO_POSITION) { - throw new IllegalStateException("layout index should not be -1 after " - + "unhiding a view:" + vh); - } - mChildHelper.detachViewFromParent(layoutIndex); - scrapView(view); - vh.addFlags(ViewHolder.FLAG_RETURNED_FROM_SCRAP - | ViewHolder.FLAG_BOUNCED_FROM_HIDDEN_LIST); - return vh; + // ending the animation should cause it to get recycled before we reuse it + mItemAnimator.endAnimation(getChildViewHolder(view)); } } @@ -5448,7 +3684,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro final ViewHolder holder = mCachedViews.get(i); // invalid view holders may be in cache if adapter has stable ids as they can be // retrieved via getScrapViewForId - if (!holder.isInvalid() && holder.getLayoutPosition() == position) { + if (!holder.isInvalid() && holder.getPosition() == position) { if (!dryRun) { mCachedViews.remove(i); } @@ -5486,8 +3722,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } return holder; } else if (!dryRun) { - // if we are running animations, it is actually better to keep it in scrap - // but this would force layout manager to lay it out which would be bad. // Recycle this scrap. Type mismatch. mAttachedScrap.remove(i); removeDetachedView(holder.itemView, false); @@ -5507,7 +3741,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } return holder; } else if (!dryRun) { - recycleCachedViewAt(i); + tryToRecycleCachedViewAt(i); } } } @@ -5522,7 +3756,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mAdapter.onViewRecycled(holder); } if (mState != null) { - mViewInfoStore.removeViewHolder(holder); + mState.onViewRecycled(holder); } if (DEBUG) Log.d(TAG, "dispatchViewRecycled: " + holder); } @@ -5566,7 +3800,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro final int cachedCount = mCachedViews.size(); for (int i = 0; i < cachedCount; i++) { final ViewHolder holder = mCachedViews.get(i); - if (holder != null && holder.mPosition >= insertedAt) { + if (holder != null && holder.getPosition() >= insertedAt) { if (DEBUG) { Log.d(TAG, "offsetPositionRecordsForInsert cached " + i + " holder " + holder + " now at position " + (holder.mPosition + count)); @@ -5588,17 +3822,28 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro for (int i = cachedCount - 1; i >= 0; i--) { final ViewHolder holder = mCachedViews.get(i); if (holder != null) { - if (holder.mPosition >= removedEnd) { + if (holder.getPosition() >= removedEnd) { if (DEBUG) { Log.d(TAG, "offsetPositionRecordsForRemove cached " + i + " holder " + holder + " now at position " + (holder.mPosition - count)); } holder.offsetPosition(-count, applyToPreLayout); - } else if (holder.mPosition >= removedFrom) { + } else if (holder.getPosition() >= removedFrom) { // Item for this view was removed. Dump it from the cache. - holder.addFlags(ViewHolder.FLAG_REMOVED); - recycleCachedViewAt(i); + if (!tryToRecycleCachedViewAt(i)) { + // if we cannot recycle it, at least invalidate so that we won't return + // it by position. + holder.addFlags(ViewHolder.FLAG_INVALID); + if (DEBUG) { + Log.d(TAG, "offsetPositionRecordsForRemove cached " + i + + " holder " + holder + " now flagged as invalid because it " + + "could not be recycled"); + } + } else if (DEBUG) { + Log.d(TAG, "offsetPositionRecordsForRemove cached " + i + + " holder " + holder + " now placed in pool"); + } } } } @@ -5628,32 +3873,21 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro void viewRangeUpdate(int positionStart, int itemCount) { final int positionEnd = positionStart + itemCount; final int cachedCount = mCachedViews.size(); - for (int i = cachedCount - 1; i >= 0; i--) { + for (int i = 0; i < cachedCount; i++) { final ViewHolder holder = mCachedViews.get(i); if (holder == null) { continue; } - final int pos = holder.getLayoutPosition(); + final int pos = holder.getPosition(); if (pos >= positionStart && pos < positionEnd) { holder.addFlags(ViewHolder.FLAG_UPDATE); - recycleCachedViewAt(i); // cached views should not be flagged as changed because this will cause them // to animate when they are returned from cache. } } } - void setAdapterPositionsAsUnknown() { - final int cachedCount = mCachedViews.size(); - for (int i = 0; i < cachedCount; i++) { - final ViewHolder holder = mCachedViews.get(i); - if (holder != null) { - holder.addFlags(ViewHolder.FLAG_ADAPTER_POSITION_UNKNOWN); - } - } - } - void markKnownViewsInvalid() { if (mAdapter != null && mAdapter.hasStableIds()) { final int cachedCount = mCachedViews.size(); @@ -5661,13 +3895,20 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro final ViewHolder holder = mCachedViews.get(i); if (holder != null) { holder.addFlags(ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID); - holder.addChangePayload(null); } } } else { - // we cannot re-use cached views in this case. Recycle them all - recycleAndClearCachedViews(); + // we cannot re-use cached views in this case. Recycle the ones we can and flag + // the remaining as invalid so that they can be recycled later on (when their + // animations end.) + for (int i = mCachedViews.size() - 1; i >= 0; i--) { + if (!tryToRecycleCachedViewAt(i)) { + final ViewHolder holder = mCachedViews.get(i); + holder.addFlags(ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID); + } + } } + } void clearOldPositions() { @@ -5702,7 +3943,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * ViewCacheExtension is a helper class to provide an additional layer of view caching that can - * be controlled by the developer. + * ben controlled by the developer. *

      * When {@link Recycler#getViewForPosition(int)} is called, Recycler checks attached scrap and * first level cache to find a matching View. If it cannot find a suitable View, Recycler will @@ -5753,9 +3994,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * layout file. *

      * The new ViewHolder will be used to display items of the adapter using - * {@link #onBindViewHolder(ViewHolder, int, List)}. Since it will be re-used to display - * different items in the data set, it is a good idea to cache references to sub views of - * the View to avoid unnecessary {@link View#findViewById(int)} calls. + * {@link #onBindViewHolder(ViewHolder, int)}. Since it will be re-used to display different + * items in the data set, it is a good idea to cache references to sub views of the View to + * avoid unnecessary {@link View#findViewById(int)} calls. * * @param parent The ViewGroup into which the new View will be added after it is bound to * an adapter position. @@ -5767,60 +4008,24 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro */ public abstract VH onCreateViewHolder(ViewGroup parent, int viewType); - /** - * Called by RecyclerView to display the data at the specified position. This method should - * update the contents of the {@link ViewHolder#itemView} to reflect the item at the given - * position. - *

      - * Note that unlike {@link android.widget.ListView}, RecyclerView will not call this method - * again if the position of the item changes in the data set unless the item itself is - * invalidated or the new position cannot be determined. For this reason, you should only - * use the position parameter while acquiring the related data item inside - * this method and should not keep a copy of it. If you need the position of an item later - * on (e.g. in a click listener), use {@link ViewHolder#getAdapterPosition()} which will - * have the updated adapter position. - * - * Override {@link #onBindViewHolder(ViewHolder, int, List)} instead if Adapter can - * handle efficient partial bind. - * - * @param holder The ViewHolder which should be updated to represent the contents of the - * item at the given position in the data set. - * @param position The position of the item within the adapter's data set. - */ - public abstract void onBindViewHolder(VH holder, int position); - /** * Called by RecyclerView to display the data at the specified position. This method * should update the contents of the {@link ViewHolder#itemView} to reflect the item at * the given position. *

      - * Note that unlike {@link android.widget.ListView}, RecyclerView will not call this method - * again if the position of the item changes in the data set unless the item itself is - * invalidated or the new position cannot be determined. For this reason, you should only - * use the position parameter while acquiring the related data item inside - * this method and should not keep a copy of it. If you need the position of an item later - * on (e.g. in a click listener), use {@link ViewHolder#getAdapterPosition()} which will - * have the updated adapter position. - *

      - * Partial bind vs full bind: - *

      - * The payloads parameter is a merge list from {@link #notifyItemChanged(int, Object)} or - * {@link #notifyItemRangeChanged(int, int, Object)}. If the payloads list is not empty, - * the ViewHolder is currently bound to old data and Adapter may run an efficient partial - * update using the payload info. If the payload is empty, Adapter must run a full bind. - * Adapter should not assume that the payload passed in notify methods will be received by - * onBindViewHolder(). For example when the view is not attached to the screen, the - * payload in notifyItemChange() will be simply dropped. + * Note that unlike {@link android.widget.ListView}, RecyclerView will not call this + * method again if the position of the item changes in the data set unless the item itself + * is invalidated or the new position cannot be determined. For this reason, you should only + * use the position parameter while acquiring the related data item inside this + * method and should not keep a copy of it. If you need the position of an item later on + * (e.g. in a click listener), use {@link ViewHolder#getPosition()} which will have the + * updated position. * * @param holder The ViewHolder which should be updated to represent the contents of the * item at the given position in the data set. * @param position The position of the item within the adapter's data set. - * @param payloads A non-null list of merged payloads. Can be empty list if requires full - * update. */ - public void onBindViewHolder(VH holder, int position, List payloads) { - onBindViewHolder(holder, position); - } + public abstract void onBindViewHolder(VH holder, int position); /** * This method calls {@link #onCreateViewHolder(ViewGroup, int)} to create a new @@ -5829,10 +4034,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @see #onCreateViewHolder(ViewGroup, int) */ public final VH createViewHolder(ViewGroup parent, int viewType) { - TraceCompat.beginSection(TRACE_CREATE_VIEW_TAG); final VH holder = onCreateViewHolder(parent, viewType); holder.mItemViewType = viewType; - TraceCompat.endSection(); return holder; } @@ -5848,17 +4051,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro if (hasStableIds()) { holder.mItemId = getItemId(position); } + onBindViewHolder(holder, position); holder.setFlags(ViewHolder.FLAG_BOUND, - ViewHolder.FLAG_BOUND | ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID - | ViewHolder.FLAG_ADAPTER_POSITION_UNKNOWN); - TraceCompat.beginSection(TRACE_BIND_VIEW_TAG); - onBindViewHolder(holder, position, holder.getUnmodifiedPayloads()); - holder.clearPayload(); - final ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams(); - if (layoutParams instanceof RecyclerView.LayoutParams) { - ((LayoutParams) layoutParams).mInsetsDirty = true; - } - TraceCompat.endSection(); + ViewHolder.FLAG_BOUND | ViewHolder.FLAG_UPDATE | ViewHolder.FLAG_INVALID); } /** @@ -5879,7 +4074,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Indicates whether each item in the data set can be represented with a unique identifier - * of type {@link java.lang.Long}. + * of type {@link Long}. * * @param hasStableIds Whether items in data set have unique identifiers or not. * @see #hasStableIds() @@ -5906,7 +4101,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * Returns the total number of items in the data set held by the adapter. + * Returns the total number of items in the data set hold by the adapter. * * @return The total number of items in this adapter. */ @@ -5932,61 +4127,18 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * attached to the parent RecyclerView. If an item view has large or expensive data * bound to it such as large bitmaps, this may be a good place to release those * resources.

      - *

      - * RecyclerView calls this method right before clearing ViewHolder's internal data and - * sending it to RecycledViewPool. This way, if ViewHolder was holding valid information - * before being recycled, you can call {@link ViewHolder#getAdapterPosition()} to get - * its adapter position. * * @param holder The ViewHolder for the view being recycled */ public void onViewRecycled(VH holder) { } - /** - * Called by the RecyclerView if a ViewHolder created by this Adapter cannot be recycled - * due to its transient state. Upon receiving this callback, Adapter can clear the - * animation(s) that effect the View's transient state and return true so that - * the View can be recycled. Keep in mind that the View in question is already removed from - * the RecyclerView. - *

      - * In some cases, it is acceptable to recycle a View although it has transient state. Most - * of the time, this is a case where the transient state will be cleared in - * {@link #onBindViewHolder(ViewHolder, int)} call when View is rebound to a new position. - * For this reason, RecyclerView leaves the decision to the Adapter and uses the return - * value of this method to decide whether the View should be recycled or not. - *

      - * Note that when all animations are created by {@link RecyclerView.ItemAnimator}, you - * should never receive this callback because RecyclerView keeps those Views as children - * until their animations are complete. This callback is useful when children of the item - * views create animations which may not be easy to implement using an {@link ItemAnimator}. - *

      - * You should never fix this issue by calling - * holder.itemView.setHasTransientState(false); unless you've previously called - * holder.itemView.setHasTransientState(true);. Each - * View.setHasTransientState(true) call must be matched by a - * View.setHasTransientState(false) call, otherwise, the state of the View - * may become inconsistent. You should always prefer to end or cancel animations that are - * triggering the transient state instead of handling it manually. - * - * @param holder The ViewHolder containing the View that could not be recycled due to its - * transient state. - * @return True if the View should be recycled, false otherwise. Note that if this method - * returns true, RecyclerView will ignore the transient state of - * the View and recycle it regardless. If this method returns false, - * RecyclerView will check the View's transient state again before giving a final decision. - * Default implementation returns false. - */ - public boolean onFailedToRecycleView(VH holder) { - return false; - } - /** * Called when a view created by this adapter has been attached to a window. * *

      This can be used as a reasonable signal that the view is about to be seen * by the user. If the adapter previously freed any resources in - * {@link #onViewDetachedFromWindow(RecyclerView.ViewHolder) onViewDetachedFromWindow} + * {@link #onViewDetachedFromWindow(ViewHolder) onViewDetachedFromWindow} * those resources should be restored here.

      * * @param holder Holder of the view being attached @@ -5999,7 +4151,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * *

      Becoming detached from the window is not necessarily a permanent condition; * the consumer of an Adapter's views may choose to cache views offscreen while they - * are not visible, attaching and detaching them as appropriate.

      + * are not visible, attaching an detaching them as appropriate.

      * * @param holder Holder of the view being detached */ @@ -6020,16 +4172,16 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * *

      The adapter may publish a variety of events describing specific changes. * Not all adapters may support all change types and some may fall back to a generic - * {@link android.support.v7.widget.RecyclerView.AdapterDataObserver#onChanged() + * {@link AdapterDataObserver#onChanged() * "something changed"} event if more specific data is not available.

      * *

      Components registering observers with an adapter are responsible for - * {@link #unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver) + * {@link #unregisterAdapterDataObserver(AdapterDataObserver) * unregistering} those observers when finished.

      * * @param observer Observer to register * - * @see #unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver) + * @see #unregisterAdapterDataObserver(AdapterDataObserver) */ public void registerAdapterDataObserver(AdapterDataObserver observer) { mObservable.registerObserver(observer); @@ -6043,32 +4195,12 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * * @param observer Observer to unregister * - * @see #registerAdapterDataObserver(RecyclerView.AdapterDataObserver) + * @see #registerAdapterDataObserver(AdapterDataObserver) */ public void unregisterAdapterDataObserver(AdapterDataObserver observer) { mObservable.unregisterObserver(observer); } - /** - * Called by RecyclerView when it starts observing this Adapter. - *

      - * Keep in mind that same adapter may be observed by multiple RecyclerViews. - * - * @param recyclerView The RecyclerView instance which started observing this adapter. - * @see #onDetachedFromRecyclerView(RecyclerView) - */ - public void onAttachedToRecyclerView(RecyclerView recyclerView) { - } - - /** - * Called by RecyclerView when it stops observing this Adapter. - * - * @param recyclerView The RecyclerView instance which stopped observing this adapter. - * @see #onAttachedToRecyclerView(RecyclerView) - */ - public void onDetachedFromRecyclerView(RecyclerView recyclerView) { - } - /** * Notify any registered observers that the data set has changed. * @@ -6104,7 +4236,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Notify any registered observers that the item at position has changed. - * Equivalent to calling notifyItemChanged(position, null);. * *

      This is an item change event, not a structural change event. It indicates that any * reflection of the data at position is out of date and should be updated. @@ -6118,38 +4249,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mObservable.notifyItemRangeChanged(position, 1); } - /** - * Notify any registered observers that the item at position has changed with an - * optional payload object. - * - *

      This is an item change event, not a structural change event. It indicates that any - * reflection of the data at position is out of date and should be updated. - * The item at position retains the same identity. - *

      - * - *

      - * Client can optionally pass a payload for partial change. These payloads will be merged - * and may be passed to adapter's {@link #onBindViewHolder(ViewHolder, int, List)} if the - * item is already represented by a ViewHolder and it will be rebound to the same - * ViewHolder. A notifyItemRangeChanged() with null payload will clear all existing - * payloads on that item and prevent future payload until - * {@link #onBindViewHolder(ViewHolder, int, List)} is called. Adapter should not assume - * that the payload will always be passed to onBindViewHolder(), e.g. when the view is not - * attached, the payload will be simply dropped. - * - * @param position Position of the item that has changed - * @param payload Optional parameter, use null to identify a "full" update - * - * @see #notifyItemRangeChanged(int, int) - */ - public final void notifyItemChanged(int position, Object payload) { - mObservable.notifyItemRangeChanged(position, 1, payload); - } - /** * Notify any registered observers that the itemCount items starting at * position positionStart have changed. - * Equivalent to calling notifyItemRangeChanged(position, itemCount, null);. * *

      This is an item change event, not a structural change event. It indicates that * any reflection of the data in the given position range is out of date and should @@ -6164,36 +4266,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mObservable.notifyItemRangeChanged(positionStart, itemCount); } - /** - * Notify any registered observers that the itemCount items starting at - * position positionStart have changed. An optional payload can be - * passed to each changed item. - * - *

      This is an item change event, not a structural change event. It indicates that any - * reflection of the data in the given position range is out of date and should be updated. - * The items in the given range retain the same identity. - *

      - * - *

      - * Client can optionally pass a payload for partial change. These payloads will be merged - * and may be passed to adapter's {@link #onBindViewHolder(ViewHolder, int, List)} if the - * item is already represented by a ViewHolder and it will be rebound to the same - * ViewHolder. A notifyItemRangeChanged() with null payload will clear all existing - * payloads on that item and prevent future payload until - * {@link #onBindViewHolder(ViewHolder, int, List)} is called. Adapter should not assume - * that the payload will always be passed to onBindViewHolder(), e.g. when the view is not - * attached, the payload will be simply dropped. - * - * @param positionStart Position of the first item that has changed - * @param itemCount Number of items that have changed - * @param payload Optional parameter, use null to identify a "full" update - * - * @see #notifyItemChanged(int) - */ - public final void notifyItemRangeChanged(int positionStart, int itemCount, Object payload) { - mObservable.notifyItemRangeChanged(positionStart, itemCount, payload); - } - /** * Notify any registered observers that the item reflected at position * has been newly inserted. The item previously at position is now at @@ -6281,31 +4353,17 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } private void dispatchChildDetached(View child) { - final ViewHolder viewHolder = getChildViewHolderInt(child); + if (mAdapter != null) { + mAdapter.onViewDetachedFromWindow(getChildViewHolderInt(child)); + } onChildDetachedFromWindow(child); - if (mAdapter != null && viewHolder != null) { - mAdapter.onViewDetachedFromWindow(viewHolder); - } - if (mOnChildAttachStateListeners != null) { - final int cnt = mOnChildAttachStateListeners.size(); - for (int i = cnt - 1; i >= 0; i--) { - mOnChildAttachStateListeners.get(i).onChildViewDetachedFromWindow(child); - } - } } private void dispatchChildAttached(View child) { - final ViewHolder viewHolder = getChildViewHolderInt(child); + if (mAdapter != null) { + mAdapter.onViewAttachedToWindow(getChildViewHolderInt(child)); + } onChildAttachedToWindow(child); - if (mAdapter != null && viewHolder != null) { - mAdapter.onViewAttachedToWindow(viewHolder); - } - if (mOnChildAttachStateListeners != null) { - final int cnt = mOnChildAttachStateListeners.size(); - for (int i = cnt - 1; i >= 0; i--) { - mOnChildAttachStateListeners.get(i).onChildViewAttachedToWindow(child); - } - } } /** @@ -6315,14 +4373,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * a RecyclerView can be used to implement a standard vertically scrolling list, * a uniform grid, staggered grids, horizontally scrolling collections and more. Several stock * layout managers are provided for general use. - *

      - * If the LayoutManager specifies a default constructor or one with the signature - * ({@link Context}, {@link AttributeSet}, {@code int}, {@code int}), RecyclerView will - * instantiate and set the LayoutManager when being inflated. Most used properties can - * be then obtained from {@link #getProperties(Context, AttributeSet, int, int)}. In case - * a LayoutManager specifies both constructors, the non-default constructor will take - * precedence. - * */ public static abstract class LayoutManager { ChildHelper mChildHelper; @@ -6333,132 +4383,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro private boolean mRequestedSimpleAnimations = false; - boolean mIsAttachedToWindow = false; - - private boolean mAutoMeasure = false; - - /** - * LayoutManager has its own more strict measurement cache to avoid re-measuring a child - * if the space that will be given to it is already larger than what it has measured before. - */ - private boolean mMeasurementCacheEnabled = true; - - - /** - * These measure specs might be the measure specs that were passed into RecyclerView's - * onMeasure method OR fake measure specs created by the RecyclerView. - * For example, when a layout is run, RecyclerView always sets these specs to be - * EXACTLY because a LayoutManager cannot resize RecyclerView during a layout pass. - *

      - * Also, to be able to use the hint in unspecified measure specs, RecyclerView checks the - * API level and sets the size to 0 pre-M to avoid any issue that might be caused by - * corrupt values. Older platforms have no responsibility to provide a size if they set - * mode to unspecified. - */ - private int mWidthMode, mHeightMode; - private int mWidth, mHeight; - void setRecyclerView(RecyclerView recyclerView) { if (recyclerView == null) { mRecyclerView = null; mChildHelper = null; - mWidth = 0; - mHeight = 0; } else { mRecyclerView = recyclerView; mChildHelper = recyclerView.mChildHelper; - mWidth = recyclerView.getWidth(); - mHeight = recyclerView.getHeight(); - } - mWidthMode = MeasureSpec.EXACTLY; - mHeightMode = MeasureSpec.EXACTLY; - } - - void setMeasureSpecs(int wSpec, int hSpec) { - mWidth = MeasureSpec.getSize(wSpec); - mWidthMode = MeasureSpec.getMode(wSpec); - if (mWidthMode == MeasureSpec.UNSPECIFIED && !ALLOW_SIZE_IN_UNSPECIFIED_SPEC) { - mWidth = 0; } - mHeight = MeasureSpec.getSize(hSpec); - mHeightMode = MeasureSpec.getMode(hSpec); - if (mHeightMode == MeasureSpec.UNSPECIFIED && !ALLOW_SIZE_IN_UNSPECIFIED_SPEC) { - mHeight = 0; - } - } - - /** - * Called after a layout is calculated during a measure pass when using auto-measure. - *

      - * It simply traverses all children to calculate a bounding box then calls - * {@link #setMeasuredDimension(Rect, int, int)}. LayoutManagers can override that method - * if they need to handle the bounding box differently. - *

      - * For example, GridLayoutManager override that method to ensure that even if a column is - * empty, the GridLayoutManager still measures wide enough to include it. - * - * @param widthSpec The widthSpec that was passing into RecyclerView's onMeasure - * @param heightSpec The heightSpec that was passing into RecyclerView's onMeasure - */ - void setMeasuredDimensionFromChildren(int widthSpec, int heightSpec) { - final int count = getChildCount(); - if (count == 0) { - mRecyclerView.defaultOnMeasure(widthSpec, heightSpec); - return; - } - int minX = Integer.MAX_VALUE; - int minY = Integer.MAX_VALUE; - int maxX = Integer.MIN_VALUE; - int maxY = Integer.MIN_VALUE; - - for (int i = 0; i < count; i++) { - View child = getChildAt(i); - LayoutParams lp = (LayoutParams) child.getLayoutParams(); - final Rect bounds = mRecyclerView.mTempRect; - getDecoratedBoundsWithMargins(child, bounds); - if (bounds.left < minX) { - minX = bounds.left; - } - if (bounds.right > maxX) { - maxX = bounds.right; - } - if (bounds.top < minY) { - minY = bounds.top; - } - if (bounds.bottom > maxY) { - maxY = bounds.bottom; - } - } - mRecyclerView.mTempRect.set(minX, minY, maxX, maxY); - setMeasuredDimension(mRecyclerView.mTempRect, widthSpec, heightSpec); - } - - /** - * Sets the measured dimensions from the given bounding box of the children and the - * measurement specs that were passed into {@link RecyclerView#onMeasure(int, int)}. It is - * called after the RecyclerView calls - * {@link LayoutManager#onLayoutChildren(Recycler, State)} during a measurement pass. - *

      - * This method should call {@link #setMeasuredDimension(int, int)}. - *

      - * The default implementation adds the RecyclerView's padding to the given bounding box - * then caps the value to be within the given measurement specs. - *

      - * This method is only called if the LayoutManager opted into the auto measurement API. - * - * @param childrenBounds The bounding box of all children - * @param wSpec The widthMeasureSpec that was passed into the RecyclerView. - * @param hSpec The heightMeasureSpec that was passed into the RecyclerView. - * - * @see #setAutoMeasureEnabled(boolean) - */ - public void setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec) { - int usedWidth = childrenBounds.width() + getPaddingLeft() + getPaddingRight(); - int usedHeight = childrenBounds.height() + getPaddingTop() + getPaddingBottom(); - int width = chooseSize(wSpec, usedWidth, getMinimumWidth()); - int height = chooseSize(hSpec, usedHeight, getMinimumHeight()); - setMeasuredDimension(width, height); } /** @@ -6483,30 +4416,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } } - /** - * Chooses a size from the given specs and parameters that is closest to the desired size - * and also complies with the spec. - * - * @param spec The measureSpec - * @param desired The preferred measurement - * @param min The minimum value - * - * @return A size that fits to the given specs - */ - public static int chooseSize(int spec, int desired, int min) { - final int mode = View.MeasureSpec.getMode(spec); - final int size = View.MeasureSpec.getSize(spec); - switch (mode) { - case View.MeasureSpec.EXACTLY: - return size; - case View.MeasureSpec.AT_MOST: - return Math.min(size, Math.max(desired, min)); - case View.MeasureSpec.UNSPECIFIED: - default: - return Math.max(desired, min); - } - } - /** * Checks if RecyclerView is in the middle of a layout or scroll and throws an * {@link IllegalStateException} if it is. @@ -6520,86 +4429,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } } - /** - * Defines whether the layout should be measured by the RecyclerView or the LayoutManager - * wants to handle the layout measurements itself. - *

      - * This method is usually called by the LayoutManager with value {@code true} if it wants - * to support WRAP_CONTENT. If you are using a public LayoutManager but want to customize - * the measurement logic, you can call this method with {@code false} and override - * {@link LayoutManager#onMeasure(int, int)} to implement your custom measurement logic. - *

      - * AutoMeasure is a convenience mechanism for LayoutManagers to easily wrap their content or - * handle various specs provided by the RecyclerView's parent. - * It works by calling {@link LayoutManager#onLayoutChildren(Recycler, State)} during an - * {@link RecyclerView#onMeasure(int, int)} call, then calculating desired dimensions based - * on children's positions. It does this while supporting all existing animation - * capabilities of the RecyclerView. - *

      - * AutoMeasure works as follows: - *

        - *
      1. LayoutManager should call {@code setAutoMeasureEnabled(true)} to enable it. All of - * the framework LayoutManagers use {@code auto-measure}.
      2. - *
      3. When {@link RecyclerView#onMeasure(int, int)} is called, if the provided specs are - * exact, RecyclerView will only call LayoutManager's {@code onMeasure} and return without - * doing any layout calculation.
      4. - *
      5. If one of the layout specs is not {@code EXACT}, the RecyclerView will start the - * layout process in {@code onMeasure} call. It will process all pending Adapter updates and - * decide whether to run a predictive layout or not. If it decides to do so, it will first - * call {@link #onLayoutChildren(Recycler, State)} with {@link State#isPreLayout()} set to - * {@code true}. At this stage, {@link #getWidth()} and {@link #getHeight()} will still - * return the width and height of the RecyclerView as of the last layout calculation. - *

        - * After handling the predictive case, RecyclerView will call - * {@link #onLayoutChildren(Recycler, State)} with {@link State#isMeasuring()} set to - * {@code true} and {@link State#isPreLayout()} set to {@code false}. The LayoutManager can - * access the measurement specs via {@link #getHeight()}, {@link #getHeightMode()}, - * {@link #getWidth()} and {@link #getWidthMode()}.

      6. - *
      7. After the layout calculation, RecyclerView sets the measured width & height by - * calculating the bounding box for the children (+ RecyclerView's padding). The - * LayoutManagers can override {@link #setMeasuredDimension(Rect, int, int)} to choose - * different values. For instance, GridLayoutManager overrides this value to handle the case - * where if it is vertical and has 3 columns but only 2 items, it should still measure its - * width to fit 3 items, not 2.
      8. - *
      9. Any following on measure call to the RecyclerView will run - * {@link #onLayoutChildren(Recycler, State)} with {@link State#isMeasuring()} set to - * {@code true} and {@link State#isPreLayout()} set to {@code false}. RecyclerView will - * take care of which views are actually added / removed / moved / changed for animations so - * that the LayoutManager should not worry about them and handle each - * {@link #onLayoutChildren(Recycler, State)} call as if it is the last one. - *
      10. - *
      11. When measure is complete and RecyclerView's - * {@link #onLayout(boolean, int, int, int, int)} method is called, RecyclerView checks - * whether it already did layout calculations during the measure pass and if so, it re-uses - * that information. It may still decide to call {@link #onLayoutChildren(Recycler, State)} - * if the last measure spec was different from the final dimensions or adapter contents - * have changed between the measure call and the layout call.
      12. - *
      13. Finally, animations are calculated and run as usual.
      14. - *
      - * - * @param enabled True if the Layout should be measured by the - * RecyclerView, false if the LayoutManager wants - * to measure itself. - * - * @see #setMeasuredDimension(Rect, int, int) - * @see #isAutoMeasureEnabled() - */ - public void setAutoMeasureEnabled(boolean enabled) { - mAutoMeasure = enabled; - } - - /** - * Returns whether the LayoutManager uses the automatic measurement API or not. - * - * @return True if the LayoutManager is measured by the RecyclerView or - * false if it measures itself. - * - * @see #setAutoMeasureEnabled(boolean) - */ - public boolean isAutoMeasureEnabled() { - return mAutoMeasure; - } - /** * Returns whether this LayoutManager supports automatic item animations. * A LayoutManager wishing to support item animations should obey certain @@ -6624,78 +4453,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return false; } - void dispatchAttachedToWindow(RecyclerView view) { - mIsAttachedToWindow = true; - onAttachedToWindow(view); - } - - void dispatchDetachedFromWindow(RecyclerView view, Recycler recycler) { - mIsAttachedToWindow = false; - onDetachedFromWindow(view, recycler); - } - - /** - * Returns whether LayoutManager is currently attached to a RecyclerView which is attached - * to a window. - * - * @return True if this LayoutManager is controlling a RecyclerView and the RecyclerView - * is attached to window. - */ - public boolean isAttachedToWindow() { - return mIsAttachedToWindow; - } - - /** - * Causes the Runnable to execute on the next animation time step. - * The runnable will be run on the user interface thread. - *

      - * Calling this method when LayoutManager is not attached to a RecyclerView has no effect. - * - * @param action The Runnable that will be executed. - * - * @see #removeCallbacks - */ - public void postOnAnimation(Runnable action) { - if (mRecyclerView != null) { - ViewCompat.postOnAnimation(mRecyclerView, action); - } - } - - /** - * Removes the specified Runnable from the message queue. - *

      - * Calling this method when LayoutManager is not attached to a RecyclerView has no effect. - * - * @param action The Runnable to remove from the message handling queue - * - * @return true if RecyclerView could ask the Handler to remove the Runnable, - * false otherwise. When the returned value is true, the Runnable - * may or may not have been actually removed from the message queue - * (for instance, if the Runnable was not in the queue already.) - * - * @see #postOnAnimation - */ - public boolean removeCallbacks(Runnable action) { - if (mRecyclerView != null) { - return mRecyclerView.removeCallbacks(action); - } - return false; - } /** * Called when this LayoutManager is both attached to a RecyclerView and that RecyclerView * is attached to a window. - *

      - * If the RecyclerView is re-attached with the same LayoutManager and Adapter, it may not - * call {@link #onLayoutChildren(Recycler, State)} if nothing has changed and a layout was - * not requested on the RecyclerView while it was detached. - *

      - * Subclass implementations should always call through to the superclass implementation. + * + *

      Subclass implementations should always call through to the superclass implementation. + *

      * * @param view The RecyclerView this LayoutManager is bound to - * - * @see #onDetachedFromWindow(RecyclerView, Recycler) */ - @CallSuper public void onAttachedToWindow(RecyclerView view) { } @@ -6711,27 +4477,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Called when this LayoutManager is detached from its parent RecyclerView or when * its parent RecyclerView is detached from its window. - *

      - * LayoutManager should clear all of its View references as another LayoutManager might be - * assigned to the RecyclerView. - *

      - * If the RecyclerView is re-attached with the same LayoutManager and Adapter, it may not - * call {@link #onLayoutChildren(Recycler, State)} if nothing has changed and a layout was - * not requested on the RecyclerView while it was detached. - *

      - * If your LayoutManager has View references that it cleans in on-detach, it should also - * call {@link RecyclerView#requestLayout()} to ensure that it is re-laid out when - * RecyclerView is re-attached. - *

      - * Subclass implementations should always call through to the superclass implementation. + * + *

      Subclass implementations should always call through to the superclass implementation. + *

      * * @param view The RecyclerView this LayoutManager is bound to * @param recycler The recycler to use if you prefer to recycle your children instead of * keeping them around. - * - * @see #onAttachedToWindow(RecyclerView) */ - @CallSuper public void onDetachedFromWindow(RecyclerView view, Recycler recycler) { onDetachedFromWindow(view); } @@ -6757,7 +4510,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * normal layout operation during {@link #onLayoutChildren(Recycler, State)}, the * RecyclerView will have enough information to run those animations in a simple * way. For example, the default ItemAnimator, {@link DefaultItemAnimator}, will - * simply fade views in and out, whether they are actually added/removed or whether + * simple fade views in and out, whether they are actuall added/removed or whether * they are moved on or off the screen due to other add/remove operations. * *

      A LayoutManager wanting a better item animation experience, where items can be @@ -6800,32 +4553,18 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro Log.e(TAG, "You must override onLayoutChildren(Recycler recycler, State state) "); } - /** - * Called after a full layout calculation is finished. The layout calculation may include - * multiple {@link #onLayoutChildren(Recycler, State)} calls due to animations or - * layout measurement but it will include only one {@link #onLayoutCompleted(State)} call. - * This method will be called at the end of {@link View#layout(int, int, int, int)} call. - *

      - * This is a good place for the LayoutManager to do some cleanup like pending scroll - * position, saved state etc. - * - * @param state Transient state of RecyclerView - */ - public void onLayoutCompleted(State state) { - } - /** * Create a default LayoutParams object for a child of the RecyclerView. * *

      LayoutManagers will often want to use a custom LayoutParams type * to store extra information specific to the layout. Client code should subclass - * {@link RecyclerView.LayoutParams} for this purpose.

      + * {@link LayoutParams} for this purpose.

      * *

      Important: if you use your own custom LayoutParams type * you must also override * {@link #checkLayoutParams(LayoutParams)}, - * {@link #generateLayoutParams(android.view.ViewGroup.LayoutParams)} and - * {@link #generateLayoutParams(android.content.Context, android.util.AttributeSet)}.

      + * {@link #generateLayoutParams(ViewGroup.LayoutParams)} and + * {@link #generateLayoutParams(Context, AttributeSet)}.

      * * @return A new LayoutParams for a child view */ @@ -6852,8 +4591,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro *

      Important: if you use your own custom LayoutParams type * you must also override * {@link #checkLayoutParams(LayoutParams)}, - * {@link #generateLayoutParams(android.view.ViewGroup.LayoutParams)} and - * {@link #generateLayoutParams(android.content.Context, android.util.AttributeSet)}.

      + * {@link #generateLayoutParams(ViewGroup.LayoutParams)} and + * {@link #generateLayoutParams(Context, AttributeSet)}.

      * * @param lp Source LayoutParams object to copy values from * @return a new LayoutParams object @@ -6875,8 +4614,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro *

      Important: if you use your own custom LayoutParams type * you must also override * {@link #checkLayoutParams(LayoutParams)}, - * {@link #generateLayoutParams(android.view.ViewGroup.LayoutParams)} and - * {@link #generateLayoutParams(android.content.Context, android.util.AttributeSet)}.

      + * {@link #generateLayoutParams(ViewGroup.LayoutParams)} and + * {@link #generateLayoutParams(Context, AttributeSet)}.

      * * @param c Context for obtaining styled attributes * @param attrs AttributeSet describing the supplied arguments @@ -6969,7 +4708,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** *

      Starts a smooth scroll using the provided SmoothScroller.

      *

      Calling this method will cancel any previous smooth scroll request.

      - * @param smoothScroller Instance which defines how smooth scroll should be animated + * @param smoothScroller Unstance which defines how smooth scroll should be animated */ public void startSmoothScroll(SmoothScroller smoothScroller) { if (mSmoothScroller != null && smoothScroller != mSmoothScroller @@ -6991,9 +4730,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Returns the resolved layout direction for this RecyclerView. * - * @return {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_RTL} if the layout + * @return {@link ViewCompat#LAYOUT_DIRECTION_RTL} if the layout * direction is RTL or returns - * {@link android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_LTR} if the layout direction + * {@link ViewCompat#LAYOUT_DIRECTION_LTR} if the layout direction * is not RTL. */ public int getLayoutDirection() { @@ -7004,7 +4743,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * Ends all animations on the view created by the {@link ItemAnimator}. * * @param view The View for which the animations should be ended. - * @see RecyclerView.ItemAnimator#endAnimations() + * @see ItemAnimator#endAnimations() */ public void endAnimation(View view) { if (mRecyclerView.mItemAnimator != null) { @@ -7074,14 +4813,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro final ViewHolder holder = getChildViewHolderInt(child); if (disappearing || holder.isRemoved()) { // these views will be hidden at the end of the layout pass. - mRecyclerView.mViewInfoStore.addToDisappearedInLayout(holder); + mRecyclerView.addToDisappearingList(child); } else { // This may look like unnecessary but may happen if layout manager supports // predictive layouts and adapter removed then re-added the same item. // In this case, added version will be visible in the post layout (because add is // deferred) but RV will still bind it to the same View. // So if a View re-appears in post layout pass, remove it from disappearing list. - mRecyclerView.mViewInfoStore.removeFromDisappearedInLayout(holder); + mRecyclerView.removeFromDisappearingList(child); } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (holder.wasReturnedFromScrap() || holder.isScrap()) { @@ -7128,7 +4867,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * Remove a view from the currently attached RecyclerView if needed. LayoutManagers should * use this method to completely remove a child view that is no longer needed. * LayoutManagers should strongly consider recycling removed views using - * {@link Recycler#recycleView(android.view.View)}. + * {@link Recycler#recycleView(View)}. * * @param child View to remove */ @@ -7140,7 +4879,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * Remove a view from the currently attached RecyclerView if needed. LayoutManagers should * use this method to completely remove a child view that is no longer needed. * LayoutManagers should strongly consider recycling removed views using - * {@link Recycler#recycleView(android.view.View)}. + * {@link Recycler#recycleView(View)}. * * @param index Index of the child view to remove */ @@ -7159,29 +4898,19 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // Only remove non-animating views final int childCount = getChildCount(); for (int i = childCount - 1; i >= 0; i--) { + final View child = getChildAt(i); mChildHelper.removeViewAt(i); } } /** - * Returns offset of the RecyclerView's text baseline from the its top boundary. - * - * @return The offset of the RecyclerView's text baseline from the its top boundary; -1 if - * there is no baseline. - */ - public int getBaseline() { - return -1; - } - - /** - * Returns the adapter position of the item represented by the given View. This does not - * contain any adapter changes that might have happened after the last layout. + * Returns the adapter position of the item represented by the given View. * * @param view The view to query * @return The adapter position of the item which is rendered by this View. */ public int getPosition(View view) { - return ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition(); + return ((LayoutParams) view.getLayoutParams()).getViewPosition(); } /** @@ -7195,36 +4924,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * Traverses the ancestors of the given view and returns the item view that contains it - * and also a direct child of the LayoutManager. *

      - * Note that this method may return null if the view is a child of the RecyclerView but - * not a child of the LayoutManager (e.g. running a disappear animation). - * - * @param view The view that is a descendant of the LayoutManager. - * - * @return The direct child of the LayoutManager which contains the given view or null if - * the provided view is not a descendant of this LayoutManager. - * - * @see RecyclerView#getChildViewHolder(View) - * @see RecyclerView#findContainingViewHolder(View) - */ - @Nullable - public View findContainingItemView(View view) { - if (mRecyclerView == null) { - return null; - } - View found = mRecyclerView.findContainingItemView(view); - if (found == null) { - return null; - } - if (mChildHelper.isHidden(found)) { - return null; - } - return found; - } - - /** * Finds the view which represents the given adapter position. *

      * This method traverses each child since it has no information about child order. @@ -7235,7 +4935,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * * @param position Position of the item in adapter * @return The child view that represents the given position or null if the position is not - * laid out + * visible */ public View findViewByPosition(int position) { final int childCount = getChildCount(); @@ -7245,7 +4945,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro if (vh == null) { continue; } - if (vh.getLayoutPosition() == position && !vh.shouldIgnore() && + if (vh.getPosition() == position && !vh.shouldIgnore() && (mRecyclerView.mState.isPreLayout() || !vh.isRemoved())) { return child; } @@ -7258,12 +4958,12 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * *

      LayoutManagers may want to perform a lightweight detach operation to rearrange * views currently attached to the RecyclerView. Generally LayoutManager implementations - * will want to use {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)} + * will want to use {@link #detachAndScrapView(View, Recycler)} * so that the detached view may be rebound and reused.

      * *

      If a LayoutManager uses this method to detach a view, it must - * {@link #attachView(android.view.View, int, RecyclerView.LayoutParams) reattach} - * or {@link #removeDetachedView(android.view.View) fully remove} the detached view + * {@link #attachView(View, int, LayoutParams) reattach} + * or {@link #removeDetachedView(View) fully remove} the detached view * before the LayoutManager entry point method called by RecyclerView returns.

      * * @param child Child to detach @@ -7280,12 +4980,12 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * *

      LayoutManagers may want to perform a lightweight detach operation to rearrange * views currently attached to the RecyclerView. Generally LayoutManager implementations - * will want to use {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)} + * will want to use {@link #detachAndScrapView(View, Recycler)} * so that the detached view may be rebound and reused.

      * *

      If a LayoutManager uses this method to detach a view, it must - * {@link #attachView(android.view.View, int, RecyclerView.LayoutParams) reattach} - * or {@link #removeDetachedView(android.view.View) fully remove} the detached view + * {@link #attachView(View, int, LayoutParams) reattach} + * or {@link #removeDetachedView(View) fully remove} the detached view * before the LayoutManager entry point method called by RecyclerView returns.

      * * @param index Index of the child to detach @@ -7302,9 +5002,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * Reattach a previously {@link #detachView(android.view.View) detached} view. + * Reattach a previously {@link #detachView(View) detached} view. * This method should not be used to reattach views that were previously - * {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)} scrapped}. + * {@link #detachAndScrapView(View, Recycler)} scrapped}. * * @param child Child to reattach * @param index Intended child index for child @@ -7313,9 +5013,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro public void attachView(View child, int index, LayoutParams lp) { ViewHolder vh = getChildViewHolderInt(child); if (vh.isRemoved()) { - mRecyclerView.mViewInfoStore.addToDisappearedInLayout(vh); + mRecyclerView.addToDisappearingList(child); } else { - mRecyclerView.mViewInfoStore.removeFromDisappearedInLayout(vh); + mRecyclerView.removeFromDisappearingList(child); } mChildHelper.attachViewToParent(child, index, lp, vh.isRemoved()); if (DISPATCH_TEMP_DETACH) { @@ -7324,9 +5024,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * Reattach a previously {@link #detachView(android.view.View) detached} view. + * Reattach a previously {@link #detachView(View) detached} view. * This method should not be used to reattach views that were previously - * {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)} scrapped}. + * {@link #detachAndScrapView(View, Recycler)} scrapped}. * * @param child Child to reattach * @param index Intended child index for child @@ -7336,9 +5036,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * Reattach a previously {@link #detachView(android.view.View) detached} view. + * Reattach a previously {@link #detachView(View) detached} view. * This method should not be used to reattach views that were previously - * {@link #detachAndScrapView(android.view.View, RecyclerView.Recycler)} scrapped}. + * {@link #detachAndScrapView(View, Recycler)} scrapped}. * * @param child Child to reattach */ @@ -7348,7 +5048,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Finish removing a view that was previously temporarily - * {@link #detachView(android.view.View) detached}. + * {@link #detachView(View) detached}. * * @param child Detached child to remove */ @@ -7442,49 +5142,13 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return mChildHelper != null ? mChildHelper.getChildAt(index) : null; } - /** - * Return the width measurement spec mode of the RecyclerView. - *

      - * This value is set only if the LayoutManager opts into the auto measure api via - * {@link #setAutoMeasureEnabled(boolean)}. - *

      - * When RecyclerView is running a layout, this value is always set to - * {@link View.MeasureSpec#EXACTLY} even if it was measured with a different spec mode. - * - * @return Width measure spec mode. - * - * @see View.MeasureSpec#getMode(int) - * @see View#onMeasure(int, int) - */ - public int getWidthMode() { - return mWidthMode; - } - - /** - * Return the height measurement spec mode of the RecyclerView. - *

      - * This value is set only if the LayoutManager opts into the auto measure api via - * {@link #setAutoMeasureEnabled(boolean)}. - *

      - * When RecyclerView is running a layout, this value is always set to - * {@link View.MeasureSpec#EXACTLY} even if it was measured with a different spec mode. - * - * @return Height measure spec mode. - * - * @see View.MeasureSpec#getMode(int) - * @see View#onMeasure(int, int) - */ - public int getHeightMode() { - return mHeightMode; - } - /** * Return the width of the parent RecyclerView * * @return Width in pixels */ public int getWidth() { - return mWidth; + return mRecyclerView != null ? mRecyclerView.getWidth() : 0; } /** @@ -7493,7 +5157,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @return Height in pixels */ public int getHeight() { - return mHeight; + return mRecyclerView != null ? mRecyclerView.getHeight() : 0; } /** @@ -7649,7 +5313,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } final ViewHolder vh = getChildViewHolderInt(view); vh.addFlags(ViewHolder.FLAG_IGNORE); - mRecyclerView.mViewInfoStore.removeViewHolder(vh); + mRecyclerView.mState.onViewIgnored(vh); } /** @@ -7691,14 +5355,13 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } return; } - if (viewHolder.isInvalid() && !viewHolder.isRemoved() && + if (viewHolder.isInvalid() && !viewHolder.isRemoved() && !viewHolder.isChanged() && !mRecyclerView.mAdapter.hasStableIds()) { removeViewAt(index); recycler.recycleViewHolderInternal(viewHolder); } else { detachViewAt(index); recycler.scrapView(view); - mRecyclerView.mViewInfoStore.onViewDetached(viewHolder); } } @@ -7710,33 +5373,23 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * call remove and invalidate RecyclerView to ensure UI update. * * @param recycler Recycler + * @param remove Whether scrapped views should be removed from ViewGroup or not. This + * method will invalidate RecyclerView if it removes any scrapped child. */ - void removeAndRecycleScrapInt(Recycler recycler) { + void removeAndRecycleScrapInt(Recycler recycler, boolean remove) { final int scrapCount = recycler.getScrapCount(); - // Loop backward, recycler might be changed by removeDetachedView() - for (int i = scrapCount - 1; i >= 0; i--) { + for (int i = 0; i < scrapCount; i++) { final View scrap = recycler.getScrapViewAt(i); - final ViewHolder vh = getChildViewHolderInt(scrap); - if (vh.shouldIgnore()) { + if (getChildViewHolderInt(scrap).shouldIgnore()) { continue; } - // If the scrap view is animating, we need to cancel them first. If we cancel it - // here, ItemAnimator callback may recycle it which will cause double recycling. - // To avoid this, we mark it as not recycleable before calling the item animator. - // Since removeDetachedView calls a user API, a common mistake (ending animations on - // the view) may recycle it too, so we guard it before we call user APIs. - vh.setIsRecyclable(false); - if (vh.isTmpDetached()) { + if (remove) { mRecyclerView.removeDetachedView(scrap, false); } - if (mRecyclerView.mItemAnimator != null) { - mRecyclerView.mItemAnimator.endAnimation(vh); - } - vh.setIsRecyclable(true); recycler.quickRecycleScrapView(scrap); } recycler.clearScrap(); - if (scrapCount > 0) { + if (remove && scrapCount > 0) { mRecyclerView.invalidate(); } } @@ -7759,85 +5412,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child); widthUsed += insets.left + insets.right; heightUsed += insets.top + insets.bottom; - final int widthSpec = getChildMeasureSpec(getWidth(), getWidthMode(), + + final int widthSpec = getChildMeasureSpec(getWidth(), getPaddingLeft() + getPaddingRight() + widthUsed, lp.width, canScrollHorizontally()); - final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(), + final int heightSpec = getChildMeasureSpec(getHeight(), getPaddingTop() + getPaddingBottom() + heightUsed, lp.height, canScrollVertically()); - if (shouldMeasureChild(child, widthSpec, heightSpec, lp)) { - child.measure(widthSpec, heightSpec); - } - } - - /** - * RecyclerView internally does its own View measurement caching which should help with - * WRAP_CONTENT. - *

      - * Use this method if the View is already measured once in this layout pass. - */ - boolean shouldReMeasureChild(View child, int widthSpec, int heightSpec, LayoutParams lp) { - return !mMeasurementCacheEnabled - || !isMeasurementUpToDate(child.getMeasuredWidth(), widthSpec, lp.width) - || !isMeasurementUpToDate(child.getMeasuredHeight(), heightSpec, lp.height); - } - - // we may consider making this public - /** - * RecyclerView internally does its own View measurement caching which should help with - * WRAP_CONTENT. - *

      - * Use this method if the View is not yet measured and you need to decide whether to - * measure this View or not. - */ - boolean shouldMeasureChild(View child, int widthSpec, int heightSpec, LayoutParams lp) { - return child.isLayoutRequested() - || !mMeasurementCacheEnabled - || !isMeasurementUpToDate(child.getWidth(), widthSpec, lp.width) - || !isMeasurementUpToDate(child.getHeight(), heightSpec, lp.height); - } - - /** - * In addition to the View Framework's measurement cache, RecyclerView uses its own - * additional measurement cache for its children to avoid re-measuring them when not - * necessary. It is on by default but it can be turned off via - * {@link #setMeasurementCacheEnabled(boolean)}. - * - * @return True if measurement cache is enabled, false otherwise. - * - * @see #setMeasurementCacheEnabled(boolean) - */ - public boolean isMeasurementCacheEnabled() { - return mMeasurementCacheEnabled; - } - - /** - * Sets whether RecyclerView should use its own measurement cache for the children. This is - * a more aggressive cache than the framework uses. - * - * @param measurementCacheEnabled True to enable the measurement cache, false otherwise. - * - * @see #isMeasurementCacheEnabled() - */ - public void setMeasurementCacheEnabled(boolean measurementCacheEnabled) { - mMeasurementCacheEnabled = measurementCacheEnabled; - } - - private static boolean isMeasurementUpToDate(int childSize, int spec, int dimension) { - final int specMode = MeasureSpec.getMode(spec); - final int specSize = MeasureSpec.getSize(spec); - if (dimension > 0 && childSize != dimension) { - return false; - } - switch (specMode) { - case MeasureSpec.UNSPECIFIED: - return true; - case MeasureSpec.AT_MOST: - return specSize >= childSize; - case MeasureSpec.EXACTLY: - return specSize == childSize; - } - return false; + child.measure(widthSpec, heightSpec); } /** @@ -7859,37 +5441,34 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro widthUsed += insets.left + insets.right; heightUsed += insets.top + insets.bottom; - final int widthSpec = getChildMeasureSpec(getWidth(), getWidthMode(), + final int widthSpec = getChildMeasureSpec(getWidth(), getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin + widthUsed, lp.width, canScrollHorizontally()); - final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(), + final int heightSpec = getChildMeasureSpec(getHeight(), getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin + heightUsed, lp.height, canScrollVertically()); - if (shouldMeasureChild(child, widthSpec, heightSpec, lp)) { - child.measure(widthSpec, heightSpec); - } + child.measure(widthSpec, heightSpec); } /** * Calculate a MeasureSpec value for measuring a child view in one dimension. * * @param parentSize Size of the parent view where the child will be placed - * @param padding Total space currently consumed by other elements of the parent + * @param padding Total space currently consumed by other elements of parent * @param childDimension Desired size of the child view, or MATCH_PARENT/WRAP_CONTENT. * Generally obtained from the child view's LayoutParams * @param canScroll true if the parent RecyclerView can scroll in this dimension * * @return a MeasureSpec value for the child view - * @deprecated use {@link #getChildMeasureSpec(int, int, int, int, boolean)} */ - @Deprecated public static int getChildMeasureSpec(int parentSize, int padding, int childDimension, boolean canScroll) { int size = Math.max(0, parentSize - padding); int resultSize = 0; int resultMode = 0; + if (canScroll) { if (childDimension >= 0) { resultSize = childDimension; @@ -7904,9 +5483,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro if (childDimension >= 0) { resultSize = childDimension; resultMode = MeasureSpec.EXACTLY; - } else if (childDimension == LayoutParams.MATCH_PARENT) { + } else if (childDimension == LayoutParams.FILL_PARENT) { resultSize = size; - // TODO this should be my spec. resultMode = MeasureSpec.EXACTLY; } else if (childDimension == LayoutParams.WRAP_CONTENT) { resultSize = size; @@ -7916,61 +5494,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return MeasureSpec.makeMeasureSpec(resultSize, resultMode); } - /** - * Calculate a MeasureSpec value for measuring a child view in one dimension. - * - * @param parentSize Size of the parent view where the child will be placed - * @param parentMode The measurement spec mode of the parent - * @param padding Total space currently consumed by other elements of parent - * @param childDimension Desired size of the child view, or MATCH_PARENT/WRAP_CONTENT. - * Generally obtained from the child view's LayoutParams - * @param canScroll true if the parent RecyclerView can scroll in this dimension - * - * @return a MeasureSpec value for the child view - */ - public static int getChildMeasureSpec(int parentSize, int parentMode, int padding, - int childDimension, boolean canScroll) { - int size = Math.max(0, parentSize - padding); - int resultSize = 0; - int resultMode = 0; - if (childDimension >= 0) { - resultSize = childDimension; - resultMode = MeasureSpec.EXACTLY; - } else if (canScroll) { - if (childDimension == LayoutParams.MATCH_PARENT){ - switch (parentMode) { - case MeasureSpec.AT_MOST: - case MeasureSpec.EXACTLY: - resultSize = size; - resultMode = parentMode; - break; - case MeasureSpec.UNSPECIFIED: - resultSize = 0; - resultMode = MeasureSpec.UNSPECIFIED; - break; - } - } else if (childDimension == LayoutParams.WRAP_CONTENT) { - resultSize = 0; - resultMode = MeasureSpec.UNSPECIFIED; - } - } else { - if (childDimension == LayoutParams.MATCH_PARENT) { - resultSize = size; - resultMode = parentMode; - } else if (childDimension == LayoutParams.WRAP_CONTENT) { - resultSize = size; - if (parentMode == MeasureSpec.AT_MOST || parentMode == MeasureSpec.EXACTLY) { - resultMode = MeasureSpec.AT_MOST; - } else { - resultMode = MeasureSpec.UNSPECIFIED; - } - - } - } - //noinspection WrongConstant - return MeasureSpec.makeMeasureSpec(resultSize, resultMode); - } - /** * Returns the measured width of the given child, plus the additional size of * any insets applied by {@link ItemDecoration ItemDecorations}. @@ -8008,8 +5531,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * ignore decoration insets within measurement and layout code. See the following * methods:

      *
        - *
      • {@link #layoutDecoratedWithMargins(View, int, int, int, int)}
      • - *
      • {@link #getDecoratedBoundsWithMargins(View, Rect)}
      • *
      • {@link #measureChild(View, int, int)}
      • *
      • {@link #measureChildWithMargins(View, int, int)}
      • *
      • {@link #getDecoratedLeft(View)}
      • @@ -8027,7 +5548,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param bottom Bottom edge, with item decoration insets included * * @see View#layout(int, int, int, int) - * @see #layoutDecoratedWithMargins(View, int, int, int, int) */ public void layoutDecorated(View child, int left, int top, int right, int bottom) { final Rect insets = ((LayoutParams) child.getLayoutParams()).mDecorInsets; @@ -8035,97 +5555,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro bottom - insets.bottom); } - /** - * Lay out the given child view within the RecyclerView using coordinates that - * include any current {@link ItemDecoration ItemDecorations} and margins. - * - *

        LayoutManagers should prefer working in sizes and coordinates that include - * item decoration insets whenever possible. This allows the LayoutManager to effectively - * ignore decoration insets within measurement and layout code. See the following - * methods:

        - *
          - *
        • {@link #layoutDecorated(View, int, int, int, int)}
        • - *
        • {@link #measureChild(View, int, int)}
        • - *
        • {@link #measureChildWithMargins(View, int, int)}
        • - *
        • {@link #getDecoratedLeft(View)}
        • - *
        • {@link #getDecoratedTop(View)}
        • - *
        • {@link #getDecoratedRight(View)}
        • - *
        • {@link #getDecoratedBottom(View)}
        • - *
        • {@link #getDecoratedMeasuredWidth(View)}
        • - *
        • {@link #getDecoratedMeasuredHeight(View)}
        • - *
        - * - * @param child Child to lay out - * @param left Left edge, with item decoration insets and left margin included - * @param top Top edge, with item decoration insets and top margin included - * @param right Right edge, with item decoration insets and right margin included - * @param bottom Bottom edge, with item decoration insets and bottom margin included - * - * @see View#layout(int, int, int, int) - * @see #layoutDecorated(View, int, int, int, int) - */ - public void layoutDecoratedWithMargins(View child, int left, int top, int right, - int bottom) { - final LayoutParams lp = (LayoutParams) child.getLayoutParams(); - final Rect insets = lp.mDecorInsets; - child.layout(left + insets.left + lp.leftMargin, top + insets.top + lp.topMargin, - right - insets.right - lp.rightMargin, - bottom - insets.bottom - lp.bottomMargin); - } - - /** - * Calculates the bounding box of the View while taking into account its matrix changes - * (translation, scale etc) with respect to the RecyclerView. - *

        - * If {@code includeDecorInsets} is {@code true}, they are applied first before applying - * the View's matrix so that the decor offsets also go through the same transformation. - * - * @param child The ItemView whose bounding box should be calculated. - * @param includeDecorInsets True if the decor insets should be included in the bounding box - * @param out The rectangle into which the output will be written. - */ - public void getTransformedBoundingBox(View child, boolean includeDecorInsets, Rect out) { - if (includeDecorInsets) { - Rect insets = ((LayoutParams) child.getLayoutParams()).mDecorInsets; - out.set(-insets.left, -insets.top, - child.getWidth() + insets.right, child.getHeight() + insets.bottom); - } else { - out.set(0, 0, child.getWidth(), child.getHeight()); - } - - if (mRecyclerView != null) { - final Matrix childMatrix = ViewCompat.getMatrix(child); - if (childMatrix != null && !childMatrix.isIdentity()) { - final RectF tempRectF = mRecyclerView.mTempRectF; - tempRectF.set(out); - childMatrix.mapRect(tempRectF); - out.set( - (int) Math.floor(tempRectF.left), - (int) Math.floor(tempRectF.top), - (int) Math.ceil(tempRectF.right), - (int) Math.ceil(tempRectF.bottom) - ); - } - } - out.offset(child.getLeft(), child.getTop()); - } - - /** - * Returns the bounds of the view including its decoration and margins. - * - * @param view The view element to check - * @param outBounds A rect that will receive the bounds of the element including its - * decoration and margins. - */ - public void getDecoratedBoundsWithMargins(View view, Rect outBounds) { - final LayoutParams lp = (LayoutParams) view.getLayoutParams(); - final Rect insets = lp.mDecorInsets; - outBounds.set(view.getLeft() - insets.left - lp.leftMargin, - view.getTop() - insets.top - lp.topMargin, - view.getRight() + insets.right + lp.rightMargin, - view.getBottom() + insets.bottom + lp.bottomMargin); - } - /** * Returns the left edge of the given child view within its parent, offset by any applied * {@link ItemDecoration ItemDecorations}. @@ -8277,7 +5706,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param state Transient state of RecyclerView * @return The chosen view to be focused */ - @Nullable public View onFocusSearchFailed(View focused, int direction, Recycler recycler, State state) { return null; @@ -8287,7 +5715,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * This method gives a LayoutManager an opportunity to intercept the initial focus search * before the default behavior of {@link FocusFinder} is used. If this method returns * null FocusFinder will attempt to find a focusable child view. If it fails - * then {@link #onFocusSearchFailed(View, int, RecyclerView.Recycler, RecyclerView.State)} + * then {@link #onFocusSearchFailed(View, int, Recycler, State)} * will be called to give the LayoutManager an opportunity to add new views for items * that did not have attached views representing them. The LayoutManager should not add * or remove views from this method. @@ -8305,8 +5733,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Called when a child of the RecyclerView wants a particular rectangle to be positioned - * onto the screen. See {@link ViewParent#requestChildRectangleOnScreen(android.view.View, - * android.graphics.Rect, boolean)} for more details. + * onto the screen. See {@link ViewParent#requestChildRectangleOnScreen(View, + * Rect, boolean)} for more details. * *

        The base implementation will attempt to perform a standard programmatic scroll * to bring the given rect into view, within the padded area of the RecyclerView.

        @@ -8324,10 +5752,10 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro final int parentTop = getPaddingTop(); final int parentRight = getWidth() - getPaddingRight(); final int parentBottom = getHeight() - getPaddingBottom(); - final int childLeft = child.getLeft() + rect.left - child.getScrollX(); - final int childTop = child.getTop() + rect.top - child.getScrollY(); - final int childRight = childLeft + rect.width(); - final int childBottom = childTop + rect.height(); + final int childLeft = child.getLeft() + rect.left; + final int childTop = child.getTop() + rect.top; + final int childRight = childLeft + rect.right; + final int childBottom = childTop + rect.bottom; final int offScreenLeft = Math.min(0, childLeft - parentLeft); final int offScreenTop = Math.min(0, childTop - parentTop); @@ -8335,27 +5763,22 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro final int offScreenBottom = Math.max(0, childBottom - parentBottom); // Favor the "start" layout direction over the end when bringing one side or the other - // of a large rect into view. If we decide to bring in end because start is already - // visible, limit the scroll such that start won't go out of bounds. + // of a large rect into view. final int dx; - if (getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL) { - dx = offScreenRight != 0 ? offScreenRight - : Math.max(offScreenLeft, childRight - parentRight); + if (ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL) { + dx = offScreenRight != 0 ? offScreenRight : offScreenLeft; } else { - dx = offScreenLeft != 0 ? offScreenLeft - : Math.min(childLeft - parentLeft, offScreenRight); + dx = offScreenLeft != 0 ? offScreenLeft : offScreenRight; } - // Favor bringing the top into view over the bottom. If top is already visible and - // we should scroll to make bottom visible, make sure top does not go out of bounds. - final int dy = offScreenTop != 0 ? offScreenTop - : Math.min(childTop - parentTop, offScreenBottom); - + // Favor bringing the top into view over the bottom + final int dy = offScreenTop; +// final int dy = offScreenTop != 0 ? offScreenTop : offScreenBottom; if (dx != 0 || dy != 0) { if (immediate) { parent.scrollBy(dx, dy); } else { - parent.smoothScrollBy(dx, dy); +// parent.smoothScrollBy(dx, dy); } return true; } @@ -8367,8 +5790,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro */ @Deprecated public boolean onRequestChildFocus(RecyclerView parent, View child, View focused) { - // eat the request if we are in the middle of a scroll or layout - return isSmoothScrolling() || parent.isComputingLayout(); + return false; } /** @@ -8403,7 +5825,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param oldAdapter The previous adapter instance. Will be null if there was previously no * adapter. * @param newAdapter The new adapter instance. Might be null if - * {@link #setAdapter(RecyclerView.Adapter)} is called with {@code null}. + * {@link #setAdapter(Adapter)} is called with {@code null}. */ public void onAdapterChanged(Adapter oldAdapter, Adapter newAdapter) { } @@ -8412,7 +5834,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * Called to populate focusable views within the RecyclerView. * *

        The LayoutManager implementation should return true if the default - * behavior of {@link ViewGroup#addFocusables(java.util.ArrayList, int)} should be + * behavior of {@link ViewGroup#addFocusables(ArrayList, int)} should be * suppressed.

        * *

        The default implementation returns false to trigger RecyclerView @@ -8470,8 +5892,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Called when items have been changed in the adapter. - * To receive payload, override {@link #onItemsUpdated(RecyclerView, int, int, Object)} - * instead, then this callback will not be invoked. * * @param recyclerView * @param positionStart @@ -8480,20 +5900,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount) { } - /** - * Called when items have been changed in the adapter and with optional payload. - * Default implementation calls {@link #onItemsUpdated(RecyclerView, int, int)}. - * - * @param recyclerView - * @param positionStart - * @param itemCount - * @param payload - */ - public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount, - Object payload) { - onItemsUpdated(recyclerView, positionStart, itemCount); - } - /** * Called when an item is moved withing the adapter. *

        @@ -8612,11 +6018,41 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * * @param recycler Recycler * @param state Transient state of RecyclerView - * @param widthSpec Width {@link android.view.View.MeasureSpec} - * @param heightSpec Height {@link android.view.View.MeasureSpec} + * @param widthSpec Width {@link MeasureSpec} + * @param heightSpec Height {@link MeasureSpec} */ public void onMeasure(Recycler recycler, State state, int widthSpec, int heightSpec) { - mRecyclerView.defaultOnMeasure(widthSpec, heightSpec); + final int widthMode = MeasureSpec.getMode(widthSpec); + final int heightMode = MeasureSpec.getMode(heightSpec); + final int widthSize = MeasureSpec.getSize(widthSpec); + final int heightSize = MeasureSpec.getSize(heightSpec); + + int width = 0; + int height = 0; + + switch (widthMode) { + case MeasureSpec.EXACTLY: + case MeasureSpec.AT_MOST: + width = widthSize; + break; + case MeasureSpec.UNSPECIFIED: + default: + width = getMinimumWidth(); + break; + } + + switch (heightMode) { + case MeasureSpec.EXACTLY: + case MeasureSpec.AT_MOST: + height = heightSize; + break; + case MeasureSpec.UNSPECIFIED: + default: + height = getMinimumHeight(); + break; + } + + setMeasuredDimension(width, height); } /** @@ -8705,7 +6141,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // called by accessibility delegate void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfoCompat info) { - onInitializeAccessibilityNodeInfo(mRecyclerView.mRecycler, mRecyclerView.mState, info); + onInitializeAccessibilityNodeInfo(mRecyclerView.mRecycler, mRecyclerView.mState, + info); } /** @@ -8713,13 +6150,13 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * be populated. *

        * Default implementation adds a {@link - * android.support.v4.view.accessibility.AccessibilityNodeInfoCompat.CollectionInfoCompat}. + * AccessibilityNodeInfoCompat.CollectionInfoCompat}. *

        * You should override - * {@link #getRowCountForAccessibility(RecyclerView.Recycler, RecyclerView.State)}, - * {@link #getColumnCountForAccessibility(RecyclerView.Recycler, RecyclerView.State)}, - * {@link #isLayoutHierarchical(RecyclerView.Recycler, RecyclerView.State)} and - * {@link #getSelectionModeForAccessibility(RecyclerView.Recycler, RecyclerView.State)} for + * {@link #getRowCountForAccessibility(Recycler, State)}, + * {@link #getColumnCountForAccessibility(Recycler, State)}, + * {@link #isLayoutHierarchical(Recycler, State)} and + * {@link #getSelectionModeForAccessibility(Recycler, State)} for * more accurate accessibility information. * * @param recycler The Recycler that can be used to convert view positions into adapter @@ -8728,13 +6165,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param info The info that should be filled by the LayoutManager * @see View#onInitializeAccessibilityNodeInfo( *android.view.accessibility.AccessibilityNodeInfo) - * @see #getRowCountForAccessibility(RecyclerView.Recycler, RecyclerView.State) - * @see #getColumnCountForAccessibility(RecyclerView.Recycler, RecyclerView.State) - * @see #isLayoutHierarchical(RecyclerView.Recycler, RecyclerView.State) - * @see #getSelectionModeForAccessibility(RecyclerView.Recycler, RecyclerView.State) + * @see #getRowCountForAccessibility(Recycler, State) + * @see #getColumnCountForAccessibility(Recycler, State) + * @see #isLayoutHierarchical(Recycler, State) + * @see #getSelectionModeForAccessibility(Recycler, State) */ public void onInitializeAccessibilityNodeInfo(Recycler recycler, State state, AccessibilityNodeInfoCompat info) { + info.setClassName(RecyclerView.class.getName()); if (ViewCompat.canScrollVertically(mRecyclerView, -1) || ViewCompat.canScrollHorizontally(mRecyclerView, -1)) { info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD); @@ -8768,7 +6206,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * positions * @param state The current state of RecyclerView * @param event The event instance to initialize - * @see View#onInitializeAccessibilityEvent(android.view.accessibility.AccessibilityEvent) + * @see View#onInitializeAccessibilityEvent(AccessibilityEvent) */ public void onInitializeAccessibilityEvent(Recycler recycler, State state, AccessibilityEvent event) { @@ -8788,13 +6226,10 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } // called by accessibility delegate - void onInitializeAccessibilityNodeInfoForItem(View host, AccessibilityNodeInfoCompat info) { - final ViewHolder vh = getChildViewHolderInt(host); - // avoid trying to create accessibility node info for removed children - if (vh != null && !vh.isRemoved() && !mChildHelper.isHidden(vh.itemView)) { - onInitializeAccessibilityNodeInfoForItem(mRecyclerView.mRecycler, - mRecyclerView.mState, host, info); - } + void onInitializeAccessibilityNodeInfoForItem(View host, + AccessibilityNodeInfoCompat info) { + onInitializeAccessibilityNodeInfoForItem(mRecyclerView.mRecycler, mRecyclerView.mState, + host, info); } /** @@ -8920,7 +6355,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param state The current state of RecyclerView * @param action The action to perform * @param args Optional action arguments - * @see View#performAccessibilityAction(int, android.os.Bundle) + * @see View#performAccessibilityAction(int, Bundle) */ public boolean performAccessibilityAction(Recycler recycler, State state, int action, Bundle args) { @@ -8961,7 +6396,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Called by AccessibilityDelegate when an accessibility action is requested on one of the - * children of LayoutManager. + * chidren of LayoutManager. *

        * Default implementation does not do anything. * @@ -8972,81 +6407,21 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param action The action to perform * @param args Optional action arguments * @return true if action is handled - * @see View#performAccessibilityAction(int, android.os.Bundle) + * @see View#performAccessibilityAction(int, Bundle) */ public boolean performAccessibilityActionForItem(Recycler recycler, State state, View view, int action, Bundle args) { return false; } + } - /** - * Parse the xml attributes to get the most common properties used by layout managers. - * - * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_android_orientation - * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_spanCount - * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_reverseLayout - * @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_stackFromEnd - * - * @return an object containing the properties as specified in the attrs. - */ - public static Properties getProperties(Context context, AttributeSet attrs, - int defStyleAttr, int defStyleRes) { - Properties properties = new Properties(); - TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecyclerView, - defStyleAttr, defStyleRes); - properties.orientation = a.getInt(R.styleable.RecyclerView_android_orientation, VERTICAL); - properties.spanCount = a.getInt(R.styleable.RecyclerView_spanCount, 1); - properties.reverseLayout = a.getBoolean(R.styleable.RecyclerView_reverseLayout, false); - properties.stackFromEnd = a.getBoolean(R.styleable.RecyclerView_stackFromEnd, false); - a.recycle(); - return properties; - } + private void removeFromDisappearingList(View child) { + mDisappearingViewsInLayoutPass.remove(child); + } - void setExactMeasureSpecsFrom(RecyclerView recyclerView) { - setMeasureSpecs( - MeasureSpec.makeMeasureSpec(recyclerView.getWidth(), MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(recyclerView.getHeight(), MeasureSpec.EXACTLY) - ); - } - - /** - * Internal API to allow LayoutManagers to be measured twice. - *

        - * This is not public because LayoutManagers should be able to handle their layouts in one - * pass but it is very convenient to make existing LayoutManagers support wrapping content - * when both orientations are undefined. - *

        - * This API will be removed after default LayoutManagers properly implement wrap content in - * non-scroll orientation. - */ - boolean shouldMeasureTwice() { - return false; - } - - boolean hasFlexibleChildInBothOrientations() { - final int childCount = getChildCount(); - for (int i = 0; i < childCount; i++) { - final View child = getChildAt(i); - final ViewGroup.LayoutParams lp = child.getLayoutParams(); - if (lp.width < 0 && lp.height < 0) { - return true; - } - } - return false; - } - - /** - * Some general properties that a LayoutManager may want to use. - */ - public static class Properties { - /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_android_orientation */ - public int orientation; - /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_spanCount */ - public int spanCount; - /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_reverseLayout */ - public boolean reverseLayout; - /** @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_stackFromEnd */ - public boolean stackFromEnd; + private void addToDisappearingList(View child) { + if (!mDisappearingViewsInLayoutPass.contains(child)) { + mDisappearingViewsInLayoutPass.add(child); } } @@ -9056,9 +6431,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * between items, highlights, visual grouping boundaries and more. * *

        All ItemDecorations are drawn in the order they were added, before the item - * views (in {@link ItemDecoration#onDraw(Canvas, RecyclerView, RecyclerView.State) onDraw()} + * views (in {@link ItemDecoration#onDraw(Canvas, RecyclerView, State) onDraw()} * and after the items (in {@link ItemDecoration#onDrawOver(Canvas, RecyclerView, - * RecyclerView.State)}.

        + * State)}.

        */ public static abstract class ItemDecoration { /** @@ -9076,7 +6451,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * @deprecated - * Override {@link #onDraw(Canvas, RecyclerView, RecyclerView.State)} + * Override {@link #onDraw(Canvas, RecyclerView, State)} */ @Deprecated public void onDraw(Canvas c, RecyclerView parent) { @@ -9097,7 +6472,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * @deprecated - * Override {@link #onDrawOver(Canvas, RecyclerView, RecyclerView.State)} + * Override {@link #onDrawOver(Canvas, RecyclerView, State)} */ @Deprecated public void onDrawOver(Canvas c, RecyclerView parent) { @@ -9118,15 +6493,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * the number of pixels that the item view should be inset by, similar to padding or margin. * The default implementation sets the bounds of outRect to 0 and returns. * - *

        - * If this ItemDecoration does not affect the positioning of item views, it should set + *

        If this ItemDecoration does not affect the positioning of item views it should set * all four fields of outRect (left, top, right, bottom) to zero - * before returning. - * - *

        - * If you need to access Adapter for additional data, you can call - * {@link RecyclerView#getChildAdapterPosition(View)} to get the adapter position of the - * View. + * before returning.

        * * @param outRect Rect to receive the output. * @param view The child view to decorate @@ -9134,7 +6503,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param state The current state of RecyclerView. */ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) { - getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(), + getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewPosition(), parent); } } @@ -9148,10 +6517,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * manipulation of item views within the RecyclerView. OnItemTouchListeners may intercept * a touch interaction already in progress even if the RecyclerView is already handling that * gesture stream itself for the purposes of scrolling.

        - * - * @see SimpleOnItemTouchListener */ - public static interface OnItemTouchListener { + public interface OnItemTouchListener { /** * Silently observe and/or take over touch events sent to the RecyclerView * before they are handled by either the RecyclerView itself or its child views. @@ -9176,53 +6543,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * the RecyclerView's coordinate system. */ public void onTouchEvent(RecyclerView rv, MotionEvent e); - - /** - * Called when a child of RecyclerView does not want RecyclerView and its ancestors to - * intercept touch events with - * {@link ViewGroup#onInterceptTouchEvent(MotionEvent)}. - * - * @param disallowIntercept True if the child does not want the parent to - * intercept touch events. - * @see ViewParent#requestDisallowInterceptTouchEvent(boolean) - */ - public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept); } /** - * An implementation of {@link RecyclerView.OnItemTouchListener} that has empty method bodies and - * default return values. - *

        - * You may prefer to extend this class if you don't need to override all methods. Another - * benefit of using this class is future compatibility. As the interface may change, we'll - * always provide a default implementation on this class so that your code won't break when - * you update to a new version of the support library. - */ - public static class SimpleOnItemTouchListener implements RecyclerView.OnItemTouchListener { - @Override - public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { - return false; - } - - @Override - public void onTouchEvent(RecyclerView rv, MotionEvent e) { - } - - @Override - public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { - } - } - - - /** - * An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event - * has occurred on that RecyclerView. - *

        - * @see RecyclerView#addOnScrollListener(OnScrollListener) - * @see RecyclerView#clearOnChildAttachStateChangeListeners() + * An OnScrollListener can be set on a RecyclerView to receive messages + * when a scrolling event has occurred on that RecyclerView. * + * @see RecyclerView#setOnScrollListener(OnScrollListener) */ - public abstract static class OnScrollListener { + abstract static public class OnScrollListener { /** * Callback method to be invoked when RecyclerView's scroll state changes. * @@ -9235,9 +6564,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Callback method to be invoked when the RecyclerView has been scrolled. This will be * called after the scroll has completed. - *

        - * This callback will also be called if visible item range changes after a layout - * calculation. In that case, dx and dy will be 0. * * @param recyclerView The RecyclerView which scrolled. * @param dx The amount of horizontal scroll. @@ -9257,37 +6583,11 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * This method is called whenever the view in the ViewHolder is recycled. * - * RecyclerView calls this method right before clearing ViewHolder's internal data and - * sending it to RecycledViewPool. This way, if ViewHolder was holding valid information - * before being recycled, you can call {@link ViewHolder#getAdapterPosition()} to get - * its adapter position. - * * @param holder The ViewHolder containing the view that was recycled */ public void onViewRecycled(ViewHolder holder); } - /** - * A Listener interface that can be attached to a RecylcerView to get notified - * whenever a ViewHolder is attached to or detached from RecyclerView. - */ - public interface OnChildAttachStateChangeListener { - - /** - * Called when a view is attached to the RecyclerView. - * - * @param view The View which is attached to the RecyclerView - */ - public void onChildViewAttachedToWindow(View view); - - /** - * Called when a view is detached from RecyclerView. - * - * @param view The View which is being detached from the RecyclerView - */ - public void onChildViewDetachedFromWindow(View view); - } - /** * A ViewHolder describes an item view and metadata about its place within the RecyclerView. * @@ -9353,6 +6653,12 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro */ static final int FLAG_RETURNED_FROM_SCRAP = 1 << 5; + /** + * This ViewHolder's contents have changed. This flag is used as an indication that + * change animations may be used, if supported by the ItemAnimator. + */ + static final int FLAG_CHANGED = 1 << 6; + /** * This ViewHolder is fully managed by the LayoutManager. We do not scrap, recycle or remove * it unless LayoutManager is replaced. @@ -9360,76 +6666,13 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro */ static final int FLAG_IGNORE = 1 << 7; - /** - * When the View is detached form the parent, we set this flag so that we can take correct - * action when we need to remove it or add it back. - */ - static final int FLAG_TMP_DETACHED = 1 << 8; - - /** - * Set when we can no longer determine the adapter position of this ViewHolder until it is - * rebound to a new position. It is different than FLAG_INVALID because FLAG_INVALID is - * set even when the type does not match. Also, FLAG_ADAPTER_POSITION_UNKNOWN is set as soon - * as adapter notification arrives vs FLAG_INVALID is set lazily before layout is - * re-calculated. - */ - static final int FLAG_ADAPTER_POSITION_UNKNOWN = 1 << 9; - - /** - * Set when a addChangePayload(null) is called - */ - static final int FLAG_ADAPTER_FULLUPDATE = 1 << 10; - - /** - * Used by ItemAnimator when a ViewHolder's position changes - */ - static final int FLAG_MOVED = 1 << 11; - - /** - * Used by ItemAnimator when a ViewHolder appears in pre-layout - */ - static final int FLAG_APPEARED_IN_PRE_LAYOUT = 1 << 12; - - /** - * Used when a ViewHolder starts the layout pass as a hidden ViewHolder but is re-used from - * hidden list (as if it was scrap) without being recycled in between. - * - * When a ViewHolder is hidden, there are 2 paths it can be re-used: - * a) Animation ends, view is recycled and used from the recycle pool. - * b) LayoutManager asks for the View for that position while the ViewHolder is hidden. - * - * This flag is used to represent "case b" where the ViewHolder is reused without being - * recycled (thus "bounced" from the hidden list). This state requires special handling - * because the ViewHolder must be added to pre layout maps for animations as if it was - * already there. - */ - static final int FLAG_BOUNCED_FROM_HIDDEN_LIST = 1 << 13; - private int mFlags; - private static final List FULLUPDATE_PAYLOADS = Collections.EMPTY_LIST; - - List mPayloads = null; - List mUnmodifiedPayloads = null; - private int mIsRecyclableCount = 0; // If non-null, view is currently considered scrap and may be reused for other data by the // scrap container. private Recycler mScrapContainer = null; - // Keeps whether this ViewHolder lives in Change scrap or Attached scrap - private boolean mInChangeScrap = false; - - // Saves isImportantForAccessibility value for the view item while it's in hidden state and - // marked as unimportant for accessibility. - private int mWasImportantForAccessibilityBeforeHidden = - ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO; - - /** - * Is set when VH is bound from the adapter and cleaned right before it is sent to - * {@link RecycledViewPool}. - */ - RecyclerView mOwnerRecyclerView; public ViewHolder(View itemView) { if (itemView == null) { @@ -9475,74 +6718,10 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return (mFlags & FLAG_IGNORE) != 0; } - /** - * @deprecated This method is deprecated because its meaning is ambiguous due to the async - * handling of adapter updates. Please use {@link #getLayoutPosition()} or - * {@link #getAdapterPosition()} depending on your use case. - * - * @see #getLayoutPosition() - * @see #getAdapterPosition() - */ - @Deprecated public final int getPosition() { return mPreLayoutPosition == NO_POSITION ? mPosition : mPreLayoutPosition; } - /** - * Returns the position of the ViewHolder in terms of the latest layout pass. - *

        - * This position is mostly used by RecyclerView components to be consistent while - * RecyclerView lazily processes adapter updates. - *

        - * For performance and animation reasons, RecyclerView batches all adapter updates until the - * next layout pass. This may cause mismatches between the Adapter position of the item and - * the position it had in the latest layout calculations. - *

        - * LayoutManagers should always call this method while doing calculations based on item - * positions. All methods in {@link RecyclerView.LayoutManager}, {@link RecyclerView.State}, - * {@link RecyclerView.Recycler} that receive a position expect it to be the layout position - * of the item. - *

        - * If LayoutManager needs to call an external method that requires the adapter position of - * the item, it can use {@link #getAdapterPosition()} or - * {@link RecyclerView.Recycler#convertPreLayoutPositionToPostLayout(int)}. - * - * @return Returns the adapter position of the ViewHolder in the latest layout pass. - * @see #getAdapterPosition() - */ - public final int getLayoutPosition() { - return mPreLayoutPosition == NO_POSITION ? mPosition : mPreLayoutPosition; - } - - /** - * Returns the Adapter position of the item represented by this ViewHolder. - *

        - * Note that this might be different than the {@link #getLayoutPosition()} if there are - * pending adapter updates but a new layout pass has not happened yet. - *

        - * RecyclerView does not handle any adapter updates until the next layout traversal. This - * may create temporary inconsistencies between what user sees on the screen and what - * adapter contents have. This inconsistency is not important since it will be less than - * 16ms but it might be a problem if you want to use ViewHolder position to access the - * adapter. Sometimes, you may need to get the exact adapter position to do - * some actions in response to user events. In that case, you should use this method which - * will calculate the Adapter position of the ViewHolder. - *

        - * Note that if you've called {@link RecyclerView.Adapter#notifyDataSetChanged()}, until the - * next layout pass, the return value of this method will be {@link #NO_POSITION}. - * - * @return The adapter position of the item if it still exists in the adapter. - * {@link RecyclerView#NO_POSITION} if item has been removed from the adapter, - * {@link RecyclerView.Adapter#notifyDataSetChanged()} has been called after the last - * layout pass or the ViewHolder has already been recycled. - */ - public final int getAdapterPosition() { - if (mOwnerRecyclerView == null) { - return NO_POSITION; - } - return mOwnerRecyclerView.getAdapterPositionFor(this); - } - /** * When LayoutManager supports animations, RecyclerView tracks 3 positions for ViewHolders * to perform animations. @@ -9561,7 +6740,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Returns The itemId represented by this ViewHolder. * - * @return The item's id if adapter has stable ids, {@link RecyclerView#NO_ID} + * @return The the item's id if adapter has stable ids, {@link RecyclerView#NO_ID} * otherwise */ public final long getItemId() { @@ -9591,17 +6770,12 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mFlags = mFlags & ~FLAG_RETURNED_FROM_SCRAP; } - void clearTmpDetachFlag() { - mFlags = mFlags & ~FLAG_TMP_DETACHED; - } - void stopIgnoring() { mFlags = mFlags & ~FLAG_IGNORE; } - void setScrapContainer(Recycler recycler, boolean isChangeScrap) { + void setScrapContainer(Recycler recycler) { mScrapContainer = recycler; - mInChangeScrap = isChangeScrap; } boolean isInvalid() { @@ -9612,6 +6786,10 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return (mFlags & FLAG_UPDATE) != 0; } + boolean isChanged() { + return (mFlags & FLAG_CHANGED) != 0; + } + boolean isBound() { return (mFlags & FLAG_BOUND) != 0; } @@ -9620,18 +6798,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return (mFlags & FLAG_REMOVED) != 0; } - boolean hasAnyOfTheFlags(int flags) { - return (mFlags & flags) != 0; - } - - boolean isTmpDetached() { - return (mFlags & FLAG_TMP_DETACHED) != 0; - } - - boolean isAdapterPositionUnknown() { - return (mFlags & FLAG_ADAPTER_POSITION_UNKNOWN) != 0 || isInvalid(); - } - void setFlags(int flags, int mask) { mFlags = (mFlags & ~mask) | (flags & mask); } @@ -9640,43 +6806,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mFlags |= flags; } - void addChangePayload(Object payload) { - if (payload == null) { - addFlags(FLAG_ADAPTER_FULLUPDATE); - } else if ((mFlags & FLAG_ADAPTER_FULLUPDATE) == 0) { - createPayloadsIfNeeded(); - mPayloads.add(payload); - } - } - - private void createPayloadsIfNeeded() { - if (mPayloads == null) { - mPayloads = new ArrayList(); - mUnmodifiedPayloads = Collections.unmodifiableList(mPayloads); - } - } - - void clearPayload() { - if (mPayloads != null) { - mPayloads.clear(); - } - mFlags = mFlags & ~FLAG_ADAPTER_FULLUPDATE; - } - - List getUnmodifiedPayloads() { - if ((mFlags & FLAG_ADAPTER_FULLUPDATE) == 0) { - if (mPayloads == null || mPayloads.size() == 0) { - // Initial state, no update being called. - return FULLUPDATE_PAYLOADS; - } - // there are none-null payloads - return mUnmodifiedPayloads; - } else { - // a full update has been called. - return FULLUPDATE_PAYLOADS; - } - } - void resetInternal() { mFlags = 0; mPosition = NO_POSITION; @@ -9686,28 +6815,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mIsRecyclableCount = 0; mShadowedHolder = null; mShadowingHolder = null; - clearPayload(); - mWasImportantForAccessibilityBeforeHidden = ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO; - } - - /** - * Called when the child view enters the hidden state - */ - private void onEnteredHiddenState() { - // While the view item is in hidden state, make it invisible for the accessibility. - mWasImportantForAccessibilityBeforeHidden = - ViewCompat.getImportantForAccessibility(itemView); - ViewCompat.setImportantForAccessibility(itemView, - ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); - } - - /** - * Called when the child view leaves the hidden state - */ - private void onLeftHiddenState() { - ViewCompat.setImportantForAccessibility( - itemView, mWasImportantForAccessibilityBeforeHidden); - mWasImportantForAccessibilityBeforeHidden = ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO; } @Override @@ -9715,19 +6822,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro final StringBuilder sb = new StringBuilder("ViewHolder{" + Integer.toHexString(hashCode()) + " position=" + mPosition + " id=" + mItemId + ", oldPos=" + mOldPosition + ", pLpos:" + mPreLayoutPosition); - if (isScrap()) { - sb.append(" scrap ") - .append(mInChangeScrap ? "[changeScrap]" : "[attachedScrap]"); - } + if (isScrap()) sb.append(" scrap"); if (isInvalid()) sb.append(" invalid"); if (!isBound()) sb.append(" unbound"); if (needsUpdate()) sb.append(" update"); if (isRemoved()) sb.append(" removed"); if (shouldIgnore()) sb.append(" ignored"); - if (isTmpDetached()) sb.append(" tmpDetached"); + if (isChanged()) sb.append(" changed"); if (!isRecyclable()) sb.append(" not recyclable(" + mIsRecyclableCount + ")"); - if (isAdapterPositionUnknown()) sb.append(" undefined adapter position"); - if (itemView.getParent() == null) sb.append(" no parent"); sb.append("}"); return sb.toString(); @@ -9773,93 +6875,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro return (mFlags & FLAG_NOT_RECYCLABLE) == 0 && !ViewCompat.hasTransientState(itemView); } - - /** - * Returns whether we have animations referring to this view holder or not. - * This is similar to isRecyclable flag but does not check transient state. - */ - private boolean shouldBeKeptAsChild() { - return (mFlags & FLAG_NOT_RECYCLABLE) != 0; - } - - /** - * @return True if ViewHolder is not referenced by RecyclerView animations but has - * transient state which will prevent it from being recycled. - */ - private boolean doesTransientStatePreventRecycling() { - return (mFlags & FLAG_NOT_RECYCLABLE) == 0 && ViewCompat.hasTransientState(itemView); - } - - boolean isUpdated() { - return (mFlags & FLAG_UPDATE) != 0; - } - } - - private int getAdapterPositionFor(ViewHolder viewHolder) { - if (viewHolder.hasAnyOfTheFlags( ViewHolder.FLAG_INVALID | - ViewHolder.FLAG_REMOVED | ViewHolder.FLAG_ADAPTER_POSITION_UNKNOWN) - || !viewHolder.isBound()) { - return RecyclerView.NO_POSITION; - } - return mAdapterHelper.applyPendingUpdatesToPosition(viewHolder.mPosition); - } - - // NestedScrollingChild - - @Override - public void setNestedScrollingEnabled(boolean enabled) { - getScrollingChildHelper().setNestedScrollingEnabled(enabled); - } - - @Override - public boolean isNestedScrollingEnabled() { - return getScrollingChildHelper().isNestedScrollingEnabled(); - } - - @Override - public boolean startNestedScroll(int axes) { - return getScrollingChildHelper().startNestedScroll(axes); - } - - @Override - public void stopNestedScroll() { - getScrollingChildHelper().stopNestedScroll(); - } - - @Override - public boolean hasNestedScrollingParent() { - return getScrollingChildHelper().hasNestedScrollingParent(); - } - - @Override - public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, - int dyUnconsumed, int[] offsetInWindow) { - return getScrollingChildHelper().dispatchNestedScroll(dxConsumed, dyConsumed, - dxUnconsumed, dyUnconsumed, offsetInWindow); - } - - @Override - public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) { - return getScrollingChildHelper().dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow); - } - - @Override - public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) { - return getScrollingChildHelper().dispatchNestedFling(velocityX, velocityY, consumed); - } - - @Override - public boolean dispatchNestedPreFling(float velocityX, float velocityY) { - return getScrollingChildHelper().dispatchNestedPreFling(velocityX, velocityY); } /** - * {@link android.view.ViewGroup.MarginLayoutParams LayoutParams} subclass for children of + * {@link MarginLayoutParams LayoutParams} subclass for children of * {@link RecyclerView}. Custom {@link LayoutManager layout managers} are encouraged * to create their own subclass of this LayoutParams class * to store any additional required per-child view metadata about the layout. */ - public static class LayoutParams extends android.view.ViewGroup.MarginLayoutParams { + public static class LayoutParams extends MarginLayoutParams { ViewHolder mViewHolder; final Rect mDecorInsets = new Rect(); boolean mInsetsDirty = true; @@ -9927,38 +6951,17 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @return true if the item the view corresponds to was changed in the data set */ public boolean isItemChanged() { - return mViewHolder.isUpdated(); + return mViewHolder.isChanged(); } /** - * @deprecated use {@link #getViewLayoutPosition()} or {@link #getViewAdapterPosition()} + * Returns the position that the view this LayoutParams is attached to corresponds to. + * + * @return the adapter position this view was bound from */ - @Deprecated public int getViewPosition() { return mViewHolder.getPosition(); } - - /** - * Returns the adapter position that the view this LayoutParams is attached to corresponds - * to as of latest layout calculation. - * - * @return the adapter position this view as of latest layout pass - */ - public int getViewLayoutPosition() { - return mViewHolder.getLayoutPosition(); - } - - /** - * Returns the up-to-date adapter position that the view this LayoutParams is attached to - * corresponds to. - * - * @return the up-to-date adapter position this view. It may return - * {@link RecyclerView#NO_POSITION} if item represented by this View has been removed or - * its up-to-date position cannot be calculated. - */ - public int getViewAdapterPosition() { - return mViewHolder.getAdapterPosition(); - } } /** @@ -9974,12 +6977,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro // do nothing } - public void onItemRangeChanged(int positionStart, int itemCount, Object payload) { - // fallback to onItemRangeChanged(positionStart, itemCount) if app - // does not override this method. - onItemRangeChanged(positionStart, itemCount); - } - public void onItemRangeInserted(int positionStart, int itemCount) { // do nothing } @@ -10023,8 +7020,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * Starts a smooth scroll for the given target position. *

        In each animation step, {@link RecyclerView} will check * for the target view and call either - * {@link #onTargetFound(android.view.View, RecyclerView.State, SmoothScroller.Action)} or - * {@link #onSeekTargetStep(int, int, RecyclerView.State, SmoothScroller.Action)} until + * {@link #onTargetFound(View, State, Action)} or + * {@link #onSeekTargetStep(int, int, State, Action)} until * SmoothScroller is stopped.

        * *

        Note that if RecyclerView finds the target view, it will automatically stop the @@ -10050,10 +7047,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * @return The LayoutManager to which this SmoothScroller is attached. Will return - * null after the SmoothScroller is stopped. + * @return The LayoutManager to which this SmoothScroller is attached */ - @Nullable public LayoutManager getLayoutManager() { return mLayoutManager; } @@ -10061,8 +7056,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * Stops running the SmoothScroller in each animation callback. Note that this does not * cancel any existing {@link Action} updated by - * {@link #onTargetFound(android.view.View, RecyclerView.State, SmoothScroller.Action)} or - * {@link #onSeekTargetStep(int, int, RecyclerView.State, SmoothScroller.Action)}. + * {@link #onTargetFound(View, State, Action)} or + * {@link #onSeekTargetStep(int, int, State, Action)}. */ final protected void stop() { if (!mRunning) { @@ -10111,16 +7106,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } private void onAnimation(int dx, int dy) { - final RecyclerView recyclerView = mRecyclerView; - if (!mRunning || mTargetPosition == RecyclerView.NO_POSITION || recyclerView == null) { + if (!mRunning || mTargetPosition == RecyclerView.NO_POSITION) { stop(); } mPendingInitialRun = false; if (mTargetView != null) { // verify target position if (getChildPosition(mTargetView) == mTargetPosition) { - onTargetFound(mTargetView, recyclerView.mState, mRecyclingAction); - mRecyclingAction.runIfNecessary(recyclerView); + onTargetFound(mTargetView, mRecyclerView.mState, mRecyclingAction); + mRecyclingAction.runIfNecessary(mRecyclerView); stop(); } else { Log.e(TAG, "Passed over target position while smooth scrolling."); @@ -10128,37 +7122,27 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } } if (mRunning) { - onSeekTargetStep(dx, dy, recyclerView.mState, mRecyclingAction); - boolean hadJumpTarget = mRecyclingAction.hasJumpTarget(); - mRecyclingAction.runIfNecessary(recyclerView); - if (hadJumpTarget) { - // It is not stopped so needs to be restarted - if (mRunning) { - mPendingInitialRun = true; - recyclerView.mViewFlinger.postOnAnimation(); - } else { - stop(); // done - } - } + onSeekTargetStep(dx, dy, mRecyclerView.mState, mRecyclingAction); + mRecyclingAction.runIfNecessary(mRecyclerView); } } /** - * @see RecyclerView#getChildLayoutPosition(android.view.View) + * @see RecyclerView#getChildPosition(View) */ public int getChildPosition(View view) { - return mRecyclerView.getChildLayoutPosition(view); + return mRecyclerView.getChildPosition(view); } /** - * @see RecyclerView.LayoutManager#getChildCount() + * @see LayoutManager#getChildCount() */ public int getChildCount() { return mRecyclerView.mLayout.getChildCount(); } /** - * @see RecyclerView.LayoutManager#findViewByPosition(int) + * @see LayoutManager#findViewByPosition(int) */ public View findViewByPosition(int position) { return mRecyclerView.mLayout.findViewByPosition(position); @@ -10166,9 +7150,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro /** * @see RecyclerView#scrollToPosition(int) - * @deprecated Use {@link Action#jumpTo(int)}. */ - @Deprecated public void instantScrollToPosition(int position) { mRecyclerView.scrollToPosition(position); } @@ -10187,10 +7169,10 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param scrollVector The vector that points to the target scroll position */ protected void normalize(PointF scrollVector) { - final double magnitude = Math.sqrt(scrollVector.x * scrollVector.x + scrollVector.y - * scrollVector.y); - scrollVector.x /= magnitude; - scrollVector.y /= magnitude; + final double magnitute = Math.sqrt(scrollVector.x * scrollVector.x + scrollVector.y * + scrollVector.y); + scrollVector.x /= magnitute; + scrollVector.y /= magnitute; } /** @@ -10211,7 +7193,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * provided {@link Action} to define the next scroll.

        * * @param dx Last scroll amount horizontally - * @param dy Last scroll amount vertically + * @param dy Last scroll amount verticaully * @param state Transient state of RecyclerView * @param action If you want to trigger a new smooth scroll and cancel the previous one, * update this object. @@ -10226,6 +7208,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * @param state Transient state of RecyclerView * @param action Action instance that you should update to define final scroll action * towards the targetView + * @return An {@link Action} to finalize the smooth scrolling */ abstract protected void onTargetFound(View targetView, State state, Action action); @@ -10242,8 +7225,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro private int mDuration; - private int mJumpToPosition = NO_POSITION; - private Interpolator mInterpolator; private boolean changed = false; @@ -10282,38 +7263,7 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mDuration = duration; mInterpolator = interpolator; } - - /** - * Instead of specifying pixels to scroll, use the target position to jump using - * {@link RecyclerView#scrollToPosition(int)}. - *

        - * You may prefer using this method if scroll target is really far away and you prefer - * to jump to a location and smooth scroll afterwards. - *

        - * Note that calling this method takes priority over other update methods such as - * {@link #update(int, int, int, Interpolator)}, {@link #setX(float)}, - * {@link #setY(float)} and #{@link #setInterpolator(Interpolator)}. If you call - * {@link #jumpTo(int)}, the other changes will not be considered for this animation - * frame. - * - * @param targetPosition The target item position to scroll to using instant scrolling. - */ - public void jumpTo(int targetPosition) { - mJumpToPosition = targetPosition; - } - - boolean hasJumpTarget() { - return mJumpToPosition >= 0; - } - private void runIfNecessary(RecyclerView recyclerView) { - if (mJumpToPosition >= 0) { - final int position = mJumpToPosition; - mJumpToPosition = NO_POSITION; - recyclerView.jumpToPositionForSmoothScroller(position); - changed = false; - return; - } if (changed) { validate(); if (mInterpolator == null) { @@ -10405,30 +7355,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro changed = true; } } - - /** - * An interface which is optionally implemented by custom {@link RecyclerView.LayoutManager} - * to provide a hint to a {@link SmoothScroller} about the location of the target position. - */ - public interface ScrollVectorProvider { - /** - * Should calculate the vector that points to the direction where the target position - * can be found. - *

        - * This method is used by the {@link LinearSmoothScroller} to initiate a scroll towards - * the target position. - *

        - * The magnitude of the vector is not important. It is always normalized before being - * used by the {@link LinearSmoothScroller}. - *

        - * LayoutManager should not check whether the position exists in the adapter or not. - * - * @param targetPosition the target position to which the returned vector should point - * - * @return the scroll vector for a given position. - */ - PointF computeScrollVectorForPosition(int targetPosition); - } } static class AdapterDataObservable extends Observable { @@ -10447,16 +7373,12 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } public void notifyItemRangeChanged(int positionStart, int itemCount) { - notifyItemRangeChanged(positionStart, itemCount, null); - } - - public void notifyItemRangeChanged(int positionStart, int itemCount, Object payload) { // since onItemRangeChanged() is implemented by the app, it could do anything, including // removing itself from {@link mObservers} - and that could cause problems if // an iterator is used on the ArrayList {@link mObservers}. // to avoid such problems, just march thru the list in the reverse order. for (int i = mObservers.size() - 1; i >= 0; i--) { - mObservers.get(i).onItemRangeChanged(positionStart, itemCount, payload); + mObservers.get(i).onItemRangeChanged(positionStart, itemCount); } } @@ -10487,21 +7409,16 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } } - /** - * This is public so that the CREATOR can be access on cold launch. - * @hide - */ - public static class SavedState extends AbsSavedState { + static class SavedState extends BaseSavedState { Parcelable mLayoutState; /** * called by CREATOR */ - SavedState(Parcel in, ClassLoader loader) { - super(in, loader); - mLayoutState = in.readParcelable( - loader != null ? loader : LayoutManager.class.getClassLoader()); + SavedState(Parcel in) { + super(in); + mLayoutState = in.readParcelable(LayoutManager.class.getClassLoader()); } /** @@ -10521,18 +7438,18 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mLayoutState = other.mLayoutState; } - public static final Creator CREATOR = ParcelableCompat.newCreator( - new ParcelableCompatCreatorCallbacks() { - @Override - public SavedState createFromParcel(Parcel in, ClassLoader loader) { - return new SavedState(in, loader); - } + public static final Creator CREATOR + = new Creator() { + @Override + public SavedState createFromParcel(Parcel in) { + return new SavedState(in); + } - @Override - public SavedState[] newArray(int size) { - return new SavedState[size]; - } - }); + @Override + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; } /** *

        Contains useful information about the current RecyclerView state like target scroll @@ -10545,28 +7462,14 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * data between your components without needing to manage their lifecycles.

        */ public static class State { - static final int STEP_START = 1; - static final int STEP_LAYOUT = 1 << 1; - static final int STEP_ANIMATIONS = 1 << 2; - - void assertLayoutStep(int accepted) { - if ((accepted & mLayoutStep) == 0) { - throw new IllegalStateException("Layout state should be one of " - + Integer.toBinaryString(accepted) + " but it is " - + Integer.toBinaryString(mLayoutStep)); - } - } - - @IntDef(flag = true, value = { - STEP_START, STEP_LAYOUT, STEP_ANIMATIONS - }) - @Retention(RetentionPolicy.SOURCE) - @interface LayoutState {} private int mTargetPosition = RecyclerView.NO_POSITION; - - @LayoutState - private int mLayoutStep = STEP_START; + ArrayMap mPreLayoutHolderMap = + new ArrayMap(); + ArrayMap mPostLayoutHolderMap = + new ArrayMap(); + // nullable + ArrayMap mOldChangedHolders = new ArrayMap(); private SparseArray mData; @@ -10594,21 +7497,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro private boolean mRunPredictiveAnimations = false; - private boolean mTrackOldChangeHolders = false; - - private boolean mIsMeasuring = false; - - /** - * This data is saved before a layout calculation happens. After the layout is finished, - * if the previously focused view has been replaced with another view for the same item, we - * move the focus to the new item automatically. - */ - int mFocusedItemPosition; - long mFocusedItemId; - // when a sub child has focus, record its id and see if we can directly request focus on - // that one instead - int mFocusedSubChildId; - State reset() { mTargetPosition = RecyclerView.NO_POSITION; if (mData != null) { @@ -10616,32 +7504,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } mItemCount = 0; mStructureChanged = false; - mIsMeasuring = false; return this; } - /** - * Returns true if the RecyclerView is currently measuring the layout. This value is - * {@code true} only if the LayoutManager opted into the auto measure API and RecyclerView - * has non-exact measurement specs. - *

        - * Note that if the LayoutManager supports predictive animations and it is calculating the - * pre-layout step, this value will be {@code false} even if the RecyclerView is in - * {@code onMeasure} call. This is because pre-layout means the previous state of the - * RecyclerView and measurements made for that state cannot change the RecyclerView's size. - * LayoutManager is always guaranteed to receive another call to - * {@link LayoutManager#onLayoutChildren(Recycler, State)} when this happens. - * - * @return True if the RecyclerView is currently calculating its bounds, false otherwise. - */ - public boolean isMeasuring() { - return mIsMeasuring; - } - - /** - * Returns true if - * @return - */ public boolean isPreLayout() { return mInPreLayout; } @@ -10768,10 +7633,34 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mItemCount; } + public void onViewRecycled(ViewHolder holder) { + mPreLayoutHolderMap.remove(holder); + mPostLayoutHolderMap.remove(holder); + if (mOldChangedHolders != null) { + removeFrom(mOldChangedHolders, holder); + } + // holder cannot be in new list. + } + + public void onViewIgnored(ViewHolder holder) { + onViewRecycled(holder); + } + + private void removeFrom(ArrayMap holderMap, ViewHolder holder) { + for (int i = holderMap.size() - 1; i >= 0; i --) { + if (holder == holderMap.valueAt(i)) { + holderMap.removeAt(i); + return; + } + } + } + @Override public String toString() { return "State{" + "mTargetPosition=" + mTargetPosition + + ", mPreLayoutHolderMap=" + mPreLayoutHolderMap + + ", mPostLayoutHolderMap=" + mPostLayoutHolderMap + ", mData=" + mData + ", mItemCount=" + mItemCount + ", mPreviousLayoutItemCount=" + mPreviousLayoutItemCount + @@ -10785,28 +7674,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } } - /** - * This class defines the behavior of fling if the developer wishes to handle it. - *

        - * Subclasses of {@link OnFlingListener} can be used to implement custom fling behavior. - * - * @see #setOnFlingListener(OnFlingListener) - */ - public static abstract class OnFlingListener { - - /** - * Override this to handle a fling given the velocities in both x and y directions. - * Note that this method will only be called if the associated {@link LayoutManager} - * supports scrolling and the fling is not handled by nested scrolls first. - * - * @param velocityX the fling velocity on the X axis - * @param velocityY the fling velocity on the Y axis - * - * @return true if the fling washandled, false otherwise. - */ - public abstract boolean onFling(int velocityX, int velocityY); - } - /** * Internal listener that manages items after animations finish. This is how items are * retained (not recycled) during animations, but allowed to be recycled afterwards. @@ -10816,21 +7683,70 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro private class ItemAnimatorRestoreListener implements ItemAnimator.ItemAnimatorListener { @Override - public void onAnimationFinished(ViewHolder item) { + public void onRemoveFinished(ViewHolder item) { item.setIsRecyclable(true); + removeAnimatingView(item.itemView); + removeDetachedView(item.itemView, false); + } + + @Override + public void onAddFinished(ViewHolder item) { + item.setIsRecyclable(true); + if (item.isRecyclable()) { + removeAnimatingView(item.itemView); + } + } + + @Override + public void onMoveFinished(ViewHolder item) { + item.setIsRecyclable(true); + if (item.isRecyclable()) { + removeAnimatingView(item.itemView); + } + } + + @Override + public void onChangeFinished(ViewHolder item) { + item.setIsRecyclable(true); + /** + * We check both shadowed and shadowing because a ViewHolder may get both roles at the + * same time. + * + * Assume this flow: + * item X is represented by VH_1. Then itemX changes, so we create VH_2 . + * RV sets the following and calls item animator: + * VH_1.shadowed = VH_2; + * VH_1.mChanged = true; + * VH_2.shadowing =VH_1; + * + * Then, before the first change finishes, item changes again so we create VH_3. + * RV sets the following and calls item animator: + * VH_2.shadowed = VH_3 + * VH_2.mChanged = true + * VH_3.shadowing = VH_2 + * + * Because VH_2 already has an animation, it will be cancelled. At this point VH_2 has + * both shadowing and shadowed fields set. Shadowing information is obsolete now + * because the first animation where VH_2 is newViewHolder is not valid anymore. + * We ended up in this case because VH_2 played both roles. On the other hand, + * we DO NOT want to clear its changed flag. + * + * If second change was simply reverting first change, we would find VH_1 in + * {@link Recycler#getScrapViewForPosition(int, int, boolean)} and recycle it before + * re-using + */ if (item.mShadowedHolder != null && item.mShadowingHolder == null) { // old vh item.mShadowedHolder = null; + item.setFlags(~ViewHolder.FLAG_CHANGED, item.mFlags); } // always null this because an OldViewHolder can never become NewViewHolder w/o being // recycled. item.mShadowingHolder = null; - if (!item.shouldBeKeptAsChild()) { - if (!removeAnimatingView(item.itemView) && item.isTmpDetached()) { - removeDetachedView(item.itemView, false); - } + if (item.isRecyclable()) { + removeAnimatingView(item.itemView); } } - } + }; /** * This class defines the animations that take place on items as changes are made @@ -10838,78 +7754,22 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * * Subclasses of ItemAnimator can be used to implement custom animations for actions on * ViewHolder items. The RecyclerView will manage retaining these items while they - * are being animated, but implementors must call {@link #dispatchAnimationFinished(ViewHolder)} - * when a ViewHolder's animation is finished. In other words, there must be a matching - * {@link #dispatchAnimationFinished(ViewHolder)} call for each - * {@link #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) animateAppearance()}, - * {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateChange()} - * {@link #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) animatePersistence()}, - * and - * {@link #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateDisappearance()} call. + * are being animated, but implementors must call the appropriate "Starting" + * ({@link #dispatchRemoveStarting(ViewHolder)}, {@link #dispatchMoveStarting(ViewHolder)}, + * {@link #dispatchChangeStarting(ViewHolder, boolean)}, or + * {@link #dispatchAddStarting(ViewHolder)}) + * and "Finished" ({@link #dispatchRemoveFinished(ViewHolder)}, + * {@link #dispatchMoveFinished(ViewHolder)}, + * {@link #dispatchChangeFinished(ViewHolder, boolean)}, + * or {@link #dispatchAddFinished(ViewHolder)}) methods when each item animation is + * being started and ended. * - *

        By default, RecyclerView uses {@link DefaultItemAnimator}.

        + *

        By default, RecyclerView uses {@link DefaultItemAnimator}

        * * @see #setItemAnimator(ItemAnimator) */ - @SuppressWarnings("UnusedParameters") public static abstract class ItemAnimator { - /** - * The Item represented by this ViewHolder is updated. - *

        - * @see #recordPreLayoutInformation(State, ViewHolder, int, List) - */ - public static final int FLAG_CHANGED = ViewHolder.FLAG_UPDATE; - - /** - * The Item represented by this ViewHolder is removed from the adapter. - *

        - * @see #recordPreLayoutInformation(State, ViewHolder, int, List) - */ - public static final int FLAG_REMOVED = ViewHolder.FLAG_REMOVED; - - /** - * Adapter {@link Adapter#notifyDataSetChanged()} has been called and the content - * represented by this ViewHolder is invalid. - *

        - * @see #recordPreLayoutInformation(State, ViewHolder, int, List) - */ - public static final int FLAG_INVALIDATED = ViewHolder.FLAG_INVALID; - - /** - * The position of the Item represented by this ViewHolder has been changed. This flag is - * not bound to {@link Adapter#notifyItemMoved(int, int)}. It might be set in response to - * any adapter change that may have a side effect on this item. (e.g. The item before this - * one has been removed from the Adapter). - *

        - * @see #recordPreLayoutInformation(State, ViewHolder, int, List) - */ - public static final int FLAG_MOVED = ViewHolder.FLAG_MOVED; - - /** - * This ViewHolder was not laid out but has been added to the layout in pre-layout state - * by the {@link LayoutManager}. This means that the item was already in the Adapter but - * invisible and it may become visible in the post layout phase. LayoutManagers may prefer - * to add new items in pre-layout to specify their virtual location when they are invisible - * (e.g. to specify the item should animate in from below the visible area). - *

        - * @see #recordPreLayoutInformation(State, ViewHolder, int, List) - */ - public static final int FLAG_APPEARED_IN_PRE_LAYOUT - = ViewHolder.FLAG_APPEARED_IN_PRE_LAYOUT; - - /** - * The set of flags that might be passed to - * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. - */ - @IntDef(flag=true, value={ - FLAG_CHANGED, FLAG_REMOVED, FLAG_MOVED, FLAG_INVALIDATED, - FLAG_APPEARED_IN_PRE_LAYOUT - }) - @Retention(RetentionPolicy.SOURCE) - public @interface AdapterChanges {} private ItemAnimatorListener mListener = null; private ArrayList mFinishedListeners = new ArrayList(); @@ -10919,6 +7779,8 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro private long mMoveDuration = 250; private long mChangeDuration = 250; + private boolean mSupportsChangeAnimations = false; + /** * Gets the current duration for which all move animations will run. * @@ -10991,6 +7853,36 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mChangeDuration = changeDuration; } + /** + * Returns whether this ItemAnimator supports animations of change events. + * + * @return true if change animations are supported, false otherwise + */ + public boolean getSupportsChangeAnimations() { + return mSupportsChangeAnimations; + } + + /** + * Sets whether this ItemAnimator supports animations of item change events. + * By default, ItemAnimator only supports animations when items are added or removed. + * By setting this property to true, actions on the data set which change the + * contents of items may also be animated. What those animations are is left + * up to the discretion of the ItemAnimator subclass, in its + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} implementation. + * The value of this property is false by default. + * + * @see Adapter#notifyItemChanged(int) + * @see Adapter#notifyItemRangeChanged(int, int) + * + * @param supportsChangeAnimations true if change animations are supported by + * this ItemAnimator, false otherwise. If the property is false, the ItemAnimator + * will not receive a call to + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} when changes occur. + */ + public void setSupportsChangeAnimations(boolean supportsChangeAnimations) { + mSupportsChangeAnimations = supportsChangeAnimations; + } + /** * Internal only: * Sets the listener that must be called when the animator is finished @@ -11003,280 +7895,220 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mListener = listener; } - /** - * Called by the RecyclerView before the layout begins. Item animator should record - * necessary information about the View before it is potentially rebound, moved or removed. - *

        - * The data returned from this method will be passed to the related animate** - * methods. - *

        - * Note that this method may be called after pre-layout phase if LayoutManager adds new - * Views to the layout in pre-layout pass. - *

        - * The default implementation returns an {@link ItemHolderInfo} which holds the bounds of - * the View and the adapter change flags. - * - * @param state The current State of RecyclerView which includes some useful data - * about the layout that will be calculated. - * @param viewHolder The ViewHolder whose information should be recorded. - * @param changeFlags Additional information about what changes happened in the Adapter - * about the Item represented by this ViewHolder. For instance, if - * item is deleted from the adapter, {@link #FLAG_REMOVED} will be set. - * @param payloads The payload list that was previously passed to - * {@link Adapter#notifyItemChanged(int, Object)} or - * {@link Adapter#notifyItemRangeChanged(int, int, Object)}. - * - * @return An ItemHolderInfo instance that preserves necessary information about the - * ViewHolder. This object will be passed back to related animate** methods - * after layout is complete. - * - * @see #recordPostLayoutInformation(State, ViewHolder) - * @see #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * @see #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * @see #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) - * @see #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) - */ - public @NonNull ItemHolderInfo recordPreLayoutInformation(@NonNull State state, - @NonNull ViewHolder viewHolder, @AdapterChanges int changeFlags, - @NonNull List payloads) { - return obtainHolderInfo().setFrom(viewHolder); - } - - /** - * Called by the RecyclerView after the layout is complete. Item animator should record - * necessary information about the View's final state. - *

        - * The data returned from this method will be passed to the related animate** - * methods. - *

        - * The default implementation returns an {@link ItemHolderInfo} which holds the bounds of - * the View. - * - * @param state The current State of RecyclerView which includes some useful data about - * the layout that will be calculated. - * @param viewHolder The ViewHolder whose information should be recorded. - * - * @return An ItemHolderInfo that preserves necessary information about the ViewHolder. - * This object will be passed back to related animate** methods when - * RecyclerView decides how items should be animated. - * - * @see #recordPreLayoutInformation(State, ViewHolder, int, List) - * @see #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * @see #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * @see #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) - * @see #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) - */ - public @NonNull ItemHolderInfo recordPostLayoutInformation(@NonNull State state, - @NonNull ViewHolder viewHolder) { - return obtainHolderInfo().setFrom(viewHolder); - } - - /** - * Called by the RecyclerView when a ViewHolder has disappeared from the layout. - *

        - * This means that the View was a child of the LayoutManager when layout started but has - * been removed by the LayoutManager. It might have been removed from the adapter or simply - * become invisible due to other factors. You can distinguish these two cases by checking - * the change flags that were passed to - * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. - *

        - * Note that when a ViewHolder both changes and disappears in the same layout pass, the - * animation callback method which will be called by the RecyclerView depends on the - * ItemAnimator's decision whether to re-use the same ViewHolder or not, and also the - * LayoutManager's decision whether to layout the changed version of a disappearing - * ViewHolder or not. RecyclerView will call - * {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateChange} instead of {@code animateDisappearance} if and only if the ItemAnimator - * returns {@code false} from - * {@link #canReuseUpdatedViewHolder(ViewHolder) canReuseUpdatedViewHolder} and the - * LayoutManager lays out a new disappearing view that holds the updated information. - * Built-in LayoutManagers try to avoid laying out updated versions of disappearing views. - *

        - * If LayoutManager supports predictive animations, it might provide a target disappear - * location for the View by laying it out in that location. When that happens, - * RecyclerView will call {@link #recordPostLayoutInformation(State, ViewHolder)} and the - * response of that call will be passed to this method as the postLayoutInfo. - *

        - * ItemAnimator must call {@link #dispatchAnimationFinished(ViewHolder)} when the animation - * is complete (or instantly call {@link #dispatchAnimationFinished(ViewHolder)} if it - * decides not to animate the view). - * - * @param viewHolder The ViewHolder which should be animated - * @param preLayoutInfo The information that was returned from - * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. - * @param postLayoutInfo The information that was returned from - * {@link #recordPostLayoutInformation(State, ViewHolder)}. Might be - * null if the LayoutManager did not layout the item. - * - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - public abstract boolean animateDisappearance(@NonNull ViewHolder viewHolder, - @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo); - - /** - * Called by the RecyclerView when a ViewHolder is added to the layout. - *

        - * In detail, this means that the ViewHolder was not a child when the layout started - * but has been added by the LayoutManager. It might be newly added to the adapter or - * simply become visible due to other factors. - *

        - * ItemAnimator must call {@link #dispatchAnimationFinished(ViewHolder)} when the animation - * is complete (or instantly call {@link #dispatchAnimationFinished(ViewHolder)} if it - * decides not to animate the view). - * - * @param viewHolder The ViewHolder which should be animated - * @param preLayoutInfo The information that was returned from - * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. - * Might be null if Item was just added to the adapter or - * LayoutManager does not support predictive animations or it could - * not predict that this ViewHolder will become visible. - * @param postLayoutInfo The information that was returned from {@link - * #recordPreLayoutInformation(State, ViewHolder, int, List)}. - * - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - public abstract boolean animateAppearance(@NonNull ViewHolder viewHolder, - @Nullable ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo); - - /** - * Called by the RecyclerView when a ViewHolder is present in both before and after the - * layout and RecyclerView has not received a {@link Adapter#notifyItemChanged(int)} call - * for it or a {@link Adapter#notifyDataSetChanged()} call. - *

        - * This ViewHolder still represents the same data that it was representing when the layout - * started but its position / size may be changed by the LayoutManager. - *

        - * If the Item's layout position didn't change, RecyclerView still calls this method because - * it does not track this information (or does not necessarily know that an animation is - * not required). Your ItemAnimator should handle this case and if there is nothing to - * animate, it should call {@link #dispatchAnimationFinished(ViewHolder)} and return - * false. - *

        - * ItemAnimator must call {@link #dispatchAnimationFinished(ViewHolder)} when the animation - * is complete (or instantly call {@link #dispatchAnimationFinished(ViewHolder)} if it - * decides not to animate the view). - * - * @param viewHolder The ViewHolder which should be animated - * @param preLayoutInfo The information that was returned from - * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. - * @param postLayoutInfo The information that was returned from {@link - * #recordPreLayoutInformation(State, ViewHolder, int, List)}. - * - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - public abstract boolean animatePersistence(@NonNull ViewHolder viewHolder, - @NonNull ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo); - - /** - * Called by the RecyclerView when an adapter item is present both before and after the - * layout and RecyclerView has received a {@link Adapter#notifyItemChanged(int)} call - * for it. This method may also be called when - * {@link Adapter#notifyDataSetChanged()} is called and adapter has stable ids so that - * RecyclerView could still rebind views to the same ViewHolders. If viewType changes when - * {@link Adapter#notifyDataSetChanged()} is called, this method will not be called, - * instead, {@link #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo)} will be - * called for the new ViewHolder and the old one will be recycled. - *

        - * If this method is called due to a {@link Adapter#notifyDataSetChanged()} call, there is - * a good possibility that item contents didn't really change but it is rebound from the - * adapter. {@link DefaultItemAnimator} will skip animating the View if its location on the - * screen didn't change and your animator should handle this case as well and avoid creating - * unnecessary animations. - *

        - * When an item is updated, ItemAnimator has a chance to ask RecyclerView to keep the - * previous presentation of the item as-is and supply a new ViewHolder for the updated - * presentation (see: {@link #canReuseUpdatedViewHolder(ViewHolder, List)}. - * This is useful if you don't know the contents of the Item and would like - * to cross-fade the old and the new one ({@link DefaultItemAnimator} uses this technique). - *

        - * When you are writing a custom item animator for your layout, it might be more performant - * and elegant to re-use the same ViewHolder and animate the content changes manually. - *

        - * When {@link Adapter#notifyItemChanged(int)} is called, the Item's view type may change. - * If the Item's view type has changed or ItemAnimator returned false for - * this ViewHolder when {@link #canReuseUpdatedViewHolder(ViewHolder, List)} was called, the - * oldHolder and newHolder will be different ViewHolder instances - * which represent the same Item. In that case, only the new ViewHolder is visible - * to the LayoutManager but RecyclerView keeps old ViewHolder attached for animations. - *

        - * ItemAnimator must call {@link #dispatchAnimationFinished(ViewHolder)} for each distinct - * ViewHolder when their animation is complete - * (or instantly call {@link #dispatchAnimationFinished(ViewHolder)} if it decides not to - * animate the view). - *

        - * If oldHolder and newHolder are the same instance, you should call - * {@link #dispatchAnimationFinished(ViewHolder)} only once. - *

        - * Note that when a ViewHolder both changes and disappears in the same layout pass, the - * animation callback method which will be called by the RecyclerView depends on the - * ItemAnimator's decision whether to re-use the same ViewHolder or not, and also the - * LayoutManager's decision whether to layout the changed version of a disappearing - * ViewHolder or not. RecyclerView will call - * {@code animateChange} instead of - * {@link #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateDisappearance} if and only if the ItemAnimator returns {@code false} from - * {@link #canReuseUpdatedViewHolder(ViewHolder) canReuseUpdatedViewHolder} and the - * LayoutManager lays out a new disappearing view that holds the updated information. - * Built-in LayoutManagers try to avoid laying out updated versions of disappearing views. - * - * @param oldHolder The ViewHolder before the layout is started, might be the same - * instance with newHolder. - * @param newHolder The ViewHolder after the layout is finished, might be the same - * instance with oldHolder. - * @param preLayoutInfo The information that was returned from - * {@link #recordPreLayoutInformation(State, ViewHolder, int, List)}. - * @param postLayoutInfo The information that was returned from {@link - * #recordPreLayoutInformation(State, ViewHolder, int, List)}. - * - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - public abstract boolean animateChange(@NonNull ViewHolder oldHolder, - @NonNull ViewHolder newHolder, - @NonNull ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo); - - @AdapterChanges static int buildAdapterChangeFlagsForAnimations(ViewHolder viewHolder) { - int flags = viewHolder.mFlags & (FLAG_INVALIDATED | FLAG_REMOVED | FLAG_CHANGED); - if (viewHolder.isInvalid()) { - return FLAG_INVALIDATED; - } - if ((flags & FLAG_INVALIDATED) == 0) { - final int oldPos = viewHolder.getOldPosition(); - final int pos = viewHolder.getAdapterPosition(); - if (oldPos != NO_POSITION && pos != NO_POSITION && oldPos != pos){ - flags |= FLAG_MOVED; - } - } - return flags; - } - /** * Called when there are pending animations waiting to be started. This state - * is governed by the return values from - * {@link #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateAppearance()}, - * {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateChange()} - * {@link #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animatePersistence()}, and - * {@link #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateDisappearance()}, which inform the RecyclerView that the ItemAnimator wants to be - * called later to start the associated animations. runPendingAnimations() will be scheduled - * to be run on the next frame. + * is governed by the return values from {@link #animateAdd(ViewHolder) animateAdd()}, + * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, and + * {@link #animateRemove(ViewHolder) animateRemove()}, which inform the + * RecyclerView that the ItemAnimator wants to be called later to start the + * associated animations. runPendingAnimations() will be scheduled to be run + * on the next frame. */ abstract public void runPendingAnimations(); + /** + * Called when an item is removed from the RecyclerView. Implementors can choose + * whether and how to animate that change, but must always call + * {@link #dispatchRemoveFinished(ViewHolder)} when done, either + * immediately (if no animation will occur) or after the animation actually finishes. + * The return value indicates whether an animation has been set up and whether the + * ItemAnimator's {@link #runPendingAnimations()} method should be called at the + * next opportunity. This mechanism allows ItemAnimator to set up individual animations + * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, + * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, + * {@link #animateRemove(ViewHolder) animateRemove()}, and + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, + * then start the animations together in the later call to {@link #runPendingAnimations()}. + * + *

        This method may also be called for disappearing items which continue to exist in the + * RecyclerView, but for which the system does not have enough information to animate + * them out of view. In that case, the default animation for removing items is run + * on those items as well.

        + * + * @param holder The item that is being removed. + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + abstract public boolean animateRemove(ViewHolder holder); + + /** + * Called when an item is added to the RecyclerView. Implementors can choose + * whether and how to animate that change, but must always call + * {@link #dispatchAddFinished(ViewHolder)} when done, either + * immediately (if no animation will occur) or after the animation actually finishes. + * The return value indicates whether an animation has been set up and whether the + * ItemAnimator's {@link #runPendingAnimations()} method should be called at the + * next opportunity. This mechanism allows ItemAnimator to set up individual animations + * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, + * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, + * {@link #animateRemove(ViewHolder) animateRemove()}, and + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, + * then start the animations together in the later call to {@link #runPendingAnimations()}. + * + *

        This method may also be called for appearing items which were already in the + * RecyclerView, but for which the system does not have enough information to animate + * them into view. In that case, the default animation for adding items is run + * on those items as well.

        + * + * @param holder The item that is being added. + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + abstract public boolean animateAdd(ViewHolder holder); + + /** + * Called when an item is moved in the RecyclerView. Implementors can choose + * whether and how to animate that change, but must always call + * {@link #dispatchMoveFinished(ViewHolder)} when done, either + * immediately (if no animation will occur) or after the animation actually finishes. + * The return value indicates whether an animation has been set up and whether the + * ItemAnimator's {@link #runPendingAnimations()} method should be called at the + * next opportunity. This mechanism allows ItemAnimator to set up individual animations + * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, + * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, + * {@link #animateRemove(ViewHolder) animateRemove()}, and + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, + * then start the animations together in the later call to {@link #runPendingAnimations()}. + * + * @param holder The item that is being moved. + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + abstract public boolean animateMove(ViewHolder holder, int fromX, int fromY, + int toX, int toY); + + /** + * Called when an item is changed in the RecyclerView, as indicated by a call to + * {@link Adapter#notifyItemChanged(int)} or + * {@link Adapter#notifyItemRangeChanged(int, int)}. + *

        + * Implementers can choose whether and how to animate changes, but must always call + * {@link #dispatchChangeFinished(ViewHolder, boolean)} for each non-null ViewHolder, + * either immediately (if no animation will occur) or after the animation actually finishes. + * The return value indicates whether an animation has been set up and whether the + * ItemAnimator's {@link #runPendingAnimations()} method should be called at the + * next opportunity. This mechanism allows ItemAnimator to set up individual animations + * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, + * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, + * {@link #animateRemove(ViewHolder) animateRemove()}, and + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, + * then start the animations together in the later call to {@link #runPendingAnimations()}. + * + * @param oldHolder The original item that changed. + * @param newHolder The new item that was created with the changed content. Might be null + * @param fromLeft Left of the old view holder + * @param fromTop Top of the old view holder + * @param toLeft Left of the new view holder + * @param toTop Top of the new view holder + * @return true if a later call to {@link #runPendingAnimations()} is requested, + * false otherwise. + */ + abstract public boolean animateChange(ViewHolder oldHolder, + ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop); + + + /** + * Method to be called by subclasses when a remove animation is done. + * + * @param item The item which has been removed + */ + public final void dispatchRemoveFinished(ViewHolder item) { + onRemoveFinished(item); + if (mListener != null) { + mListener.onRemoveFinished(item); + } + } + + /** + * Method to be called by subclasses when a move animation is done. + * + * @param item The item which has been moved + */ + public final void dispatchMoveFinished(ViewHolder item) { + onMoveFinished(item); + if (mListener != null) { + mListener.onMoveFinished(item); + } + } + + /** + * Method to be called by subclasses when an add animation is done. + * + * @param item The item which has been added + */ + public final void dispatchAddFinished(ViewHolder item) { + onAddFinished(item); + if (mListener != null) { + mListener.onAddFinished(item); + } + } + + /** + * Method to be called by subclasses when a change animation is done. + * + * @see #animateChange(ViewHolder, ViewHolder, int, int, int, int) + * @param item The item which has been changed (this method must be called for + * each non-null ViewHolder passed into + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}). + * @param oldItem true if this is the old item that was changed, false if + * it is the new item that replaced the old item. + */ + public final void dispatchChangeFinished(ViewHolder item, boolean oldItem) { + onChangeFinished(item, oldItem); + if (mListener != null) { + mListener.onChangeFinished(item); + } + } + + /** + * Method to be called by subclasses when a remove animation is being started. + * + * @param item The item being removed + */ + public final void dispatchRemoveStarting(ViewHolder item) { + onRemoveStarting(item); + } + + /** + * Method to be called by subclasses when a move animation is being started. + * + * @param item The item being moved + */ + public final void dispatchMoveStarting(ViewHolder item) { + onMoveStarting(item); + } + + /** + * Method to be called by subclasses when an add animation is being started. + * + * @param item The item being added + */ + public final void dispatchAddStarting(ViewHolder item) { + onAddStarting(item); + } + + /** + * Method to be called by subclasses when a change animation is being started. + * + * @param item The item which has been changed (this method must be called for + * each non-null ViewHolder passed into + * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}). + * @param oldItem true if this is the old item that was changed, false if + * it is the new item that replaced the old item. + */ + public final void dispatchChangeStarting(ViewHolder item, boolean oldItem) { + onChangeStarting(item, oldItem); + } + /** * Method called when an animation on a view should be ended immediately. * This could happen when other events, like scrolling, occur, so that * animating views can be quickly put into their proper end locations. * Implementations should ensure that any animations running on the item * are canceled and affected properties are set to their end values. - * Also, {@link #dispatchAnimationFinished(ViewHolder)} should be called for each finished - * animation since the animations are effectively done when this method is called. + * Also, appropriate dispatch methods (e.g., {@link #dispatchAddFinished(ViewHolder)} + * should be called since the animations are effectively done when this + * method is called. * * @param item The item for which an animation should be stopped. */ @@ -11288,8 +8120,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro * animating views can be quickly put into their proper end locations. * Implementations should ensure that any animations running on any items * are canceled and affected properties are set to their end values. - * Also, {@link #dispatchAnimationFinished(ViewHolder)} should be called for each finished - * animation since the animations are effectively done when this method is called. + * Also, appropriate dispatch methods (e.g., {@link #dispatchAddFinished(ViewHolder)} + * should be called since the animations are effectively done when this + * method is called. */ abstract public void endAnimations(); @@ -11302,85 +8135,9 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro */ abstract public boolean isRunning(); - /** - * Method to be called by subclasses when an animation is finished. - *

        - * For each call RecyclerView makes to - * {@link #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateAppearance()}, - * {@link #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animatePersistence()}, or - * {@link #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateDisappearance()}, there - * should - * be a matching {@link #dispatchAnimationFinished(ViewHolder)} call by the subclass. - *

        - * For {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateChange()}, subclass should call this method for both the oldHolder - * and newHolder (if they are not the same instance). - * - * @param viewHolder The ViewHolder whose animation is finished. - * @see #onAnimationFinished(ViewHolder) - */ - public final void dispatchAnimationFinished(ViewHolder viewHolder) { - onAnimationFinished(viewHolder); - if (mListener != null) { - mListener.onAnimationFinished(viewHolder); - } - } - - /** - * Called after {@link #dispatchAnimationFinished(ViewHolder)} is called by the - * ItemAnimator. - * - * @param viewHolder The ViewHolder whose animation is finished. There might still be other - * animations running on this ViewHolder. - * @see #dispatchAnimationFinished(ViewHolder) - */ - public void onAnimationFinished(ViewHolder viewHolder) { - } - - /** - * Method to be called by subclasses when an animation is started. - *

        - * For each call RecyclerView makes to - * {@link #animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateAppearance()}, - * {@link #animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animatePersistence()}, or - * {@link #animateDisappearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateDisappearance()}, there should be a matching - * {@link #dispatchAnimationStarted(ViewHolder)} call by the subclass. - *

        - * For {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo) - * animateChange()}, subclass should call this method for both the oldHolder - * and newHolder (if they are not the same instance). - *

        - * If your ItemAnimator decides not to animate a ViewHolder, it should call - * {@link #dispatchAnimationFinished(ViewHolder)} without calling - * {@link #dispatchAnimationStarted(ViewHolder)}. - * - * @param viewHolder The ViewHolder whose animation is starting. - * @see #onAnimationStarted(ViewHolder) - */ - public final void dispatchAnimationStarted(ViewHolder viewHolder) { - onAnimationStarted(viewHolder); - } - - /** - * Called when a new animation is started on the given ViewHolder. - * - * @param viewHolder The ViewHolder which started animating. Note that the ViewHolder - * might already be animating and this might be another animation. - * @see #dispatchAnimationStarted(ViewHolder) - */ - public void onAnimationStarted(ViewHolder viewHolder) { - - } - /** * Like {@link #isRunning()}, this method returns whether there are any item - * animations currently running. Additionally, the listener passed in will be called + * animations currently running. Addtionally, the listener passed in will be called * when there are no item animations running, either immediately (before the method * returns) if no animations are currently running, or when the currently running * animations are {@link #dispatchAnimationsFinished() finished}. @@ -11407,58 +8164,15 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * When an item is changed, ItemAnimator can decide whether it wants to re-use - * the same ViewHolder for animations or RecyclerView should create a copy of the - * item and ItemAnimator will use both to run the animation (e.g. cross-fade). - *

        - * Note that this method will only be called if the {@link ViewHolder} still has the same - * type ({@link Adapter#getItemViewType(int)}). Otherwise, ItemAnimator will always receive - * both {@link ViewHolder}s in the - * {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo)} method. - *

        - * If your application is using change payloads, you can override - * {@link #canReuseUpdatedViewHolder(ViewHolder, List)} to decide based on payloads. - * - * @param viewHolder The ViewHolder which represents the changed item's old content. - * - * @return True if RecyclerView should just rebind to the same ViewHolder or false if - * RecyclerView should create a new ViewHolder and pass this ViewHolder to the - * ItemAnimator to animate. Default implementation returns true. - * - * @see #canReuseUpdatedViewHolder(ViewHolder, List) + * The interface to be implemented by listeners to animation events from this + * ItemAnimator. This is used internally and is not intended for developers to + * create directly. */ - public boolean canReuseUpdatedViewHolder(@NonNull ViewHolder viewHolder) { - return true; - } - - /** - * When an item is changed, ItemAnimator can decide whether it wants to re-use - * the same ViewHolder for animations or RecyclerView should create a copy of the - * item and ItemAnimator will use both to run the animation (e.g. cross-fade). - *

        - * Note that this method will only be called if the {@link ViewHolder} still has the same - * type ({@link Adapter#getItemViewType(int)}). Otherwise, ItemAnimator will always receive - * both {@link ViewHolder}s in the - * {@link #animateChange(ViewHolder, ViewHolder, ItemHolderInfo, ItemHolderInfo)} method. - * - * @param viewHolder The ViewHolder which represents the changed item's old content. - * @param payloads A non-null list of merged payloads that were sent with change - * notifications. Can be empty if the adapter is invalidated via - * {@link RecyclerView.Adapter#notifyDataSetChanged()}. The same list of - * payloads will be passed into - * {@link RecyclerView.Adapter#onBindViewHolder(ViewHolder, int, List)} - * method if this method returns true. - * - * @return True if RecyclerView should just rebind to the same ViewHolder or false if - * RecyclerView should create a new ViewHolder and pass this ViewHolder to the - * ItemAnimator to animate. Default implementation calls - * {@link #canReuseUpdatedViewHolder(ViewHolder)}. - * - * @see #canReuseUpdatedViewHolder(ViewHolder) - */ - public boolean canReuseUpdatedViewHolder(@NonNull ViewHolder viewHolder, - @NonNull List payloads) { - return canReuseUpdatedViewHolder(viewHolder); + interface ItemAnimatorListener { + void onRemoveFinished(ViewHolder item); + void onAddFinished(ViewHolder item); + void onMoveFinished(ViewHolder item); + void onChangeFinished(ViewHolder item); } /** @@ -11473,28 +8187,6 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro mFinishedListeners.clear(); } - /** - * Returns a new {@link ItemHolderInfo} which will be used to store information about the - * ViewHolder. This information will later be passed into animate** methods. - *

        - * You can override this method if you want to extend {@link ItemHolderInfo} and provide - * your own instances. - * - * @return A new {@link ItemHolderInfo}. - */ - public ItemHolderInfo obtainHolderInfo() { - return new ItemHolderInfo(); - } - - /** - * The interface to be implemented by listeners to animation events from this - * ItemAnimator. This is used internally and is not intended for developers to - * create directly. - */ - interface ItemAnimatorListener { - void onAnimationFinished(ViewHolder item); - } - /** * This interface is used to inform listeners when all pending or running animations * in an ItemAnimator are finished. This can be used, for example, to delay an action @@ -11507,117 +8199,105 @@ public class RecyclerView extends ViewGroup implements ScrollingView, NestedScro } /** - * A simple data structure that holds information about an item's bounds. - * This information is used in calculating item animations. Default implementation of - * {@link #recordPreLayoutInformation(RecyclerView.State, ViewHolder, int, List)} and - * {@link #recordPostLayoutInformation(RecyclerView.State, ViewHolder)} returns this data - * structure. You can extend this class if you would like to keep more information about - * the Views. - *

        - * If you want to provide your own implementation but still use `super` methods to record - * basic information, you can override {@link #obtainHolderInfo()} to provide your own - * instances. + * Called when a remove animation is being started on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. */ - public static class ItemHolderInfo { + public void onRemoveStarting(ViewHolder item) {} - /** - * The left edge of the View (excluding decorations) - */ - public int left; + /** + * Called when a remove animation has ended on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + public void onRemoveFinished(ViewHolder item) {} - /** - * The top edge of the View (excluding decorations) - */ - public int top; + /** + * Called when an add animation is being started on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + public void onAddStarting(ViewHolder item) {} - /** - * The right edge of the View (excluding decorations) - */ - public int right; + /** + * Called when an add animation has ended on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + public void onAddFinished(ViewHolder item) {} - /** - * The bottom edge of the View (excluding decorations) - */ - public int bottom; + /** + * Called when a move animation is being started on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + public void onMoveStarting(ViewHolder item) {} - /** - * The change flags that were passed to - * {@link #recordPreLayoutInformation(RecyclerView.State, ViewHolder, int, List)}. - */ - @AdapterChanges - public int changeFlags; + /** + * Called when a move animation has ended on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + */ + public void onMoveFinished(ViewHolder item) {} - public ItemHolderInfo() { - } + /** + * Called when a change animation is being started on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + * @param oldItem true if this is the old item that was changed, false if + * it is the new item that replaced the old item. + */ + public void onChangeStarting(ViewHolder item, boolean oldItem) {} - /** - * Sets the {@link #left}, {@link #top}, {@link #right} and {@link #bottom} values from - * the given ViewHolder. Clears all {@link #changeFlags}. - * - * @param holder The ViewHolder whose bounds should be copied. - * @return This {@link ItemHolderInfo} - */ - public ItemHolderInfo setFrom(RecyclerView.ViewHolder holder) { - return setFrom(holder, 0); - } + /** + * Called when a change animation has ended on the given ViewHolder. + * The default implementation does nothing. Subclasses may wish to override + * this method to handle any ViewHolder-specific operations linked to animation + * lifecycles. + * + * @param item The ViewHolder being animated. + * @param oldItem true if this is the old item that was changed, false if + * it is the new item that replaced the old item. + */ + public void onChangeFinished(ViewHolder item, boolean oldItem) {} - /** - * Sets the {@link #left}, {@link #top}, {@link #right} and {@link #bottom} values from - * the given ViewHolder and sets the {@link #changeFlags} to the given flags parameter. - * - * @param holder The ViewHolder whose bounds should be copied. - * @param flags The adapter change flags that were passed into - * {@link #recordPreLayoutInformation(RecyclerView.State, ViewHolder, int, - * List)}. - * @return This {@link ItemHolderInfo} - */ - public ItemHolderInfo setFrom(RecyclerView.ViewHolder holder, - @AdapterChanges int flags) { - final View view = holder.itemView; - this.left = view.getLeft(); - this.top = view.getTop(); - this.right = view.getRight(); - this.bottom = view.getBottom(); - return this; - } - } - } - - @Override - protected int getChildDrawingOrder(int childCount, int i) { - if (mChildDrawingOrderCallback == null) { - return super.getChildDrawingOrder(childCount, i); - } else { - return mChildDrawingOrderCallback.onGetChildDrawingOrder(childCount, i); - } } /** - * A callback interface that can be used to alter the drawing order of RecyclerView children. - *

        - * It works using the {@link ViewGroup#getChildDrawingOrder(int, int)} method, so any case - * that applies to that method also applies to this callback. For example, changing the drawing - * order of two views will not have any effect if their elevation values are different since - * elevation overrides the result of this callback. + * Internal data structure that holds information about an item's bounds. + * This information is used in calculating item animations. */ - public interface ChildDrawingOrderCallback { - /** - * Returns the index of the child to draw for this iteration. Override this - * if you want to change the drawing order of children. By default, it - * returns i. - * - * @param i The current iteration. - * @return The index of the child to draw this iteration. - * - * @see RecyclerView#setChildDrawingOrderCallback(RecyclerView.ChildDrawingOrderCallback) - */ - int onGetChildDrawingOrder(int childCount, int i); - } + private static class ItemHolderInfo { + ViewHolder holder; + int left, top, right, bottom; - private NestedScrollingChildHelper getScrollingChildHelper() { - if (mScrollingChildHelper == null) { - mScrollingChildHelper = new NestedScrollingChildHelper(this); + ItemHolderInfo(ViewHolder holder, int left, int top, int right, int bottom) { + this.holder = holder; + this.left = left; + this.top = top; + this.right = right; + this.bottom = bottom; } - return mScrollingChildHelper; } } diff --git a/app/src/main/java/android/support/v7/widget/RecyclerViewAccessibilityDelegate.java b/app/src/main/java/android/support/v7/widget/RecyclerViewAccessibilityDelegate.java index 1283f03b8b..ed7dfd6f63 100644 --- a/app/src/main/java/android/support/v7/widget/RecyclerViewAccessibilityDelegate.java +++ b/app/src/main/java/android/support/v7/widget/RecyclerViewAccessibilityDelegate.java @@ -35,16 +35,12 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp mRecyclerView = recyclerView; } - private boolean shouldIgnore() { - return mRecyclerView.hasPendingAdapterUpdates(); - } - @Override public boolean performAccessibilityAction(View host, int action, Bundle args) { if (super.performAccessibilityAction(host, action, args)) { return true; } - if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { + if (mRecyclerView.getLayoutManager() != null) { return mRecyclerView.getLayoutManager().performAccessibilityAction(action, args); } @@ -55,7 +51,7 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { super.onInitializeAccessibilityNodeInfo(host, info); info.setClassName(RecyclerView.class.getName()); - if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { + if (mRecyclerView.getLayoutManager() != null) { mRecyclerView.getLayoutManager().onInitializeAccessibilityNodeInfo(info); } } @@ -64,7 +60,7 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { super.onInitializeAccessibilityEvent(host, event); event.setClassName(RecyclerView.class.getName()); - if (host instanceof RecyclerView && !shouldIgnore()) { + if (host instanceof RecyclerView) { RecyclerView rv = (RecyclerView) host; if (rv.getLayoutManager() != null) { rv.getLayoutManager().onInitializeAccessibilityEvent(event); @@ -72,12 +68,7 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp } } - /** - * Gets the AccessibilityDelegate for an individual item in the RecyclerView. - * A basic item delegate is provided by default, but you can override this - * method to provide a custom per-item delegate. - */ - public AccessibilityDelegateCompat getItemDelegate() { + AccessibilityDelegateCompat getItemDelegate() { return mItemDelegate; } @@ -85,7 +76,7 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp @Override public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { super.onInitializeAccessibilityNodeInfo(host, info); - if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { + if (mRecyclerView.getLayoutManager() != null) { mRecyclerView.getLayoutManager(). onInitializeAccessibilityNodeInfoForItem(host, info); } @@ -96,7 +87,7 @@ public class RecyclerViewAccessibilityDelegate extends AccessibilityDelegateComp if (super.performAccessibilityAction(host, action, args)) { return true; } - if (!shouldIgnore() && mRecyclerView.getLayoutManager() != null) { + if (mRecyclerView.getLayoutManager() != null) { return mRecyclerView.getLayoutManager(). performAccessibilityActionForItem(host, action, args); } diff --git a/app/src/main/java/android/support/v7/widget/ScrollbarHelper.java b/app/src/main/java/android/support/v7/widget/ScrollbarHelper.java index 724fac8a66..0903f6414a 100644 --- a/app/src/main/java/android/support/v7/widget/ScrollbarHelper.java +++ b/app/src/main/java/android/support/v7/widget/ScrollbarHelper.java @@ -33,20 +33,17 @@ class ScrollbarHelper { endChild == null) { return 0; } - final int minPosition = Math.min(lm.getPosition(startChild), - lm.getPosition(endChild)); - final int maxPosition = Math.max(lm.getPosition(startChild), - lm.getPosition(endChild)); + final int minPosition = Math.min(lm.getPosition(startChild), lm.getPosition(endChild)); + final int maxPosition = Math.max(lm.getPosition(startChild), lm.getPosition(endChild)); final int itemsBefore = reverseLayout ? Math.max(0, state.getItemCount() - maxPosition - 1) - : Math.max(0, minPosition); + : Math.max(0, minPosition - 1); if (!smoothScrollbarEnabled) { return itemsBefore; } final int laidOutArea = Math.abs(orientation.getDecoratedEnd(endChild) - orientation.getDecoratedStart(startChild)); - final int itemRange = Math.abs(lm.getPosition(startChild) - - lm.getPosition(endChild)) + 1; + final int itemRange = Math.abs(lm.getPosition(startChild) - lm.getPosition(endChild)) + 1; final float avgSizePerRow = (float) laidOutArea / itemRange; return Math.round(itemsBefore * avgSizePerRow + (orientation.getStartAfterPadding() @@ -89,8 +86,7 @@ class ScrollbarHelper { // smooth scrollbar enabled. try to estimate better. final int laidOutArea = orientation.getDecoratedEnd(endChild) - orientation.getDecoratedStart(startChild); - final int laidOutRange = Math.abs(lm.getPosition(startChild) - - lm.getPosition(endChild)) + final int laidOutRange = Math.abs(lm.getPosition(startChild) - lm.getPosition(endChild)) + 1; // estimate a size for full list. return (int) ((float) laidOutArea / laidOutRange * state.getItemCount()); diff --git a/app/src/main/java/android/support/v7/widget/SimpleItemAnimator.java b/app/src/main/java/android/support/v7/widget/SimpleItemAnimator.java deleted file mode 100644 index 2db75413e7..0000000000 --- a/app/src/main/java/android/support/v7/widget/SimpleItemAnimator.java +++ /dev/null @@ -1,442 +0,0 @@ -package android.support.v7.widget; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.widget.RecyclerView.Adapter; -import android.support.v7.widget.RecyclerView.ViewHolder; -import android.support.v7.widget.RecyclerView.ItemAnimator.ItemHolderInfo; -import android.util.Log; -import android.view.View; - -import java.util.List; - -/** - * A wrapper class for ItemAnimator that records View bounds and decides whether it should run - * move, change, add or remove animations. This class also replicates the original ItemAnimator - * API. - *

        - * It uses {@link ItemHolderInfo} to track the bounds information of the Views. If you would like - * to - * extend this class, you can override {@link #obtainHolderInfo()} method to provide your own info - * class that extends {@link ItemHolderInfo}. - */ -abstract public class SimpleItemAnimator extends RecyclerView.ItemAnimator { - - private static final boolean DEBUG = false; - - private static final String TAG = "SimpleItemAnimator"; - - boolean mSupportsChangeAnimations = true; - - /** - * Returns whether this ItemAnimator supports animations of change events. - * - * @return true if change animations are supported, false otherwise - */ - @SuppressWarnings("unused") - public boolean getSupportsChangeAnimations() { - return mSupportsChangeAnimations; - } - - /** - * Sets whether this ItemAnimator supports animations of item change events. - * If you set this property to false, actions on the data set which change the - * contents of items will not be animated. What those animations do is left - * up to the discretion of the ItemAnimator subclass, in its - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} implementation. - * The value of this property is true by default. - * - * @param supportsChangeAnimations true if change animations are supported by - * this ItemAnimator, false otherwise. If the property is false, - * the ItemAnimator - * will not receive a call to - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, - * int)} when changes occur. - * @see Adapter#notifyItemChanged(int) - * @see Adapter#notifyItemRangeChanged(int, int) - */ - public void setSupportsChangeAnimations(boolean supportsChangeAnimations) { - mSupportsChangeAnimations = supportsChangeAnimations; - } - - /** - * {@inheritDoc} - * - * @return True if change animations are not supported or the ViewHolder is invalid, - * false otherwise. - * - * @see #setSupportsChangeAnimations(boolean) - */ - @Override - public boolean canReuseUpdatedViewHolder(@NonNull RecyclerView.ViewHolder viewHolder) { - return !mSupportsChangeAnimations || viewHolder.isInvalid(); - } - - @Override - public boolean animateDisappearance(@NonNull ViewHolder viewHolder, - @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo) { - int oldLeft = preLayoutInfo.left; - int oldTop = preLayoutInfo.top; - View disappearingItemView = viewHolder.itemView; - int newLeft = postLayoutInfo == null ? disappearingItemView.getLeft() : postLayoutInfo.left; - int newTop = postLayoutInfo == null ? disappearingItemView.getTop() : postLayoutInfo.top; - if (!viewHolder.isRemoved() && (oldLeft != newLeft || oldTop != newTop)) { - disappearingItemView.layout(newLeft, newTop, - newLeft + disappearingItemView.getWidth(), - newTop + disappearingItemView.getHeight()); - if (DEBUG) { - Log.d(TAG, "DISAPPEARING: " + viewHolder + " with view " + disappearingItemView); - } - return animateMove(viewHolder, oldLeft, oldTop, newLeft, newTop); - } else { - if (DEBUG) { - Log.d(TAG, "REMOVED: " + viewHolder + " with view " + disappearingItemView); - } - return animateRemove(viewHolder); - } - } - - @Override - public boolean animateAppearance(@NonNull ViewHolder viewHolder, - @Nullable ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) { - if (preLayoutInfo != null && (preLayoutInfo.left != postLayoutInfo.left - || preLayoutInfo.top != postLayoutInfo.top)) { - // slide items in if before/after locations differ - if (DEBUG) { - Log.d(TAG, "APPEARING: " + viewHolder + " with view " + viewHolder); - } - return animateMove(viewHolder, preLayoutInfo.left, preLayoutInfo.top, - postLayoutInfo.left, postLayoutInfo.top); - } else { - if (DEBUG) { - Log.d(TAG, "ADDED: " + viewHolder + " with view " + viewHolder); - } - return animateAdd(viewHolder); - } - } - - @Override - public boolean animatePersistence(@NonNull ViewHolder viewHolder, - @NonNull ItemHolderInfo preInfo, @NonNull ItemHolderInfo postInfo) { - if (preInfo.left != postInfo.left || preInfo.top != postInfo.top) { - if (DEBUG) { - Log.d(TAG, "PERSISTENT: " + viewHolder + - " with view " + viewHolder.itemView); - } - return animateMove(viewHolder, - preInfo.left, preInfo.top, postInfo.left, postInfo.top); - } - dispatchMoveFinished(viewHolder); - return false; - } - - @Override - public boolean animateChange(@NonNull ViewHolder oldHolder, @NonNull ViewHolder newHolder, - @NonNull ItemHolderInfo preInfo, @NonNull ItemHolderInfo postInfo) { - if (DEBUG) { - Log.d(TAG, "CHANGED: " + oldHolder + " with view " + oldHolder.itemView); - } - final int fromLeft = preInfo.left; - final int fromTop = preInfo.top; - final int toLeft, toTop; - if (newHolder.shouldIgnore()) { - toLeft = preInfo.left; - toTop = preInfo.top; - } else { - toLeft = postInfo.left; - toTop = postInfo.top; - } - return animateChange(oldHolder, newHolder, fromLeft, fromTop, toLeft, toTop); - } - - /** - * Called when an item is removed from the RecyclerView. Implementors can choose - * whether and how to animate that change, but must always call - * {@link #dispatchRemoveFinished(ViewHolder)} when done, either - * immediately (if no animation will occur) or after the animation actually finishes. - * The return value indicates whether an animation has been set up and whether the - * ItemAnimator's {@link #runPendingAnimations()} method should be called at the - * next opportunity. This mechanism allows ItemAnimator to set up individual animations - * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, - * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, - * {@link #animateRemove(ViewHolder) animateRemove()}, and - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, - * then start the animations together in the later call to {@link #runPendingAnimations()}. - * - *

        This method may also be called for disappearing items which continue to exist in the - * RecyclerView, but for which the system does not have enough information to animate - * them out of view. In that case, the default animation for removing items is run - * on those items as well.

        - * - * @param holder The item that is being removed. - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - abstract public boolean animateRemove(ViewHolder holder); - - /** - * Called when an item is added to the RecyclerView. Implementors can choose - * whether and how to animate that change, but must always call - * {@link #dispatchAddFinished(ViewHolder)} when done, either - * immediately (if no animation will occur) or after the animation actually finishes. - * The return value indicates whether an animation has been set up and whether the - * ItemAnimator's {@link #runPendingAnimations()} method should be called at the - * next opportunity. This mechanism allows ItemAnimator to set up individual animations - * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, - * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, - * {@link #animateRemove(ViewHolder) animateRemove()}, and - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, - * then start the animations together in the later call to {@link #runPendingAnimations()}. - * - *

        This method may also be called for appearing items which were already in the - * RecyclerView, but for which the system does not have enough information to animate - * them into view. In that case, the default animation for adding items is run - * on those items as well.

        - * - * @param holder The item that is being added. - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - abstract public boolean animateAdd(ViewHolder holder); - - /** - * Called when an item is moved in the RecyclerView. Implementors can choose - * whether and how to animate that change, but must always call - * {@link #dispatchMoveFinished(ViewHolder)} when done, either - * immediately (if no animation will occur) or after the animation actually finishes. - * The return value indicates whether an animation has been set up and whether the - * ItemAnimator's {@link #runPendingAnimations()} method should be called at the - * next opportunity. This mechanism allows ItemAnimator to set up individual animations - * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, - * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, - * {@link #animateRemove(ViewHolder) animateRemove()}, and - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, - * then start the animations together in the later call to {@link #runPendingAnimations()}. - * - * @param holder The item that is being moved. - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - abstract public boolean animateMove(ViewHolder holder, int fromX, int fromY, - int toX, int toY); - - /** - * Called when an item is changed in the RecyclerView, as indicated by a call to - * {@link Adapter#notifyItemChanged(int)} or - * {@link Adapter#notifyItemRangeChanged(int, int)}. - *

        - * Implementers can choose whether and how to animate changes, but must always call - * {@link #dispatchChangeFinished(ViewHolder, boolean)} for each non-null distinct ViewHolder, - * either immediately (if no animation will occur) or after the animation actually finishes. - * If the {@code oldHolder} is the same ViewHolder as the {@code newHolder}, you must call - * {@link #dispatchChangeFinished(ViewHolder, boolean)} once and only once. In that case, the - * second parameter of {@code dispatchChangeFinished} is ignored. - *

        - * The return value indicates whether an animation has been set up and whether the - * ItemAnimator's {@link #runPendingAnimations()} method should be called at the - * next opportunity. This mechanism allows ItemAnimator to set up individual animations - * as separate calls to {@link #animateAdd(ViewHolder) animateAdd()}, - * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, - * {@link #animateRemove(ViewHolder) animateRemove()}, and - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)} come in one by one, - * then start the animations together in the later call to {@link #runPendingAnimations()}. - * - * @param oldHolder The original item that changed. - * @param newHolder The new item that was created with the changed content. Might be null - * @param fromLeft Left of the old view holder - * @param fromTop Top of the old view holder - * @param toLeft Left of the new view holder - * @param toTop Top of the new view holder - * @return true if a later call to {@link #runPendingAnimations()} is requested, - * false otherwise. - */ - abstract public boolean animateChange(ViewHolder oldHolder, - ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop); - - /** - * Method to be called by subclasses when a remove animation is done. - * - * @param item The item which has been removed - * @see RecyclerView.ItemAnimator#animateDisappearance(ViewHolder, ItemHolderInfo, - * ItemHolderInfo) - */ - public final void dispatchRemoveFinished(ViewHolder item) { - onRemoveFinished(item); - dispatchAnimationFinished(item); - } - - /** - * Method to be called by subclasses when a move animation is done. - * - * @param item The item which has been moved - * @see RecyclerView.ItemAnimator#animateDisappearance(ViewHolder, ItemHolderInfo, - * ItemHolderInfo) - * @see RecyclerView.ItemAnimator#animatePersistence(ViewHolder, ItemHolderInfo, ItemHolderInfo) - * @see RecyclerView.ItemAnimator#animateAppearance(ViewHolder, ItemHolderInfo, ItemHolderInfo) - */ - public final void dispatchMoveFinished(ViewHolder item) { - onMoveFinished(item); - dispatchAnimationFinished(item); - } - - /** - * Method to be called by subclasses when an add animation is done. - * - * @param item The item which has been added - */ - public final void dispatchAddFinished(ViewHolder item) { - onAddFinished(item); - dispatchAnimationFinished(item); - } - - /** - * Method to be called by subclasses when a change animation is done. - * - * @param item The item which has been changed (this method must be called for - * each non-null ViewHolder passed into - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}). - * @param oldItem true if this is the old item that was changed, false if - * it is the new item that replaced the old item. - * @see #animateChange(ViewHolder, ViewHolder, int, int, int, int) - */ - public final void dispatchChangeFinished(ViewHolder item, boolean oldItem) { - onChangeFinished(item, oldItem); - dispatchAnimationFinished(item); - } - - /** - * Method to be called by subclasses when a remove animation is being started. - * - * @param item The item being removed - */ - public final void dispatchRemoveStarting(ViewHolder item) { - onRemoveStarting(item); - } - - /** - * Method to be called by subclasses when a move animation is being started. - * - * @param item The item being moved - */ - public final void dispatchMoveStarting(ViewHolder item) { - onMoveStarting(item); - } - - /** - * Method to be called by subclasses when an add animation is being started. - * - * @param item The item being added - */ - public final void dispatchAddStarting(ViewHolder item) { - onAddStarting(item); - } - - /** - * Method to be called by subclasses when a change animation is being started. - * - * @param item The item which has been changed (this method must be called for - * each non-null ViewHolder passed into - * {@link #animateChange(ViewHolder, ViewHolder, int, int, int, int)}). - * @param oldItem true if this is the old item that was changed, false if - * it is the new item that replaced the old item. - */ - public final void dispatchChangeStarting(ViewHolder item, boolean oldItem) { - onChangeStarting(item, oldItem); - } - - /** - * Called when a remove animation is being started on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - @SuppressWarnings("UnusedParameters") - public void onRemoveStarting(ViewHolder item) { - } - - /** - * Called when a remove animation has ended on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - public void onRemoveFinished(ViewHolder item) { - } - - /** - * Called when an add animation is being started on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - @SuppressWarnings("UnusedParameters") - public void onAddStarting(ViewHolder item) { - } - - /** - * Called when an add animation has ended on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - public void onAddFinished(ViewHolder item) { - } - - /** - * Called when a move animation is being started on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - @SuppressWarnings("UnusedParameters") - public void onMoveStarting(ViewHolder item) { - } - - /** - * Called when a move animation has ended on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - */ - public void onMoveFinished(ViewHolder item) { - } - - /** - * Called when a change animation is being started on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - * @param oldItem true if this is the old item that was changed, false if - * it is the new item that replaced the old item. - */ - @SuppressWarnings("UnusedParameters") - public void onChangeStarting(ViewHolder item, boolean oldItem) { - } - - /** - * Called when a change animation has ended on the given ViewHolder. - * The default implementation does nothing. Subclasses may wish to override - * this method to handle any ViewHolder-specific operations linked to animation - * lifecycles. - * - * @param item The ViewHolder being animated. - * @param oldItem true if this is the old item that was changed, false if - * it is the new item that replaced the old item. - */ - public void onChangeFinished(ViewHolder item, boolean oldItem) { - } -} diff --git a/app/src/main/java/android/support/v7/widget/SnapHelper.java b/app/src/main/java/android/support/v7/widget/SnapHelper.java deleted file mode 100644 index a2c557d873..0000000000 --- a/app/src/main/java/android/support/v7/widget/SnapHelper.java +++ /dev/null @@ -1,274 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.widget; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v7.widget.RecyclerView.LayoutManager; -import android.util.DisplayMetrics; -import android.view.View; -import android.view.animation.DecelerateInterpolator; -import android.widget.Scroller; -import android.support.v7.widget.RecyclerView.SmoothScroller.ScrollVectorProvider; - -/** - * Class intended to support snapping for a {@link RecyclerView}. - *

        - * SnapHelper tries to handle fling as well but for this to work properly, the - * {@link RecyclerView.LayoutManager} must implement the {@link ScrollVectorProvider} interface or - * you should override {@link #onFling(int, int)} and handle fling manually. - */ -public abstract class SnapHelper extends RecyclerView.OnFlingListener { - - private static final float MILLISECONDS_PER_INCH = 100f; - - private RecyclerView mRecyclerView; - private Scroller mGravityScroller; - - // Handles the snap on scroll case. - private final RecyclerView.OnScrollListener mScrollListener = - new RecyclerView.OnScrollListener() { - @Override - public void onScrollStateChanged(RecyclerView recyclerView, int newState) { - super.onScrollStateChanged(recyclerView, newState); - if (newState == RecyclerView.SCROLL_STATE_IDLE) { - snapToTargetExistingView(); - } - } - }; - - @Override - public boolean onFling(int velocityX, int velocityY) { - LayoutManager layoutManager = mRecyclerView.getLayoutManager(); - if (layoutManager == null) { - return false; - } - RecyclerView.Adapter adapter = mRecyclerView.getAdapter(); - if (adapter == null) { - return false; - } - int minFlingVelocity = mRecyclerView.getMinFlingVelocity(); - return (Math.abs(velocityY) > minFlingVelocity || Math.abs(velocityX) > minFlingVelocity) - && snapFromFling(layoutManager, velocityX, velocityY); - } - - /** - * Attaches the {@link SnapHelper} to the provided RecyclerView, by calling - * {@link RecyclerView#setOnFlingListener(RecyclerView.OnFlingListener)}. - * You can call this method with {@code null} to detach it from the current RecyclerView. - * - * @param recyclerView The RecyclerView instance to which you want to add this helper or - * {@code null} if you want to remove SnapHelper from the current - * RecyclerView. - * - * @throws IllegalArgumentException if there is already a {@link RecyclerView.OnFlingListener} - * attached to the provided {@link RecyclerView}. - * - */ - public void attachToRecyclerView(@Nullable RecyclerView recyclerView) - throws IllegalStateException { - if (mRecyclerView == recyclerView) { - return; // nothing to do - } - if (mRecyclerView != null) { - destroyCallbacks(); - } - mRecyclerView = recyclerView; - if (mRecyclerView != null) { - setupCallbacks(); - mGravityScroller = new Scroller(mRecyclerView.getContext(), - new DecelerateInterpolator()); - snapToTargetExistingView(); - } - } - - /** - * Called when an instance of a {@link RecyclerView} is attached. - */ - private void setupCallbacks() throws IllegalStateException { - if (mRecyclerView.getOnFlingListener() != null) { - throw new IllegalStateException("An instance of OnFlingListener already set."); - } - mRecyclerView.addOnScrollListener(mScrollListener); - mRecyclerView.setOnFlingListener(this); - } - - /** - * Called when the instance of a {@link RecyclerView} is detached. - */ - private void destroyCallbacks() { - mRecyclerView.removeOnScrollListener(mScrollListener); - mRecyclerView.setOnFlingListener(null); - } - - /** - * Calculated the estimated scroll distance in each direction given velocities on both axes. - * - * @param velocityX Fling velocity on the horizontal axis. - * @param velocityY Fling velocity on the vertical axis. - * - * @return array holding the calculated distances in x and y directions - * respectively. - */ - public int[] calculateScrollDistance(int velocityX, int velocityY) { - int[] outDist = new int[2]; - mGravityScroller.fling(0, 0, velocityX, velocityY, - Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE); - outDist[0] = mGravityScroller.getFinalX(); - outDist[1] = mGravityScroller.getFinalY(); - return outDist; - } - - /** - * Helper method to facilitate for snapping triggered by a fling. - * - * @param layoutManager The {@link LayoutManager} associated with the attached - * {@link RecyclerView}. - * @param velocityX Fling velocity on the horizontal axis. - * @param velocityY Fling velocity on the vertical axis. - * - * @return true if it is handled, false otherwise. - */ - private boolean snapFromFling(@NonNull LayoutManager layoutManager, int velocityX, - int velocityY) { - if (!(layoutManager instanceof ScrollVectorProvider)) { - return false; - } - - RecyclerView.SmoothScroller smoothScroller = createSnapScroller(layoutManager); - if (smoothScroller == null) { - return false; - } - - int targetPosition = findTargetSnapPosition(layoutManager, velocityX, velocityY); - if (targetPosition == RecyclerView.NO_POSITION) { - return false; - } - - smoothScroller.setTargetPosition(targetPosition); - layoutManager.startSmoothScroll(smoothScroller); - return true; - } - - /** - * Snaps to a target view which currently exists in the attached {@link RecyclerView}. This - * method is used to snap the view when the {@link RecyclerView} is first attached; when - * snapping was triggered by a scroll and when the fling is at its final stages. - */ - private void snapToTargetExistingView() { - if (mRecyclerView == null) { - return; - } - LayoutManager layoutManager = mRecyclerView.getLayoutManager(); - if (layoutManager == null) { - return; - } - View snapView = findSnapView(layoutManager); - if (snapView == null) { - return; - } - int[] snapDistance = calculateDistanceToFinalSnap(layoutManager, snapView); - if (snapDistance[0] != 0 || snapDistance[1] != 0) { - mRecyclerView.smoothScrollBy(snapDistance[0], snapDistance[1]); - } - } - - /** - * Creates a scroller to be used in the snapping implementation. - * - * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached - * {@link RecyclerView}. - * - * @return a {@link LinearSmoothScroller} which will handle the scrolling. - */ - @Nullable - private LinearSmoothScroller createSnapScroller(LayoutManager layoutManager) { - if (!(layoutManager instanceof ScrollVectorProvider)) { - return null; - } - return new LinearSmoothScroller(mRecyclerView.getContext()) { - @Override - protected void onTargetFound(View targetView, RecyclerView.State state, Action action) { - int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), - targetView); - final int dx = snapDistances[0]; - final int dy = snapDistances[1]; - final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy))); - if (time > 0) { - action.update(dx, dy, time, mDecelerateInterpolator); - } - } - - @Override - protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { - return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; - } - }; - } - - /** - * Override this method to snap to a particular point within the target view or the container - * view on any axis. - *

        - * This method is called when the {@link SnapHelper} has intercepted a fling and it needs - * to know the exact distance required to scroll by in order to snap to the target view. - * - * @param layoutManager the {@link RecyclerView.LayoutManager} associated with the attached - * {@link RecyclerView} - * @param targetView the target view that is chosen as the view to snap - * - * @return the output coordinates the put the result into. out[0] is the distance - * on horizontal axis and out[1] is the distance on vertical axis. - */ - @SuppressWarnings("WeakerAccess") - @Nullable - public abstract int[] calculateDistanceToFinalSnap(@NonNull LayoutManager layoutManager, - @NonNull View targetView); - - /** - * Override this method to provide a particular target view for snapping. - *

        - * This method is called when the {@link SnapHelper} is ready to start snapping and requires - * a target view to snap to. It will be explicitly called when the scroll state becomes idle - * after a scroll. It will also be called when the {@link SnapHelper} is preparing to snap - * after a fling and requires a reference view from the current set of child views. - *

        - * If this method returns {@code null}, SnapHelper will not snap to any view. - * - * @param layoutManager the {@link RecyclerView.LayoutManager} associated with the attached - * {@link RecyclerView} - * - * @return the target view to which to snap on fling or end of scroll - */ - @SuppressWarnings("WeakerAccess") - @Nullable - public abstract View findSnapView(LayoutManager layoutManager); - - /** - * Override to provide a particular adapter target position for snapping. - * - * @param layoutManager the {@link RecyclerView.LayoutManager} associated with the attached - * {@link RecyclerView} - * @param velocityX fling velocity on the horizontal axis - * @param velocityY fling velocity on the vertical axis - * - * @return the target adapter position to you want to snap or {@link RecyclerView#NO_POSITION} - * if no snapping should happen - */ - public abstract int findTargetSnapPosition(LayoutManager layoutManager, int velocityX, - int velocityY); -} \ No newline at end of file diff --git a/app/src/main/java/android/support/v7/widget/StaggeredGridLayoutManager.java b/app/src/main/java/android/support/v7/widget/StaggeredGridLayoutManager.java index 12745fe04f..0389012358 100644 --- a/app/src/main/java/android/support/v7/widget/StaggeredGridLayoutManager.java +++ b/app/src/main/java/android/support/v7/widget/StaggeredGridLayoutManager.java @@ -16,19 +16,11 @@ package android.support.v7.widget; -import static android.support.v7.widget.LayoutState.ITEM_DIRECTION_HEAD; -import static android.support.v7.widget.LayoutState.ITEM_DIRECTION_TAIL; -import static android.support.v7.widget.LayoutState.LAYOUT_END; -import static android.support.v7.widget.LayoutState.LAYOUT_START; -import static android.support.v7.widget.RecyclerView.NO_POSITION; - import android.content.Context; import android.graphics.PointF; import android.graphics.Rect; import android.os.Parcel; import android.os.Parcelable; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; import android.support.v4.view.ViewCompat; import android.support.v4.view.accessibility.AccessibilityEventCompat; import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; @@ -44,6 +36,12 @@ import java.util.Arrays; import java.util.BitSet; import java.util.List; +import static android.support.v7.widget.LayoutState.ITEM_DIRECTION_HEAD; +import static android.support.v7.widget.LayoutState.ITEM_DIRECTION_TAIL; +import static android.support.v7.widget.LayoutState.LAYOUT_END; +import static android.support.v7.widget.LayoutState.LAYOUT_START; +import static android.support.v7.widget.RecyclerView.NO_POSITION; + /** * A LayoutManager that lays out children in a staggered grid formation. * It supports horizontal & vertical layout as well as an ability to layout children in reverse. @@ -52,10 +50,9 @@ import java.util.List; * StaggeredGridLayoutManager can offset spans independently or move items between spans. You can * control this behavior via {@link #setGapStrategy(int)}. */ -public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager implements - RecyclerView.SmoothScroller.ScrollVectorProvider { +public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager { - private static final String TAG = "StaggeredGridLayoutManager"; + public static final String TAG = "StaggeredGridLayoutManager"; private static final boolean DEBUG = false; @@ -68,10 +65,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple */ public static final int GAP_HANDLING_NONE = 0; - /** - * @deprecated No longer supported. - */ - @SuppressWarnings("unused") @Deprecated public static final int GAP_HANDLING_LAZY = 1; @@ -81,28 +74,21 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple * and move items to correct positions with animations. *

        * For example, if LayoutManager ends up with the following layout due to adapter changes: - *

        +     * 
              * AAA
              * _BC
              * DDD
        -     * 
        - *

        + * * It will animate to the following state: - *

        +     * 
              * AAA
              * BC_
              * DDD
        -     * 
        + * */ public static final int GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS = 2; private static final int INVALID_OFFSET = Integer.MIN_VALUE; - /** - * While trying to find next view to focus, LayoutManager will not try to scroll more - * than this factor times the total space of the list. If layout is vertical, total space is the - * height minus padding, if layout is horizontal, total space is the width minus padding. - */ - private static final float MAX_SCROLL_FACTOR = 1 / 3f; /** * Number of spans @@ -115,9 +101,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple * Primary orientation is the layout's orientation, secondary orientation is the orientation * for spans. Having both makes code much cleaner for calculations. */ - @NonNull OrientationHelper mPrimaryOrientation; - @NonNull OrientationHelper mSecondaryOrientation; private int mOrientation; @@ -127,8 +111,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple */ private int mSizePerSpan; - @NonNull - private final LayoutState mLayoutState; + private LayoutState mLayoutState; private boolean mReverseLayout = false; @@ -184,12 +167,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple /** * Re-used measurement specs. updated by onLayout. */ - private int mFullSizeSpec; - - /** - * Re-used rectangle to get child decor offsets. - */ - private final Rect mTmpRect = new Rect(); + private int mFullSizeSpec, mWidthSpec, mHeightSpec; /** * Re-used anchor info. @@ -210,29 +188,13 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple */ private boolean mSmoothScrollbarEnabled = true; - private final Runnable mCheckForGapsRunnable = new Runnable() { + private final Runnable checkForGapsRunnable = new Runnable() { @Override public void run() { checkForGaps(); } }; - /** - * Constructor used when layout manager is set in XML by RecyclerView attribute - * "layoutManager". Defaults to single column and vertical. - */ - @SuppressWarnings("unused") - public StaggeredGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, - int defStyleRes) { - Properties properties = getProperties(context, attrs, defStyleAttr, defStyleRes); - setOrientation(properties.orientation); - setSpanCount(properties.spanCount); - setReverseLayout(properties.reverseLayout); - setAutoMeasureEnabled(mGapStrategy != GAP_HANDLING_NONE); - mLayoutState = new LayoutState(); - createOrientationHelpers(); - } - /** * Creates a StaggeredGridLayoutManager with given parameters. * @@ -243,15 +205,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple public StaggeredGridLayoutManager(int spanCount, int orientation) { mOrientation = orientation; setSpanCount(spanCount); - setAutoMeasureEnabled(mGapStrategy != GAP_HANDLING_NONE); - mLayoutState = new LayoutState(); - createOrientationHelpers(); - } - - private void createOrientationHelpers() { - mPrimaryOrientation = OrientationHelper.createOrientationHelper(this, mOrientation); - mSecondaryOrientation = OrientationHelper - .createOrientationHelper(this, 1 - mOrientation); } /** @@ -260,9 +213,9 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple * When a full span item is laid out in reverse direction, it sets a flag which we check when * scroll is stopped (or re-layout happens) and re-layout after first valid item. */ - private boolean checkForGaps() { - if (getChildCount() == 0 || mGapStrategy == GAP_HANDLING_NONE || !isAttachedToWindow()) { - return false; + private void checkForGaps() { + if (getChildCount() == 0 || mGapStrategy == GAP_HANDLING_NONE) { + return; } final int minPos, maxPos; if (mShouldReverseLayout) { @@ -278,23 +231,23 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple mLazySpanLookup.clear(); requestSimpleAnimationsInNextLayout(); requestLayout(); - return true; + return; } } if (!mLaidOutInvalidFullSpan) { - return false; + return; } int invalidGapDir = mShouldReverseLayout ? LAYOUT_START : LAYOUT_END; final LazySpanLookup.FullSpanItem invalidFsi = mLazySpanLookup - .getFirstFullSpanItemInRange(minPos, maxPos + 1, invalidGapDir, true); + .getFirstFullSpanItemInRange(minPos, maxPos + 1, invalidGapDir); if (invalidFsi == null) { mLaidOutInvalidFullSpan = false; mLazySpanLookup.forceInvalidateAfter(maxPos + 1); - return false; + return; } final LazySpanLookup.FullSpanItem validFsi = mLazySpanLookup .getFirstFullSpanItemInRange(minPos, invalidFsi.mPosition, - invalidGapDir * -1, true); + invalidGapDir * -1); if (validFsi == null) { mLazySpanLookup.forceInvalidateAfter(invalidFsi.mPosition); } else { @@ -302,7 +255,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } requestSimpleAnimationsInNextLayout(); requestLayout(); - return true; } @Override @@ -314,12 +266,9 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple @Override public void onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycler) { - removeCallbacks(mCheckForGapsRunnable); for (int i = 0; i < mSpanCount; i++) { mSpans[i].clear(); } - // SGLM will require fresh layout call to recover state after detach - view.requestLayout(); } /** @@ -337,11 +286,11 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple final int preferredSpanDir = mOrientation == VERTICAL && isLayoutRTL() ? 1 : -1; if (mShouldReverseLayout) { - firstChildIndex = endChildIndex; + firstChildIndex = endChildIndex - 1; childLimit = startChildIndex - 1; } else { firstChildIndex = startChildIndex; - childLimit = endChildIndex + 1; + childLimit = endChildIndex; } final int nextChildDiff = firstChildIndex < childLimit ? 1 : -1; for (int i = firstChildIndex; i != childLimit; i += nextChildDiff) { @@ -394,16 +343,10 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple private boolean checkSpanForGap(Span span) { if (mShouldReverseLayout) { if (span.getEndLine() < mPrimaryOrientation.getEndAfterPadding()) { - // if it is full span, it is OK - final View endView = span.mViews.get(span.mViews.size() - 1); - final LayoutParams lp = span.getLayoutParams(endView); - return !lp.mFullSpan; + return true; } } else if (span.getStartLine() > mPrimaryOrientation.getStartAfterPadding()) { - // if it is full span, it is OK - final View startView = span.mViews.get(0); - final LayoutParams lp = span.getLayoutParams(startView); - return !lp.mFullSpan; + return true; } return false; } @@ -446,9 +389,12 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return; } mOrientation = orientation; - OrientationHelper tmp = mPrimaryOrientation; - mPrimaryOrientation = mSecondaryOrientation; - mSecondaryOrientation = tmp; + if (mPrimaryOrientation != null && mSecondaryOrientation != null) { + // swap + OrientationHelper tmp = mPrimaryOrientation; + mPrimaryOrientation = mSecondaryOrientation; + mSecondaryOrientation = tmp; + } requestLayout(); } @@ -512,7 +458,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple + "or GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS"); } mGapStrategy = gapStrategy; - setAutoMeasureEnabled(mGapStrategy != GAP_HANDLING_NONE); requestLayout(); } @@ -543,6 +488,15 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple requestLayout(); } + private void ensureOrientationHelper() { + if (mPrimaryOrientation == null) { + mPrimaryOrientation = OrientationHelper.createOrientationHelper(this, mOrientation); + mSecondaryOrientation = OrientationHelper + .createOrientationHelper(this, 1 - mOrientation); + mLayoutState = new LayoutState(); + } + } + /** * Calculates the views' layout order. (e.g. from end to start or start to end) * RTL layout support is applied automatically. So if layout is RTL and @@ -573,57 +527,25 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return mReverseLayout; } - @Override - public void setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec) { - // we don't like it to wrap content in our non-scroll direction. - final int width, height; - final int horizontalPadding = getPaddingLeft() + getPaddingRight(); - final int verticalPadding = getPaddingTop() + getPaddingBottom(); - if (mOrientation == VERTICAL) { - final int usedHeight = childrenBounds.height() + verticalPadding; - height = chooseSize(hSpec, usedHeight, getMinimumHeight()); - width = chooseSize(wSpec, mSizePerSpan * mSpanCount + horizontalPadding, - getMinimumWidth()); - } else { - final int usedWidth = childrenBounds.width() + horizontalPadding; - width = chooseSize(wSpec, usedWidth, getMinimumWidth()); - height = chooseSize(hSpec, mSizePerSpan * mSpanCount + verticalPadding, - getMinimumHeight()); - } - setMeasuredDimension(width, height); - } - @Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { - onLayoutChildren(recycler, state, true); - } + ensureOrientationHelper(); + // Update adapter size. + mLazySpanLookup.mAdapterSize = state.getItemCount(); - - private void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state, - boolean shouldCheckForGaps) { final AnchorInfo anchorInfo = mAnchorInfo; - if (mPendingSavedState != null || mPendingScrollPosition != NO_POSITION) { - if (state.getItemCount() == 0) { - removeAndRecycleAllViews(recycler); - anchorInfo.reset(); - return; - } + anchorInfo.reset(); + + if (mPendingSavedState != null) { + applyPendingSavedState(anchorInfo); + } else { + resolveShouldLayoutReverse(); + anchorInfo.mLayoutFromEnd = mShouldReverseLayout; } - if (!anchorInfo.mValid || mPendingScrollPosition != NO_POSITION || - mPendingSavedState != null) { - anchorInfo.reset(); - if (mPendingSavedState != null) { - applyPendingSavedState(anchorInfo); - } else { - resolveShouldLayoutReverse(); - anchorInfo.mLayoutFromEnd = mShouldReverseLayout; - } + updateAnchorInfoForLayout(state, anchorInfo); - updateAnchorInfoForLayout(state, anchorInfo); - anchorInfo.mValid = true; - } - if (mPendingSavedState == null && mPendingScrollPosition == NO_POSITION) { + if (mPendingSavedState == null) { if (anchorInfo.mLayoutFromEnd != mLastLayoutFromEnd || isLayoutRTL() != mLastLayoutRTL) { mLazySpanLookup.clear(); @@ -648,30 +570,26 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } } detachAndScrapAttachedViews(recycler); - mLayoutState.mRecycle = false; mLaidOutInvalidFullSpan = false; - updateMeasureSpecs(mSecondaryOrientation.getTotalSpace()); - updateLayoutState(anchorInfo.mPosition, state); + updateMeasureSpecs(); if (anchorInfo.mLayoutFromEnd) { // Layout start. - setLayoutStateDirection(LAYOUT_START); + updateLayoutStateToFillStart(anchorInfo.mPosition, state); fill(recycler, mLayoutState, state); // Layout end. - setLayoutStateDirection(LAYOUT_END); - mLayoutState.mCurrentPosition = anchorInfo.mPosition + mLayoutState.mItemDirection; + updateLayoutStateToFillEnd(anchorInfo.mPosition, state); + mLayoutState.mCurrentPosition += mLayoutState.mItemDirection; fill(recycler, mLayoutState, state); } else { // Layout end. - setLayoutStateDirection(LAYOUT_END); + updateLayoutStateToFillEnd(anchorInfo.mPosition, state); fill(recycler, mLayoutState, state); // Layout start. - setLayoutStateDirection(LAYOUT_START); - mLayoutState.mCurrentPosition = anchorInfo.mPosition + mLayoutState.mItemDirection; + updateLayoutStateToFillStart(anchorInfo.mPosition, state); + mLayoutState.mCurrentPosition += mLayoutState.mItemDirection; fill(recycler, mLayoutState, state); } - repositionToWrapContentIfNecessary(); - if (getChildCount() > 0) { if (mShouldReverseLayout) { fixEndGap(recycler, state, true); @@ -681,85 +599,18 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple fixEndGap(recycler, state, false); } } - boolean hasGaps = false; - if (shouldCheckForGaps && !state.isPreLayout()) { - final boolean needToCheckForGaps = mGapStrategy != GAP_HANDLING_NONE - && getChildCount() > 0 - && (mLaidOutInvalidFullSpan || hasGapsToFix() != null); - if (needToCheckForGaps) { - removeCallbacks(mCheckForGapsRunnable); - if (checkForGaps()) { - hasGaps = true; - } + + if (!state.isPreLayout()) { + if (getChildCount() > 0 && mPendingScrollPosition != NO_POSITION && + mLaidOutInvalidFullSpan) { + ViewCompat.postOnAnimation(getChildAt(0), checkForGapsRunnable); } - } - if (state.isPreLayout()) { - mAnchorInfo.reset(); + mPendingScrollPosition = NO_POSITION; + mPendingScrollPositionOffset = INVALID_OFFSET; } mLastLayoutFromEnd = anchorInfo.mLayoutFromEnd; mLastLayoutRTL = isLayoutRTL(); - if (hasGaps) { - mAnchorInfo.reset(); - onLayoutChildren(recycler, state, false); - } - } - - @Override - public void onLayoutCompleted(RecyclerView.State state) { - super.onLayoutCompleted(state); - mPendingScrollPosition = NO_POSITION; - mPendingScrollPositionOffset = INVALID_OFFSET; mPendingSavedState = null; // we don't need this anymore - mAnchorInfo.reset(); - } - - private void repositionToWrapContentIfNecessary() { - if (mSecondaryOrientation.getMode() == View.MeasureSpec.EXACTLY) { - return; // nothing to do - } - float maxSize = 0; - final int childCount = getChildCount(); - for (int i = 0; i < childCount; i ++) { - View child = getChildAt(i); - float size = mSecondaryOrientation.getDecoratedMeasurement(child); - if (size < maxSize) { - continue; - } - LayoutParams layoutParams = (LayoutParams) child.getLayoutParams(); - if (layoutParams.isFullSpan()) { - size = 1f * size / mSpanCount; - } - maxSize = Math.max(maxSize, size); - } - int before = mSizePerSpan; - int desired = Math.round(maxSize * mSpanCount); - if (mSecondaryOrientation.getMode() == View.MeasureSpec.AT_MOST) { - desired = Math.min(desired, mSecondaryOrientation.getTotalSpace()); - } - updateMeasureSpecs(desired); - if (mSizePerSpan == before) { - return; // nothing has changed - } - for (int i = 0; i < childCount; i ++) { - View child = getChildAt(i); - final LayoutParams lp = (LayoutParams) child.getLayoutParams(); - if (lp.mFullSpan) { - continue; - } - if (isLayoutRTL() && mOrientation == VERTICAL) { - int newOffset = -(mSpanCount - 1 - lp.mSpan.mIndex) * mSizePerSpan; - int prevOffset = -(mSpanCount - 1 - lp.mSpan.mIndex) * before; - child.offsetLeftAndRight(newOffset - prevOffset); - } else { - int newOffset = lp.mSpan.mIndex * mSizePerSpan; - int prevOffset = lp.mSpan.mIndex * before; - if (mOrientation == VERTICAL) { - child.offsetLeftAndRight(newOffset - prevOffset); - } else { - child.offsetTopAndBottom(newOffset - prevOffset); - } - } - } } private void applyPendingSavedState(AnchorInfo anchorInfo) { @@ -848,6 +699,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple // child anchorInfo.mPosition = mShouldReverseLayout ? getLastChildPosition() : getFirstChildPosition(); + if (mPendingScrollPositionOffset != INVALID_OFFSET) { if (anchorInfo.mLayoutFromEnd) { final int target = mPrimaryOrientation.getEndAfterPadding() - @@ -906,11 +758,17 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return true; } - void updateMeasureSpecs(int totalSpace) { - mSizePerSpan = totalSpace / mSpanCount; - //noinspection ResourceType + void updateMeasureSpecs() { + mSizePerSpan = mSecondaryOrientation.getTotalSpace() / mSpanCount; mFullSizeSpec = View.MeasureSpec.makeMeasureSpec( - totalSpace, mSecondaryOrientation.getMode()); + mSecondaryOrientation.getTotalSpace(), View.MeasureSpec.EXACTLY); + if (mOrientation == VERTICAL) { + mWidthSpec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan, View.MeasureSpec.EXACTLY); + mHeightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); + } else { + mHeightSpec = View.MeasureSpec.makeMeasureSpec(mSizePerSpan, View.MeasureSpec.EXACTLY); + mWidthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); + } } @Override @@ -1056,8 +914,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return 0; } return ScrollbarHelper.computeScrollOffset(state, mPrimaryOrientation, - findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled, true) - , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled, true), + findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled) + , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled), this, mSmoothScrollbarEnabled, mShouldReverseLayout); } @@ -1076,8 +934,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return 0; } return ScrollbarHelper.computeScrollExtent(state, mPrimaryOrientation, - findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled, true) - , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled, true), + findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled) + , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled), this, mSmoothScrollbarEnabled); } @@ -1096,8 +954,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return 0; } return ScrollbarHelper.computeScrollRange(state, mPrimaryOrientation, - findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled, true) - , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled, true), + findFirstVisibleItemClosestToStart(!mSmoothScrollbarEnabled) + , findFirstVisibleItemClosestToEnd(!mSmoothScrollbarEnabled), this, mSmoothScrollbarEnabled); } @@ -1106,48 +964,27 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return computeScrollRange(state); } - private void measureChildWithDecorationsAndMargin(View child, LayoutParams lp, - boolean alreadyMeasured) { + private void measureChildWithDecorationsAndMargin(View child, LayoutParams lp) { if (lp.mFullSpan) { if (mOrientation == VERTICAL) { - measureChildWithDecorationsAndMargin(child, mFullSizeSpec, - getChildMeasureSpec(getHeight(), getHeightMode(), 0, lp.height, true), - alreadyMeasured); + measureChildWithDecorationsAndMargin(child, mFullSizeSpec, mHeightSpec); } else { - measureChildWithDecorationsAndMargin(child, - getChildMeasureSpec(getWidth(), getWidthMode(), 0, lp.width, true), - mFullSizeSpec, alreadyMeasured); + measureChildWithDecorationsAndMargin(child, mWidthSpec, mFullSizeSpec); } } else { - if (mOrientation == VERTICAL) { - measureChildWithDecorationsAndMargin(child, - getChildMeasureSpec(mSizePerSpan, getWidthMode(), 0, lp.width, false), - getChildMeasureSpec(getHeight(), getHeightMode(), 0, lp.height, true), - alreadyMeasured); - } else { - measureChildWithDecorationsAndMargin(child, - getChildMeasureSpec(getWidth(), getWidthMode(), 0, lp.width, true), - getChildMeasureSpec(mSizePerSpan, getHeightMode(), 0, lp.height, false), - alreadyMeasured); - } + measureChildWithDecorationsAndMargin(child, mWidthSpec, mHeightSpec); } } private void measureChildWithDecorationsAndMargin(View child, int widthSpec, - int heightSpec, boolean alreadyMeasured) { - calculateItemDecorationsForChild(child, mTmpRect); + int heightSpec) { + final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child); LayoutParams lp = (LayoutParams) child.getLayoutParams(); - widthSpec = updateSpecWithExtra(widthSpec, lp.leftMargin + mTmpRect.left, - lp.rightMargin + mTmpRect.right); - heightSpec = updateSpecWithExtra(heightSpec, lp.topMargin + mTmpRect.top, - lp.bottomMargin + mTmpRect.bottom); - final boolean measure = alreadyMeasured - ? shouldReMeasureChild(child, widthSpec, heightSpec, lp) - : shouldMeasureChild(child, widthSpec, heightSpec, lp); - if (measure) { - child.measure(widthSpec, heightSpec); - } - + widthSpec = updateSpecWithExtra(widthSpec, lp.leftMargin + insets.left, + lp.rightMargin + insets.right); + heightSpec = updateSpecWithExtra(heightSpec, lp.topMargin + insets.top, + lp.bottomMargin + insets.bottom); + child.measure(widthSpec, heightSpec); } private int updateSpecWithExtra(int spec, int startInset, int endInset) { @@ -1157,7 +994,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple final int mode = View.MeasureSpec.getMode(spec); if (mode == View.MeasureSpec.AT_MOST || mode == View.MeasureSpec.EXACTLY) { return View.MeasureSpec.makeMeasureSpec( - Math.max(0, View.MeasureSpec.getSize(spec) - startInset - endInset), mode); + View.MeasureSpec.getSize(spec) - startInset - endInset, mode); } return spec; } @@ -1250,8 +1087,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple if (getChildCount() > 0) { final AccessibilityRecordCompat record = AccessibilityEventCompat .asRecord(event); - final View start = findFirstVisibleItemClosestToStart(false, true); - final View end = findFirstVisibleItemClosestToEnd(false, true); + final View start = findFirstVisibleItemClosestToStart(false); + final View end = findFirstVisibleItemClosestToEnd(false); if (start == null || end == null) { return; } @@ -1269,12 +1106,11 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple /** * Finds the first fully visible child to be used as an anchor child if span count changes when - * state is restored. If no children is fully visible, returns a partially visible child instead - * of returning null. + * state is restored. */ int findFirstVisibleItemPositionInt() { - final View first = mShouldReverseLayout ? findFirstVisibleItemClosestToEnd(true, true) : - findFirstVisibleItemClosestToStart(true, true); + final View first = mShouldReverseLayout ? findFirstVisibleItemClosestToEnd(true) : + findFirstVisibleItemClosestToStart(true); return first == null ? NO_POSITION : getPosition(first); } @@ -1296,71 +1132,36 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return super.getColumnCountForAccessibility(recycler, state); } - /** - * This is for internal use. Not necessarily the child closest to start but the first child - * we find that matches the criteria. - * This method does not do any sorting based on child's start coordinate, instead, it uses - * children order. - */ - View findFirstVisibleItemClosestToStart(boolean fullyVisible, boolean acceptPartiallyVisible) { + View findFirstVisibleItemClosestToStart(boolean fullyVisible) { final int boundsStart = mPrimaryOrientation.getStartAfterPadding(); final int boundsEnd = mPrimaryOrientation.getEndAfterPadding(); final int limit = getChildCount(); - View partiallyVisible = null; - for (int i = 0; i < limit; i++) { + for (int i = 0; i < limit; i ++) { final View child = getChildAt(i); - final int childStart = mPrimaryOrientation.getDecoratedStart(child); - final int childEnd = mPrimaryOrientation.getDecoratedEnd(child); - if(childEnd <= boundsStart || childStart >= boundsEnd) { - continue; // not visible at all - } - if (childStart >= boundsStart || !fullyVisible) { - // when checking for start, it is enough even if part of the child's top is visible - // as long as fully visible is not requested. + if ((!fullyVisible || mPrimaryOrientation.getDecoratedStart(child) >= boundsStart) + && mPrimaryOrientation.getDecoratedEnd(child) <= boundsEnd) { return child; } - if (acceptPartiallyVisible && partiallyVisible == null) { - partiallyVisible = child; - } } - return partiallyVisible; + return null; } - /** - * This is for internal use. Not necessarily the child closest to bottom but the first child - * we find that matches the criteria. - * This method does not do any sorting based on child's end coordinate, instead, it uses - * children order. - */ - View findFirstVisibleItemClosestToEnd(boolean fullyVisible, boolean acceptPartiallyVisible) { + View findFirstVisibleItemClosestToEnd(boolean fullyVisible) { final int boundsStart = mPrimaryOrientation.getStartAfterPadding(); final int boundsEnd = mPrimaryOrientation.getEndAfterPadding(); - View partiallyVisible = null; - for (int i = getChildCount() - 1; i >= 0; i--) { + for (int i = getChildCount() - 1; i >= 0; i --) { final View child = getChildAt(i); - final int childStart = mPrimaryOrientation.getDecoratedStart(child); - final int childEnd = mPrimaryOrientation.getDecoratedEnd(child); - if(childEnd <= boundsStart || childStart >= boundsEnd) { - continue; // not visible at all - } - if (childEnd <= boundsEnd || !fullyVisible) { - // when checking for end, it is enough even if part of the child's bottom is visible - // as long as fully visible is not requested. + if (mPrimaryOrientation.getDecoratedStart(child) >= boundsStart && (!fullyVisible + || mPrimaryOrientation.getDecoratedEnd(child) <= boundsEnd)) { return child; } - if (acceptPartiallyVisible && partiallyVisible == null) { - partiallyVisible = child; - } } - return partiallyVisible; + return null; } private void fixEndGap(RecyclerView.Recycler recycler, RecyclerView.State state, boolean canOffsetChildren) { - final int maxEndLine = getMaxEnd(Integer.MIN_VALUE); - if (maxEndLine == Integer.MIN_VALUE) { - return; - } + final int maxEndLine = getMaxEnd(mPrimaryOrientation.getEndAfterPadding()); int gap = mPrimaryOrientation.getEndAfterPadding() - maxEndLine; int fixOffset; if (gap > 0) { @@ -1376,10 +1177,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple private void fixStartGap(RecyclerView.Recycler recycler, RecyclerView.State state, boolean canOffsetChildren) { - final int minStartLine = getMinStart(Integer.MAX_VALUE); - if (minStartLine == Integer.MAX_VALUE) { - return; - } + final int minStartLine = getMinStart(mPrimaryOrientation.getStartAfterPadding()); int gap = minStartLine - mPrimaryOrientation.getStartAfterPadding(); int fixOffset; if (gap > 0) { @@ -1393,41 +1191,40 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } } - private void updateLayoutState(int anchorPosition, RecyclerView.State state) { + private void updateLayoutStateToFillStart(int anchorPosition, RecyclerView.State state) { mLayoutState.mAvailable = 0; mLayoutState.mCurrentPosition = anchorPosition; - int startExtra = 0; - int endExtra = 0; if (isSmoothScrolling()) { final int targetPos = state.getTargetScrollPosition(); - if (targetPos != NO_POSITION) { - if (mShouldReverseLayout == targetPos < anchorPosition) { - endExtra = mPrimaryOrientation.getTotalSpace(); - } else { - startExtra = mPrimaryOrientation.getTotalSpace(); - } + if (mShouldReverseLayout == targetPos < anchorPosition) { + mLayoutState.mExtra = 0; + } else { + mLayoutState.mExtra = mPrimaryOrientation.getTotalSpace(); } - } - - // Line of the furthest row. - final boolean clipToPadding = getClipToPadding(); - if (clipToPadding) { - mLayoutState.mStartLine = mPrimaryOrientation.getStartAfterPadding() - startExtra; - mLayoutState.mEndLine = mPrimaryOrientation.getEndAfterPadding() + endExtra; } else { - mLayoutState.mEndLine = mPrimaryOrientation.getEnd() + endExtra; - mLayoutState.mStartLine = -startExtra; + mLayoutState.mExtra = 0; } - mLayoutState.mStopInFocusable = false; - mLayoutState.mRecycle = true; - mLayoutState.mInfinite = mPrimaryOrientation.getMode() == View.MeasureSpec.UNSPECIFIED && - mPrimaryOrientation.getEnd() == 0; + mLayoutState.mLayoutDirection = LAYOUT_START; + mLayoutState.mItemDirection = mShouldReverseLayout ? ITEM_DIRECTION_TAIL + : ITEM_DIRECTION_HEAD; } - private void setLayoutStateDirection(int direction) { - mLayoutState.mLayoutDirection = direction; - mLayoutState.mItemDirection = (mShouldReverseLayout == (direction == LAYOUT_START)) ? - ITEM_DIRECTION_TAIL : ITEM_DIRECTION_HEAD; + private void updateLayoutStateToFillEnd(int anchorPosition, RecyclerView.State state) { + mLayoutState.mAvailable = 0; + mLayoutState.mCurrentPosition = anchorPosition; + if (isSmoothScrolling()) { + final int targetPos = state.getTargetScrollPosition(); + if (mShouldReverseLayout == targetPos > anchorPosition) { + mLayoutState.mExtra = 0; + } else { + mLayoutState.mExtra = mPrimaryOrientation.getTotalSpace(); + } + } else { + mLayoutState.mExtra = 0; + } + mLayoutState.mLayoutDirection = LAYOUT_END; + mLayoutState.mItemDirection = mShouldReverseLayout ? ITEM_DIRECTION_HEAD + : ITEM_DIRECTION_TAIL; } @Override @@ -1468,8 +1265,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } @Override - public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount, - Object payload) { + public void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount) { handleUpdate(positionStart, itemCount, AdapterHelper.UpdateOp.UPDATE); } @@ -1478,23 +1274,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple */ private void handleUpdate(int positionStart, int itemCountOrToPosition, int cmd) { int minPosition = mShouldReverseLayout ? getLastChildPosition() : getFirstChildPosition(); - final int affectedRangeEnd;// exclusive - final int affectedRangeStart;// inclusive - - if (cmd == AdapterHelper.UpdateOp.MOVE) { - if (positionStart < itemCountOrToPosition) { - affectedRangeEnd = itemCountOrToPosition + 1; - affectedRangeStart = positionStart; - } else { - affectedRangeEnd = positionStart + 1; - affectedRangeStart = itemCountOrToPosition; - } - } else { - affectedRangeStart = positionStart; - affectedRangeEnd = positionStart + itemCountOrToPosition; - } - - mLazySpanLookup.invalidateAfter(affectedRangeStart); + mLazySpanLookup.invalidateAfter(positionStart); switch (cmd) { case AdapterHelper.UpdateOp.ADD: mLazySpanLookup.offsetForAddition(positionStart, itemCountOrToPosition); @@ -1509,12 +1289,12 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple break; } - if (affectedRangeEnd <= minPosition) { + if (positionStart + itemCountOrToPosition <= minPosition) { return; - } + } int maxPosition = mShouldReverseLayout ? getFirstChildPosition() : getLastChildPosition(); - if (affectedRangeStart <= maxPosition) { + if (positionStart <= maxPosition) { requestLayout(); } } @@ -1524,41 +1304,45 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple mRemainingSpans.set(0, mSpanCount, true); // The target position we are trying to reach. final int targetLine; - + /* + * The line until which we can recycle, as long as we add views. + * Keep in mind, it is still the line in layout direction which means; to calculate the + * actual recycle line, we should subtract/add the size in orientation. + */ + final int recycleLine; // Line of the furthest row. - if (mLayoutState.mInfinite) { - if (layoutState.mLayoutDirection == LAYOUT_END) { - targetLine = Integer.MAX_VALUE; - } else { // LAYOUT_START - targetLine = Integer.MIN_VALUE; - } - } else { - if (layoutState.mLayoutDirection == LAYOUT_END) { - targetLine = layoutState.mEndLine + layoutState.mAvailable; - } else { // LAYOUT_START - targetLine = layoutState.mStartLine - layoutState.mAvailable; - } - } + if (layoutState.mLayoutDirection == LAYOUT_END) { + // ignore padding for recycler + recycleLine = mPrimaryOrientation.getEndAfterPadding() + mLayoutState.mAvailable; + targetLine = recycleLine + mLayoutState.mExtra + mPrimaryOrientation.getEndPadding(); - updateAllRemainingSpans(layoutState.mLayoutDirection, targetLine); - if (DEBUG) { - Log.d(TAG, "FILLING targetLine: " + targetLine + "," + - "remaining spans:" + mRemainingSpans + ", state: " + layoutState); + } else { // LAYOUT_START + // ignore padding for recycler + recycleLine = mPrimaryOrientation.getStartAfterPadding() - mLayoutState.mAvailable; + targetLine = recycleLine - mLayoutState.mExtra - + mPrimaryOrientation.getStartAfterPadding(); } + updateAllRemainingSpans(layoutState.mLayoutDirection, targetLine); // the default coordinate to add new view. final int defaultNewViewLine = mShouldReverseLayout ? mPrimaryOrientation.getEndAfterPadding() : mPrimaryOrientation.getStartAfterPadding(); - boolean added = false; - while (layoutState.hasMore(state) - && (mLayoutState.mInfinite || !mRemainingSpans.isEmpty())) { + + while (layoutState.hasMore(state) && !mRemainingSpans.isEmpty()) { View view = layoutState.next(recycler); LayoutParams lp = ((LayoutParams) view.getLayoutParams()); - final int position = lp.getViewLayoutPosition(); + if (layoutState.mLayoutDirection == LAYOUT_END) { + addView(view); + } else { + addView(view, 0); + } + measureChildWithDecorationsAndMargin(view, lp); + + final int position = lp.getViewPosition(); final int spanIndex = mLazySpanLookup.getSpan(position); Span currentSpan; - final boolean assignSpan = spanIndex == LayoutParams.INVALID_SPAN_ID; + boolean assignSpan = spanIndex == LayoutParams.INVALID_SPAN_ID; if (assignSpan) { currentSpan = lp.mFullSpan ? mSpans[0] : getNextSpan(layoutState); mLazySpanLookup.setSpan(position, currentSpan); @@ -1571,17 +1355,9 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } currentSpan = mSpans[spanIndex]; } - // assign span before measuring so that item decorators can get updated span index - lp.mSpan = currentSpan; - if (layoutState.mLayoutDirection == LAYOUT_END) { - addView(view); - } else { - addView(view, 0); - } - measureChildWithDecorationsAndMargin(view, lp, false); - final int start; final int end; + if (layoutState.mLayoutDirection == LAYOUT_END) { start = lp.mFullSpan ? getMaxEnd(defaultNewViewLine) : currentSpan.getEndLine(defaultNewViewLine); @@ -1607,41 +1383,16 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } // check if this item may create gaps in the future - if (lp.mFullSpan && layoutState.mItemDirection == ITEM_DIRECTION_HEAD) { - if (assignSpan) { - mLaidOutInvalidFullSpan = true; - } else { - final boolean hasInvalidGap; - if (layoutState.mLayoutDirection == LAYOUT_END) { - hasInvalidGap = !areAllEndsEqual(); - } else { // layoutState.mLayoutDirection == LAYOUT_START - hasInvalidGap = !areAllStartsEqual(); - } - if (hasInvalidGap) { - final LazySpanLookup.FullSpanItem fullSpanItem = mLazySpanLookup - .getFullSpanItem(position); - if (fullSpanItem != null) { - fullSpanItem.mHasUnwantedGapAfter = true; - } - mLaidOutInvalidFullSpan = true; - } - } - } - attachViewToSpans(view, lp, layoutState); - final int otherStart; - final int otherEnd; - if (isLayoutRTL() && mOrientation == VERTICAL) { - otherEnd = lp.mFullSpan ? mSecondaryOrientation.getEndAfterPadding() : - mSecondaryOrientation.getEndAfterPadding() - - (mSpanCount - 1 - currentSpan.mIndex) * mSizePerSpan; - otherStart = otherEnd - mSecondaryOrientation.getDecoratedMeasurement(view); - } else { - otherStart = lp.mFullSpan ? mSecondaryOrientation.getStartAfterPadding() - : currentSpan.mIndex * mSizePerSpan + - mSecondaryOrientation.getStartAfterPadding(); - otherEnd = otherStart + mSecondaryOrientation.getDecoratedMeasurement(view); + if (lp.mFullSpan && layoutState.mItemDirection == ITEM_DIRECTION_HEAD && assignSpan) { + mLaidOutInvalidFullSpan = true; } + lp.mSpan = currentSpan; + attachViewToSpans(view, lp, layoutState); + final int otherStart = lp.mFullSpan ? mSecondaryOrientation.getStartAfterPadding() + : currentSpan.mIndex * mSizePerSpan + + mSecondaryOrientation.getStartAfterPadding(); + final int otherEnd = otherStart + mSecondaryOrientation.getDecoratedMeasurement(view); if (mOrientation == VERTICAL) { layoutDecoratedWithMargins(view, otherStart, start, otherEnd, end); } else { @@ -1653,28 +1404,18 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } else { updateRemainingSpans(currentSpan, mLayoutState.mLayoutDirection, targetLine); } - recycle(recycler, mLayoutState); - if (mLayoutState.mStopInFocusable && view.isFocusable()) { - if (lp.mFullSpan) { - mRemainingSpans.clear(); - } else { - mRemainingSpans.set(currentSpan.mIndex, false); - } - } - added = true; + recycle(recycler, mLayoutState, currentSpan, recycleLine); } - if (!added) { - recycle(recycler, mLayoutState); + if (DEBUG) { + Log.d(TAG, "fill, " + getChildCount()); } - final int diff; if (mLayoutState.mLayoutDirection == LAYOUT_START) { final int minStart = getMinStart(mPrimaryOrientation.getStartAfterPadding()); - diff = mPrimaryOrientation.getStartAfterPadding() - minStart; + return Math.max(0, mLayoutState.mAvailable + (recycleLine - minStart)); } else { - final int maxEnd = getMaxEnd(mPrimaryOrientation.getEndAfterPadding()); - diff = maxEnd - mPrimaryOrientation.getEndAfterPadding(); + final int max = getMaxEnd(mPrimaryOrientation.getEndAfterPadding()); + return Math.max(0, mLayoutState.mAvailable + (max - recycleLine)); } - return diff > 0 ? Math.min(layoutState.mAvailable, diff) : 0; } private LazySpanLookup.FullSpanItem createFullSpanItemFromEnd(int newItemTop) { @@ -1711,43 +1452,19 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } } - private void recycle(RecyclerView.Recycler recycler, LayoutState layoutState) { - if (!layoutState.mRecycle || layoutState.mInfinite) { - return; - } - if (layoutState.mAvailable == 0) { - // easy, recycle line is still valid - if (layoutState.mLayoutDirection == LAYOUT_START) { - recycleFromEnd(recycler, layoutState.mEndLine); - } else { - recycleFromStart(recycler, layoutState.mStartLine); - } + private void recycle(RecyclerView.Recycler recycler, LayoutState layoutState, + Span updatedSpan, int recycleLine) { + if (layoutState.mLayoutDirection == LAYOUT_START) { + // calculate recycle line + int maxStart = getMaxStart(updatedSpan.getStartLine()); + recycleFromEnd(recycler, Math.max(recycleLine, maxStart) + + (mPrimaryOrientation.getEnd() - mPrimaryOrientation.getStartAfterPadding())); } else { - // scrolling case, recycle line can be shifted by how much space we could cover - // by adding new views - if (layoutState.mLayoutDirection == LAYOUT_START) { - // calculate recycle line - int scrolled = layoutState.mStartLine - getMaxStart(layoutState.mStartLine); - final int line; - if (scrolled < 0) { - line = layoutState.mEndLine; - } else { - line = layoutState.mEndLine - Math.min(scrolled, layoutState.mAvailable); - } - recycleFromEnd(recycler, line); - } else { - // calculate recycle line - int scrolled = getMinEnd(layoutState.mEndLine) - layoutState.mEndLine; - final int line; - if (scrolled < 0) { - line = layoutState.mStartLine; - } else { - line = layoutState.mStartLine + Math.min(scrolled, layoutState.mAvailable); - } - recycleFromStart(recycler, line); - } + // calculate recycle line + int minEnd = getMinEnd(updatedSpan.getEndLine()); + recycleFromStart(recycler, Math.min(recycleLine, minEnd) - + (mPrimaryOrientation.getEnd() - mPrimaryOrientation.getStartAfterPadding())); } - } private void appendViewToAllSpans(View view) { @@ -1764,6 +1481,18 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } } + private void layoutDecoratedWithMargins(View child, int left, int top, int right, int bottom) { + LayoutParams lp = (LayoutParams) child.getLayoutParams(); + if (DEBUG) { + Log.d(TAG, "layout decorated pos: " + lp.getViewPosition() + ", span:" + + lp.getSpanIndex() + ", fullspan:" + lp.mFullSpan + + ". l:" + left + ",t:" + top + + ", r:" + right + ", b:" + bottom); + } + layoutDecorated(child, left + lp.leftMargin, top + lp.topMargin, right - lp.rightMargin + , bottom - lp.bottomMargin); + } + private void updateAllRemainingSpans(int layoutDir, int targetLine) { for (int i = 0; i < mSpanCount; i++) { if (mSpans[i].mViews.isEmpty()) { @@ -1777,12 +1506,12 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple final int deletedSize = span.getDeletedSize(); if (layoutDir == LAYOUT_START) { final int line = span.getStartLine(); - if (line + deletedSize <= targetLine) { + if (line + deletedSize < targetLine) { mRemainingSpans.set(span.mIndex, false); } } else { final int line = span.getEndLine(); - if (line - deletedSize >= targetLine) { + if (line - deletedSize > targetLine) { mRemainingSpans.set(span.mIndex, false); } } @@ -1810,26 +1539,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return minStart; } - boolean areAllEndsEqual() { - int end = mSpans[0].getEndLine(Span.INVALID_LINE); - for (int i = 1; i < mSpanCount; i++) { - if (mSpans[i].getEndLine(Span.INVALID_LINE) != end) { - return false; - } - } - return true; - } - - boolean areAllStartsEqual() { - int start = mSpans[0].getStartLine(Span.INVALID_LINE); - for (int i = 1; i < mSpanCount; i++) { - if (mSpans[i].getStartLine(Span.INVALID_LINE) != start) { - return false; - } - } - return true; - } - private int getMaxEnd(int def) { int maxEnd = mSpans[0].getEndLine(def); for (int i = 1; i < mSpanCount; i++) { @@ -1853,25 +1562,18 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } private void recycleFromStart(RecyclerView.Recycler recycler, int line) { + if (DEBUG) { + Log.d(TAG, "recycling from start for line " + line); + } while (getChildCount() > 0) { View child = getChildAt(0); - if (mPrimaryOrientation.getDecoratedEnd(child) <= line && - mPrimaryOrientation.getTransformedEndWithDecoration(child) <= line) { + if (mPrimaryOrientation.getDecoratedEnd(child) < line) { LayoutParams lp = (LayoutParams) child.getLayoutParams(); - // Don't recycle the last View in a span not to lose span's start/end lines if (lp.mFullSpan) { - for (int j = 0; j < mSpanCount; j++) { - if (mSpans[j].mViews.size() == 1) { - return; - } - } for (int j = 0; j < mSpanCount; j++) { mSpans[j].popStart(); } } else { - if (lp.mSpan.mViews.size() == 1) { - return; - } lp.mSpan.popStart(); } removeAndRecycleView(child, recycler); @@ -1886,23 +1588,13 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple int i; for (i = childCount - 1; i >= 0; i--) { View child = getChildAt(i); - if (mPrimaryOrientation.getDecoratedStart(child) >= line && - mPrimaryOrientation.getTransformedStartWithDecoration(child) >= line) { + if (mPrimaryOrientation.getDecoratedStart(child) > line) { LayoutParams lp = (LayoutParams) child.getLayoutParams(); - // Don't recycle the last View in a span not to lose span's start/end lines if (lp.mFullSpan) { - for (int j = 0; j < mSpanCount; j++) { - if (mSpans[j].mViews.size() == 1) { - return; - } - } for (int j = 0; j < mSpanCount; j++) { mSpans[j].popEnd(); } } else { - if (lp.mSpan.mViews.size() == 1) { - return; - } lp.mSpan.popEnd(); } removeAndRecycleView(child, recycler); @@ -1996,27 +1688,23 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return position < firstChildPos != mShouldReverseLayout ? LAYOUT_START : LAYOUT_END; } - @Override - public PointF computeScrollVectorForPosition(int targetPosition) { - final int direction = calculateScrollDirectionForPosition(targetPosition); - PointF outVector = new PointF(); - if (direction == 0) { - return null; - } - if (mOrientation == HORIZONTAL) { - outVector.x = direction; - outVector.y = 0; - } else { - outVector.x = 0; - outVector.y = direction; - } - return outVector; - } - @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { - LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()); + LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) { + @Override + public PointF computeScrollVectorForPosition(int targetPosition) { + final int direction = calculateScrollDirectionForPosition(targetPosition); + if (direction == 0) { + return null; + } + if (mOrientation == HORIZONTAL) { + return new PointF(direction, 0); + } else { + return new PointF(0, direction); + } + } + }; scroller.setTargetPosition(position); startSmoothScroll(scroller); } @@ -2054,21 +1742,23 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } int scrollBy(int dt, RecyclerView.Recycler recycler, RecyclerView.State state) { + ensureOrientationHelper(); final int referenceChildPosition; - final int layoutDir; if (dt > 0) { // layout towards end - layoutDir = LAYOUT_END; + mLayoutState.mLayoutDirection = LAYOUT_END; + mLayoutState.mItemDirection = mShouldReverseLayout ? ITEM_DIRECTION_HEAD + : ITEM_DIRECTION_TAIL; referenceChildPosition = getLastChildPosition(); } else { - layoutDir = LAYOUT_START; + mLayoutState.mLayoutDirection = LAYOUT_START; + mLayoutState.mItemDirection = mShouldReverseLayout ? ITEM_DIRECTION_TAIL + : ITEM_DIRECTION_HEAD; referenceChildPosition = getFirstChildPosition(); } - mLayoutState.mRecycle = true; - updateLayoutState(referenceChildPosition, state); - setLayoutStateDirection(layoutDir); mLayoutState.mCurrentPosition = referenceChildPosition + mLayoutState.mItemDirection; final int absDt = Math.abs(dt); mLayoutState.mAvailable = absDt; + mLayoutState.mExtra = isSmoothScrolling() ? mPrimaryOrientation.getTotalSpace() : 0; int consumed = fill(recycler, mLayoutState, state); final int totalScroll; if (absDt < consumed) { @@ -2131,16 +1821,10 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return 0; } - @SuppressWarnings("deprecation") @Override public RecyclerView.LayoutParams generateDefaultLayoutParams() { - if (mOrientation == HORIZONTAL) { - return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, - ViewGroup.LayoutParams.MATCH_PARENT); - } else { - return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT); - } + return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT); } @Override @@ -2166,123 +1850,9 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return mOrientation; } - @Nullable - @Override - public View onFocusSearchFailed(View focused, int direction, RecyclerView.Recycler recycler, - RecyclerView.State state) { - if (getChildCount() == 0) { - return null; - } - - final View directChild = findContainingItemView(focused); - if (directChild == null) { - return null; - } - - resolveShouldLayoutReverse(); - final int layoutDir = convertFocusDirectionToLayoutDirection(direction); - if (layoutDir == LayoutState.INVALID_LAYOUT) { - return null; - } - LayoutParams prevFocusLayoutParams = (LayoutParams) directChild.getLayoutParams(); - boolean prevFocusFullSpan = prevFocusLayoutParams.mFullSpan; - final Span prevFocusSpan = prevFocusLayoutParams.mSpan; - final int referenceChildPosition; - if (layoutDir == LAYOUT_END) { // layout towards end - referenceChildPosition = getLastChildPosition(); - } else { - referenceChildPosition = getFirstChildPosition(); - } - updateLayoutState(referenceChildPosition, state); - setLayoutStateDirection(layoutDir); - - mLayoutState.mCurrentPosition = referenceChildPosition + mLayoutState.mItemDirection; - mLayoutState.mAvailable = (int) (MAX_SCROLL_FACTOR * mPrimaryOrientation.getTotalSpace()); - mLayoutState.mStopInFocusable = true; - mLayoutState.mRecycle = false; - fill(recycler, mLayoutState, state); - mLastLayoutFromEnd = mShouldReverseLayout; - if (!prevFocusFullSpan) { - View view = prevFocusSpan.getFocusableViewAfter(referenceChildPosition, layoutDir); - if (view != null && view != directChild) { - return view; - } - } - // either could not find from the desired span or prev view is full span. - // traverse all spans - if (preferLastSpan(layoutDir)) { - for (int i = mSpanCount - 1; i >= 0; i --) { - View view = mSpans[i].getFocusableViewAfter(referenceChildPosition, layoutDir); - if (view != null && view != directChild) { - return view; - } - } - } else { - for (int i = 0; i < mSpanCount; i ++) { - View view = mSpans[i].getFocusableViewAfter(referenceChildPosition, layoutDir); - if (view != null && view != directChild) { - return view; - } - } - } - return null; - } - - /** - * Converts a focusDirection to orientation. - * - * @param focusDirection One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN}, - * {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT}, - * {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD} - * or 0 for not applicable - * @return {@link LayoutState#LAYOUT_START} or {@link LayoutState#LAYOUT_END} if focus direction - * is applicable to current state, {@link LayoutState#INVALID_LAYOUT} otherwise. - */ - private int convertFocusDirectionToLayoutDirection(int focusDirection) { - switch (focusDirection) { - case View.FOCUS_BACKWARD: - if (mOrientation == VERTICAL) { - return LayoutState.LAYOUT_START; - } else if (isLayoutRTL()) { - return LayoutState.LAYOUT_END; - } else { - return LayoutState.LAYOUT_START; - } - case View.FOCUS_FORWARD: - if (mOrientation == VERTICAL) { - return LayoutState.LAYOUT_END; - } else if (isLayoutRTL()) { - return LayoutState.LAYOUT_START; - } else { - return LayoutState.LAYOUT_END; - } - case View.FOCUS_UP: - return mOrientation == VERTICAL ? LayoutState.LAYOUT_START - : LayoutState.INVALID_LAYOUT; - case View.FOCUS_DOWN: - return mOrientation == VERTICAL ? LayoutState.LAYOUT_END - : LayoutState.INVALID_LAYOUT; - case View.FOCUS_LEFT: - return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_START - : LayoutState.INVALID_LAYOUT; - case View.FOCUS_RIGHT: - return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_END - : LayoutState.INVALID_LAYOUT; - default: - if (DEBUG) { - Log.d(TAG, "Unknown focus request:" + focusDirection); - } - return LayoutState.INVALID_LAYOUT; - } - - } /** * LayoutParams used by StaggeredGridLayoutManager. - *

        - * Note that if the orientation is {@link #VERTICAL}, the width parameter is ignored and if the - * orientation is {@link #HORIZONTAL} the height parameter is ignored because child view is - * expected to fill all of the space given to it. */ public static class LayoutParams extends RecyclerView.LayoutParams { @@ -2356,7 +1926,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple class Span { static final int INVALID_LINE = Integer.MIN_VALUE; - private ArrayList mViews = new ArrayList<>(); + private ArrayList mViews = new ArrayList(); int mCachedStart = INVALID_LINE; int mCachedEnd = INVALID_LINE; int mDeletedSize = 0; @@ -2383,7 +1953,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple mCachedStart = mPrimaryOrientation.getDecoratedStart(startView); if (lp.mFullSpan) { LazySpanLookup.FullSpanItem fsi = mLazySpanLookup - .getFullSpanItem(lp.getViewLayoutPosition()); + .getFullSpanItem(lp.getViewPosition()); if (fsi != null && fsi.mGapDir == LAYOUT_START) { mCachedStart -= fsi.getGapForSpan(mIndex); } @@ -2417,7 +1987,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple mCachedEnd = mPrimaryOrientation.getDecoratedEnd(endView); if (lp.mFullSpan) { LazySpanLookup.FullSpanItem fsi = mLazySpanLookup - .getFullSpanItem(lp.getViewLayoutPosition()); + .getFullSpanItem(lp.getViewPosition()); if (fsi != null && fsi.mGapDir == LAYOUT_END) { mCachedEnd += fsi.getGapForSpan(mIndex); } @@ -2472,7 +2042,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return; } if ((reverseLayout && reference < mPrimaryOrientation.getEndAfterPadding()) || - (!reverseLayout && reference > mPrimaryOrientation.getStartAfterPadding())) { + (!reverseLayout && reference > mPrimaryOrientation.getStartAfterPadding()) ) { return; } if (offset != INVALID_OFFSET) { @@ -2540,28 +2110,67 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } } + // normalized offset is how much this span can scroll + int getNormalizedOffset(int dt, int targetStart, int targetEnd) { + if (mViews.size() == 0) { + return 0; + } + if (dt < 0) { + final int endSpace = getEndLine() - targetEnd; + if (endSpace <= 0) { + return 0; + } + return -dt > endSpace ? -endSpace : dt; + } else { + final int startSpace = targetStart - getStartLine(); + if (startSpace <= 0) { + return 0; + } + return startSpace < dt ? startSpace : dt; + } + } + + /** + * Returns if there is no child between start-end lines + * + * @param start The start line + * @param end The end line + * @return true if a new child can be added between start and end + */ + boolean isEmpty(int start, int end) { + final int count = mViews.size(); + for (int i = 0; i < count; i++) { + final View view = mViews.get(i); + if (mPrimaryOrientation.getDecoratedStart(view) < end && + mPrimaryOrientation.getDecoratedEnd(view) > start) { + return false; + } + } + return true; + } + public int findFirstVisibleItemPosition() { return mReverseLayout - ? findOneVisibleChild(mViews.size() - 1, -1, false) + ? findOneVisibleChild(mViews.size() -1, -1, false) : findOneVisibleChild(0, mViews.size(), false); } public int findFirstCompletelyVisibleItemPosition() { return mReverseLayout - ? findOneVisibleChild(mViews.size() - 1, -1, true) + ? findOneVisibleChild(mViews.size() -1, -1, true) : findOneVisibleChild(0, mViews.size(), true); } public int findLastVisibleItemPosition() { return mReverseLayout ? findOneVisibleChild(0, mViews.size(), false) - : findOneVisibleChild(mViews.size() - 1, -1, false); + : findOneVisibleChild(mViews.size() -1, -1, false); } public int findLastCompletelyVisibleItemPosition() { return mReverseLayout ? findOneVisibleChild(0, mViews.size(), true) - : findOneVisibleChild(mViews.size() - 1, -1, true); + : findOneVisibleChild(mViews.size() -1, -1, true); } int findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible) { @@ -2584,36 +2193,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } return NO_POSITION; } - - /** - * Depending on the layout direction, returns the View that is after the given position. - */ - public View getFocusableViewAfter(int referenceChildPosition, int layoutDir) { - View candidate = null; - if (layoutDir == LAYOUT_START) { - final int limit = mViews.size(); - for (int i = 0; i < limit; i++) { - final View view = mViews.get(i); - if (view.isFocusable() && - (getPosition(view) > referenceChildPosition == mReverseLayout) ) { - candidate = view; - } else { - break; - } - } - } else { - for (int i = mViews.size() - 1; i >= 0; i--) { - final View view = mViews.get(i); - if (view.isFocusable() && - (getPosition(view) > referenceChildPosition == !mReverseLayout)) { - candidate = view; - } else { - break; - } - } - } - return candidate; - } } /** @@ -2624,6 +2203,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple private static final int MIN_SIZE = 10; int[] mData; + int mAdapterSize; // we don't want to grow beyond that, unless it grows List mFullSpanItems; @@ -2681,6 +2261,9 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple while (len <= position) { len *= 2; } + if (len > mAdapterSize) { + len = mAdapterSize; + } return len; } @@ -2790,7 +2373,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple public void addFullSpanItem(FullSpanItem fullSpanItem) { if (mFullSpanItems == null) { - mFullSpanItems = new ArrayList<>(); + mFullSpanItems = new ArrayList(); } final int size = mFullSpanItems.size(); for (int i = 0; i < size; i++) { @@ -2828,23 +2411,17 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple * @param minPos inclusive * @param maxPos exclusive * @param gapDir if not 0, returns FSIs on in that direction - * @param hasUnwantedGapAfter If true, when full span item has unwanted gaps, it will be - * returned even if its gap direction does not match. */ - public FullSpanItem getFirstFullSpanItemInRange(int minPos, int maxPos, int gapDir, - boolean hasUnwantedGapAfter) { + public FullSpanItem getFirstFullSpanItemInRange(int minPos, int maxPos, int gapDir) { if (mFullSpanItems == null) { return null; } - final int limit = mFullSpanItems.size(); - for (int i = 0; i < limit; i++) { + for (int i = 0; i < mFullSpanItems.size(); i++) { FullSpanItem fsi = mFullSpanItems.get(i); if (fsi.mPosition >= maxPos) { return null; } - if (fsi.mPosition >= minPos - && (gapDir == 0 || fsi.mGapDir == gapDir || - (hasUnwantedGapAfter && fsi.mHasUnwantedGapAfter))) { + if (fsi.mPosition >= minPos && (gapDir == 0 || fsi.mGapDir == gapDir)) { return fsi; } } @@ -2859,15 +2436,10 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple int mPosition; int mGapDir; int[] mGapPerSpan; - // A full span may be laid out in primary direction but may have gaps due to - // invalidation of views after it. This is recorded during a reverse scroll and if - // view is still on the screen after scroll stops, we have to recalculate layout - boolean mHasUnwantedGapAfter; public FullSpanItem(Parcel in) { mPosition = in.readInt(); mGapDir = in.readInt(); - mHasUnwantedGapAfter = in.readInt() == 1; int spanCount = in.readInt(); if (spanCount > 0) { mGapPerSpan = new int[spanCount]; @@ -2882,6 +2454,10 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return mGapPerSpan == null ? 0 : mGapPerSpan[spanIndex]; } + public void invalidateSpanGaps() { + mGapPerSpan = null; + } + @Override public int describeContents() { return 0; @@ -2891,7 +2467,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple public void writeToParcel(Parcel dest, int flags) { dest.writeInt(mPosition); dest.writeInt(mGapDir); - dest.writeInt(mHasUnwantedGapAfter ? 1 : 0); if (mGapPerSpan != null && mGapPerSpan.length > 0) { dest.writeInt(mGapPerSpan.length); dest.writeIntArray(mGapPerSpan); @@ -2905,13 +2480,12 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple return "FullSpanItem{" + "mPosition=" + mPosition + ", mGapDir=" + mGapDir + - ", mHasUnwantedGapAfter=" + mHasUnwantedGapAfter + ", mGapPerSpan=" + Arrays.toString(mGapPerSpan) + '}'; } - public static final Parcelable.Creator CREATOR - = new Parcelable.Creator() { + public static final Creator CREATOR + = new Creator() { @Override public FullSpanItem createFromParcel(Parcel in) { return new FullSpanItem(in); @@ -2925,10 +2499,7 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple } } - /** - * @hide - */ - public static class SavedState implements Parcelable { + static class SavedState implements Parcelable { int mAnchorPosition; int mVisibleAnchorPosition; // Replacement for span info when spans are invalidated @@ -2961,7 +2532,6 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple mReverseLayout = in.readInt() == 1; mAnchorLayoutFromEnd = in.readInt() == 1; mLastLayoutRTL = in.readInt() == 1; - //noinspection unchecked mFullSpanItems = in.readArrayList( LazySpanLookup.FullSpanItem.class.getClassLoader()); } @@ -3017,8 +2587,8 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple dest.writeList(mFullSpanItems); } - public static final Parcelable.Creator CREATOR - = new Parcelable.Creator() { + public static final Creator CREATOR + = new Creator() { @Override public SavedState createFromParcel(Parcel in) { return new SavedState(in); @@ -3034,24 +2604,18 @@ public class StaggeredGridLayoutManager extends RecyclerView.LayoutManager imple /** * Data class to hold the information about an anchor position which is used in onLayout call. */ - class AnchorInfo { + private class AnchorInfo { int mPosition; int mOffset; boolean mLayoutFromEnd; boolean mInvalidateOffsets; - boolean mValid; - - public AnchorInfo() { - reset(); - } void reset() { mPosition = NO_POSITION; mOffset = INVALID_OFFSET; mLayoutFromEnd = false; mInvalidateOffsets = false; - mValid = false; } void assignCoordinateFromPadding() { diff --git a/app/src/main/java/android/support/v7/widget/ViewInfoStore.java b/app/src/main/java/android/support/v7/widget/ViewInfoStore.java deleted file mode 100644 index f01a38222c..0000000000 --- a/app/src/main/java/android/support/v7/widget/ViewInfoStore.java +++ /dev/null @@ -1,331 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package android.support.v7.widget; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.annotation.VisibleForTesting; -import android.support.v4.util.ArrayMap; -import android.support.v4.util.LongSparseArray; -import android.support.v4.util.Pools; -import android.view.View; - -import static android.support.v7.widget.RecyclerView.ViewHolder; -import static android.support.v7.widget.RecyclerView.ItemAnimator.ItemHolderInfo; - -import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_APPEAR_PRE_AND_POST; -import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_APPEAR_AND_DISAPPEAR; -import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_PRE_AND_POST; -import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_DISAPPEARED; -import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_APPEAR; -import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_PRE; -import static android.support.v7.widget.ViewInfoStore.InfoRecord.FLAG_POST; -/** - * This class abstracts all tracking for Views to run animations - * - * @hide - */ -class ViewInfoStore { - - private static final boolean DEBUG = false; - - /** - * View data records for pre-layout - */ - @VisibleForTesting - final ArrayMap mLayoutHolderMap = new ArrayMap<>(); - - @VisibleForTesting - final LongSparseArray mOldChangedHolders = new LongSparseArray<>(); - - /** - * Clears the state and all existing tracking data - */ - void clear() { - mLayoutHolderMap.clear(); - mOldChangedHolders.clear(); - } - - /** - * Adds the item information to the prelayout tracking - * @param holder The ViewHolder whose information is being saved - * @param info The information to save - */ - void addToPreLayout(ViewHolder holder, ItemHolderInfo info) { - InfoRecord record = mLayoutHolderMap.get(holder); - if (record == null) { - record = InfoRecord.obtain(); - mLayoutHolderMap.put(holder, record); - } - record.preInfo = info; - record.flags |= FLAG_PRE; - } - - boolean isDisappearing(ViewHolder holder) { - final InfoRecord record = mLayoutHolderMap.get(holder); - return record != null && ((record.flags & FLAG_DISAPPEARED) != 0); - } - - /** - * Finds the ItemHolderInfo for the given ViewHolder in preLayout list and removes it. - * - * @param vh The ViewHolder whose information is being queried - * @return The ItemHolderInfo for the given ViewHolder or null if it does not exist - */ - @Nullable - ItemHolderInfo popFromPreLayout(ViewHolder vh) { - return popFromLayoutStep(vh, FLAG_PRE); - } - - /** - * Finds the ItemHolderInfo for the given ViewHolder in postLayout list and removes it. - * - * @param vh The ViewHolder whose information is being queried - * @return The ItemHolderInfo for the given ViewHolder or null if it does not exist - */ - @Nullable - ItemHolderInfo popFromPostLayout(ViewHolder vh) { - return popFromLayoutStep(vh, FLAG_POST); - } - - private ItemHolderInfo popFromLayoutStep(ViewHolder vh, int flag) { - int index = mLayoutHolderMap.indexOfKey(vh); - if (index < 0) { - return null; - } - final InfoRecord record = mLayoutHolderMap.valueAt(index); - if (record != null && (record.flags & flag) != 0) { - record.flags &= ~flag; - final ItemHolderInfo info; - if (flag == FLAG_PRE) { - info = record.preInfo; - } else if (flag == FLAG_POST) { - info = record.postInfo; - } else { - throw new IllegalArgumentException("Must provide flag PRE or POST"); - } - // if not pre-post flag is left, clear. - if ((record.flags & (FLAG_PRE | FLAG_POST)) == 0) { - mLayoutHolderMap.removeAt(index); - InfoRecord.recycle(record); - } - return info; - } - return null; - } - - /** - * Adds the given ViewHolder to the oldChangeHolders list - * @param key The key to identify the ViewHolder. - * @param holder The ViewHolder to store - */ - void addToOldChangeHolders(long key, ViewHolder holder) { - mOldChangedHolders.put(key, holder); - } - - /** - * Adds the given ViewHolder to the appeared in pre layout list. These are Views added by the - * LayoutManager during a pre-layout pass. We distinguish them from other views that were - * already in the pre-layout so that ItemAnimator can choose to run a different animation for - * them. - * - * @param holder The ViewHolder to store - * @param info The information to save - */ - void addToAppearedInPreLayoutHolders(ViewHolder holder, ItemHolderInfo info) { - InfoRecord record = mLayoutHolderMap.get(holder); - if (record == null) { - record = InfoRecord.obtain(); - mLayoutHolderMap.put(holder, record); - } - record.flags |= FLAG_APPEAR; - record.preInfo = info; - } - - /** - * Checks whether the given ViewHolder is in preLayout list - * @param viewHolder The ViewHolder to query - * - * @return True if the ViewHolder is present in preLayout, false otherwise - */ - boolean isInPreLayout(ViewHolder viewHolder) { - final InfoRecord record = mLayoutHolderMap.get(viewHolder); - return record != null && (record.flags & FLAG_PRE) != 0; - } - - /** - * Queries the oldChangeHolder list for the given key. If they are not tracked, simply returns - * null. - * @param key The key to be used to find the ViewHolder. - * - * @return A ViewHolder if exists or null if it does not exist. - */ - ViewHolder getFromOldChangeHolders(long key) { - return mOldChangedHolders.get(key); - } - - /** - * Adds the item information to the post layout list - * @param holder The ViewHolder whose information is being saved - * @param info The information to save - */ - void addToPostLayout(ViewHolder holder, ItemHolderInfo info) { - InfoRecord record = mLayoutHolderMap.get(holder); - if (record == null) { - record = InfoRecord.obtain(); - mLayoutHolderMap.put(holder, record); - } - record.postInfo = info; - record.flags |= FLAG_POST; - } - - /** - * A ViewHolder might be added by the LayoutManager just to animate its disappearance. - * This list holds such items so that we can animate / recycle these ViewHolders properly. - * - * @param holder The ViewHolder which disappeared during a layout. - */ - void addToDisappearedInLayout(ViewHolder holder) { - InfoRecord record = mLayoutHolderMap.get(holder); - if (record == null) { - record = InfoRecord.obtain(); - mLayoutHolderMap.put(holder, record); - } - record.flags |= FLAG_DISAPPEARED; - } - - /** - * Removes a ViewHolder from disappearing list. - * @param holder The ViewHolder to be removed from the disappearing list. - */ - void removeFromDisappearedInLayout(ViewHolder holder) { - InfoRecord record = mLayoutHolderMap.get(holder); - if (record == null) { - return; - } - record.flags &= ~FLAG_DISAPPEARED; - } - - void process(ProcessCallback callback) { - for (int index = mLayoutHolderMap.size() - 1; index >= 0; index --) { - final ViewHolder viewHolder = mLayoutHolderMap.keyAt(index); - final InfoRecord record = mLayoutHolderMap.removeAt(index); - if ((record.flags & FLAG_APPEAR_AND_DISAPPEAR) == FLAG_APPEAR_AND_DISAPPEAR) { - // Appeared then disappeared. Not useful for animations. - callback.unused(viewHolder); - } else if ((record.flags & FLAG_DISAPPEARED) != 0) { - // Set as "disappeared" by the LayoutManager (addDisappearingView) - if (record.preInfo == null) { - // similar to appear disappear but happened between different layout passes. - // this can happen when the layout manager is using auto-measure - callback.unused(viewHolder); - } else { - callback.processDisappeared(viewHolder, record.preInfo, record.postInfo); - } - } else if ((record.flags & FLAG_APPEAR_PRE_AND_POST) == FLAG_APPEAR_PRE_AND_POST) { - // Appeared in the layout but not in the adapter (e.g. entered the viewport) - callback.processAppeared(viewHolder, record.preInfo, record.postInfo); - } else if ((record.flags & FLAG_PRE_AND_POST) == FLAG_PRE_AND_POST) { - // Persistent in both passes. Animate persistence - callback.processPersistent(viewHolder, record.preInfo, record.postInfo); - } else if ((record.flags & FLAG_PRE) != 0) { - // Was in pre-layout, never been added to post layout - callback.processDisappeared(viewHolder, record.preInfo, null); - } else if ((record.flags & FLAG_POST) != 0) { - // Was not in pre-layout, been added to post layout - callback.processAppeared(viewHolder, record.preInfo, record.postInfo); - } else if ((record.flags & FLAG_APPEAR) != 0) { - // Scrap view. RecyclerView will handle removing/recycling this. - } else if (DEBUG) { - throw new IllegalStateException("record without any reasonable flag combination:/"); - } - InfoRecord.recycle(record); - } - } - - /** - * Removes the ViewHolder from all list - * @param holder The ViewHolder which we should stop tracking - */ - void removeViewHolder(ViewHolder holder) { - for (int i = mOldChangedHolders.size() - 1; i >= 0; i--) { - if (holder == mOldChangedHolders.valueAt(i)) { - mOldChangedHolders.removeAt(i); - break; - } - } - final InfoRecord info = mLayoutHolderMap.remove(holder); - if (info != null) { - InfoRecord.recycle(info); - } - } - - void onDetach() { - InfoRecord.drainCache(); - } - - public void onViewDetached(ViewHolder viewHolder) { - removeFromDisappearedInLayout(viewHolder); - } - - interface ProcessCallback { - void processDisappeared(ViewHolder viewHolder, @NonNull ItemHolderInfo preInfo, - @Nullable ItemHolderInfo postInfo); - void processAppeared(ViewHolder viewHolder, @Nullable ItemHolderInfo preInfo, - ItemHolderInfo postInfo); - void processPersistent(ViewHolder viewHolder, @NonNull ItemHolderInfo preInfo, - @NonNull ItemHolderInfo postInfo); - void unused(ViewHolder holder); - } - - static class InfoRecord { - // disappearing list - static final int FLAG_DISAPPEARED = 1; - // appear in pre layout list - static final int FLAG_APPEAR = 1 << 1; - // pre layout, this is necessary to distinguish null item info - static final int FLAG_PRE = 1 << 2; - // post layout, this is necessary to distinguish null item info - static final int FLAG_POST = 1 << 3; - static final int FLAG_APPEAR_AND_DISAPPEAR = FLAG_APPEAR | FLAG_DISAPPEARED; - static final int FLAG_PRE_AND_POST = FLAG_PRE | FLAG_POST; - static final int FLAG_APPEAR_PRE_AND_POST = FLAG_APPEAR | FLAG_PRE | FLAG_POST; - int flags; - @Nullable ItemHolderInfo preInfo; - @Nullable ItemHolderInfo postInfo; - static Pools.Pool sPool = new Pools.SimplePool<>(20); - - private InfoRecord() { - } - - static InfoRecord obtain() { - InfoRecord record = sPool.acquire(); - return record == null ? new InfoRecord() : record; - } - - static void recycle(InfoRecord record) { - record.flags = 0; - record.preInfo = null; - record.postInfo = null; - sPool.release(record); - } - - static void drainCache() { - //noinspection StatementWithEmptyBody - while (sPool.acquire() != null); - } - } -} diff --git a/app/src/main/java/android/support/v7/widget/helper/ItemTouchHelper.java b/app/src/main/java/android/support/v7/widget/helper/ItemTouchHelper.java deleted file mode 100644 index 5bbc4587a4..0000000000 --- a/app/src/main/java/android/support/v7/widget/helper/ItemTouchHelper.java +++ /dev/null @@ -1,2408 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.widget.helper; - -import android.content.res.Resources; -import android.graphics.Canvas; -import android.graphics.Rect; -import android.os.Build; -import android.support.annotation.Nullable; -import android.support.v4.animation.AnimatorCompatHelper; -import android.support.v4.animation.AnimatorListenerCompat; -import android.support.v4.animation.AnimatorUpdateListenerCompat; -import android.support.v4.animation.ValueAnimatorCompat; -import android.support.v4.view.GestureDetectorCompat; -import android.support.v4.view.MotionEventCompat; -import android.support.v4.view.VelocityTrackerCompat; -import android.support.v4.view.ViewCompat; -import android.support.v7.recyclerview.R; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.support.v7.widget.RecyclerView.OnItemTouchListener; -import android.support.v7.widget.RecyclerView.ViewHolder; -import android.util.Log; -import android.view.GestureDetector; -import android.view.HapticFeedbackConstants; -import android.view.MotionEvent; -import android.view.VelocityTracker; -import android.view.View; -import android.view.ViewConfiguration; -import android.view.ViewParent; -import android.view.animation.Interpolator; - -import java.util.ArrayList; -import java.util.List; - -/** - * This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView. - *

        - * It works with a RecyclerView and a Callback class, which configures what type of interactions - * are enabled and also receives events when user performs these actions. - *

        - * Depending on which functionality you support, you should override - * {@link Callback#onMove(RecyclerView, ViewHolder, ViewHolder)} and / or - * {@link Callback#onSwiped(ViewHolder, int)}. - *

        - * This class is designed to work with any LayoutManager but for certain situations, it can be - * optimized for your custom LayoutManager by extending methods in the - * {@link ItemTouchHelper.Callback} class or implementing {@link ItemTouchHelper.ViewDropHandler} - * interface in your LayoutManager. - *

        - * By default, ItemTouchHelper moves the items' translateX/Y properties to reposition them. On - * platforms older than Honeycomb, ItemTouchHelper uses canvas translations and View's visibility - * property to move items in response to touch events. You can customize these behaviors by - * overriding {@link Callback#onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int, - * boolean)} - * or {@link Callback#onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, - * boolean)}. - *

        - * Most of the time, you only need to override onChildDraw but due to limitations of - * platform prior to Honeycomb, you may need to implement onChildDrawOver as well. - */ -public class ItemTouchHelper extends RecyclerView.ItemDecoration - implements RecyclerView.OnChildAttachStateChangeListener { - - /** - * Up direction, used for swipe & drag control. - */ - public static final int UP = 1; - - /** - * Down direction, used for swipe & drag control. - */ - public static final int DOWN = 1 << 1; - - /** - * Left direction, used for swipe & drag control. - */ - public static final int LEFT = 1 << 2; - - /** - * Right direction, used for swipe & drag control. - */ - public static final int RIGHT = 1 << 3; - - // If you change these relative direction values, update Callback#convertToAbsoluteDirection, - // Callback#convertToRelativeDirection. - /** - * Horizontal start direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout - * direction. Used for swipe & drag control. - */ - public static final int START = LEFT << 2; - - /** - * Horizontal end direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout - * direction. Used for swipe & drag control. - */ - public static final int END = RIGHT << 2; - - /** - * ItemTouchHelper is in idle state. At this state, either there is no related motion event by - * the user or latest motion events have not yet triggered a swipe or drag. - */ - public static final int ACTION_STATE_IDLE = 0; - - /** - * A View is currently being swiped. - */ - public static final int ACTION_STATE_SWIPE = 1; - - /** - * A View is currently being dragged. - */ - public static final int ACTION_STATE_DRAG = 2; - - /** - * Animation type for views which are swiped successfully. - */ - public static final int ANIMATION_TYPE_SWIPE_SUCCESS = 1 << 1; - - /** - * Animation type for views which are not completely swiped thus will animate back to their - * original position. - */ - public static final int ANIMATION_TYPE_SWIPE_CANCEL = 1 << 2; - - /** - * Animation type for views that were dragged and now will animate to their final position. - */ - public static final int ANIMATION_TYPE_DRAG = 1 << 3; - - private static final String TAG = "ItemTouchHelper"; - - private static final boolean DEBUG = false; - - private static final int ACTIVE_POINTER_ID_NONE = -1; - - private static final int DIRECTION_FLAG_COUNT = 8; - - private static final int ACTION_MODE_IDLE_MASK = (1 << DIRECTION_FLAG_COUNT) - 1; - - private static final int ACTION_MODE_SWIPE_MASK = ACTION_MODE_IDLE_MASK << DIRECTION_FLAG_COUNT; - - private static final int ACTION_MODE_DRAG_MASK = ACTION_MODE_SWIPE_MASK << DIRECTION_FLAG_COUNT; - - /** - * The unit we are using to track velocity - */ - private static final int PIXELS_PER_SECOND = 1000; - - /** - * Views, whose state should be cleared after they are detached from RecyclerView. - * This is necessary after swipe dismissing an item. We wait until animator finishes its job - * to clean these views. - */ - final List mPendingCleanup = new ArrayList(); - - /** - * Re-use array to calculate dx dy for a ViewHolder - */ - private final float[] mTmpPosition = new float[2]; - - /** - * Currently selected view holder - */ - ViewHolder mSelected = null; - - /** - * The reference coordinates for the action start. For drag & drop, this is the time long - * press is completed vs for swipe, this is the initial touch point. - */ - float mInitialTouchX; - - float mInitialTouchY; - - /** - * Set when ItemTouchHelper is assigned to a RecyclerView. - */ - float mSwipeEscapeVelocity; - - /** - * Set when ItemTouchHelper is assigned to a RecyclerView. - */ - float mMaxSwipeVelocity; - - /** - * The diff between the last event and initial touch. - */ - float mDx; - - float mDy; - - /** - * The coordinates of the selected view at the time it is selected. We record these values - * when action starts so that we can consistently position it even if LayoutManager moves the - * View. - */ - float mSelectedStartX; - - float mSelectedStartY; - - /** - * The pointer we are tracking. - */ - int mActivePointerId = ACTIVE_POINTER_ID_NONE; - - /** - * Developer callback which controls the behavior of ItemTouchHelper. - */ - Callback mCallback; - - /** - * Current mode. - */ - int mActionState = ACTION_STATE_IDLE; - - /** - * The direction flags obtained from unmasking - * {@link Callback#getAbsoluteMovementFlags(RecyclerView, ViewHolder)} for the current - * action state. - */ - int mSelectedFlags; - - /** - * When a View is dragged or swiped and needs to go back to where it was, we create a Recover - * Animation and animate it to its location using this custom Animator, instead of using - * framework Animators. - * Using framework animators has the side effect of clashing with ItemAnimator, creating - * jumpy UIs. - */ - List mRecoverAnimations = new ArrayList(); - - private int mSlop; - - private RecyclerView mRecyclerView; - - /** - * When user drags a view to the edge, we start scrolling the LayoutManager as long as View - * is partially out of bounds. - */ - private final Runnable mScrollRunnable = new Runnable() { - @Override - public void run() { - if (mSelected != null && scrollIfNecessary()) { - if (mSelected != null) { //it might be lost during scrolling - moveIfNecessary(mSelected); - } - mRecyclerView.removeCallbacks(mScrollRunnable); - ViewCompat.postOnAnimation(mRecyclerView, this); - } - } - }; - - /** - * Used for detecting fling swipe - */ - private VelocityTracker mVelocityTracker; - - //re-used list for selecting a swap target - private List mSwapTargets; - - //re used for for sorting swap targets - private List mDistances; - - /** - * If drag & drop is supported, we use child drawing order to bring them to front. - */ - private RecyclerView.ChildDrawingOrderCallback mChildDrawingOrderCallback = null; - - /** - * This keeps a reference to the child dragged by the user. Even after user stops dragging, - * until view reaches its final position (end of recover animation), we keep a reference so - * that it can be drawn above other children. - */ - private View mOverdrawChild = null; - - /** - * We cache the position of the overdraw child to avoid recalculating it each time child - * position callback is called. This value is invalidated whenever a child is attached or - * detached. - */ - private int mOverdrawChildPosition = -1; - - /** - * Used to detect long press. - */ - private GestureDetectorCompat mGestureDetector; - - private final OnItemTouchListener mOnItemTouchListener - = new OnItemTouchListener() { - @Override - public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) { - mGestureDetector.onTouchEvent(event); - if (DEBUG) { - Log.d(TAG, "intercept: x:" + event.getX() + ",y:" + event.getY() + ", " + event); - } - final int action = MotionEventCompat.getActionMasked(event); - if (action == MotionEvent.ACTION_DOWN) { - mActivePointerId = event.getPointerId(0); - mInitialTouchX = event.getX(); - mInitialTouchY = event.getY(); - obtainVelocityTracker(); - if (mSelected == null) { - final RecoverAnimation animation = findAnimation(event); - if (animation != null) { - mInitialTouchX -= animation.mX; - mInitialTouchY -= animation.mY; - endRecoverAnimation(animation.mViewHolder, true); - if (mPendingCleanup.remove(animation.mViewHolder.itemView)) { - mCallback.clearView(mRecyclerView, animation.mViewHolder); - } - select(animation.mViewHolder, animation.mActionState); - updateDxDy(event, mSelectedFlags, 0); - } - } - } else if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { - mActivePointerId = ACTIVE_POINTER_ID_NONE; - select(null, ACTION_STATE_IDLE); - } else if (mActivePointerId != ACTIVE_POINTER_ID_NONE) { - // in a non scroll orientation, if distance change is above threshold, we - // can select the item - final int index = event.findPointerIndex(mActivePointerId); - if (DEBUG) { - Log.d(TAG, "pointer index " + index); - } - if (index >= 0) { - checkSelectForSwipe(action, event, index); - } - } - if (mVelocityTracker != null) { - mVelocityTracker.addMovement(event); - } - return mSelected != null; - } - - @Override - public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) { - mGestureDetector.onTouchEvent(event); - if (DEBUG) { - Log.d(TAG, - "on touch: x:" + mInitialTouchX + ",y:" + mInitialTouchY + ", :" + event); - } - if (mVelocityTracker != null) { - mVelocityTracker.addMovement(event); - } - if (mActivePointerId == ACTIVE_POINTER_ID_NONE) { - return; - } - final int action = MotionEventCompat.getActionMasked(event); - final int activePointerIndex = event.findPointerIndex(mActivePointerId); - if (activePointerIndex >= 0) { - checkSelectForSwipe(action, event, activePointerIndex); - } - ViewHolder viewHolder = mSelected; - if (viewHolder == null) { - return; - } - switch (action) { - case MotionEvent.ACTION_MOVE: { - // Find the index of the active pointer and fetch its position - if (activePointerIndex >= 0) { - updateDxDy(event, mSelectedFlags, activePointerIndex); - moveIfNecessary(viewHolder); - mRecyclerView.removeCallbacks(mScrollRunnable); - mScrollRunnable.run(); - mRecyclerView.invalidate(); - } - break; - } - case MotionEvent.ACTION_CANCEL: - if (mVelocityTracker != null) { - mVelocityTracker.clear(); - } - // fall through - case MotionEvent.ACTION_UP: - select(null, ACTION_STATE_IDLE); - mActivePointerId = ACTIVE_POINTER_ID_NONE; - break; - case MotionEvent.ACTION_POINTER_UP: { - final int pointerIndex = MotionEventCompat.getActionIndex(event); - final int pointerId = event.getPointerId(pointerIndex); - if (pointerId == mActivePointerId) { - // This was our active pointer going up. Choose a new - // active pointer and adjust accordingly. - final int newPointerIndex = pointerIndex == 0 ? 1 : 0; - mActivePointerId = event.getPointerId(newPointerIndex); - updateDxDy(event, mSelectedFlags, pointerIndex); - } - break; - } - } - } - - @Override - public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { - if (!disallowIntercept) { - return; - } - select(null, ACTION_STATE_IDLE); - } - }; - - /** - * Temporary rect instance that is used when we need to lookup Item decorations. - */ - private Rect mTmpRect; - - /** - * When user started to drag scroll. Reset when we don't scroll - */ - private long mDragScrollStartTimeInMs; - - /** - * Creates an ItemTouchHelper that will work with the given Callback. - *

        - * You can attach ItemTouchHelper to a RecyclerView via - * {@link #attachToRecyclerView(RecyclerView)}. Upon attaching, it will add an item decoration, - * an onItemTouchListener and a Child attach / detach listener to the RecyclerView. - * - * @param callback The Callback which controls the behavior of this touch helper. - */ - public ItemTouchHelper(Callback callback) { - mCallback = callback; - } - - private static boolean hitTest(View child, float x, float y, float left, float top) { - return x >= left && - x <= left + child.getWidth() && - y >= top && - y <= top + child.getHeight(); - } - - /** - * Attaches the ItemTouchHelper to the provided RecyclerView. If TouchHelper is already - * attached to a RecyclerView, it will first detach from the previous one. You can call this - * method with {@code null} to detach it from the current RecyclerView. - * - * @param recyclerView The RecyclerView instance to which you want to add this helper or - * {@code null} if you want to remove ItemTouchHelper from the current - * RecyclerView. - */ - public void attachToRecyclerView(@Nullable RecyclerView recyclerView) { - if (mRecyclerView == recyclerView) { - return; // nothing to do - } - if (mRecyclerView != null) { - destroyCallbacks(); - } - mRecyclerView = recyclerView; - if (mRecyclerView != null) { - final Resources resources = recyclerView.getResources(); - mSwipeEscapeVelocity = resources - .getDimension(R.dimen.item_touch_helper_swipe_escape_velocity); - mMaxSwipeVelocity = resources - .getDimension(R.dimen.item_touch_helper_swipe_escape_max_velocity); - setupCallbacks(); - } - } - - private void setupCallbacks() { - ViewConfiguration vc = ViewConfiguration.get(mRecyclerView.getContext()); - mSlop = vc.getScaledTouchSlop(); - mRecyclerView.addItemDecoration(this); - mRecyclerView.addOnItemTouchListener(mOnItemTouchListener); - mRecyclerView.addOnChildAttachStateChangeListener(this); - initGestureDetector(); - } - - private void destroyCallbacks() { - mRecyclerView.removeItemDecoration(this); - mRecyclerView.removeOnItemTouchListener(mOnItemTouchListener); - mRecyclerView.removeOnChildAttachStateChangeListener(this); - // clean all attached - final int recoverAnimSize = mRecoverAnimations.size(); - for (int i = recoverAnimSize - 1; i >= 0; i--) { - final RecoverAnimation recoverAnimation = mRecoverAnimations.get(0); - mCallback.clearView(mRecyclerView, recoverAnimation.mViewHolder); - } - mRecoverAnimations.clear(); - mOverdrawChild = null; - mOverdrawChildPosition = -1; - releaseVelocityTracker(); - } - - private void initGestureDetector() { - if (mGestureDetector != null) { - return; - } - mGestureDetector = new GestureDetectorCompat(mRecyclerView.getContext(), - new ItemTouchHelperGestureListener()); - } - - private void getSelectedDxDy(float[] outPosition) { - if ((mSelectedFlags & (LEFT | RIGHT)) != 0) { - outPosition[0] = mSelectedStartX + mDx - mSelected.itemView.getLeft(); - } else { - outPosition[0] = ViewCompat.getTranslationX(mSelected.itemView); - } - if ((mSelectedFlags & (UP | DOWN)) != 0) { - outPosition[1] = mSelectedStartY + mDy - mSelected.itemView.getTop(); - } else { - outPosition[1] = ViewCompat.getTranslationY(mSelected.itemView); - } - } - - @Override - public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { - float dx = 0, dy = 0; - if (mSelected != null) { - getSelectedDxDy(mTmpPosition); - dx = mTmpPosition[0]; - dy = mTmpPosition[1]; - } - mCallback.onDrawOver(c, parent, mSelected, - mRecoverAnimations, mActionState, dx, dy); - } - - @Override - public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { - // we don't know if RV changed something so we should invalidate this index. - mOverdrawChildPosition = -1; - float dx = 0, dy = 0; - if (mSelected != null) { - getSelectedDxDy(mTmpPosition); - dx = mTmpPosition[0]; - dy = mTmpPosition[1]; - } - mCallback.onDraw(c, parent, mSelected, - mRecoverAnimations, mActionState, dx, dy); - } - - /** - * Starts dragging or swiping the given View. Call with null if you want to clear it. - * - * @param selected The ViewHolder to drag or swipe. Can be null if you want to cancel the - * current action - * @param actionState The type of action - */ - private void select(ViewHolder selected, int actionState) { - if (selected == mSelected && actionState == mActionState) { - return; - } - mDragScrollStartTimeInMs = Long.MIN_VALUE; - final int prevActionState = mActionState; - // prevent duplicate animations - endRecoverAnimation(selected, true); - mActionState = actionState; - if (actionState == ACTION_STATE_DRAG) { - // we remove after animation is complete. this means we only elevate the last drag - // child but that should perform good enough as it is very hard to start dragging a - // new child before the previous one settles. - mOverdrawChild = selected.itemView; - addChildDrawingOrderCallback(); - } - int actionStateMask = (1 << (DIRECTION_FLAG_COUNT + DIRECTION_FLAG_COUNT * actionState)) - - 1; - boolean preventLayout = false; - - if (mSelected != null) { - final ViewHolder prevSelected = mSelected; - if (prevSelected.itemView.getParent() != null) { - final int swipeDir = prevActionState == ACTION_STATE_DRAG ? 0 - : swipeIfNecessary(prevSelected); - releaseVelocityTracker(); - // find where we should animate to - final float targetTranslateX, targetTranslateY; - int animationType; - switch (swipeDir) { - case LEFT: - case RIGHT: - case START: - case END: - targetTranslateY = 0; - targetTranslateX = Math.signum(mDx) * mRecyclerView.getWidth(); - break; - case UP: - case DOWN: - targetTranslateX = 0; - targetTranslateY = Math.signum(mDy) * mRecyclerView.getHeight(); - break; - default: - targetTranslateX = 0; - targetTranslateY = 0; - } - if (prevActionState == ACTION_STATE_DRAG) { - animationType = ANIMATION_TYPE_DRAG; - } else if (swipeDir > 0) { - animationType = ANIMATION_TYPE_SWIPE_SUCCESS; - } else { - animationType = ANIMATION_TYPE_SWIPE_CANCEL; - } - getSelectedDxDy(mTmpPosition); - final float currentTranslateX = mTmpPosition[0]; - final float currentTranslateY = mTmpPosition[1]; - final RecoverAnimation rv = new RecoverAnimation(prevSelected, animationType, - prevActionState, currentTranslateX, currentTranslateY, - targetTranslateX, targetTranslateY) { - @Override - public void onAnimationEnd(ValueAnimatorCompat animation) { - super.onAnimationEnd(animation); - if (this.mOverridden) { - return; - } - if (swipeDir <= 0) { - // this is a drag or failed swipe. recover immediately - mCallback.clearView(mRecyclerView, prevSelected); - // full cleanup will happen on onDrawOver - } else { - // wait until remove animation is complete. - mPendingCleanup.add(prevSelected.itemView); - mIsPendingCleanup = true; - if (swipeDir > 0) { - // Animation might be ended by other animators during a layout. - // We defer callback to avoid editing adapter during a layout. - postDispatchSwipe(this, swipeDir); - } - } - // removed from the list after it is drawn for the last time - if (mOverdrawChild == prevSelected.itemView) { - removeChildDrawingOrderCallbackIfNecessary(prevSelected.itemView); - } - } - }; - final long duration = mCallback.getAnimationDuration(mRecyclerView, animationType, - targetTranslateX - currentTranslateX, targetTranslateY - currentTranslateY); - rv.setDuration(duration); - mRecoverAnimations.add(rv); - rv.start(); - preventLayout = true; - } else { - removeChildDrawingOrderCallbackIfNecessary(prevSelected.itemView); - mCallback.clearView(mRecyclerView, prevSelected); - } - mSelected = null; - } - if (selected != null) { - mSelectedFlags = - (mCallback.getAbsoluteMovementFlags(mRecyclerView, selected) & actionStateMask) - >> (mActionState * DIRECTION_FLAG_COUNT); - mSelectedStartX = selected.itemView.getLeft(); - mSelectedStartY = selected.itemView.getTop(); - mSelected = selected; - - if (actionState == ACTION_STATE_DRAG) { - mSelected.itemView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); - } - } - final ViewParent rvParent = mRecyclerView.getParent(); - if (rvParent != null) { - rvParent.requestDisallowInterceptTouchEvent(mSelected != null); - } - if (!preventLayout) { - mRecyclerView.getLayoutManager().requestSimpleAnimationsInNextLayout(); - } - mCallback.onSelectedChanged(mSelected, mActionState); - mRecyclerView.invalidate(); - } - - private void postDispatchSwipe(final RecoverAnimation anim, final int swipeDir) { - // wait until animations are complete. - mRecyclerView.post(new Runnable() { - @Override - public void run() { - if (mRecyclerView != null && mRecyclerView.isAttachedToWindow() && - !anim.mOverridden && - anim.mViewHolder.getAdapterPosition() != RecyclerView.NO_POSITION) { - final RecyclerView.ItemAnimator animator = mRecyclerView.getItemAnimator(); - // if animator is running or we have other active recover animations, we try - // not to call onSwiped because DefaultItemAnimator is not good at merging - // animations. Instead, we wait and batch. - if ((animator == null || !animator.isRunning(null)) - && !hasRunningRecoverAnim()) { - mCallback.onSwiped(anim.mViewHolder, swipeDir); - } else { - mRecyclerView.post(this); - } - } - } - }); - } - - private boolean hasRunningRecoverAnim() { - final int size = mRecoverAnimations.size(); - for (int i = 0; i < size; i++) { - if (!mRecoverAnimations.get(i).mEnded) { - return true; - } - } - return false; - } - - /** - * If user drags the view to the edge, trigger a scroll if necessary. - */ - private boolean scrollIfNecessary() { - if (mSelected == null) { - mDragScrollStartTimeInMs = Long.MIN_VALUE; - return false; - } - final long now = System.currentTimeMillis(); - final long scrollDuration = mDragScrollStartTimeInMs - == Long.MIN_VALUE ? 0 : now - mDragScrollStartTimeInMs; - RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager(); - if (mTmpRect == null) { - mTmpRect = new Rect(); - } - int scrollX = 0; - int scrollY = 0; - lm.calculateItemDecorationsForChild(mSelected.itemView, mTmpRect); - if (lm.canScrollHorizontally()) { - int curX = (int) (mSelectedStartX + mDx); - final int leftDiff = curX - mTmpRect.left - mRecyclerView.getPaddingLeft(); - if (mDx < 0 && leftDiff < 0) { - scrollX = leftDiff; - } else if (mDx > 0) { - final int rightDiff = - curX + mSelected.itemView.getWidth() + mTmpRect.right - - (mRecyclerView.getWidth() - mRecyclerView.getPaddingRight()); - if (rightDiff > 0) { - scrollX = rightDiff; - } - } - } - if (lm.canScrollVertically()) { - int curY = (int) (mSelectedStartY + mDy); - final int topDiff = curY - mTmpRect.top - mRecyclerView.getPaddingTop(); - if (mDy < 0 && topDiff < 0) { - scrollY = topDiff; - } else if (mDy > 0) { - final int bottomDiff = curY + mSelected.itemView.getHeight() + mTmpRect.bottom - - (mRecyclerView.getHeight() - mRecyclerView.getPaddingBottom()); - if (bottomDiff > 0) { - scrollY = bottomDiff; - } - } - } - if (scrollX != 0) { - scrollX = mCallback.interpolateOutOfBoundsScroll(mRecyclerView, - mSelected.itemView.getWidth(), scrollX, - mRecyclerView.getWidth(), scrollDuration); - } - if (scrollY != 0) { - scrollY = mCallback.interpolateOutOfBoundsScroll(mRecyclerView, - mSelected.itemView.getHeight(), scrollY, - mRecyclerView.getHeight(), scrollDuration); - } - if (scrollX != 0 || scrollY != 0) { - if (mDragScrollStartTimeInMs == Long.MIN_VALUE) { - mDragScrollStartTimeInMs = now; - } - mRecyclerView.scrollBy(scrollX, scrollY); - return true; - } - mDragScrollStartTimeInMs = Long.MIN_VALUE; - return false; - } - - private List findSwapTargets(ViewHolder viewHolder) { - if (mSwapTargets == null) { - mSwapTargets = new ArrayList(); - mDistances = new ArrayList(); - } else { - mSwapTargets.clear(); - mDistances.clear(); - } - final int margin = mCallback.getBoundingBoxMargin(); - final int left = Math.round(mSelectedStartX + mDx) - margin; - final int top = Math.round(mSelectedStartY + mDy) - margin; - final int right = left + viewHolder.itemView.getWidth() + 2 * margin; - final int bottom = top + viewHolder.itemView.getHeight() + 2 * margin; - final int centerX = (left + right) / 2; - final int centerY = (top + bottom) / 2; - final RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager(); - final int childCount = lm.getChildCount(); - for (int i = 0; i < childCount; i++) { - View other = lm.getChildAt(i); - if (other == viewHolder.itemView) { - continue;//myself! - } - if (other.getBottom() < top || other.getTop() > bottom - || other.getRight() < left || other.getLeft() > right) { - continue; - } - final ViewHolder otherVh = mRecyclerView.getChildViewHolder(other); - if (mCallback.canDropOver(mRecyclerView, mSelected, otherVh)) { - // find the index to add - final int dx = Math.abs(centerX - (other.getLeft() + other.getRight()) / 2); - final int dy = Math.abs(centerY - (other.getTop() + other.getBottom()) / 2); - final int dist = dx * dx + dy * dy; - - int pos = 0; - final int cnt = mSwapTargets.size(); - for (int j = 0; j < cnt; j++) { - if (dist > mDistances.get(j)) { - pos++; - } else { - break; - } - } - mSwapTargets.add(pos, otherVh); - mDistances.add(pos, dist); - } - } - return mSwapTargets; - } - - /** - * Checks if we should swap w/ another view holder. - */ - private void moveIfNecessary(ViewHolder viewHolder) { - if (mRecyclerView.isLayoutRequested()) { - return; - } - if (mActionState != ACTION_STATE_DRAG) { - return; - } - - final float threshold = mCallback.getMoveThreshold(viewHolder); - final int x = (int) (mSelectedStartX + mDx); - final int y = (int) (mSelectedStartY + mDy); - if (Math.abs(y - viewHolder.itemView.getTop()) < viewHolder.itemView.getHeight() * threshold - && Math.abs(x - viewHolder.itemView.getLeft()) - < viewHolder.itemView.getWidth() * threshold) { - return; - } - List swapTargets = findSwapTargets(viewHolder); - if (swapTargets.size() == 0) { - return; - } - // may swap. - ViewHolder target = mCallback.chooseDropTarget(viewHolder, swapTargets, x, y); - if (target == null) { - mSwapTargets.clear(); - mDistances.clear(); - return; - } - final int toPosition = target.getAdapterPosition(); - final int fromPosition = viewHolder.getAdapterPosition(); - if (mCallback.onMove(mRecyclerView, viewHolder, target)) { - // keep target visible - mCallback.onMoved(mRecyclerView, viewHolder, fromPosition, - target, toPosition, x, y); - } - } - - @Override - public void onChildViewAttachedToWindow(View view) { - } - - @Override - public void onChildViewDetachedFromWindow(View view) { - removeChildDrawingOrderCallbackIfNecessary(view); - final ViewHolder holder = mRecyclerView.getChildViewHolder(view); - if (holder == null) { - return; - } - if (mSelected != null && holder == mSelected) { - select(null, ACTION_STATE_IDLE); - } else { - endRecoverAnimation(holder, false); // this may push it into pending cleanup list. - if (mPendingCleanup.remove(holder.itemView)) { - mCallback.clearView(mRecyclerView, holder); - } - } - } - - /** - * Returns the animation type or 0 if cannot be found. - */ - private int endRecoverAnimation(ViewHolder viewHolder, boolean override) { - final int recoverAnimSize = mRecoverAnimations.size(); - for (int i = recoverAnimSize - 1; i >= 0; i--) { - final RecoverAnimation anim = mRecoverAnimations.get(i); - if (anim.mViewHolder == viewHolder) { - anim.mOverridden |= override; - if (!anim.mEnded) { - anim.cancel(); - } - mRecoverAnimations.remove(i); - return anim.mAnimationType; - } - } - return 0; - } - - @Override - public void getItemOffsets(Rect outRect, View view, RecyclerView parent, - RecyclerView.State state) { - outRect.setEmpty(); - } - - private void obtainVelocityTracker() { - if (mVelocityTracker != null) { - mVelocityTracker.recycle(); - } - mVelocityTracker = VelocityTracker.obtain(); - } - - private void releaseVelocityTracker() { - if (mVelocityTracker != null) { - mVelocityTracker.recycle(); - mVelocityTracker = null; - } - } - - private ViewHolder findSwipedView(MotionEvent motionEvent) { - final RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager(); - if (mActivePointerId == ACTIVE_POINTER_ID_NONE) { - return null; - } - final int pointerIndex = motionEvent.findPointerIndex(mActivePointerId); - final float dx = motionEvent.getX(pointerIndex) - mInitialTouchX; - final float dy = motionEvent.getY(pointerIndex) - mInitialTouchY; - final float absDx = Math.abs(dx); - final float absDy = Math.abs(dy); - - if (absDx < mSlop && absDy < mSlop) { - return null; - } - if (absDx > absDy && lm.canScrollHorizontally()) { - return null; - } else if (absDy > absDx && lm.canScrollVertically()) { - return null; - } - View child = findChildView(motionEvent); - if (child == null) { - return null; - } - return mRecyclerView.getChildViewHolder(child); - } - - /** - * Checks whether we should select a View for swiping. - */ - private boolean checkSelectForSwipe(int action, MotionEvent motionEvent, int pointerIndex) { - if (mSelected != null || action != MotionEvent.ACTION_MOVE - || mActionState == ACTION_STATE_DRAG || !mCallback.isItemViewSwipeEnabled()) { - return false; - } - if (mRecyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) { - return false; - } - final ViewHolder vh = findSwipedView(motionEvent); - if (vh == null) { - return false; - } - final int movementFlags = mCallback.getAbsoluteMovementFlags(mRecyclerView, vh); - - final int swipeFlags = (movementFlags & ACTION_MODE_SWIPE_MASK) - >> (DIRECTION_FLAG_COUNT * ACTION_STATE_SWIPE); - - if (swipeFlags == 0) { - return false; - } - - // mDx and mDy are only set in allowed directions. We use custom x/y here instead of - // updateDxDy to avoid swiping if user moves more in the other direction - final float x = motionEvent.getX(pointerIndex); - final float y = motionEvent.getY(pointerIndex); - - // Calculate the distance moved - final float dx = x - mInitialTouchX; - final float dy = y - mInitialTouchY; - // swipe target is chose w/o applying flags so it does not really check if swiping in that - // direction is allowed. This why here, we use mDx mDy to check slope value again. - final float absDx = Math.abs(dx); - final float absDy = Math.abs(dy); - - if (absDx < mSlop && absDy < mSlop) { - return false; - } - if (absDx > absDy) { - if (dx < 0 && (swipeFlags & LEFT) == 0) { - return false; - } - if (dx > 0 && (swipeFlags & RIGHT) == 0) { - return false; - } - } else { - if (dy < 0 && (swipeFlags & UP) == 0) { - return false; - } - if (dy > 0 && (swipeFlags & DOWN) == 0) { - return false; - } - } - mDx = mDy = 0f; - mActivePointerId = motionEvent.getPointerId(0); - select(vh, ACTION_STATE_SWIPE); - return true; - } - - private View findChildView(MotionEvent event) { - // first check elevated views, if none, then call RV - final float x = event.getX(); - final float y = event.getY(); - if (mSelected != null) { - final View selectedView = mSelected.itemView; - if (hitTest(selectedView, x, y, mSelectedStartX + mDx, mSelectedStartY + mDy)) { - return selectedView; - } - } - for (int i = mRecoverAnimations.size() - 1; i >= 0; i--) { - final RecoverAnimation anim = mRecoverAnimations.get(i); - final View view = anim.mViewHolder.itemView; - if (hitTest(view, x, y, anim.mX, anim.mY)) { - return view; - } - } - return mRecyclerView.findChildViewUnder(x, y); - } - - /** - * Starts dragging the provided ViewHolder. By default, ItemTouchHelper starts a drag when a - * View is long pressed. You can disable that behavior by overriding - * {@link ItemTouchHelper.Callback#isLongPressDragEnabled()}. - *

        - * For this method to work: - *

          - *
        • The provided ViewHolder must be a child of the RecyclerView to which this - * ItemTouchHelper - * is attached.
        • - *
        • {@link ItemTouchHelper.Callback} must have dragging enabled.
        • - *
        • There must be a previous touch event that was reported to the ItemTouchHelper - * through RecyclerView's ItemTouchListener mechanism. As long as no other ItemTouchListener - * grabs previous events, this should work as expected.
        • - *
        - * - * For example, if you would like to let your user to be able to drag an Item by touching one - * of its descendants, you may implement it as follows: - *
        -     *     viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() {
        -     *         public boolean onTouch(View v, MotionEvent event) {
        -     *             if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
        -     *                 mItemTouchHelper.startDrag(viewHolder);
        -     *             }
        -     *             return false;
        -     *         }
        -     *     });
        -     * 
        - *

        - * - * @param viewHolder The ViewHolder to start dragging. It must be a direct child of - * RecyclerView. - * @see ItemTouchHelper.Callback#isItemViewSwipeEnabled() - */ - public void startDrag(ViewHolder viewHolder) { - if (!mCallback.hasDragFlag(mRecyclerView, viewHolder)) { - Log.e(TAG, "Start drag has been called but swiping is not enabled"); - return; - } - if (viewHolder.itemView.getParent() != mRecyclerView) { - Log.e(TAG, "Start drag has been called with a view holder which is not a child of " - + "the RecyclerView which is controlled by this ItemTouchHelper."); - return; - } - obtainVelocityTracker(); - mDx = mDy = 0f; - select(viewHolder, ACTION_STATE_DRAG); - } - - /** - * Starts swiping the provided ViewHolder. By default, ItemTouchHelper starts swiping a View - * when user swipes their finger (or mouse pointer) over the View. You can disable this - * behavior - * by overriding {@link ItemTouchHelper.Callback} - *

        - * For this method to work: - *

          - *
        • The provided ViewHolder must be a child of the RecyclerView to which this - * ItemTouchHelper is attached.
        • - *
        • {@link ItemTouchHelper.Callback} must have swiping enabled.
        • - *
        • There must be a previous touch event that was reported to the ItemTouchHelper - * through RecyclerView's ItemTouchListener mechanism. As long as no other ItemTouchListener - * grabs previous events, this should work as expected.
        • - *
        - * - * For example, if you would like to let your user to be able to swipe an Item by touching one - * of its descendants, you may implement it as follows: - *
        -     *     viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() {
        -     *         public boolean onTouch(View v, MotionEvent event) {
        -     *             if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
        -     *                 mItemTouchHelper.startSwipe(viewHolder);
        -     *             }
        -     *             return false;
        -     *         }
        -     *     });
        -     * 
        - * - * @param viewHolder The ViewHolder to start swiping. It must be a direct child of - * RecyclerView. - */ - public void startSwipe(ViewHolder viewHolder) { - if (!mCallback.hasSwipeFlag(mRecyclerView, viewHolder)) { - Log.e(TAG, "Start swipe has been called but dragging is not enabled"); - return; - } - if (viewHolder.itemView.getParent() != mRecyclerView) { - Log.e(TAG, "Start swipe has been called with a view holder which is not a child of " - + "the RecyclerView controlled by this ItemTouchHelper."); - return; - } - obtainVelocityTracker(); - mDx = mDy = 0f; - select(viewHolder, ACTION_STATE_SWIPE); - } - - private RecoverAnimation findAnimation(MotionEvent event) { - if (mRecoverAnimations.isEmpty()) { - return null; - } - View target = findChildView(event); - for (int i = mRecoverAnimations.size() - 1; i >= 0; i--) { - final RecoverAnimation anim = mRecoverAnimations.get(i); - if (anim.mViewHolder.itemView == target) { - return anim; - } - } - return null; - } - - private void updateDxDy(MotionEvent ev, int directionFlags, int pointerIndex) { - final float x = ev.getX(pointerIndex); - final float y = ev.getY(pointerIndex); - - // Calculate the distance moved - mDx = x - mInitialTouchX; - mDy = y - mInitialTouchY; - if ((directionFlags & LEFT) == 0) { - mDx = Math.max(0, mDx); - } - if ((directionFlags & RIGHT) == 0) { - mDx = Math.min(0, mDx); - } - if ((directionFlags & UP) == 0) { - mDy = Math.max(0, mDy); - } - if ((directionFlags & DOWN) == 0) { - mDy = Math.min(0, mDy); - } - } - - private int swipeIfNecessary(ViewHolder viewHolder) { - if (mActionState == ACTION_STATE_DRAG) { - return 0; - } - final int originalMovementFlags = mCallback.getMovementFlags(mRecyclerView, viewHolder); - final int absoluteMovementFlags = mCallback.convertToAbsoluteDirection( - originalMovementFlags, - ViewCompat.getLayoutDirection(mRecyclerView)); - final int flags = (absoluteMovementFlags - & ACTION_MODE_SWIPE_MASK) >> (ACTION_STATE_SWIPE * DIRECTION_FLAG_COUNT); - if (flags == 0) { - return 0; - } - final int originalFlags = (originalMovementFlags - & ACTION_MODE_SWIPE_MASK) >> (ACTION_STATE_SWIPE * DIRECTION_FLAG_COUNT); - int swipeDir; - if (Math.abs(mDx) > Math.abs(mDy)) { - if ((swipeDir = checkHorizontalSwipe(viewHolder, flags)) > 0) { - // if swipe dir is not in original flags, it should be the relative direction - if ((originalFlags & swipeDir) == 0) { - // convert to relative - return Callback.convertToRelativeDirection(swipeDir, - ViewCompat.getLayoutDirection(mRecyclerView)); - } - return swipeDir; - } - if ((swipeDir = checkVerticalSwipe(viewHolder, flags)) > 0) { - return swipeDir; - } - } else { - if ((swipeDir = checkVerticalSwipe(viewHolder, flags)) > 0) { - return swipeDir; - } - if ((swipeDir = checkHorizontalSwipe(viewHolder, flags)) > 0) { - // if swipe dir is not in original flags, it should be the relative direction - if ((originalFlags & swipeDir) == 0) { - // convert to relative - return Callback.convertToRelativeDirection(swipeDir, - ViewCompat.getLayoutDirection(mRecyclerView)); - } - return swipeDir; - } - } - return 0; - } - - private int checkHorizontalSwipe(ViewHolder viewHolder, int flags) { - if ((flags & (LEFT | RIGHT)) != 0) { - final int dirFlag = mDx > 0 ? RIGHT : LEFT; - if (mVelocityTracker != null && mActivePointerId > -1) { - mVelocityTracker.computeCurrentVelocity(PIXELS_PER_SECOND, - mCallback.getSwipeVelocityThreshold(mMaxSwipeVelocity)); - final float xVelocity = VelocityTrackerCompat - .getXVelocity(mVelocityTracker, mActivePointerId); - final float yVelocity = VelocityTrackerCompat - .getYVelocity(mVelocityTracker, mActivePointerId); - final int velDirFlag = xVelocity > 0f ? RIGHT : LEFT; - final float absXVelocity = Math.abs(xVelocity); - if ((velDirFlag & flags) != 0 && dirFlag == velDirFlag && - absXVelocity >= mCallback.getSwipeEscapeVelocity(mSwipeEscapeVelocity) && - absXVelocity > Math.abs(yVelocity)) { - return velDirFlag; - } - } - - final float threshold = mRecyclerView.getWidth() * mCallback - .getSwipeThreshold(viewHolder); - - if ((flags & dirFlag) != 0 && Math.abs(mDx) > threshold) { - return dirFlag; - } - } - return 0; - } - - private int checkVerticalSwipe(ViewHolder viewHolder, int flags) { - if ((flags & (UP | DOWN)) != 0) { - final int dirFlag = mDy > 0 ? DOWN : UP; - if (mVelocityTracker != null && mActivePointerId > -1) { - mVelocityTracker.computeCurrentVelocity(PIXELS_PER_SECOND, - mCallback.getSwipeVelocityThreshold(mMaxSwipeVelocity)); - final float xVelocity = VelocityTrackerCompat - .getXVelocity(mVelocityTracker, mActivePointerId); - final float yVelocity = VelocityTrackerCompat - .getYVelocity(mVelocityTracker, mActivePointerId); - final int velDirFlag = yVelocity > 0f ? DOWN : UP; - final float absYVelocity = Math.abs(yVelocity); - if ((velDirFlag & flags) != 0 && velDirFlag == dirFlag && - absYVelocity >= mCallback.getSwipeEscapeVelocity(mSwipeEscapeVelocity) && - absYVelocity > Math.abs(xVelocity)) { - return velDirFlag; - } - } - - final float threshold = mRecyclerView.getHeight() * mCallback - .getSwipeThreshold(viewHolder); - if ((flags & dirFlag) != 0 && Math.abs(mDy) > threshold) { - return dirFlag; - } - } - return 0; - } - - private void addChildDrawingOrderCallback() { - if (Build.VERSION.SDK_INT >= 21) { - return;// we use elevation on Lollipop - } - if (mChildDrawingOrderCallback == null) { - mChildDrawingOrderCallback = new RecyclerView.ChildDrawingOrderCallback() { - @Override - public int onGetChildDrawingOrder(int childCount, int i) { - if (mOverdrawChild == null) { - return i; - } - int childPosition = mOverdrawChildPosition; - if (childPosition == -1) { - childPosition = mRecyclerView.indexOfChild(mOverdrawChild); - mOverdrawChildPosition = childPosition; - } - if (i == childCount - 1) { - return childPosition; - } - return i < childPosition ? i : i + 1; - } - }; - } - mRecyclerView.setChildDrawingOrderCallback(mChildDrawingOrderCallback); - } - - private void removeChildDrawingOrderCallbackIfNecessary(View view) { - if (view == mOverdrawChild) { - mOverdrawChild = null; - // only remove if we've added - if (mChildDrawingOrderCallback != null) { - mRecyclerView.setChildDrawingOrderCallback(null); - } - } - } - - /** - * An interface which can be implemented by LayoutManager for better integration with - * {@link ItemTouchHelper}. - */ - public static interface ViewDropHandler { - - /** - * Called by the {@link ItemTouchHelper} after a View is dropped over another View. - *

        - * A LayoutManager should implement this interface to get ready for the upcoming move - * operation. - *

        - * For example, LinearLayoutManager sets up a "scrollToPositionWithOffset" calls so that - * the View under drag will be used as an anchor View while calculating the next layout, - * making layout stay consistent. - * - * @param view The View which is being dragged. It is very likely that user is still - * dragging this View so there might be other - * {@link #prepareForDrop(View, View, int, int)} after this one. - * @param target The target view which is being dropped on. - * @param x The left offset of the View that is being dragged. This value - * includes the movement caused by the user. - * @param y The top offset of the View that is being dragged. This value - * includes the movement caused by the user. - */ - public void prepareForDrop(View view, View target, int x, int y); - } - - /** - * This class is the contract between ItemTouchHelper and your application. It lets you control - * which touch behaviors are enabled per each ViewHolder and also receive callbacks when user - * performs these actions. - *

        - * To control which actions user can take on each view, you should override - * {@link #getMovementFlags(RecyclerView, ViewHolder)} and return appropriate set - * of direction flags. ({@link #LEFT}, {@link #RIGHT}, {@link #START}, {@link #END}, - * {@link #UP}, {@link #DOWN}). You can use - * {@link #makeMovementFlags(int, int)} to easily construct it. Alternatively, you can use - * {@link SimpleCallback}. - *

        - * If user drags an item, ItemTouchHelper will call - * {@link Callback#onMove(RecyclerView, ViewHolder, ViewHolder) - * onMove(recyclerView, dragged, target)}. - * Upon receiving this callback, you should move the item from the old position - * ({@code dragged.getAdapterPosition()}) to new position ({@code target.getAdapterPosition()}) - * in your adapter and also call {@link RecyclerView.Adapter#notifyItemMoved(int, int)}. - * To control where a View can be dropped, you can override - * {@link #canDropOver(RecyclerView, ViewHolder, ViewHolder)}. When a - * dragging View overlaps multiple other views, Callback chooses the closest View with which - * dragged View might have changed positions. Although this approach works for many use cases, - * if you have a custom LayoutManager, you can override - * {@link #chooseDropTarget(ViewHolder, java.util.List, int, int)} to select a - * custom drop target. - *

        - * When a View is swiped, ItemTouchHelper animates it until it goes out of bounds, then calls - * {@link #onSwiped(ViewHolder, int)}. At this point, you should update your - * adapter (e.g. remove the item) and call related Adapter#notify event. - */ - @SuppressWarnings("UnusedParameters") - public abstract static class Callback { - - public static final int DEFAULT_DRAG_ANIMATION_DURATION = 200; - - public static final int DEFAULT_SWIPE_ANIMATION_DURATION = 250; - - static final int RELATIVE_DIR_FLAGS = START | END | - ((START | END) << DIRECTION_FLAG_COUNT) | - ((START | END) << (2 * DIRECTION_FLAG_COUNT)); - - private static final ItemTouchUIUtil sUICallback; - - private static final int ABS_HORIZONTAL_DIR_FLAGS = LEFT | RIGHT | - ((LEFT | RIGHT) << DIRECTION_FLAG_COUNT) | - ((LEFT | RIGHT) << (2 * DIRECTION_FLAG_COUNT)); - - private static final Interpolator sDragScrollInterpolator = new Interpolator() { - @Override - public float getInterpolation(float t) { - return t * t * t * t * t; - } - }; - - private static final Interpolator sDragViewScrollCapInterpolator = new Interpolator() { - @Override - public float getInterpolation(float t) { - t -= 1.0f; - return t * t * t * t * t + 1.0f; - } - }; - - /** - * Drag scroll speed keeps accelerating until this many milliseconds before being capped. - */ - private static final long DRAG_SCROLL_ACCELERATION_LIMIT_TIME_MS = 2000; - - private int mCachedMaxScrollSpeed = -1; - - static { - if (Build.VERSION.SDK_INT >= 21) { - sUICallback = new ItemTouchUIUtilImpl.Lollipop(); - } else if (Build.VERSION.SDK_INT >= 11) { - sUICallback = new ItemTouchUIUtilImpl.Honeycomb(); - } else { - sUICallback = new ItemTouchUIUtilImpl.Gingerbread(); - } - } - - /** - * Returns the {@link ItemTouchUIUtil} that is used by the {@link Callback} class for - * visual - * changes on Views in response to user interactions. {@link ItemTouchUIUtil} has different - * implementations for different platform versions. - *

        - * By default, {@link Callback} applies these changes on - * {@link RecyclerView.ViewHolder#itemView}. - *

        - * For example, if you have a use case where you only want the text to move when user - * swipes over the view, you can do the following: - *

        -         *     public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder){
        -         *         getDefaultUIUtil().clearView(((ItemTouchViewHolder) viewHolder).textView);
        -         *     }
        -         *     public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        -         *         if (viewHolder != null){
        -         *             getDefaultUIUtil().onSelected(((ItemTouchViewHolder) viewHolder).textView);
        -         *         }
        -         *     }
        -         *     public void onChildDraw(Canvas c, RecyclerView recyclerView,
        -         *             RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState,
        -         *             boolean isCurrentlyActive) {
        -         *         getDefaultUIUtil().onDraw(c, recyclerView,
        -         *                 ((ItemTouchViewHolder) viewHolder).textView, dX, dY,
        -         *                 actionState, isCurrentlyActive);
        -         *         return true;
        -         *     }
        -         *     public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
        -         *             RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState,
        -         *             boolean isCurrentlyActive) {
        -         *         getDefaultUIUtil().onDrawOver(c, recyclerView,
        -         *                 ((ItemTouchViewHolder) viewHolder).textView, dX, dY,
        -         *                 actionState, isCurrentlyActive);
        -         *         return true;
        -         *     }
        -         * 
        - * - * @return The {@link ItemTouchUIUtil} instance that is used by the {@link Callback} - */ - public static ItemTouchUIUtil getDefaultUIUtil() { - return sUICallback; - } - - /** - * Replaces a movement direction with its relative version by taking layout direction into - * account. - * - * @param flags The flag value that include any number of movement flags. - * @param layoutDirection The layout direction of the View. Can be obtained from - * {@link ViewCompat#getLayoutDirection(android.view.View)}. - * @return Updated flags which uses relative flags ({@link #START}, {@link #END}) instead - * of {@link #LEFT}, {@link #RIGHT}. - * @see #convertToAbsoluteDirection(int, int) - */ - public static int convertToRelativeDirection(int flags, int layoutDirection) { - int masked = flags & ABS_HORIZONTAL_DIR_FLAGS; - if (masked == 0) { - return flags;// does not have any abs flags, good. - } - flags &= ~masked; //remove left / right. - if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) { - // no change. just OR with 2 bits shifted mask and return - flags |= masked << 2; // START is 2 bits after LEFT, END is 2 bits after RIGHT. - return flags; - } else { - // add RIGHT flag as START - flags |= ((masked << 1) & ~ABS_HORIZONTAL_DIR_FLAGS); - // first clean RIGHT bit then add LEFT flag as END - flags |= ((masked << 1) & ABS_HORIZONTAL_DIR_FLAGS) << 2; - } - return flags; - } - - /** - * Convenience method to create movement flags. - *

        - * For instance, if you want to let your items be drag & dropped vertically and swiped - * left to be dismissed, you can call this method with: - * makeMovementFlags(UP | DOWN, LEFT); - * - * @param dragFlags The directions in which the item can be dragged. - * @param swipeFlags The directions in which the item can be swiped. - * @return Returns an integer composed of the given drag and swipe flags. - */ - public static int makeMovementFlags(int dragFlags, int swipeFlags) { - return makeFlag(ACTION_STATE_IDLE, swipeFlags | dragFlags) | - makeFlag(ACTION_STATE_SWIPE, swipeFlags) | makeFlag(ACTION_STATE_DRAG, - dragFlags); - } - - /** - * Shifts the given direction flags to the offset of the given action state. - * - * @param actionState The action state you want to get flags in. Should be one of - * {@link #ACTION_STATE_IDLE}, {@link #ACTION_STATE_SWIPE} or - * {@link #ACTION_STATE_DRAG}. - * @param directions The direction flags. Can be composed from {@link #UP}, {@link #DOWN}, - * {@link #RIGHT}, {@link #LEFT} {@link #START} and {@link #END}. - * @return And integer that represents the given directions in the provided actionState. - */ - public static int makeFlag(int actionState, int directions) { - return directions << (actionState * DIRECTION_FLAG_COUNT); - } - - /** - * Should return a composite flag which defines the enabled move directions in each state - * (idle, swiping, dragging). - *

        - * Instead of composing this flag manually, you can use {@link #makeMovementFlags(int, - * int)} - * or {@link #makeFlag(int, int)}. - *

        - * This flag is composed of 3 sets of 8 bits, where first 8 bits are for IDLE state, next - * 8 bits are for SWIPE state and third 8 bits are for DRAG state. - * Each 8 bit sections can be constructed by simply OR'ing direction flags defined in - * {@link ItemTouchHelper}. - *

        - * For example, if you want it to allow swiping LEFT and RIGHT but only allow starting to - * swipe by swiping RIGHT, you can return: - *

        -         *      makeFlag(ACTION_STATE_IDLE, RIGHT) | makeFlag(ACTION_STATE_SWIPE, LEFT | RIGHT);
        -         * 
        - * This means, allow right movement while IDLE and allow right and left movement while - * swiping. - * - * @param recyclerView The RecyclerView to which ItemTouchHelper is attached. - * @param viewHolder The ViewHolder for which the movement information is necessary. - * @return flags specifying which movements are allowed on this ViewHolder. - * @see #makeMovementFlags(int, int) - * @see #makeFlag(int, int) - */ - public abstract int getMovementFlags(RecyclerView recyclerView, - ViewHolder viewHolder); - - /** - * Converts a given set of flags to absolution direction which means {@link #START} and - * {@link #END} are replaced with {@link #LEFT} and {@link #RIGHT} depending on the layout - * direction. - * - * @param flags The flag value that include any number of movement flags. - * @param layoutDirection The layout direction of the RecyclerView. - * @return Updated flags which includes only absolute direction values. - */ - public int convertToAbsoluteDirection(int flags, int layoutDirection) { - int masked = flags & RELATIVE_DIR_FLAGS; - if (masked == 0) { - return flags;// does not have any relative flags, good. - } - flags &= ~masked; //remove start / end - if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) { - // no change. just OR with 2 bits shifted mask and return - flags |= masked >> 2; // START is 2 bits after LEFT, END is 2 bits after RIGHT. - return flags; - } else { - // add START flag as RIGHT - flags |= ((masked >> 1) & ~RELATIVE_DIR_FLAGS); - // first clean start bit then add END flag as LEFT - flags |= ((masked >> 1) & RELATIVE_DIR_FLAGS) >> 2; - } - return flags; - } - - final int getAbsoluteMovementFlags(RecyclerView recyclerView, - ViewHolder viewHolder) { - final int flags = getMovementFlags(recyclerView, viewHolder); - return convertToAbsoluteDirection(flags, ViewCompat.getLayoutDirection(recyclerView)); - } - - private boolean hasDragFlag(RecyclerView recyclerView, ViewHolder viewHolder) { - final int flags = getAbsoluteMovementFlags(recyclerView, viewHolder); - return (flags & ACTION_MODE_DRAG_MASK) != 0; - } - - private boolean hasSwipeFlag(RecyclerView recyclerView, - ViewHolder viewHolder) { - final int flags = getAbsoluteMovementFlags(recyclerView, viewHolder); - return (flags & ACTION_MODE_SWIPE_MASK) != 0; - } - - /** - * Return true if the current ViewHolder can be dropped over the the target ViewHolder. - *

        - * This method is used when selecting drop target for the dragged View. After Views are - * eliminated either via bounds check or via this method, resulting set of views will be - * passed to {@link #chooseDropTarget(ViewHolder, java.util.List, int, int)}. - *

        - * Default implementation returns true. - * - * @param recyclerView The RecyclerView to which ItemTouchHelper is attached to. - * @param current The ViewHolder that user is dragging. - * @param target The ViewHolder which is below the dragged ViewHolder. - * @return True if the dragged ViewHolder can be replaced with the target ViewHolder, false - * otherwise. - */ - public boolean canDropOver(RecyclerView recyclerView, ViewHolder current, - ViewHolder target) { - return true; - } - - /** - * Called when ItemTouchHelper wants to move the dragged item from its old position to - * the new position. - *

        - * If this method returns true, ItemTouchHelper assumes {@code viewHolder} has been moved - * to the adapter position of {@code target} ViewHolder - * ({@link ViewHolder#getAdapterPosition() - * ViewHolder#getAdapterPosition()}). - *

        - * If you don't support drag & drop, this method will never be called. - * - * @param recyclerView The RecyclerView to which ItemTouchHelper is attached to. - * @param viewHolder The ViewHolder which is being dragged by the user. - * @param target The ViewHolder over which the currently active item is being - * dragged. - * @return True if the {@code viewHolder} has been moved to the adapter position of - * {@code target}. - * @see #onMoved(RecyclerView, ViewHolder, int, ViewHolder, int, int, int) - */ - public abstract boolean onMove(RecyclerView recyclerView, - ViewHolder viewHolder, ViewHolder target); - - /** - * Returns whether ItemTouchHelper should start a drag and drop operation if an item is - * long pressed. - *

        - * Default value returns true but you may want to disable this if you want to start - * dragging on a custom view touch using {@link #startDrag(ViewHolder)}. - * - * @return True if ItemTouchHelper should start dragging an item when it is long pressed, - * false otherwise. Default value is true. - * @see #startDrag(ViewHolder) - */ - public boolean isLongPressDragEnabled() { - return true; - } - - /** - * Returns whether ItemTouchHelper should start a swipe operation if a pointer is swiped - * over the View. - *

        - * Default value returns true but you may want to disable this if you want to start - * swiping on a custom view touch using {@link #startSwipe(ViewHolder)}. - * - * @return True if ItemTouchHelper should start swiping an item when user swipes a pointer - * over the View, false otherwise. Default value is true. - * @see #startSwipe(ViewHolder) - */ - public boolean isItemViewSwipeEnabled() { - return true; - } - - /** - * When finding views under a dragged view, by default, ItemTouchHelper searches for views - * that overlap with the dragged View. By overriding this method, you can extend or shrink - * the search box. - * - * @return The extra margin to be added to the hit box of the dragged View. - */ - public int getBoundingBoxMargin() { - return 0; - } - - /** - * Returns the fraction that the user should move the View to be considered as swiped. - * The fraction is calculated with respect to RecyclerView's bounds. - *

        - * Default value is .5f, which means, to swipe a View, user must move the View at least - * half of RecyclerView's width or height, depending on the swipe direction. - * - * @param viewHolder The ViewHolder that is being dragged. - * @return A float value that denotes the fraction of the View size. Default value - * is .5f . - */ - public float getSwipeThreshold(ViewHolder viewHolder) { - return .5f; - } - - /** - * Returns the fraction that the user should move the View to be considered as it is - * dragged. After a view is moved this amount, ItemTouchHelper starts checking for Views - * below it for a possible drop. - * - * @param viewHolder The ViewHolder that is being dragged. - * @return A float value that denotes the fraction of the View size. Default value is - * .5f . - */ - public float getMoveThreshold(ViewHolder viewHolder) { - return .5f; - } - - /** - * Defines the minimum velocity which will be considered as a swipe action by the user. - *

        - * You can increase this value to make it harder to swipe or decrease it to make it easier. - * Keep in mind that ItemTouchHelper also checks the perpendicular velocity and makes sure - * current direction velocity is larger then the perpendicular one. Otherwise, user's - * movement is ambiguous. You can change the threshold by overriding - * {@link #getSwipeVelocityThreshold(float)}. - *

        - * The velocity is calculated in pixels per second. - *

        - * The default framework value is passed as a parameter so that you can modify it with a - * multiplier. - * - * @param defaultValue The default value (in pixels per second) used by the - * ItemTouchHelper. - * @return The minimum swipe velocity. The default implementation returns the - * defaultValue parameter. - * @see #getSwipeVelocityThreshold(float) - * @see #getSwipeThreshold(ViewHolder) - */ - public float getSwipeEscapeVelocity(float defaultValue) { - return defaultValue; - } - - /** - * Defines the maximum velocity ItemTouchHelper will ever calculate for pointer movements. - *

        - * To consider a movement as swipe, ItemTouchHelper requires it to be larger than the - * perpendicular movement. If both directions reach to the max threshold, none of them will - * be considered as a swipe because it is usually an indication that user rather tried to - * scroll then swipe. - *

        - * The velocity is calculated in pixels per second. - *

        - * You can customize this behavior by changing this method. If you increase the value, it - * will be easier for the user to swipe diagonally and if you decrease the value, user will - * need to make a rather straight finger movement to trigger a swipe. - * - * @param defaultValue The default value(in pixels per second) used by the ItemTouchHelper. - * @return The velocity cap for pointer movements. The default implementation returns the - * defaultValue parameter. - * @see #getSwipeEscapeVelocity(float) - */ - public float getSwipeVelocityThreshold(float defaultValue) { - return defaultValue; - } - - /** - * Called by ItemTouchHelper to select a drop target from the list of ViewHolders that - * are under the dragged View. - *

        - * Default implementation filters the View with which dragged item have changed position - * in the drag direction. For instance, if the view is dragged UP, it compares the - * view.getTop() of the two views before and after drag started. If that value - * is different, the target view passes the filter. - *

        - * Among these Views which pass the test, the one closest to the dragged view is chosen. - *

        - * This method is called on the main thread every time user moves the View. If you want to - * override it, make sure it does not do any expensive operations. - * - * @param selected The ViewHolder being dragged by the user. - * @param dropTargets The list of ViewHolder that are under the dragged View and - * candidate as a drop. - * @param curX The updated left value of the dragged View after drag translations - * are applied. This value does not include margins added by - * {@link RecyclerView.ItemDecoration}s. - * @param curY The updated top value of the dragged View after drag translations - * are applied. This value does not include margins added by - * {@link RecyclerView.ItemDecoration}s. - * @return A ViewHolder to whose position the dragged ViewHolder should be - * moved to. - */ - public ViewHolder chooseDropTarget(ViewHolder selected, - List dropTargets, int curX, int curY) { - int right = curX + selected.itemView.getWidth(); - int bottom = curY + selected.itemView.getHeight(); - ViewHolder winner = null; - int winnerScore = -1; - final int dx = curX - selected.itemView.getLeft(); - final int dy = curY - selected.itemView.getTop(); - final int targetsSize = dropTargets.size(); - for (int i = 0; i < targetsSize; i++) { - final ViewHolder target = dropTargets.get(i); - if (dx > 0) { - int diff = target.itemView.getRight() - right; - if (diff < 0 && target.itemView.getRight() > selected.itemView.getRight()) { - final int score = Math.abs(diff); - if (score > winnerScore) { - winnerScore = score; - winner = target; - } - } - } - if (dx < 0) { - int diff = target.itemView.getLeft() - curX; - if (diff > 0 && target.itemView.getLeft() < selected.itemView.getLeft()) { - final int score = Math.abs(diff); - if (score > winnerScore) { - winnerScore = score; - winner = target; - } - } - } - if (dy < 0) { - int diff = target.itemView.getTop() - curY; - if (diff > 0 && target.itemView.getTop() < selected.itemView.getTop()) { - final int score = Math.abs(diff); - if (score > winnerScore) { - winnerScore = score; - winner = target; - } - } - } - - if (dy > 0) { - int diff = target.itemView.getBottom() - bottom; - if (diff < 0 && target.itemView.getBottom() > selected.itemView.getBottom()) { - final int score = Math.abs(diff); - if (score > winnerScore) { - winnerScore = score; - winner = target; - } - } - } - } - return winner; - } - - /** - * Called when a ViewHolder is swiped by the user. - *

        - * If you are returning relative directions ({@link #START} , {@link #END}) from the - * {@link #getMovementFlags(RecyclerView, ViewHolder)} method, this method - * will also use relative directions. Otherwise, it will use absolute directions. - *

        - * If you don't support swiping, this method will never be called. - *

        - * ItemTouchHelper will keep a reference to the View until it is detached from - * RecyclerView. - * As soon as it is detached, ItemTouchHelper will call - * {@link #clearView(RecyclerView, ViewHolder)}. - * - * @param viewHolder The ViewHolder which has been swiped by the user. - * @param direction The direction to which the ViewHolder is swiped. It is one of - * {@link #UP}, {@link #DOWN}, - * {@link #LEFT} or {@link #RIGHT}. If your - * {@link #getMovementFlags(RecyclerView, ViewHolder)} - * method - * returned relative flags instead of {@link #LEFT} / {@link #RIGHT}; - * `direction` will be relative as well. ({@link #START} or {@link - * #END}). - */ - public abstract void onSwiped(ViewHolder viewHolder, int direction); - - /** - * Called when the ViewHolder swiped or dragged by the ItemTouchHelper is changed. - *

        - * If you override this method, you should call super. - * - * @param viewHolder The new ViewHolder that is being swiped or dragged. Might be null if - * it is cleared. - * @param actionState One of {@link ItemTouchHelper#ACTION_STATE_IDLE}, - * {@link ItemTouchHelper#ACTION_STATE_SWIPE} or - * {@link ItemTouchHelper#ACTION_STATE_DRAG}. - * @see #clearView(RecyclerView, RecyclerView.ViewHolder) - */ - public void onSelectedChanged(ViewHolder viewHolder, int actionState) { - if (viewHolder != null) { - sUICallback.onSelected(viewHolder.itemView); - } - } - - private int getMaxDragScroll(RecyclerView recyclerView) { - if (mCachedMaxScrollSpeed == -1) { - mCachedMaxScrollSpeed = recyclerView.getResources().getDimensionPixelSize( - R.dimen.item_touch_helper_max_drag_scroll_per_frame); - } - return mCachedMaxScrollSpeed; - } - - /** - * Called when {@link #onMove(RecyclerView, ViewHolder, ViewHolder)} returns true. - *

        - * ItemTouchHelper does not create an extra Bitmap or View while dragging, instead, it - * modifies the existing View. Because of this reason, it is important that the View is - * still part of the layout after it is moved. This may not work as intended when swapped - * Views are close to RecyclerView bounds or there are gaps between them (e.g. other Views - * which were not eligible for dropping over). - *

        - * This method is responsible to give necessary hint to the LayoutManager so that it will - * keep the View in visible area. For example, for LinearLayoutManager, this is as simple - * as calling {@link LinearLayoutManager#scrollToPositionWithOffset(int, int)}. - * - * Default implementation calls {@link RecyclerView#scrollToPosition(int)} if the View's - * new position is likely to be out of bounds. - *

        - * It is important to ensure the ViewHolder will stay visible as otherwise, it might be - * removed by the LayoutManager if the move causes the View to go out of bounds. In that - * case, drag will end prematurely. - * - * @param recyclerView The RecyclerView controlled by the ItemTouchHelper. - * @param viewHolder The ViewHolder under user's control. - * @param fromPos The previous adapter position of the dragged item (before it was - * moved). - * @param target The ViewHolder on which the currently active item has been dropped. - * @param toPos The new adapter position of the dragged item. - * @param x The updated left value of the dragged View after drag translations - * are applied. This value does not include margins added by - * {@link RecyclerView.ItemDecoration}s. - * @param y The updated top value of the dragged View after drag translations - * are applied. This value does not include margins added by - * {@link RecyclerView.ItemDecoration}s. - */ - public void onMoved(final RecyclerView recyclerView, - final ViewHolder viewHolder, int fromPos, final ViewHolder target, int toPos, int x, - int y) { - final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); - if (layoutManager instanceof ViewDropHandler) { - ((ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView, - target.itemView, x, y); - return; - } - - // if layout manager cannot handle it, do some guesswork - if (layoutManager.canScrollHorizontally()) { - final int minLeft = layoutManager.getDecoratedLeft(target.itemView); - if (minLeft <= recyclerView.getPaddingLeft()) { - recyclerView.scrollToPosition(toPos); - } - final int maxRight = layoutManager.getDecoratedRight(target.itemView); - if (maxRight >= recyclerView.getWidth() - recyclerView.getPaddingRight()) { - recyclerView.scrollToPosition(toPos); - } - } - - if (layoutManager.canScrollVertically()) { - final int minTop = layoutManager.getDecoratedTop(target.itemView); - if (minTop <= recyclerView.getPaddingTop()) { - recyclerView.scrollToPosition(toPos); - } - final int maxBottom = layoutManager.getDecoratedBottom(target.itemView); - if (maxBottom >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) { - recyclerView.scrollToPosition(toPos); - } - } - } - - private void onDraw(Canvas c, RecyclerView parent, ViewHolder selected, - List recoverAnimationList, - int actionState, float dX, float dY) { - final int recoverAnimSize = recoverAnimationList.size(); - for (int i = 0; i < recoverAnimSize; i++) { - final ItemTouchHelper.RecoverAnimation anim = recoverAnimationList.get(i); - anim.update(); - final int count = c.save(); - onChildDraw(c, parent, anim.mViewHolder, anim.mX, anim.mY, anim.mActionState, - false); - c.restoreToCount(count); - } - if (selected != null) { - final int count = c.save(); - onChildDraw(c, parent, selected, dX, dY, actionState, true); - c.restoreToCount(count); - } - } - - private void onDrawOver(Canvas c, RecyclerView parent, ViewHolder selected, - List recoverAnimationList, - int actionState, float dX, float dY) { - final int recoverAnimSize = recoverAnimationList.size(); - for (int i = 0; i < recoverAnimSize; i++) { - final ItemTouchHelper.RecoverAnimation anim = recoverAnimationList.get(i); - final int count = c.save(); - onChildDrawOver(c, parent, anim.mViewHolder, anim.mX, anim.mY, anim.mActionState, - false); - c.restoreToCount(count); - } - if (selected != null) { - final int count = c.save(); - onChildDrawOver(c, parent, selected, dX, dY, actionState, true); - c.restoreToCount(count); - } - boolean hasRunningAnimation = false; - for (int i = recoverAnimSize - 1; i >= 0; i--) { - final RecoverAnimation anim = recoverAnimationList.get(i); - if (anim.mEnded && !anim.mIsPendingCleanup) { - recoverAnimationList.remove(i); - } else if (!anim.mEnded) { - hasRunningAnimation = true; - } - } - if (hasRunningAnimation) { - parent.invalidate(); - } - } - - /** - * Called by the ItemTouchHelper when the user interaction with an element is over and it - * also completed its animation. - *

        - * This is a good place to clear all changes on the View that was done in - * {@link #onSelectedChanged(RecyclerView.ViewHolder, int)}, - * {@link #onChildDraw(Canvas, RecyclerView, ViewHolder, float, float, int, - * boolean)} or - * {@link #onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, boolean)}. - * - * @param recyclerView The RecyclerView which is controlled by the ItemTouchHelper. - * @param viewHolder The View that was interacted by the user. - */ - public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) { - sUICallback.clearView(viewHolder.itemView); - } - - /** - * Called by ItemTouchHelper on RecyclerView's onDraw callback. - *

        - * If you would like to customize how your View's respond to user interactions, this is - * a good place to override. - *

        - * Default implementation translates the child by the given dX, - * dY. - * ItemTouchHelper also takes care of drawing the child after other children if it is being - * dragged. This is done using child re-ordering mechanism. On platforms prior to L, this - * is - * achieved via {@link android.view.ViewGroup#getChildDrawingOrder(int, int)} and on L - * and after, it changes View's elevation value to be greater than all other children.) - * - * @param c The canvas which RecyclerView is drawing its children - * @param recyclerView The RecyclerView to which ItemTouchHelper is attached to - * @param viewHolder The ViewHolder which is being interacted by the User or it was - * interacted and simply animating to its original position - * @param dX The amount of horizontal displacement caused by user's action - * @param dY The amount of vertical displacement caused by user's action - * @param actionState The type of interaction on the View. Is either {@link - * #ACTION_STATE_DRAG} or {@link #ACTION_STATE_SWIPE}. - * @param isCurrentlyActive True if this view is currently being controlled by the user or - * false it is simply animating back to its original state. - * @see #onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, - * boolean) - */ - public void onChildDraw(Canvas c, RecyclerView recyclerView, - ViewHolder viewHolder, - float dX, float dY, int actionState, boolean isCurrentlyActive) { - sUICallback.onDraw(c, recyclerView, viewHolder.itemView, dX, dY, actionState, - isCurrentlyActive); - } - - /** - * Called by ItemTouchHelper on RecyclerView's onDraw callback. - *

        - * If you would like to customize how your View's respond to user interactions, this is - * a good place to override. - *

        - * Default implementation translates the child by the given dX, - * dY. - * ItemTouchHelper also takes care of drawing the child after other children if it is being - * dragged. This is done using child re-ordering mechanism. On platforms prior to L, this - * is - * achieved via {@link android.view.ViewGroup#getChildDrawingOrder(int, int)} and on L - * and after, it changes View's elevation value to be greater than all other children.) - * - * @param c The canvas which RecyclerView is drawing its children - * @param recyclerView The RecyclerView to which ItemTouchHelper is attached to - * @param viewHolder The ViewHolder which is being interacted by the User or it was - * interacted and simply animating to its original position - * @param dX The amount of horizontal displacement caused by user's action - * @param dY The amount of vertical displacement caused by user's action - * @param actionState The type of interaction on the View. Is either {@link - * #ACTION_STATE_DRAG} or {@link #ACTION_STATE_SWIPE}. - * @param isCurrentlyActive True if this view is currently being controlled by the user or - * false it is simply animating back to its original state. - * @see #onChildDrawOver(Canvas, RecyclerView, ViewHolder, float, float, int, - * boolean) - */ - public void onChildDrawOver(Canvas c, RecyclerView recyclerView, - ViewHolder viewHolder, - float dX, float dY, int actionState, boolean isCurrentlyActive) { - sUICallback.onDrawOver(c, recyclerView, viewHolder.itemView, dX, dY, actionState, - isCurrentlyActive); - } - - /** - * Called by the ItemTouchHelper when user action finished on a ViewHolder and now the View - * will be animated to its final position. - *

        - * Default implementation uses ItemAnimator's duration values. If - * animationType is {@link #ANIMATION_TYPE_DRAG}, it returns - * {@link RecyclerView.ItemAnimator#getMoveDuration()}, otherwise, it returns - * {@link RecyclerView.ItemAnimator#getRemoveDuration()}. If RecyclerView does not have - * any {@link RecyclerView.ItemAnimator} attached, this method returns - * {@code DEFAULT_DRAG_ANIMATION_DURATION} or {@code DEFAULT_SWIPE_ANIMATION_DURATION} - * depending on the animation type. - * - * @param recyclerView The RecyclerView to which the ItemTouchHelper is attached to. - * @param animationType The type of animation. Is one of {@link #ANIMATION_TYPE_DRAG}, - * {@link #ANIMATION_TYPE_SWIPE_CANCEL} or - * {@link #ANIMATION_TYPE_SWIPE_SUCCESS}. - * @param animateDx The horizontal distance that the animation will offset - * @param animateDy The vertical distance that the animation will offset - * @return The duration for the animation - */ - public long getAnimationDuration(RecyclerView recyclerView, int animationType, - float animateDx, float animateDy) { - final RecyclerView.ItemAnimator itemAnimator = recyclerView.getItemAnimator(); - if (itemAnimator == null) { - return animationType == ANIMATION_TYPE_DRAG ? DEFAULT_DRAG_ANIMATION_DURATION - : DEFAULT_SWIPE_ANIMATION_DURATION; - } else { - return animationType == ANIMATION_TYPE_DRAG ? itemAnimator.getMoveDuration() - : itemAnimator.getRemoveDuration(); - } - } - - /** - * Called by the ItemTouchHelper when user is dragging a view out of bounds. - *

        - * You can override this method to decide how much RecyclerView should scroll in response - * to this action. Default implementation calculates a value based on the amount of View - * out of bounds and the time it spent there. The longer user keeps the View out of bounds, - * the faster the list will scroll. Similarly, the larger portion of the View is out of - * bounds, the faster the RecyclerView will scroll. - * - * @param recyclerView The RecyclerView instance to which ItemTouchHelper is - * attached to. - * @param viewSize The total size of the View in scroll direction, excluding - * item decorations. - * @param viewSizeOutOfBounds The total size of the View that is out of bounds. This value - * is negative if the View is dragged towards left or top edge. - * @param totalSize The total size of RecyclerView in the scroll direction. - * @param msSinceStartScroll The time passed since View is kept out of bounds. - * @return The amount that RecyclerView should scroll. Keep in mind that this value will - * be passed to {@link RecyclerView#scrollBy(int, int)} method. - */ - public int interpolateOutOfBoundsScroll(RecyclerView recyclerView, - int viewSize, int viewSizeOutOfBounds, - int totalSize, long msSinceStartScroll) { - final int maxScroll = getMaxDragScroll(recyclerView); - final int absOutOfBounds = Math.abs(viewSizeOutOfBounds); - final int direction = (int) Math.signum(viewSizeOutOfBounds); - // might be negative if other direction - float outOfBoundsRatio = Math.min(1f, 1f * absOutOfBounds / viewSize); - final int cappedScroll = (int) (direction * maxScroll * - sDragViewScrollCapInterpolator.getInterpolation(outOfBoundsRatio)); - final float timeRatio; - if (msSinceStartScroll > DRAG_SCROLL_ACCELERATION_LIMIT_TIME_MS) { - timeRatio = 1f; - } else { - timeRatio = (float) msSinceStartScroll / DRAG_SCROLL_ACCELERATION_LIMIT_TIME_MS; - } - final int value = (int) (cappedScroll * sDragScrollInterpolator - .getInterpolation(timeRatio)); - if (value == 0) { - return viewSizeOutOfBounds > 0 ? 1 : -1; - } - return value; - } - } - - /** - * A simple wrapper to the default Callback which you can construct with drag and swipe - * directions and this class will handle the flag callbacks. You should still override onMove - * or - * onSwiped depending on your use case. - * - *

        -     * ItemTouchHelper mIth = new ItemTouchHelper(
        -     *     new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
        -     *         ItemTouchHelper.LEFT) {
        -     *         public abstract boolean onMove(RecyclerView recyclerView,
        -     *             ViewHolder viewHolder, ViewHolder target) {
        -     *             final int fromPos = viewHolder.getAdapterPosition();
        -     *             final int toPos = target.getAdapterPosition();
        -     *             // move item in `fromPos` to `toPos` in adapter.
        -     *             return true;// true if moved, false otherwise
        -     *         }
        -     *         public void onSwiped(ViewHolder viewHolder, int direction) {
        -     *             // remove from adapter
        -     *         }
        -     * });
        -     * 
        - */ - public abstract static class SimpleCallback extends Callback { - - private int mDefaultSwipeDirs; - - private int mDefaultDragDirs; - - /** - * Creates a Callback for the given drag and swipe allowance. These values serve as - * defaults - * and if you want to customize behavior per ViewHolder, you can override - * {@link #getSwipeDirs(RecyclerView, ViewHolder)} - * and / or {@link #getDragDirs(RecyclerView, ViewHolder)}. - * - * @param dragDirs Binary OR of direction flags in which the Views can be dragged. Must be - * composed of {@link #LEFT}, {@link #RIGHT}, {@link #START}, {@link - * #END}, - * {@link #UP} and {@link #DOWN}. - * @param swipeDirs Binary OR of direction flags in which the Views can be swiped. Must be - * composed of {@link #LEFT}, {@link #RIGHT}, {@link #START}, {@link - * #END}, - * {@link #UP} and {@link #DOWN}. - */ - public SimpleCallback(int dragDirs, int swipeDirs) { - mDefaultSwipeDirs = swipeDirs; - mDefaultDragDirs = dragDirs; - } - - /** - * Updates the default swipe directions. For example, you can use this method to toggle - * certain directions depending on your use case. - * - * @param defaultSwipeDirs Binary OR of directions in which the ViewHolders can be swiped. - */ - public void setDefaultSwipeDirs(int defaultSwipeDirs) { - mDefaultSwipeDirs = defaultSwipeDirs; - } - - /** - * Updates the default drag directions. For example, you can use this method to toggle - * certain directions depending on your use case. - * - * @param defaultDragDirs Binary OR of directions in which the ViewHolders can be dragged. - */ - public void setDefaultDragDirs(int defaultDragDirs) { - mDefaultDragDirs = defaultDragDirs; - } - - /** - * Returns the swipe directions for the provided ViewHolder. - * Default implementation returns the swipe directions that was set via constructor or - * {@link #setDefaultSwipeDirs(int)}. - * - * @param recyclerView The RecyclerView to which the ItemTouchHelper is attached to. - * @param viewHolder The RecyclerView for which the swipe direction is queried. - * @return A binary OR of direction flags. - */ - public int getSwipeDirs(RecyclerView recyclerView, ViewHolder viewHolder) { - return mDefaultSwipeDirs; - } - - /** - * Returns the drag directions for the provided ViewHolder. - * Default implementation returns the drag directions that was set via constructor or - * {@link #setDefaultDragDirs(int)}. - * - * @param recyclerView The RecyclerView to which the ItemTouchHelper is attached to. - * @param viewHolder The RecyclerView for which the swipe direction is queried. - * @return A binary OR of direction flags. - */ - public int getDragDirs(RecyclerView recyclerView, ViewHolder viewHolder) { - return mDefaultDragDirs; - } - - @Override - public int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder) { - return makeMovementFlags(getDragDirs(recyclerView, viewHolder), - getSwipeDirs(recyclerView, viewHolder)); - } - } - - private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener { - - @Override - public boolean onDown(MotionEvent e) { - return true; - } - - @Override - public void onLongPress(MotionEvent e) { - View child = findChildView(e); - if (child != null) { - ViewHolder vh = mRecyclerView.getChildViewHolder(child); - if (vh != null) { - if (!mCallback.hasDragFlag(mRecyclerView, vh)) { - return; - } - int pointerId = e.getPointerId(0); - // Long press is deferred. - // Check w/ active pointer id to avoid selecting after motion - // event is canceled. - if (pointerId == mActivePointerId) { - final int index = e.findPointerIndex(mActivePointerId); - final float x = e.getX(index); - final float y = e.getY(index); - mInitialTouchX = x; - mInitialTouchY = y; - mDx = mDy = 0f; - if (DEBUG) { - Log.d(TAG, - "onlong press: x:" + mInitialTouchX + ",y:" + mInitialTouchY); - } - if (mCallback.isLongPressDragEnabled()) { - select(vh, ACTION_STATE_DRAG); - } - } - } - } - } - } - - private class RecoverAnimation implements AnimatorListenerCompat { - - final float mStartDx; - - final float mStartDy; - - final float mTargetX; - - final float mTargetY; - - final ViewHolder mViewHolder; - - final int mActionState; - - private final ValueAnimatorCompat mValueAnimator; - - private final int mAnimationType; - - public boolean mIsPendingCleanup; - - float mX; - - float mY; - - // if user starts touching a recovering view, we put it into interaction mode again, - // instantly. - boolean mOverridden = false; - - private boolean mEnded = false; - - private float mFraction; - - public RecoverAnimation(ViewHolder viewHolder, int animationType, - int actionState, float startDx, float startDy, float targetX, float targetY) { - mActionState = actionState; - mAnimationType = animationType; - mViewHolder = viewHolder; - mStartDx = startDx; - mStartDy = startDy; - mTargetX = targetX; - mTargetY = targetY; - mValueAnimator = AnimatorCompatHelper.emptyValueAnimator(); - mValueAnimator.addUpdateListener( - new AnimatorUpdateListenerCompat() { - @Override - public void onAnimationUpdate(ValueAnimatorCompat animation) { - setFraction(animation.getAnimatedFraction()); - } - }); - mValueAnimator.setTarget(viewHolder.itemView); - mValueAnimator.addListener(this); - setFraction(0f); - } - - public void setDuration(long duration) { - mValueAnimator.setDuration(duration); - } - - public void start() { - mViewHolder.setIsRecyclable(false); - mValueAnimator.start(); - } - - public void cancel() { - mValueAnimator.cancel(); - } - - public void setFraction(float fraction) { - mFraction = fraction; - } - - /** - * We run updates on onDraw method but use the fraction from animator callback. - * This way, we can sync translate x/y values w/ the animators to avoid one-off frames. - */ - public void update() { - if (mStartDx == mTargetX) { - mX = ViewCompat.getTranslationX(mViewHolder.itemView); - } else { - mX = mStartDx + mFraction * (mTargetX - mStartDx); - } - if (mStartDy == mTargetY) { - mY = ViewCompat.getTranslationY(mViewHolder.itemView); - } else { - mY = mStartDy + mFraction * (mTargetY - mStartDy); - } - } - - @Override - public void onAnimationStart(ValueAnimatorCompat animation) { - - } - - @Override - public void onAnimationEnd(ValueAnimatorCompat animation) { - if (!mEnded) { - mViewHolder.setIsRecyclable(true); - } - mEnded = true; - } - - @Override - public void onAnimationCancel(ValueAnimatorCompat animation) { - setFraction(1f); //make sure we recover the view's state. - } - - @Override - public void onAnimationRepeat(ValueAnimatorCompat animation) { - - } - } -} \ No newline at end of file diff --git a/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtil.java b/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtil.java deleted file mode 100644 index 520a95e994..0000000000 --- a/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtil.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.widget.helper; - -import android.graphics.Canvas; -import android.support.v7.widget.RecyclerView; -import android.view.View; - -/** - * Utility class for {@link ItemTouchHelper} which handles item transformations for different - * API versions. - *

        - * This class has methods that map to {@link ItemTouchHelper.Callback}'s drawing methods. Default - * implementations in {@link ItemTouchHelper.Callback} call these methods with - * {@link RecyclerView.ViewHolder#itemView} and {@link ItemTouchUIUtil} makes necessary changes - * on the View depending on the API level. You can access the instance of {@link ItemTouchUIUtil} - * via {@link ItemTouchHelper.Callback#getDefaultUIUtil()} and call its methods with the children - * of ViewHolder that you want to apply default effects. - * - * @see ItemTouchHelper.Callback#getDefaultUIUtil() - */ -public interface ItemTouchUIUtil { - - /** - * The default implementation for {@link ItemTouchHelper.Callback#onChildDraw(Canvas, - * RecyclerView, RecyclerView.ViewHolder, float, float, int, boolean)} - */ - void onDraw(Canvas c, RecyclerView recyclerView, View view, - float dX, float dY, int actionState, boolean isCurrentlyActive); - - /** - * The default implementation for {@link ItemTouchHelper.Callback#onChildDrawOver(Canvas, - * RecyclerView, RecyclerView.ViewHolder, float, float, int, boolean)} - */ - void onDrawOver(Canvas c, RecyclerView recyclerView, View view, - float dX, float dY, int actionState, boolean isCurrentlyActive); - - /** - * The default implementation for {@link ItemTouchHelper.Callback#clearView(RecyclerView, - * RecyclerView.ViewHolder)} - */ - void clearView(View view); - - /** - * The default implementation for {@link ItemTouchHelper.Callback#onSelectedChanged( - * RecyclerView.ViewHolder, int)} - */ - void onSelected(View view); -} - diff --git a/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtilImpl.java b/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtilImpl.java deleted file mode 100644 index ea25477bef..0000000000 --- a/app/src/main/java/android/support/v7/widget/helper/ItemTouchUIUtilImpl.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.widget.helper; - -import android.graphics.Canvas; -import android.support.v4.view.ViewCompat; -import android.support.v7.widget.RecyclerView; -import android.view.View; -import android.support.v7.recyclerview.R; - - -/** - * Package private class to keep implementations. Putting them inside ItemTouchUIUtil makes them - * public API, which is not desired in this case. - */ -class ItemTouchUIUtilImpl { - static class Lollipop extends Honeycomb { - @Override - public void onDraw(Canvas c, RecyclerView recyclerView, View view, - float dX, float dY, int actionState, boolean isCurrentlyActive) { - if (isCurrentlyActive) { - Object originalElevation = view.getTag(R.id.item_touch_helper_previous_elevation); - if (originalElevation == null) { - originalElevation = ViewCompat.getElevation(view); - float newElevation = 1f + findMaxElevation(recyclerView, view); - ViewCompat.setElevation(view, newElevation); - view.setTag(R.id.item_touch_helper_previous_elevation, originalElevation); - } - } - super.onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive); - } - - private float findMaxElevation(RecyclerView recyclerView, View itemView) { - final int childCount = recyclerView.getChildCount(); - float max = 0; - for (int i = 0; i < childCount; i++) { - final View child = recyclerView.getChildAt(i); - if (child == itemView) { - continue; - } - final float elevation = ViewCompat.getElevation(child); - if (elevation > max) { - max = elevation; - } - } - return max; - } - - @Override - public void clearView(View view) { - final Object tag = view.getTag(R.id.item_touch_helper_previous_elevation); - if (tag != null && tag instanceof Float) { - ViewCompat.setElevation(view, (Float) tag); - } - view.setTag(R.id.item_touch_helper_previous_elevation, null); - super.clearView(view); - } - } - - static class Honeycomb implements ItemTouchUIUtil { - - @Override - public void clearView(View view) { - ViewCompat.setTranslationX(view, 0f); - ViewCompat.setTranslationY(view, 0f); - } - - @Override - public void onSelected(View view) { - - } - - @Override - public void onDraw(Canvas c, RecyclerView recyclerView, View view, - float dX, float dY, int actionState, boolean isCurrentlyActive) { - ViewCompat.setTranslationX(view, dX); - ViewCompat.setTranslationY(view, dY); - } - - @Override - public void onDrawOver(Canvas c, RecyclerView recyclerView, - View view, float dX, float dY, int actionState, boolean isCurrentlyActive) { - - } - } - - static class Gingerbread implements ItemTouchUIUtil { - - private void draw(Canvas c, RecyclerView parent, View view, - float dX, float dY) { - c.save(); - c.translate(dX, dY); - parent.drawChild(c, view, 0); - c.restore(); - } - - @Override - public void clearView(View view) { - view.setVisibility(View.VISIBLE); - } - - @Override - public void onSelected(View view) { - view.setVisibility(View.INVISIBLE); - } - - @Override - public void onDraw(Canvas c, RecyclerView recyclerView, View view, - float dX, float dY, int actionState, boolean isCurrentlyActive) { - if (actionState != ItemTouchHelper.ACTION_STATE_DRAG) { - draw(c, recyclerView, view, dX, dY); - } - } - - @Override - public void onDrawOver(Canvas c, RecyclerView recyclerView, - View view, float dX, float dY, - int actionState, boolean isCurrentlyActive) { - if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) { - draw(c, recyclerView, view, dX, dY); - } - } - } -} diff --git a/app/src/main/java/android/support/v7/widget/util/SortedListAdapterCallback.java b/app/src/main/java/android/support/v7/widget/util/SortedListAdapterCallback.java deleted file mode 100644 index 4921541b3c..0000000000 --- a/app/src/main/java/android/support/v7/widget/util/SortedListAdapterCallback.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.support.v7.widget.util; - -import android.support.v7.util.SortedList; -import android.support.v7.widget.RecyclerView; - -/** - * A {@link SortedList.Callback} implementation that can bind a {@link SortedList} to a - * {@link RecyclerView.Adapter}. - */ -public abstract class SortedListAdapterCallback extends SortedList.Callback { - - final RecyclerView.Adapter mAdapter; - - /** - * Creates a {@link SortedList.Callback} that will forward data change events to the provided - * Adapter. - * - * @param adapter The Adapter instance which should receive events from the SortedList. - */ - public SortedListAdapterCallback(RecyclerView.Adapter adapter) { - mAdapter = adapter; - } - - @Override - public void onInserted(int position, int count) { - mAdapter.notifyItemRangeInserted(position, count); - } - - @Override - public void onRemoved(int position, int count) { - mAdapter.notifyItemRangeRemoved(position, count); - } - - @Override - public void onMoved(int fromPosition, int toPosition) { - mAdapter.notifyItemMoved(fromPosition, toPosition); - } - - @Override - public void onChanged(int position, int count) { - mAdapter.notifyItemRangeChanged(position, count); - } -}