Skip to content

Commit 4f11baf

Browse files
added save instance fix layout for small dataset with rtl
1 parent 0d20e77 commit 4f11baf

File tree

6 files changed

+95
-39
lines changed

6 files changed

+95
-39
lines changed

library/src/main/java/com/cleveroad/adaptivetablelayout/AdaptiveTableLayout.java

+59-19
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public class AdaptiveTableLayout extends ViewGroup implements ScrollHelper.Scrol
105105
*/
106106
private ShadowHelper mShadowHelper;
107107

108+
private int mLayoutDirection;
109+
108110
/**
109111
* Instant state
110112
*/
@@ -139,12 +141,14 @@ public AdaptiveTableLayout(Context context, AttributeSet attrs, int defStyleAttr
139141
* @return true if layout direction is RightToLeft
140142
*/
141143
public boolean isRTL() {
142-
return LayoutDirectionHelper.isRTL(this);
144+
return LayoutDirectionHelper.isRTL();
143145
}
144146

145147
@Override
146148
public void setLayoutDirection(int layoutDirection) {
147149
super.setLayoutDirection(layoutDirection);
150+
LayoutDirectionHelper.setLayoutDirection(layoutDirection);
151+
mLayoutDirection = layoutDirection;
148152
mShadowHelper.onLayoutDirectionChanged();
149153
}
150154

@@ -203,6 +207,7 @@ protected Parcelable onSaveInstanceState() {
203207
mSaver = new TableInstanceSaver();
204208
mSaver.mScrollX = mState.getScrollX();
205209
mSaver.mScrollY = mState.getScrollY();
210+
mSaver.mLayoutDirection = mLayoutDirection;
206211
if (mAdapter != null) {
207212
mAdapter.onSaveInstanceState(bundle);
208213
}
@@ -264,10 +269,11 @@ private void initItems() {
264269
mState.getScrollY() + mSettings.getLayoutHeight());
265270
addViewHolders(mVisibleArea);
266271
if (mSaver != null) {
272+
setLayoutDirection(mSaver.mLayoutDirection);
267273
scrollTo(mSaver.mScrollX, mSaver.mScrollY);
268274
mSaver = null;
269275
} else if (isRTL()) {
270-
scrollTo((int) mManager.getFullWidth(), 0);
276+
scrollTo(mSettings.getLayoutWidth(), 0);
271277
}
272278
}
273279

@@ -365,7 +371,7 @@ public void scrollTo(int x, int y) {
365371
long maxX = mManager.getFullWidth() + shadowShiftX;
366372
long maxY = mManager.getFullHeight() + shadowShiftY;
367373

368-
if (x < 0) {
374+
if (x <= 0) {
369375
// scroll over view to the left
370376
absoluteX = 0;
371377
} else if (mSettings.getLayoutWidth() + x > maxX) {
@@ -375,7 +381,7 @@ public void scrollTo(int x, int y) {
375381

376382
mState.setScrollX(absoluteX);
377383

378-
if (y < 0) {
384+
if (y <= 0) {
379385
// scroll over view to the top
380386
absoluteY = 0;
381387
} else if (mSettings.getLayoutHeight() + absoluteY > maxY) {
@@ -411,7 +417,7 @@ public void scrollBy(int x, int y) {
411417
long maxX = mManager.getFullWidth() + shadowShiftX;
412418
long maxY = mManager.getFullHeight() + shadowShiftY;
413419

414-
if (mState.getScrollX() + tempX < 0) {
420+
if (mState.getScrollX() + tempX <= 0) {
415421
// scroll over view to the left
416422
diffX = mState.getScrollX();
417423
mState.setScrollX(0);
@@ -428,7 +434,7 @@ public void scrollBy(int x, int y) {
428434
mState.setScrollX(mState.getScrollX() + tempX);
429435
}
430436

431-
if (mState.getScrollY() + tempY < 0) {
437+
if (mState.getScrollY() + tempY <= 0) {
432438
// scroll over view to the top
433439
diffY = mState.getScrollY();
434440
mState.setScrollY(0);
@@ -525,6 +531,11 @@ private void refreshItemViewHolder(@NonNull ViewHolder holder,
525531
boolean isRowDragging, boolean isColumnDragging) {
526532

527533
int left = mManager.getColumnsWidth(0, Math.max(0, holder.getColumnIndex()));
534+
if (isRTL() && mManager.getFullWidth() <= mSettings.getLayoutWidth()) {
535+
left = getRowHeaderStartX()
536+
- (mManager.getColumnsWidthRtl(mManager.getColumnCount() - 1, Math.max(0, holder.getColumnIndex()))
537+
+ mSettings.getCellMargin());
538+
}
528539
int top = mManager.getRowsHeight(0, Math.max(0, holder.getRowIndex()));
529540
View view = holder.getItemView();
530541
if (isColumnDragging && holder.isDragging() && mDragAndDropPoints.getOffset().x > 0) {
@@ -541,12 +552,13 @@ private void refreshItemViewHolder(@NonNull ViewHolder holder,
541552
}
542553
int leftMargin = holder.getColumnIndex() * mSettings.getCellMargin() + mSettings.getCellMargin();
543554
int topMargin = holder.getRowIndex() * mSettings.getCellMargin() + mSettings.getCellMargin();
555+
if (!isRTL()) {
556+
left += mManager.getHeaderRowWidth();
557+
}
544558

545559
// calculate view layout positions
546560
int viewPosLeft = left - mState.getScrollX() + leftMargin;
547-
if (!isRTL()) {
548-
viewPosLeft += mManager.getHeaderRowWidth();
549-
}
561+
550562
int viewPosRight = viewPosLeft + mManager.getColumnWidth(holder.getColumnIndex());
551563
int viewPosTop = top - mState.getScrollY() + mManager.getHeaderColumnHeight() + topMargin;
552564
int viewPosBottom = viewPosTop + mManager.getRowHeight(holder.getRowIndex());
@@ -574,6 +586,9 @@ private void refreshHeaderColumnViewHolder(ViewHolder holder) {
574586
int left = mManager.getColumnsWidth(0, Math.max(0, holder.getColumnIndex()));
575587
if (!isRTL()) {
576588
left += mManager.getHeaderRowWidth();
589+
} else if (isRTL() && mManager.getFullWidth() <= mSettings.getLayoutWidth()) {
590+
left = getRowHeaderStartX() - (mManager.getColumnsWidthRtl(mManager.getColumnCount() - 1,
591+
holder.getColumnIndex()) + mSettings.getCellMargin());
577592
}
578593

579594
int top = mSettings.isHeaderFixed() ? 0 : -mState.getScrollY();
@@ -653,8 +668,9 @@ private void refreshHeaderColumnViewHolder(ViewHolder holder) {
653668
private void refreshHeaderRowViewHolder(ViewHolder holder) {
654669
int top = mManager.getRowsHeight(0, Math.max(0, holder.getRowIndex())) + mManager.getHeaderColumnHeight();
655670
int left = calculateRowHeadersLeft();
656-
if (isRTL())
671+
if (isRTL()) {
657672
left += mSettings.getCellMargin();
673+
}
658674
View view = holder.getItemView();
659675

660676
int leftMargin = holder.getColumnIndex() * mSettings.getCellMargin() + mSettings.getCellMargin();
@@ -754,9 +770,13 @@ private int calculateRowHeadersLeft() {
754770
if (!isRTL()) {
755771
left = -mState.getScrollX();
756772
} else {
757-
left = -mState.getScrollX()
758-
+ (int) (mManager.getFullWidth() - mManager.getHeaderRowWidth())
759-
+ mManager.getColumnCount() * mSettings.getCellMargin();
773+
if (mManager.getFullWidth() <= mSettings.getLayoutWidth()) {
774+
left = -mState.getScrollX() + getRowHeaderStartX();
775+
} else {
776+
left = -mState.getScrollX()
777+
+ (int) (mManager.getFullWidth() - mManager.getHeaderRowWidth())
778+
+ mManager.getColumnCount() * mSettings.getCellMargin();
779+
}
760780
}
761781
}
762782
return left;
@@ -798,6 +818,9 @@ private void recycleViewHolders(boolean isRecycleAll) {
798818
}
799819
}
800820
}
821+
if (!headerKeysToRemove.isEmpty()) {
822+
headerKeysToRemove.clear();
823+
}
801824
// column header view holders
802825
for (int count = mHeaderColumnViewHolders.size(), i = 0; i < count; i++) {
803826
int key = mHeaderColumnViewHolders.keyAt(i);
@@ -817,6 +840,9 @@ private void recycleViewHolders(boolean isRecycleAll) {
817840

818841
removeKeys(headerKeysToRemove, mHeaderColumnViewHolders);
819842

843+
if (!headerKeysToRemove.isEmpty()) {
844+
headerKeysToRemove.clear();
845+
}
820846
// row header view holders
821847
for (int count = mHeaderRowViewHolders.size(), i = 0; i < count; i++) {
822848
int key = mHeaderRowViewHolders.keyAt(i);
@@ -861,6 +887,15 @@ private void recycleViewHolder(ViewHolder holder) {
861887
mAdapter.onViewHolderRecycled(holder);
862888
}
863889

890+
891+
private int getEmptySpace() {
892+
if (isRTL() && mSettings.getLayoutWidth() > mManager.getFullWidth()) {
893+
return mSettings.getLayoutWidth() - (int) mManager.getFullWidth();
894+
} else {
895+
return 0;
896+
}
897+
}
898+
864899
/**
865900
* Create and add view holders with views to the layout.
866901
*
@@ -1049,7 +1084,7 @@ public boolean onTouchEvent(MotionEvent event) {
10491084
return mScrollHelper.onTouch(event);
10501085
}
10511086
// calculate absolute x, y
1052-
int absoluteX = (int) (mState.getScrollX() + event.getX());
1087+
int absoluteX = (int) (mState.getScrollX() + event.getX()) - getEmptySpace();
10531088
int absoluteY = (int) (mState.getScrollY() + event.getY());
10541089

10551090
// if column drag and drop mode and column offset > SHIFT_VIEWS_THRESHOLD
@@ -1062,8 +1097,9 @@ public boolean onTouchEvent(MotionEvent event) {
10621097
if (fromColumn != toColumn) {
10631098
int columnWidth = mManager.getColumnWidth(toColumn);
10641099
int absoluteColumnX = mManager.getColumnsWidth(0, toColumn);
1065-
if (!isRTL())
1100+
if (!isRTL()) {
10661101
absoluteColumnX += mManager.getHeaderRowWidth();
1102+
}
10671103

10681104
if (fromColumn < toColumn) {
10691105
// left column is dragging one
@@ -1327,6 +1363,7 @@ protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
13271363
int itemsAndColumnsLeft = !isRTL()
13281364
? Math.max(0, mManager.getHeaderRowWidth() - headerFixedX)
13291365
: 0;
1366+
13301367
int itemsAndColumnsRight = mSettings.getLayoutWidth();
13311368
if (isRTL()) {
13321369
itemsAndColumnsRight += mSettings.getCellMargin()
@@ -1577,7 +1614,7 @@ private ViewHolder getViewHolderByPosition(int x, int y) {
15771614
} else if (mSettings.isHeaderFixed()) {
15781615
if (tempY < mManager.getHeaderColumnHeight()) {
15791616
// coordinate x, y in the column header's area
1580-
int column = mManager.getColumnByXWithShift(absX, mSettings.getCellMargin());
1617+
int column = mManager.getColumnByXWithShift(absX - getEmptySpace(), mSettings.getCellMargin());
15811618
viewHolder = mHeaderColumnViewHolders.get(column);
15821619
} else if (tempX < mManager.getHeaderRowWidth() && !isRTL()
15831620
|| tempX > calculateRowHeadersLeft() && isRTL()) {
@@ -1586,14 +1623,14 @@ private ViewHolder getViewHolderByPosition(int x, int y) {
15861623
viewHolder = mHeaderRowViewHolders.get(row);
15871624
} else {
15881625
// coordinate x, y in the items area
1589-
int column = mManager.getColumnByXWithShift(absX, mSettings.getCellMargin());
1626+
int column = mManager.getColumnByXWithShift(absX - getEmptySpace(), mSettings.getCellMargin());
15901627
int row = mManager.getRowByYWithShift(absY, mSettings.getCellMargin());
15911628
viewHolder = mViewHolders.get(row, column);
15921629
}
15931630
} else {
15941631
if (absY < mManager.getHeaderColumnHeight()) {
15951632
// coordinate x, y in the column header's area
1596-
int column = mManager.getColumnByXWithShift(absX, mSettings.getCellMargin());
1633+
int column = mManager.getColumnByXWithShift(absX - getEmptySpace(), mSettings.getCellMargin());
15971634
viewHolder = mHeaderColumnViewHolders.get(column);
15981635
} else if (absX < mManager.getHeaderRowWidth() && !isRTL()
15991636
|| absX > calculateRowHeadersLeft() && isRTL()) {
@@ -1602,7 +1639,7 @@ private ViewHolder getViewHolderByPosition(int x, int y) {
16021639
viewHolder = mHeaderRowViewHolders.get(row);
16031640
} else {
16041641
// coordinate x, y in the items area
1605-
int column = mManager.getColumnByXWithShift(absX, mSettings.getCellMargin());
1642+
int column = mManager.getColumnByXWithShift(absX - getEmptySpace(), mSettings.getCellMargin());
16061643
int row = mManager.getRowByYWithShift(absY, mSettings.getCellMargin());
16071644
viewHolder = mViewHolders.get(row, column);
16081645
}
@@ -1748,6 +1785,7 @@ public TableInstanceSaver[] newArray(int size) {
17481785
};
17491786
private int mScrollX;
17501787
private int mScrollY;
1788+
private int mLayoutDirection;
17511789

17521790
public TableInstanceSaver() {
17531791
// no default data
@@ -1756,6 +1794,7 @@ public TableInstanceSaver() {
17561794
protected TableInstanceSaver(Parcel in) {
17571795
this.mScrollX = in.readInt();
17581796
this.mScrollY = in.readInt();
1797+
this.mLayoutDirection = in.readInt();
17591798
}
17601799

17611800
@Override
@@ -1767,6 +1806,7 @@ public int describeContents() {
17671806
public void writeToParcel(Parcel dest, int flags) {
17681807
dest.writeInt(this.mScrollX);
17691808
dest.writeInt(this.mScrollY);
1809+
dest.writeInt(this.mLayoutDirection);
17701810
}
17711811
}
17721812

library/src/main/java/com/cleveroad/adaptivetablelayout/AdaptiveTableManager.java

+9
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ int getColumnsWidth(int from, int to) {
116116
return width;
117117
}
118118

119+
int getColumnsWidthRtl(int from, int to){
120+
checkForInit();
121+
int width = 0;
122+
for (int i = from; i >= to && mColumnWidths != null; i--){
123+
width+=mColumnWidths[i];
124+
}
125+
return width;
126+
}
127+
119128
/**
120129
* @return columns count
121130
*/
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package com.cleveroad.adaptivetablelayout;
22

3-
import android.os.Build;
4-
import android.view.View;
5-
63
/**
74
* Helper for convenient work with layout direction with the version API below 17
85
*/
96
class LayoutDirectionHelper {
7+
private static int mLayoutDirection;
8+
9+
LayoutDirectionHelper(int direction) {
10+
mLayoutDirection = direction;
11+
}
12+
13+
static int getLayoutDirection() {
14+
return mLayoutDirection;
15+
}
1016

11-
static int getLayoutDirection(View view) {
12-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
13-
return view.getLayoutDirection();
14-
} else {
15-
return LayoutDirection.LTR;
16-
}
17+
static void setLayoutDirection(int direction) {
18+
mLayoutDirection = direction;
1719
}
1820

19-
static boolean isRTL(View view) {
20-
return getLayoutDirection(view) == LayoutDirection.RTL;
21+
static boolean isRTL() {
22+
return getLayoutDirection() == LayoutDirection.RTL;
2123
}
2224

2325
}

library/src/main/java/com/cleveroad/adaptivetablelayout/ShadowHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ShadowHelper(View v) {
2727
}
2828

2929
private int getLayoutDirection() {
30-
return LayoutDirectionHelper.getLayoutDirection(mParentView);
30+
return LayoutDirectionHelper.getLayoutDirection();
3131
}
3232

3333
@NonNull

sample/src/main/java/com/cleveroad/sample/ui/SampleActivity.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ protected void onCreate(Bundle savedInstanceState) {
2626

2727
@Override
2828
public void onCsvFileSelected(String fileName) {
29-
File file = new File(fileName);
30-
if (file.exists() && fileName.endsWith(".csv")) {
31-
getSupportFragmentManager()
32-
.beginTransaction()
33-
.replace(R.id.container, TableLayoutFragment.newInstance(fileName), CsvPickerFragment.class.getSimpleName())
34-
.addToBackStack(CsvPickerFragment.class.getSimpleName())
35-
.commit();
29+
if (fileName != null && !fileName.isEmpty()) {
30+
File file = new File(fileName);
31+
if (file.exists() && fileName.endsWith(".csv")) {
32+
getSupportFragmentManager()
33+
.beginTransaction()
34+
.replace(R.id.container, TableLayoutFragment.newInstance(fileName), CsvPickerFragment.class.getSimpleName())
35+
.addToBackStack(CsvPickerFragment.class.getSimpleName())
36+
.commit();
37+
} else {
38+
Toast.makeText(this, R.string.not_csv_file_error, Toast.LENGTH_SHORT).show();
39+
}
3640
} else {
37-
Toast.makeText(this, R.string.not_csv_file_error, Toast.LENGTH_SHORT).show();
41+
Toast.makeText(this, R.string.no_such_file_error, Toast.LENGTH_SHORT).show();
3842
}
3943
}
4044
}

sample/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
<string name="changes_saved">All changes have been saved</string>
4343
<string name="unexpected_error">Unexpected error</string>
44+
<string name="no_such_file_error">No such csv file</string>
4445
<string name="not_csv_file_error">Not csv file</string>
4546

4647
</resources>

0 commit comments

Comments
 (0)