Created
May 20, 2015 21:37
-
-
Save ashughes/c1e0943c84b6c80497e0 to your computer and use it in GitHub Desktop.
RecyclerView Scroll on Insert Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/recycle" | |
android:layout_width="match_parent" | |
android:scrollbars="vertical" | |
android:layout_height="match_parent" | |
tools:context=".RecyclerActivity" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 22 | |
buildToolsVersion "22.0.1" | |
defaultConfig { | |
applicationId "com.steadfastinnovation.recyclerissue" | |
minSdkVersion 15 | |
targetSdkVersion 22 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile 'com.android.support:appcompat-v7:22.1.1' | |
compile 'com.android.support:recyclerview-v7:22.1.0' // 'Bug' introduced in 22.1.0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<View xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="200dp" | |
android:layout_height="200dp" | |
android:layout_margin="8dp" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.steadfastinnovation.recyclerissue; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.GridLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
public class RecyclerActivity extends AppCompatActivity { | |
RecyclerView mRecyclerView; | |
CustomAdapter mAdapter; | |
RecyclerView.LayoutManager mLayoutManager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_recycler); | |
mRecyclerView = (RecyclerView) findViewById(R.id.recycle); | |
mLayoutManager = new GridLayoutManager(this, 3); | |
mAdapter = new CustomAdapter(); | |
mRecyclerView.setAdapter(mAdapter); | |
mRecyclerView.setLayoutManager(mLayoutManager); | |
// Prepopulate with data | |
for (int i = 0; i < 20; i++) { | |
mAdapter.add(false); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_recycler, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_add) { | |
mAdapter.add(false); | |
return true; | |
} | |
if (id == R.id.action_add_to_front) { | |
mAdapter.add(true); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
private static class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { | |
public class ViewHolder extends RecyclerView.ViewHolder { | |
View view; | |
public ViewHolder(View itemView) { | |
super(itemView); | |
view = itemView; | |
} | |
public void bindData(Data data) { | |
view.setBackgroundColor(data.color); | |
} | |
} | |
List<Data> mData = new ArrayList<>(); | |
public CustomAdapter() { | |
} | |
private void add(boolean toFront) { | |
Data data = new Data(); | |
int pos; | |
if (toFront) { | |
pos = 0; | |
mData.add(0, data); | |
} else { | |
pos = mData.size(); | |
mData.add(data); | |
} | |
notifyItemInserted(pos); | |
} | |
private Data getData(int i) { | |
return mData.get(i); | |
} | |
private int getSize() { | |
return mData.size(); | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View v = LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.grid_item, parent, false); | |
return new ViewHolder(v); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder viewHolder, int i) { | |
viewHolder.bindData(getData(i)); | |
} | |
@Override | |
public int getItemCount() { | |
return getSize(); | |
} | |
} | |
private static class Data { | |
private static final Random RANDOM = new Random(System.currentTimeMillis()); | |
int color; | |
public Data() { | |
color = Color.argb(255, RANDOM.nextInt(256), RANDOM.nextInt(256), RANDOM.nextInt(256)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment