Skip to content

Commit

Permalink
reset adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
BakerJQ committed Mar 23, 2017
1 parent c564f8e commit 17f9a25
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
38 changes: 8 additions & 30 deletions app/src/main/java/com/bakerj/infinitecards/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,18 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
private InfiniteCardView mCardView;
private MyAdapter mAdapter;
private BaseAdapter mAdapter1, mAdapter2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCardView = (InfiniteCardView) findViewById(R.id.view);
mAdapter = new MyAdapter();
mAdapter.addColor(Color.WHITE);
mAdapter.addColor(Color.RED);
mAdapter.addColor(Color.BLACK);
mAdapter.addColor(Color.YELLOW);
mAdapter.addColor(Color.CYAN);
mCardView.setAdapter(mAdapter);
mAdapter1 = new MyAdapter();
mCardView.setAdapter(mAdapter1);
initButton();
}

Expand All @@ -45,7 +39,7 @@ private void initButton() {
@Override
public void onClick(View v) {
setStyle1();
mCardView.bringCardToFront(mAdapter.getCount() - 1);
mCardView.bringCardToFront(mAdapter1.getCount() - 1);
}
});
findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -151,16 +145,16 @@ public void transformInterpolatedAnimation(CardItem card, float fraction, int ca
}

private static class MyAdapter extends BaseAdapter {
private List<Integer> colors = new ArrayList<>();
private int[] colors = {Color.WHITE, Color.RED, Color.BLACK, Color.CYAN, Color.YELLOW};

@Override
public int getCount() {
return colors.size();
return colors.length;
}

@Override
public Integer getItem(int position) {
return colors.get(position);
return colors[position];
}

@Override
Expand All @@ -174,24 +168,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout
.item_card, parent, false);
}
convertView.setBackgroundColor(colors.get(position));
convertView.setBackgroundColor(colors[position]);
return convertView;
}

void addColor(int color) {
colors.add(color);
}

void removeColor() {
if (colors.size() > 0) {
colors.remove(0);
}
}

void changeColor(int position, int color) {
if (colors.size() > position) {
colors.set(position, color);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,12 @@ public void onAnimationRepeat(Animator animation) {
*
* @param adapter adapter
*/
void initAdapterView(BaseAdapter adapter) {
void initAdapterView(BaseAdapter adapter, boolean reset) {
if (mCardWidth > 0 && mCardHeight > 0) {
if (mCards == null) {
mCardView.removeAllViews();
firstSetAdapter(adapter);
} else if (mCards.size() != adapter.getCount()) {
} else if (reset || mCards.size() != adapter.getCount()) {
resetAdapter(adapter);
} else {
notifySetAdapter(adapter);
Expand All @@ -367,7 +367,7 @@ void initAdapterView(BaseAdapter adapter) {
private void resetAdapter(BaseAdapter adapter) {
if (mTransformerAnimRemove == null) {
mCardView.removeAllViews();
initAdapterView(adapter);
firstSetAdapter(adapter);
} else {
mIsAddRemoveAnim = true;
for (int i = 0; i < mCardCount; i++) {
Expand Down Expand Up @@ -541,7 +541,7 @@ void notifyDataSetChanged(BaseAdapter adapter) {
mTempAdapter = adapter;
} else {
mTempAdapter = null;
initAdapterView(adapter);
initAdapterView(adapter, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mCardWidth = getMeasuredWidth();
mCardHeight = (int) (mCardWidth * mCardRatio);
mAnimationHelper.setCardSize(mCardWidth, mCardHeight);
mAnimationHelper.initAdapterView(mAdapter);
mAnimationHelper.initAdapterView(mAdapter, true);
}
}

Expand Down Expand Up @@ -128,14 +128,14 @@ public void bringCardToFront(int position) {
* @param adapter adapter
*/
public void setAdapter(BaseAdapter adapter) {
removeAllViews();
this.mAdapter = adapter;
mAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
mAnimationHelper.notifyDataSetChanged(mAdapter);
}
});
mAnimationHelper.initAdapterView(adapter, true);
}

public void setTransformerToFront(AnimationTransformer toFrontTransformer) {
Expand Down

0 comments on commit 17f9a25

Please sign in to comment.