From 3edd4b8e28a831d6b9d07525b798c8bb54286ed8 Mon Sep 17 00:00:00 2001 From: guliash Date: Fri, 22 Jul 2016 11:21:06 +0300 Subject: [PATCH] adapter with position dependent types; example animation; --- .idea/.name | 1 - .idea/gradle.xml | 9 ++---- .idea/misc.xml | 18 ----------- README.md | 7 +++++ app/build.gradle | 10 +++--- .../ItemAdapter.java | 2 +- .../MainActivity.java | 10 ++++++ .../PositionTypeAdapter.java | 22 +++++++++++++ .../RecyclerViewActivity.java | 29 ++++++++++-------- app/src/main/res/layout/activity_main.xml | 9 ++++++ pictures/grid.gif | Bin 0 -> 433445 bytes pictures/linear.gif | Bin 0 -> 310526 bytes 12 files changed, 72 insertions(+), 45 deletions(-) delete mode 100644 .idea/.name create mode 100644 README.md create mode 100644 app/src/main/java/ru/rma/androiddraganddropexample/PositionTypeAdapter.java create mode 100644 pictures/grid.gif create mode 100644 pictures/linear.gif diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 341398e..0000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -AndroidDragAndDropExample \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 97c5821..cfe4315 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -5,19 +5,14 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 13e6014..7c1371c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,8 +1,5 @@ - - - \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..eada0df --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +GridLayoutManager with different view types flickering + +![Grid](pictures/grid.gif "Grid") + +LinearLayoutManager with position dependent view types + +![Grid](pictures/linear.gif "Grid") \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 4013af9..48c2c1b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 24 - buildToolsVersion "24.0.1" + compileSdkVersion 23 + buildToolsVersion "23.0.3" defaultConfig { applicationId "ru.rma.androiddraganddropexample" minSdkVersion 14 - targetSdkVersion 24 + targetSdkVersion 23 versionCode 1 versionName "1.0" } @@ -21,6 +21,6 @@ android { dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') - compile 'com.android.support:appcompat-v7:24.1.0' - compile 'com.android.support:recyclerview-v7:24.1.0' + compile 'com.android.support:appcompat-v7:23.2.1' + compile 'com.android.support:recyclerview-v7:23.2.1' } diff --git a/app/src/main/java/ru/rma/androiddraganddropexample/ItemAdapter.java b/app/src/main/java/ru/rma/androiddraganddropexample/ItemAdapter.java index a658bef..123eb93 100644 --- a/app/src/main/java/ru/rma/androiddraganddropexample/ItemAdapter.java +++ b/app/src/main/java/ru/rma/androiddraganddropexample/ItemAdapter.java @@ -55,7 +55,7 @@ public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_type_10, parent, false); break; default: - throw new RuntimeException("Unknow view type: " + viewType); + throw new RuntimeException("Unknown view type: " + viewType); } return new ItemViewHolder(view); } diff --git a/app/src/main/java/ru/rma/androiddraganddropexample/MainActivity.java b/app/src/main/java/ru/rma/androiddraganddropexample/MainActivity.java index 71a9432..419b397 100644 --- a/app/src/main/java/ru/rma/androiddraganddropexample/MainActivity.java +++ b/app/src/main/java/ru/rma/androiddraganddropexample/MainActivity.java @@ -4,6 +4,7 @@ import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.widget.RecyclerView; import android.view.View; @@ -37,14 +38,23 @@ public void onClick(View view) { case R.id.btn_staggeredGridDiff: startRecyclerViewActivity(RecyclerViewActivity.LAYOUT_MANAGER_STAGGERED_GRID, true); break; + case R.id.btn_linearPositionDependentTypes: + startRecyclerViewActivity(RecyclerViewActivity.LAYOUT_MANAGER_LINEAR, true, true); + break; } } private void startRecyclerViewActivity(int layoutManagerType, boolean diffViewTypes) { + startRecyclerViewActivity(layoutManagerType, diffViewTypes, false); + } + + private void startRecyclerViewActivity(int layoutManagerType, boolean diffViewTypes, + boolean typesPosDependent) { Intent intent = new Intent(this, RecyclerViewActivity.class); intent.putExtra(RecyclerViewActivity.KEY_LAYOUT_MANAGER_TYPE, layoutManagerType); intent.putExtra(RecyclerViewActivity.KEY_DIFFERENT_VIEW_TYPES, diffViewTypes); + intent.putExtra(RecyclerViewActivity.KEY_TYPES_POS_DEPENDENT, typesPosDependent); ActivityCompat.startActivity(this, intent, null); } } diff --git a/app/src/main/java/ru/rma/androiddraganddropexample/PositionTypeAdapter.java b/app/src/main/java/ru/rma/androiddraganddropexample/PositionTypeAdapter.java new file mode 100644 index 0000000..0c57710 --- /dev/null +++ b/app/src/main/java/ru/rma/androiddraganddropexample/PositionTypeAdapter.java @@ -0,0 +1,22 @@ +package ru.rma.androiddraganddropexample; + +import android.support.annotation.NonNull; + +import java.util.List; + +public class PositionTypeAdapter extends ItemAdapter { + public PositionTypeAdapter(@NonNull List items) { + super(items); + } + + @Override + public int getItemViewType(int position) { + if(position == 0) { + return R.id.item_type_1; + } else if(position == getItemCount() - 1) { + return R.id.item_type_2; + } else { + return R.id.item_type_3; + } + } +} diff --git a/app/src/main/java/ru/rma/androiddraganddropexample/RecyclerViewActivity.java b/app/src/main/java/ru/rma/androiddraganddropexample/RecyclerViewActivity.java index 60ad17f..5fd5430 100644 --- a/app/src/main/java/ru/rma/androiddraganddropexample/RecyclerViewActivity.java +++ b/app/src/main/java/ru/rma/androiddraganddropexample/RecyclerViewActivity.java @@ -1,6 +1,5 @@ package ru.rma.androiddraganddropexample; -import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -18,6 +17,7 @@ public class RecyclerViewActivity extends AppCompatActivity { public static final String KEY_DIFFERENT_VIEW_TYPES = "ru.rma.androiddraganddropexample.RecyclerViewActivity.KEY_DIFFERENT_VIEW_TYPES"; public static final String KEY_LAYOUT_MANAGER_TYPE = "ru.rma.androiddraganddropexample.RecyclerViewActivity.KEY_LAYOUT_MANAGER_TYPE"; + public static final String KEY_TYPES_POS_DEPENDENT = "ru.rma.androiddraganddropexample.RecyclerViewActivity.KEY_TYPES_POS_DEPENDENT"; public static final int LAYOUT_MANAGER_LINEAR = 1; public static final int LAYOUT_MANAGER_GRID = 2; public static final int LAYOUT_MANAGER_STAGGERED_GRID = 3; @@ -27,7 +27,8 @@ public class RecyclerViewActivity extends AppCompatActivity { private int mLayoutManagerType; - private boolean isDifferentViewTypes; + private boolean mIsDifferentViewTypes; + private boolean mTypesPosDependent; @Override protected void onCreate(Bundle savedInstanceState) { @@ -39,19 +40,19 @@ protected void onCreate(Bundle savedInstanceState) { ab.setDisplayHomeAsUpEnabled(true); } - Intent intent = getIntent(); - if (savedInstanceState != null) { - isDifferentViewTypes = savedInstanceState.getBoolean(KEY_DIFFERENT_VIEW_TYPES, true); - mLayoutManagerType = savedInstanceState.getInt(KEY_LAYOUT_MANAGER_TYPE, LAYOUT_MANAGER_LINEAR); - } else if (intent != null) { - isDifferentViewTypes = intent.getBooleanExtra(KEY_DIFFERENT_VIEW_TYPES, true); - mLayoutManagerType = intent.getIntExtra(KEY_LAYOUT_MANAGER_TYPE, LAYOUT_MANAGER_LINEAR); + Bundle bundle = savedInstanceState != null ? savedInstanceState : getIntent().getExtras(); + if (bundle != null) { + mIsDifferentViewTypes = bundle.getBoolean(KEY_DIFFERENT_VIEW_TYPES, true); + mLayoutManagerType = bundle.getInt(KEY_LAYOUT_MANAGER_TYPE, LAYOUT_MANAGER_LINEAR); + mTypesPosDependent = bundle.getBoolean(KEY_TYPES_POS_DEPENDENT, false); } else { - isDifferentViewTypes = true; + mIsDifferentViewTypes = true; mLayoutManagerType = LAYOUT_MANAGER_LINEAR; + mTypesPosDependent = false; } - - ItemAdapter adapter = new ItemAdapter(createItemList()); + List models = createItemList(); + ItemAdapter adapter = mTypesPosDependent ? new PositionTypeAdapter(models) : + new ItemAdapter(models); RecyclerView rv = (RecyclerView) findViewById(R.id.rv_list); switch (mLayoutManagerType) { case LAYOUT_MANAGER_GRID: @@ -76,13 +77,15 @@ protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(KEY_LAYOUT_MANAGER_TYPE, mLayoutManagerType); + outState.putBoolean(KEY_DIFFERENT_VIEW_TYPES, mIsDifferentViewTypes); + outState.putBoolean(KEY_TYPES_POS_DEPENDENT, mTypesPosDependent); } private List createItemList() { List list = new ArrayList<>(); - if (isDifferentViewTypes) { + if (mIsDifferentViewTypes) { list.add(new ItemModel(R.id.item_type_1, "1")); list.add(new ItemModel(R.id.item_type_2, "2")); list.add(new ItemModel(R.id.item_type_3, "3")); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6173193..a3aaafc 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -62,4 +62,13 @@ android:onClick="onClick" android:text="Staggered Grid Layout Manager Diff" android:gravity="center_vertical|start"/> + +