Skip to content

SortableTableView icon position #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
android:layout_height="match_parent"
custom:tableView_columnCount="4"
custom:tableView_headerColor="@color/primary"
custom:sortIconPosition="right"
custom:tableView_headerElevation="10"/>

</RelativeLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SortableTableHeaderView extends TableHeaderView {

private SortStateViewProvider sortStateViewProvider = SortStateViewProviders.darkArrows();

private int iconPosition = SortableTableView.SORT_ICON_LEFT;

/**
* Creates a new SortableTableHeaderView.
Expand Down Expand Up @@ -125,6 +126,10 @@ public TableHeaderAdapter getAdapter() {
return super.getAdapter();
}

public void setIconPosition(int iconPosition) {
this.iconPosition = iconPosition;
}

private class SortStateArrayAdapter extends TableHeaderAdapter {

private final TableHeaderAdapter delegate;
Expand Down Expand Up @@ -178,7 +183,11 @@ public int getCount() {
public View getHeaderView(final int columnIndex, final ViewGroup parentView) {

// create column header layout
final LinearLayout headerLayout = (LinearLayout) delegate.getLayoutInflater().inflate(R.layout.sortable_header, parentView, false);
// check if icon position is left or right
final LinearLayout headerLayout = (LinearLayout)
(iconPosition == 0 ?
delegate.getLayoutInflater().inflate(R.layout.sortable_header, parentView, false)
: delegate.getLayoutInflater().inflate(R.layout.sortable_header_right, parentView, false));
headerLayout.setOnClickListener(new InternalHeaderClickListener(columnIndex, getHeaderClickListeners()));

// create header
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.codecrafters.tableview;

import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Parcelable;
Expand Down Expand Up @@ -36,6 +37,9 @@ public class SortableTableView<T> extends TableView<T> {
private final SortableTableHeaderView sortableTableHeaderView;
private final SortingController sortingController;

public static final int SORT_ICON_LEFT = 0;
public static final int SORT_ICON_RIGHT = 1;
private int sortIconPosition;

/**
* Creates a new SortableTableView with the given context.\n
Expand Down Expand Up @@ -67,11 +71,11 @@ public SortableTableView(final Context context, final AttributeSet attributes) {
*/
public SortableTableView(final Context context, final AttributeSet attributes, final int styleAttributes) {
super(context, attributes, styleAttributes);

setCustomAttributes(attributes);
sortableTableHeaderView = new SortableTableHeaderView(context);
sortableTableHeaderView.setBackgroundColor(0xFFCCCCCC);
sortableTableHeaderView.setIconPosition(sortIconPosition);
setHeaderView(sortableTableHeaderView);

sortingController = new SortingController();
sortableTableHeaderView.addHeaderClickListener(sortingController);
}
Expand Down Expand Up @@ -176,6 +180,12 @@ protected void onRestoreInstanceState(final Parcelable state) {
}
}

private void setCustomAttributes(AttributeSet attributes) {
final TypedArray styledAttributes = getContext().obtainStyledAttributes(attributes, R.styleable.SortableTableView);
sortIconPosition = styledAttributes.getInt(R.styleable.SortableTableView_sortIconPosition, SORT_ICON_LEFT);
}


/**
* A controller managing all actions that are in the context of sorting.
*
Expand Down
21 changes: 21 additions & 0 deletions tableview/src/main/res/layout/sortable_header_right.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">

<FrameLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/sort_view"
android:layout_width="10dp"
android:layout_height="15dp"
android:contentDescription="@string/sortable_indicator_content_description"
android:src="@mipmap/ic_dark_sortable" />
</LinearLayout>
7 changes: 7 additions & 0 deletions tableview/src/main/res/values/attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@
<attr name="tableView_headerElevation" format="integer"/>
<attr name="tableView_headerColor" format="color"/>
</declare-styleable>
<declare-styleable name="SortableTableView">
<!--Set the left or right position for the sort icon. Default is left-->
<attr name="sortIconPosition" format="enum">
<enum name="left" value="0"/>
<enum name="right" value="1"/>
</attr>
</declare-styleable>
</resources>