Commit abd3d572 authored by rahadi's avatar rahadi

WIP: added pagination to TableView

parent 840cfb84
......@@ -41,7 +41,7 @@ public class ListingInstanceActivity extends AppCompatActivity {
tableHeaderView.setColumns(headerList);
Map<String, Map<String, String>> rowData = new LinkedHashMap<>();
for (int i = 0; i < 15; i++) {
for (int i = 0; i < 31; i++) {
Map<String, String> row = new LinkedHashMap<>();
row.put("no_sls", (i < 9 ? "00" : "0") + (i + 1));
row.put("no_bs", (i < 9 ? "00" : "0") + (i + 1));
......
package id.ac.stis.capi.lessthink.listeners;
import android.view.View;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public interface OnFooterNavigationListener {
int onGoToNextPage(View v);
int onGoToPrevPage(View v);
}
......@@ -10,7 +10,7 @@ import java.util.List;
import java.util.Set;
import id.ac.stis.capi.lessthink.listeners.DependenciesResolverListener;
import id.ac.stis.capi.lessthink.logics.DependencyDetails;
import id.ac.stis.capi.lessthink.models.DependencyDetails;
import id.ac.stis.capi.odk.application.Collect;
import id.ac.stis.capi.odk.dao.FormsDao;
import id.ac.stis.capi.odk.dto.Form;
......
......@@ -6,7 +6,6 @@ import android.graphics.PorterDuff;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
......@@ -16,6 +15,7 @@ import android.widget.TextView;
import android.widget.Toast;
import id.ac.stis.capi.R;
import id.ac.stis.capi.lessthink.listeners.OnFooterNavigationListener;
/**
* Author : Rahadi Jalu
......@@ -25,8 +25,10 @@ import id.ac.stis.capi.R;
public class TableFooterView extends RelativeLayout implements View.OnClickListener {
private ImageView addButton;
private TextView currPage;
private LinearLayout pageIndicator, navigationWrapper;
private int textSize, componentColor, footerColor;
private int textSize, componentColor, footerColor, currentPage, pageCount;
private OnFooterNavigationListener onFooterNavigationListener;
public TableFooterView(Context context, AttributeSet attrs) {
super(context, attrs);
......@@ -34,6 +36,12 @@ public class TableFooterView extends RelativeLayout implements View.OnClickListe
this.textSize = 12;
this.componentColor = ContextCompat.getColor(getContext(), R.color.white);
this.footerColor = ContextCompat.getColor(getContext(), R.color.accent);
this.currentPage = 1;
this.pageCount = 1;
if (getId() == NO_ID) {
setId(R.id.row_view_footer_id);
}
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TableFooterView,
0, 0);
......@@ -69,11 +77,11 @@ public class TableFooterView extends RelativeLayout implements View.OnClickListe
pageIndicator.addView(page);
TextView currPage = new TextView(getContext());
currPage = new TextView(getContext());
currPage.setTextColor(getComponentColor());
currPage.setTextSize(TypedValue.COMPLEX_UNIT_SP, getTextSize());
currPage.setText("1 of 3");
currPage.setText("1 of 1");
pageIndicator.addView(currPage);
......@@ -147,6 +155,15 @@ public class TableFooterView extends RelativeLayout implements View.OnClickListe
addView(addButton);
}
public void setPageCountInfo(int pageCount) {
this.pageCount = pageCount;
currPage.setText(currentPage + " of " + pageCount);
}
public void setOnFooterNavigationListener(OnFooterNavigationListener onFooterNavigationListener) {
this.onFooterNavigationListener = onFooterNavigationListener;
}
public int getTextSize() {
return textSize;
}
......@@ -184,10 +201,22 @@ public class TableFooterView extends RelativeLayout implements View.OnClickListe
Toast.makeText(getContext(), "ADD", Toast.LENGTH_LONG).show();
break;
case R.id.footer_view_prev_button:
Toast.makeText(getContext(), "PREV", Toast.LENGTH_LONG).show();
// Toast.makeText(getContext(), "PREV", Toast.LENGTH_LONG).show();
if (onFooterNavigationListener != null) {
this.currentPage = onFooterNavigationListener.onGoToPrevPage(view);
}
currPage.setText(currentPage + " of " + pageCount);
break;
case R.id.footer_view_next_button:
Toast.makeText(getContext(), "NEXT", Toast.LENGTH_LONG).show();
// Toast.makeText(getContext(), "NEXT", Toast.LENGTH_LONG).show();
if (onFooterNavigationListener != null) {
this.currentPage = onFooterNavigationListener.onGoToNextPage(view);
}
currPage.setText(currentPage + " of " + pageCount);
break;
}
}
......
......@@ -179,14 +179,14 @@ public class TableHeaderView extends HorizontalScrollView {
getHandleSize(), getHandleSize());
hand.setLayoutParams(handParams);
hand.setPadding(10, 10, 10, 10);
hand.setPadding(15, 15, 15, 15);
hand.setImageResource(R.drawable.ic_dot);
hand.setColorFilter(getComponentColor(), PorterDuff.Mode.MULTIPLY);
handler.addView(hand);
setHandlerPosition();
setDragBehaviour(key);
setDragBehavior(key);
}
container.addView(handler);
......@@ -195,7 +195,7 @@ public class TableHeaderView extends HorizontalScrollView {
requestLayout();
}
private void setDragBehaviour(final String key) {
private void setDragBehavior(final String key) {
final View handler = this.handler.findViewWithTag(key);
final TextView column = this.header.findViewWithTag(key);
......
......@@ -19,6 +19,7 @@ import java.util.Map;
import java.util.Set;
import id.ac.stis.capi.R;
import id.ac.stis.capi.lessthink.listeners.OnFooterNavigationListener;
import id.ac.stis.capi.lessthink.listeners.OnHandlerMovedListener;
import timber.log.Timber;
......@@ -27,19 +28,24 @@ import timber.log.Timber;
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class TableRowView extends HorizontalScrollView implements OnHandlerMovedListener {
public class TableRowView extends HorizontalScrollView implements OnHandlerMovedListener,
OnFooterNavigationListener {
private final long animationDuration = 200L;
private LinearLayout row;
private Map<String, Map<String, String>> rows;
private RelativeLayout separator, container;
private int headerViewRef, rowPadding, textSize;
private int headerViewRef, footerViewRef, rowPadding, textSize, rowCount, pageCount, currentPage;
private TableHeaderView headerView;
private TableFooterView footerView;
private OnClickListener onClickListener;
public TableRowView(Context context, AttributeSet attrs) {
super(context, attrs);
this.headerViewRef = R.id.row_view_header_id;
this.footerViewRef = R.id.row_view_footer_id;
this.rowPadding = 0;
this.textSize = 13;
......@@ -47,6 +53,7 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
0, 0);
try {
setHeaderViewRef(a.getResourceId(R.styleable.TableRowView_tableHeaderView, getHeaderViewRef()));
setFooterViewRef(a.getResourceId(R.styleable.TableRowView_tableFooterView, getFooterViewRef()));
setRowPadding(a.getDimensionPixelSize(R.styleable.TableRowView_rowPadding, getRowPadding()));
setTextSize(a.getDimensionPixelSize(R.styleable.TableRowView_rowTextSize, getTextSize()));
} finally {
......@@ -59,12 +66,20 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
this.onClickListener = onClickListener;
}
private int getHeaderViewRef() {
return headerViewRef;
}
public void setHeaderViewRef(int headerViewRef) {
this.headerViewRef = headerViewRef;
}
private int getHeaderViewRef() {
return headerViewRef;
private int getFooterViewRef() {
return footerViewRef;
}
public void setFooterViewRef(int footerViewRef) {
this.footerViewRef = footerViewRef;
}
public int getRowPadding() {
......@@ -83,8 +98,21 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
this.textSize = textSize;
}
public int getRowCount() {
return rowCount;
}
public int getPageCount() {
return pageCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setRows(Map<String, Map<String, String>> rows) {
this.rows = rows;
this.currentPage = 1;
if (headerViewRef != 0) {
headerView = getRootView().findViewById(headerViewRef);
......@@ -94,6 +122,14 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
headerView.setOnHandlerMovedListener(this);
}
if (footerViewRef != 0) {
footerView = getRootView().findViewById(footerViewRef);
}
if (footerView != null) {
footerView.setOnFooterNavigationListener(this);
}
this.container = new RelativeLayout(getContext());
RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
......@@ -115,10 +151,36 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
container.addView(row);
this.separator = new RelativeLayout(getContext());
RelativeLayout.LayoutParams handlerParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
separator.setLayoutParams(handlerParams);
container.addView(separator);
ArrayList<View> handlers = headerView.getHandlers();
for (View handler : handlers) {
String key = handler.getTag().toString();
View sep = new View(getContext());
sep.setTag(key);
sep.setContentDescription("separator_" + key);
RelativeLayout.LayoutParams separatorParams = new RelativeLayout.LayoutParams(
4, ViewGroup.LayoutParams.MATCH_PARENT);
sep.setLayoutParams(separatorParams);
sep.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.smooth_gray));
this.separator.addView(sep);
setSeparatorPosition();
}
container.post(new Runnable() {
@Override
public void run() {
addRowChildren();
addRowChildren(currentPage);
}
});
......@@ -126,17 +188,23 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
requestLayout();
}
private void addRowChildren() {
int count = 0;
int currHeight = 0;
int containerHeight = container.getHeight();
private void addRowChildren(int page) {
row.removeAllViews();
Set<String> rowKeySet = rows.keySet();
for (String rowKey : rowKeySet) {
Timber.d("Count: %s, Container Height: %s, Current Height: %s", count, containerHeight, currHeight);
if (count > 0 && containerHeight - currHeight < currHeight / count) {
break;
}
int rowHeight = 0;
int availableHeight = container.getHeight();
int currIdx = this.rowCount * page - this.rowCount;
int maxIdx = this.rowCount != 0 && this.rowCount * page < rows.size() ? this.rowCount * page : rows.size();
Timber.d("Max Idx : %s", maxIdx);
String[] rowKeySet = new String[rows.size()];
rows.keySet().toArray(rowKeySet);
while (availableHeight > rowHeight && currIdx < maxIdx) {
Timber.d("Available Height : %s, Curr Idx : %s", availableHeight, currIdx);
LinearLayout column = new LinearLayout(getContext());
LinearLayout.LayoutParams columnParams = new LinearLayout.LayoutParams(
......@@ -145,7 +213,7 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
int currMargin = 0;
Map<String, String> rowValue = rows.get(rowKey);
Map<String, String> rowValue = rows.get(rowKeySet[currIdx]);
Set<String> rowValueKeySet = rowValue.keySet();
for (String rowValueKey : rowValueKeySet) {
......@@ -177,14 +245,12 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
column.addView(col);
}
if (count % 2 == 0) {
if (currIdx % 2 == 0) {
column.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white));
} else {
column.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.smoother_gray));
}
count++;
final FrameLayout columnWrapper = new FrameLayout(getContext());
LinearLayout.LayoutParams columnWrapperParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
......@@ -193,7 +259,7 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
columnWrapper.setTag(rowKey);
columnWrapper.setTag(rowKeySet[currIdx]);
columnWrapper.setForeground(ContextCompat.getDrawable(getContext(), outValue.resourceId));
columnWrapper.addView(column);
......@@ -204,34 +270,30 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
row.addView(columnWrapper);
currHeight = measureViewHeight(row);
}
currIdx++;
this.separator = new RelativeLayout(getContext());
RelativeLayout.LayoutParams handlerParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
separator.setLayoutParams(handlerParams);
container.addView(separator);
rowHeight = measureViewHeight(columnWrapper);
availableHeight -= rowHeight;
ArrayList<View> handlers = headerView.getHandlers();
for (View handler : handlers) {
String key = handler.getTag().toString();
View sep = new View(getContext());
// Timber.d("Row Height : %s, Available Height : %s", rowHeight, availableHeight);
}
sep.setTag(key);
sep.setContentDescription("separator_" + key);
this.rowCount = container.getHeight() / rowHeight;
this.currentPage = page;
RelativeLayout.LayoutParams separatorParams = new RelativeLayout.LayoutParams(
4, ViewGroup.LayoutParams.MATCH_PARENT);
sep.setLayoutParams(separatorParams);
sep.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.smooth_gray));
calculatePageCount();
this.separator.addView(sep);
animate().alpha(1.0f).setDuration(animationDuration);
}
setSeparatorPosition();
private void calculatePageCount() {
int pageCount = rows.size() / rowCount;
if (rows.size() % rowCount > 0) {
pageCount++;
}
footerView.setPageCountInfo(pageCount);
this.pageCount = pageCount;
}
private void setColumnSize(String key) {
......@@ -302,11 +364,43 @@ public class TableRowView extends HorizontalScrollView implements OnHandlerMoved
super.onScrollChanged(l, t, oldl, oldt);
}
public int goToNextPage() {
return 0;
@Override
public int onGoToNextPage(View v) {
// post(new Runnable() {
// @Override
// public void run() {
if (currentPage < pageCount) {
animate().cancel();
setAlpha(0.6f);
TableRowView.this.currentPage++;
addRowChildren(currentPage);
Timber.d("Current Page: %s, Page Count : %s", currentPage, pageCount);
}
// }
// });
return currentPage;
}
@Override
public int onGoToPrevPage(View v) {
// post(new Runnable() {
// @Override
// public void run() {
if (currentPage > 1) {
animate().cancel();
setAlpha(0.6f);
TableRowView.this.currentPage--;
addRowChildren(currentPage);
Timber.d("Current Page: %s", currentPage);
}
// }
// });
public int goToPreviousPage() {
return 0;
return currentPage;
}
}
......@@ -23,7 +23,7 @@ import org.kxml2.kdom.Document;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;
import id.ac.stis.capi.R;
import id.ac.stis.capi.lessthink.logics.DependencyDetails;
import id.ac.stis.capi.lessthink.models.DependencyDetails;
import id.ac.stis.capi.odk.application.Collect;
import java.io.File;
......
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
......@@ -10,11 +10,9 @@
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:theme="@style/AppTheme.NoActionBar.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
android:theme="@style/AppTheme.NoActionBar.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
......@@ -32,26 +30,21 @@
<id.ac.stis.capi.lessthink.views.TableRowView
android:id="@+id/row_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/footer_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/footer_view"
android:layout_below="@+id/appBarLayout"
app:rowPadding="4dp"
app:tableFooterView="@+id/footer_view"
app:tableHeaderView="@+id/header_view">
</id.ac.stis.capi.lessthink.views.TableRowView>
<id.ac.stis.capi.lessthink.views.TableFooterView
android:id="@+id/footer_view"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/row_view"
app:tableRowView="@+id/row_view" />
android:layout_alignParentBottom="true" />
<!--<id.ac.stis.capi.lessthink.views.ListingTableView-->
<!--android:id="@+id/listingTableView"-->
......@@ -60,10 +53,6 @@
<!--app:header_color="@color/colorPrimary"-->
<!--app:header_text_color="@color/white"-->
<!--app:header_padding="10dp"-->
<!--app:layout_constraintBottom_toBottomOf="parent"-->
<!--app:layout_constraintEnd_toEndOf="parent"-->
<!--app:layout_constraintStart_toStartOf="parent"-->
<!--app:layout_constraintTop_toBottomOf="@+id/appBarLayout"-->
<!--app:row_padding="10dp" />-->
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
</RelativeLayout>
\ No newline at end of file
......@@ -10,13 +10,13 @@
<declare-styleable name="TableRowView">
<attr name="rowTextSize" format="dimension" />
<attr name="tableHeaderView" format="reference" />
<attr name="tableFooterView" format="reference" />
<attr name="rowPadding" format="dimension" />
</declare-styleable>
<declare-styleable name="TableFooterView">
<attr name="footerComponentColor" format="color" />
<attr name="footerColor" format="color" />
<attr name="tableRowView" format="reference" />
<attr name="footerTextSize" format="dimension" />
</declare-styleable>
</resources>
\ No newline at end of file
......@@ -5,5 +5,5 @@
<item name="footer_view_next_button" type="id"/>
<item name="footer_view_add_button" type="id"/>
<item name="row_view_header_id" type="id"/>
<item name="footer_view_row_id" type="id"/>
<item name="row_view_footer_id" type="id"/>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment