Skip to content

Commit ab7e95e

Browse files
author
Iain Connor
committed
Fixed a couple bugs with small datasets
1 parent a0f95d4 commit ab7e95e

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

app/src/main/java/com/tippingcanoe/dewey/app/DemoAdapter.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.graphics.Color;
44
import android.support.v7.widget.RecyclerView;
5+
import android.util.TypedValue;
6+
import android.view.Gravity;
57
import android.view.LayoutInflater;
68
import android.view.View;
79
import android.view.ViewGroup;
@@ -22,6 +24,12 @@ public DemoAdapter () {
2224
@Override
2325
public DeweyViewHolder onCreateViewHolder ( ViewGroup parent, int viewType ) {
2426
View deweyCell = LayoutInflater.from(parent.getContext()).inflate(R.layout.dewey_cell, parent, false);
27+
28+
//TextView deweyCell = new TextView(parent.getContext());
29+
//deweyCell.setLayoutParams(new RecyclerView.LayoutParams(40, ViewGroup.LayoutParams.MATCH_PARENT));
30+
//deweyCell.setGravity(Gravity.CENTER_HORIZONTAL);
31+
//deweyCell.setPadding(0, 10, 0, 10);
32+
2533
return new DeweyViewHolder(deweyCell);
2634
}
2735

@@ -32,7 +40,7 @@ public void onBindViewHolder ( DeweyViewHolder holder, int position ) {
3240

3341
@Override
3442
public int getItemCount () {
35-
return 50;//return sentances.size();
43+
return 2;//return sentances.size();
3644
}
3745

3846
protected List<String> getRandomSentances () {

library/src/main/java/com/tippingcanoe/dewey/DeweyDecorator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ protected void drawStrip ( RecyclerView parent, Canvas c, int firstVisiblePos, i
396396
if ((stripPosition == 0 && headerView != null) || (stripPosition == footerPos && footerView != null) || (stripPosition >= firstVisiblePos && stripPosition <= lastVisiblePos)) {
397397
View currentFocusedChild;
398398

399-
if (stripPosition == 0) {
399+
if (stripPosition == 0 && headerView != null) {
400400
currentFocusedChild = headerView;
401401
} else if (stripPosition == footerPos && footerView != null) {
402402
currentFocusedChild = footerView;

library/src/main/java/com/tippingcanoe/dewey/DeweyLayoutManager.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
class DeweyLayoutManager extends LinearLayoutManager {
1212
boolean uniformCells = false;
1313
int uniformCellWidth = 0;
14+
int forcedCellWidth = 0;
15+
1416
/**
1517
* Creates a vertical LinearLayoutManager
1618
*
@@ -91,15 +93,14 @@ protected int measureFirstChildHeightAndObtainWidth(RecyclerView.Recycler recycl
9193

9294
detachView(view);
9395

96+
updateForcedCellWidth();
97+
9498
return height;
9599
}
96100

97101
return 0;
98102
}
99103

100-
/**
101-
* {@inheritDoc}
102-
*/
103104
@Override
104105
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
105106
return updateLayoutParamsForUniformWidthIfNeeded(super.generateDefaultLayoutParams());
@@ -116,9 +117,14 @@ public RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet at
116117
return updateLayoutParamsForUniformWidthIfNeeded(super.generateLayoutParams(c, attrs));
117118
}
118119

120+
@Override
121+
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
122+
return super.checkLayoutParams(lp) && (forcedCellWidth == 0 || lp.width == forcedCellWidth);
123+
}
124+
119125
protected RecyclerView.LayoutParams updateLayoutParamsForUniformWidthIfNeeded ( RecyclerView.LayoutParams layoutParams ) {
120-
if ( uniformCells && uniformCellWidth != 0 && (uniformCellWidth * getItemCount()) < getWidth() ) {
121-
layoutParams.width = (int) ((float) getWidth() / (float) getItemCount());
126+
if ( forcedCellWidth != 0 ) {
127+
layoutParams.width = forcedCellWidth;
122128
}
123129

124130
return layoutParams;
@@ -134,5 +140,12 @@ public int getUniformCellWidth() {
134140

135141
public void setUniformCells(boolean uniformCells) {
136142
this.uniformCells = uniformCells;
143+
updateForcedCellWidth();
144+
}
145+
146+
protected void updateForcedCellWidth () {
147+
if ( uniformCells && uniformCellWidth != 0 && (uniformCellWidth * getItemCount()) < getWidth() ) {
148+
forcedCellWidth = (int) ((float) getWidth() / (float) getItemCount());
149+
}
137150
}
138151
}

0 commit comments

Comments
 (0)