Skip to content

Commit f6249ab

Browse files
author
ivan_syabro_cr
committed
Merge branches 'develop' and 'feature/issue_2/only_view_mode' of https://gitlab.cleveroad.com/open-source/android/table-layout into develop
1 parent 223524c commit f6249ab

File tree

11 files changed

+94
-36
lines changed

11 files changed

+94
-36
lines changed

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ public boolean isRTL() {
140140
return LayoutDirectionHelper.isRTL(this);
141141
}
142142

143+
@Override
144+
public void setLayoutDirection(int layoutDirection) {
145+
super.setLayoutDirection(layoutDirection);
146+
mShadowHelper.onLayoutDirectionChanged();
147+
}
148+
143149
@Override
144150
protected void onLayout(boolean changed, int l, int t, int r, int b) {
145151
if (changed) {
@@ -161,6 +167,7 @@ private void initAttrs(Context context, AttributeSet attrs) {
161167
mSettings.setHeaderFixed(a.getBoolean(R.styleable.AdaptiveTableLayout_fixedHeaders, true));
162168
mSettings.setCellMargin(a.getDimensionPixelSize(R.styleable.AdaptiveTableLayout_cellMargin, 0));
163169
mSettings.setSolidRowHeader(a.getBoolean(R.styleable.AdaptiveTableLayout_solidRowHeaders, true));
170+
mSettings.setDragAndDropEnabled(a.getBoolean(R.styleable.AdaptiveTableLayout_dragAndDropEnabled, true));
164171
} finally {
165172
a.recycle();
166173
}
@@ -1375,6 +1382,11 @@ public void onLongPress(MotionEvent e) {
13751382
// search view holder by x, y
13761383
ViewHolder viewHolder = getViewHolderByPosition((int) e.getX(), (int) e.getY());
13771384
if (viewHolder != null) {
1385+
1386+
if (!mSettings.isDragAndDropEnabled()){
1387+
checkLongPressForItemAndFirstHeader(viewHolder);
1388+
return;
1389+
}
13781390
// save start dragging touch position
13791391
mDragAndDropPoints.setStart((int) (mState.getScrollX() + e.getX()), (int) (mState.getScrollY() + e.getY()));
13801392
if (viewHolder.getItemType() == ViewHolderType.COLUMN_HEADER) {
@@ -1410,14 +1422,18 @@ public void onLongPress(MotionEvent e) {
14101422
refreshViewHolders();
14111423

14121424
} else {
1413-
OnItemLongClickListener onItemClickListener = mAdapter.getOnItemLongClickListener();
1414-
if (onItemClickListener != null) {
1415-
if (viewHolder.getItemType() == ViewHolderType.ITEM) {
1416-
onItemClickListener.onItemLongClick(viewHolder.getRowIndex(), viewHolder.getColumnIndex());
1417-
} else if (viewHolder.getItemType() == ViewHolderType.FIRST_HEADER) {
1418-
onItemClickListener.onLeftTopHeaderLongClick();
1419-
}
1420-
}
1425+
checkLongPressForItemAndFirstHeader(viewHolder);
1426+
}
1427+
}
1428+
}
1429+
1430+
private void checkLongPressForItemAndFirstHeader(ViewHolder viewHolder){
1431+
OnItemLongClickListener onItemClickListener = mAdapter.getOnItemLongClickListener();
1432+
if (onItemClickListener != null) {
1433+
if (viewHolder.getItemType() == ViewHolderType.ITEM) {
1434+
onItemClickListener.onItemLongClick(viewHolder.getRowIndex(), viewHolder.getColumnIndex());
1435+
} else if (viewHolder.getItemType() == ViewHolderType.FIRST_HEADER) {
1436+
onItemClickListener.onLeftTopHeaderLongClick();
14211437
}
14221438
}
14231439
}
@@ -1682,6 +1698,14 @@ public void setSolidRowHeader(boolean solidRowHeader) {
16821698
mSettings.setSolidRowHeader(solidRowHeader);
16831699
}
16841700

1701+
public boolean isDragAndDropEnabled(){
1702+
return mSettings.isDragAndDropEnabled();
1703+
}
1704+
1705+
public void setDragAndDropEnabled(boolean enabled){
1706+
mSettings.setDragAndDropEnabled(enabled);
1707+
}
1708+
16851709
private static class TableInstanceSaver implements Parcelable {
16861710
public static final Creator<TableInstanceSaver> CREATOR = new Creator<TableInstanceSaver>() {
16871711
@Override

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class AdaptiveTableLayoutSettings {
2424
*/
2525
private boolean mSolidRowHeader;
2626

27+
/**
28+
* If true, then the table can be edited, otherwise it is impossible
29+
*/
30+
private boolean mDragAndDropEnabled;
31+
2732

2833
AdaptiveTableLayoutSettings() {
2934
}
@@ -72,4 +77,12 @@ public AdaptiveTableLayoutSettings setSolidRowHeader(boolean solidRowHeader) {
7277
mSolidRowHeader = solidRowHeader;
7378
return this;
7479
}
80+
81+
public boolean isDragAndDropEnabled() {
82+
return mDragAndDropEnabled;
83+
}
84+
85+
public void setDragAndDropEnabled(boolean dragAndDropEnabled) {
86+
mDragAndDropEnabled = dragAndDropEnabled;
87+
}
7588
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ShadowHelper {
1919
private View mColumnsHeadersShadow;
2020
@Nullable
2121
private View mRowsHeadersShadow;
22+
2223
private View mParentView;
2324

2425
public ShadowHelper(View v) {
@@ -48,7 +49,7 @@ View getColumnsHeadersShadow() {
4849
View addRowsHeadersShadow(ViewGroup group) {
4950
if (mRowsHeadersShadow == null) {
5051
mRowsHeadersShadow = new View(group.getContext());
51-
mRowsHeadersShadow.setBackgroundResource(getLayoutDirection() == LayoutDirection.RTL
52+
mRowsHeadersShadow.setBackgroundResource(getLayoutDirection() == LayoutDirection.LTR
5253
? R.drawable.shadow_right
5354
: R.drawable.shadow_left);
5455
group.addView(mRowsHeadersShadow, 0);
@@ -156,4 +157,14 @@ void removeAllDragAndDropShadows(ViewGroup group) {
156157
mBottomShadow = null;
157158
}
158159
}
160+
161+
public void onLayoutDirectionChanged() {
162+
if (getRowsHeadersShadow() != null) {
163+
getRowsHeadersShadow().setBackgroundResource(
164+
getLayoutDirection() == LayoutDirection.LTR
165+
? R.drawable.shadow_right
166+
: R.drawable.shadow_left);
167+
getRowsHeadersShadow().requestLayout();
168+
}
169+
}
159170
}

library/src/main/res/drawable-ldrtl/shadow_left.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

library/src/main/res/drawable-ldrtl/shadow_right.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

library/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<attr name="fixedHeaders" format="boolean"/>
55
<attr name="cellMargin" format="dimension"/>
66
<attr name="solidRowHeaders" format="boolean"/>
7+
<attr name="dragAndDropEnabled" format="boolean"/>
78
</declare-styleable>
89
</resources>

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,15 @@ public boolean onMenuItemClick(MenuItem item) {
9494
SettingsDialog.newInstance(
9595
mTableLayout.isHeaderFixed(),
9696
mTableLayout.isSolidRowHeader(),
97-
mTableLayout.isRTL())
97+
mTableLayout.isRTL(),
98+
mTableLayout.isDragAndDropEnabled())
9899
.show(getChildFragmentManager(), SettingsDialog.class.getSimpleName());
99100
}
100101
return true;
101102
}
102103
});
103104
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
104-
mTableLayout.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
105+
// mTableLayout.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
105106
}
106107
initAdapter();
107108

@@ -154,6 +155,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
154155
data.getBooleanExtra(SettingsDialog.EXTRA_VALUE_RTL_DIRECTION, mTableLayout.isRTL())
155156
? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);
156157
}
158+
mTableLayout.setDragAndDropEnabled(data.getBooleanExtra(
159+
SettingsDialog.EXTRA_VALUE_DRAG_AND_DROP_ENABLED, mTableLayout.isDragAndDropEnabled()));
157160
mTableAdapter.notifyDataSetChanged();
158161
}
159162
}

sample/src/main/java/com/cleveroad/sample/ui/dialogs/SettingsDialog.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class SettingsDialog extends DialogFragment implements View.OnClickListen
2222
public static final String EXTRA_VALUE_SOLID_HEADER = "EXTRA_VALUE_SOLID_HEADER";
2323
public static final String EXTRA_VALUE_HEADER_FIXED = "EXTRA_VALUE_HEADER_FIXED";
2424
public static final String EXTRA_VALUE_RTL_DIRECTION = "EXTRA_VALUE_RTL_DIRECTION";
25+
public static final String EXTRA_VALUE_DRAG_AND_DROP_ENABLED = "EXTRA_VALUE_DRAG_AND_DROP_ENABLED";
2526

2627
/**
2728
* if true - value of row header fixed to the row. Fixed to the data
@@ -33,22 +34,25 @@ public class SettingsDialog extends DialogFragment implements View.OnClickListen
3334

3435
private boolean mIsRtlDirection;
3536

37+
private boolean mIsDragAndDropEnabled;
38+
3639
private SwitchCompat swSolidRow;
3740

3841
private SwitchCompat swFixedHeaders;
3942

4043
private SwitchCompat swRtlDirection;
4144

42-
public static SettingsDialog newInstance(boolean isHeaderFixed, boolean solidRowHeader) {
43-
return newInstance(isHeaderFixed, solidRowHeader, false);
44-
}
45+
private SwitchCompat swDragAndDropEnabled;
4546

46-
public static SettingsDialog newInstance(boolean isHeaderFixed, boolean solidRowHeader, boolean isRtlDirection) {
47+
48+
public static SettingsDialog newInstance(boolean isHeaderFixed, boolean solidRowHeader,
49+
boolean isRtlDirection ,boolean isDragAndDropEnabled) {
4750
SettingsDialog dialog = new SettingsDialog();
4851
Bundle args = new Bundle();
4952
args.putBoolean(EXTRA_VALUE_HEADER_FIXED, isHeaderFixed);
5053
args.putBoolean(EXTRA_VALUE_SOLID_HEADER, solidRowHeader);
5154
args.putBoolean(EXTRA_VALUE_RTL_DIRECTION, isRtlDirection);
55+
args.putBoolean(EXTRA_VALUE_DRAG_AND_DROP_ENABLED, isDragAndDropEnabled);
5256
dialog.setArguments(args);
5357
return dialog;
5458
}
@@ -59,6 +63,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
5963
mSolidRowHeader = getArguments().getBoolean(EXTRA_VALUE_SOLID_HEADER);
6064
mIsHeaderFixed = getArguments().getBoolean(EXTRA_VALUE_HEADER_FIXED);
6165
mIsRtlDirection = getArguments().getBoolean(EXTRA_VALUE_RTL_DIRECTION);
66+
mIsDragAndDropEnabled = getArguments().getBoolean(EXTRA_VALUE_DRAG_AND_DROP_ENABLED);
6267
}
6368

6469
@Nullable
@@ -71,6 +76,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
7176
swSolidRow = (SwitchCompat) view.findViewById(R.id.swSolidRow);
7277
swFixedHeaders = (SwitchCompat) view.findViewById(R.id.swFixedHeaders);
7378
swRtlDirection = (SwitchCompat) view.findViewById(R.id.swRtlDirection);
79+
swDragAndDropEnabled = (SwitchCompat) view.findViewById(R.id.swDragAndDropEnabled);
7480

7581
return view;
7682
}
@@ -85,6 +91,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
8591
swFixedHeaders.setChecked(mIsHeaderFixed);
8692
swSolidRow.setChecked(mSolidRowHeader);
8793
swRtlDirection.setChecked(mIsRtlDirection);
94+
swDragAndDropEnabled.setChecked(mIsDragAndDropEnabled);
8895

8996
swFixedHeaders.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
9097
@Override
@@ -104,6 +111,12 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
104111
mIsRtlDirection = isChecked;
105112
}
106113
});
114+
swDragAndDropEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
115+
@Override
116+
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
117+
mIsDragAndDropEnabled = isChecked;
118+
}
119+
});
107120

108121

109122
}
@@ -130,6 +143,7 @@ public void onClick(View v) {
130143
intent.putExtra(EXTRA_VALUE_SOLID_HEADER, mSolidRowHeader);
131144
intent.putExtra(EXTRA_VALUE_HEADER_FIXED, mIsHeaderFixed);
132145
intent.putExtra(EXTRA_VALUE_RTL_DIRECTION, mIsRtlDirection);
146+
intent.putExtra(EXTRA_VALUE_DRAG_AND_DROP_ENABLED, mIsDragAndDropEnabled);
133147
getParentFragment().onActivityResult(REQUEST_CODE_SETTINGS, Activity.RESULT_OK, intent);
134148
break;
135149
case R.id.bNegative:

sample/src/main/res/layout/dialog_settings.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@
5757
android:textColor="@color/colorTextWhite"
5858
android:textSize="18sp"/>
5959

60+
<android.support.v7.widget.SwitchCompat
61+
android:id="@+id/swDragAndDropEnabled"
62+
android:layout_width="match_parent"
63+
android:layout_height="wrap_content"
64+
android:layout_below="@+id/swRtlDirection"
65+
android:layout_marginTop="16dp"
66+
android:text="@string/settings_drag_and_drop_enabled"
67+
android:textColor="@color/colorTextWhite"
68+
android:textSize="18sp"/>
69+
6070
<LinearLayout
6171
android:id="@+id/llButtonsContainer"
6272
android:layout_width="match_parent"

sample/src/main/res/layout/fragment_tab_layout.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
android:layout_height="match_parent"
2626
android:layout_below="@+id/toolbar"
2727
app:cellMargin="1dp"
28+
app:dragAndDropEnabled="true"
2829
app:fixedHeaders="true"
29-
app:solidRowHeaders="true" />
30+
app:solidRowHeaders="true"/>
3031

3132
<View
3233
android:id="@+id/separator"

0 commit comments

Comments
 (0)