Skip to content

Commit

Permalink
Fix the issue that Payload isn't correctly passed to an Adapter. (#299)
Browse files Browse the repository at this point in the history
The issue was caused because FlexboxLayoutManager returned a scrapped
view holder when it should be returned through the RecyclerView (it's
also returned from the scrapped views, but additional procedures are
processed, such as passing the payload objects if any,
if returned through the RecyclerView)
  • Loading branch information
thagikura committed Jun 16, 2017
1 parent b3c4e4a commit 73642e9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
// NOTE: Do not place your application dependencies here; they belong
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@

package com.google.android.flexbox.recyclerview;

import com.google.android.apps.flexbox.R;
import com.google.android.flexbox.FlexItemChangedListenerImplRecyclerView;
import com.google.android.flexbox.FlexItemClickListener;
import com.google.android.flexbox.FlexboxLayoutManager;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.apps.flexbox.R;
import com.google.android.flexbox.FlexItemChangedListenerImplRecyclerView;
import com.google.android.flexbox.FlexItemClickListener;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -52,6 +51,7 @@ public FlexItemAdapter(AppCompatActivity activity, FlexboxLayoutManager layoutMa
public FlexItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.viewholder_flex_item, parent, false);

return new FlexItemViewHolder(view);
}

Expand Down Expand Up @@ -80,7 +80,7 @@ public void removeItem(int position) {
}

public List<FlexboxLayoutManager.LayoutParams> getItems() {
return Collections.unmodifiableList(mLayoutParams);
return new ArrayList<>(mLayoutParams);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3360,6 +3360,45 @@ public void run() {
}
}


@Test
@FlakyTest
public void testNotifyItemChange_withPayload() throws Throwable {
// This test verifies the payload is correctly passed to the Adapter in the case
// that notifying an item with payload
// https://github.com/google/flexbox-layout/issues/297

final FlexboxTestActivity activity = mActivityRule.getActivity();
final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager();
final TestAdapter adapter = new TestAdapter();
mActivityRule.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.setContentView(R.layout.recyclerview);
RecyclerView recyclerView = (RecyclerView) activity.findViewById(R.id.recyclerview);
layoutManager.setFlexDirection(FlexDirection.COLUMN);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70);
adapter.addItem(lp);
}
});
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
assertThat(adapter.getPayloads().size(), is(0));

final String payload = "payload";
mActivityRule.runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.changeItemWithPayload(0, payload);
}
});
InstrumentationRegistry.getInstrumentation().waitForIdleSync();

assertThat(adapter.getPayloads().size(), is(1));
assertThat((String) adapter.getPayloads().get(0), is(payload));
}

/**
* Creates a new flex item.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class TestAdapter extends RecyclerView.Adapter<TestViewHolder> {

private List<FlexboxLayoutManager.LayoutParams> mLayoutParams;

private List<Object> mReceivedPayloads = new ArrayList<>();

TestAdapter() {
this(new ArrayList<FlexboxLayoutManager.LayoutParams>());
}
Expand All @@ -57,6 +59,12 @@ public void onBindViewHolder(TestViewHolder holder, int position) {
holder.mTextView.setLayoutParams(mLayoutParams.get(position));
}

@Override
public void onBindViewHolder(TestViewHolder holder, int position, List<Object> payloads) {
mReceivedPayloads.addAll(payloads);
onBindViewHolder(holder, position);
}

void addItem(int position, FlexboxLayoutManager.LayoutParams flexItem) {
mLayoutParams.add(position, flexItem);
notifyItemInserted(position);
Expand All @@ -67,6 +75,14 @@ void addItem(FlexboxLayoutManager.LayoutParams flexItem) {
notifyItemInserted(mLayoutParams.size() - 1);
}

void changeItemWithPayload(int position, Object payload) {
notifyItemChanged(position, payload);
}

List<Object> getPayloads() {
return new ArrayList<>(mReceivedPayloads);
}

FlexboxLayoutManager.LayoutParams getItemAt(int index) {
return mLayoutParams.get(index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,6 @@ public View getFlexItemAt(int index) {
if (cachedView != null) {
return cachedView;
}

// Look up from the scrap next if there is a matching recycled view
// to avoid the same view holder is created from the adapter again
List<RecyclerView.ViewHolder> scrapList = mRecycler.getScrapList();
for (int i = 0, scrapCount = scrapList.size(); i < scrapCount; i++) {
RecyclerView.ViewHolder viewHolder = scrapList.get(i);
if (viewHolder.getAdapterPosition() == index) {
return viewHolder.itemView;
}
}
return mRecycler.getViewForPosition(index);
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Aug 17 13:26:39 JST 2016
#Tue Jun 13 19:10:11 JST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

0 comments on commit 73642e9

Please sign in to comment.