Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

9 changes: 2 additions & 7 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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")
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}
Expand All @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<ItemModel> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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<ItemModel> 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:
Expand All @@ -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<ItemModel> createItemList() {
List<ItemModel> 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"));
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,13 @@
android:onClick="onClick"
android:text="Staggered Grid Layout Manager Diff"
android:gravity="center_vertical|start"/>

<Button
android:id="@+id/btn_linearPositionDependentTypes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_staggeredGridDiff"
android:onClick="onClick"
android:text="Linear Layout Manager with pos dependent types"
android:gravity="center_vertical|start"/>
</RelativeLayout>
Binary file added pictures/grid.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/linear.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.