Use DividerItemDecoration from the support library
This commit is contained in:
parent
43870fb9d3
commit
cc75b1f7b1
@ -27,28 +27,39 @@ import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v7.widget.DividerItemDecoration;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.text.ClipboardManager;
|
||||
import android.text.TextUtils;
|
||||
import android.view.*;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.views.DividerItemDecoration;
|
||||
import org.solovyev.android.calculator.BaseActivity;
|
||||
import org.solovyev.android.calculator.BaseFragment;
|
||||
import org.solovyev.android.calculator.CalculatorActivity;
|
||||
import org.solovyev.android.calculator.Keyboard;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
|
||||
public abstract class BaseEntitiesFragment<E extends MathEntity> extends BaseFragment {
|
||||
|
||||
@ -90,7 +101,7 @@ public abstract class BaseEntitiesFragment<E extends MathEntity> extends BaseFra
|
||||
adapter = new EntitiesAdapter(context, TextUtils.isEmpty(category) ? getEntities() : getEntities(category));
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
|
||||
recyclerView.setAdapter(adapter);
|
||||
recyclerView.addItemDecoration(new DividerItemDecoration(context, null));
|
||||
recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
|
||||
return view;
|
||||
}
|
||||
|
||||
|
@ -22,35 +22,48 @@
|
||||
|
||||
package org.solovyev.android.calculator.history;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v7.widget.DividerItemDecoration;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.text.ClipboardManager;
|
||||
import android.text.format.DateUtils;
|
||||
import android.view.*;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
import com.squareup.otto.Bus;
|
||||
import com.squareup.otto.Subscribe;
|
||||
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.AppComponent;
|
||||
import org.solovyev.android.calculator.BaseActivity;
|
||||
import org.solovyev.android.calculator.BaseFragment;
|
||||
import org.solovyev.android.calculator.CalculatorActivity;
|
||||
import org.solovyev.android.calculator.Editor;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||
import org.solovyev.android.views.DividerItemDecoration;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public abstract class BaseHistoryFragment extends BaseFragment {
|
||||
private final boolean recentHistory;
|
||||
@ -104,7 +117,7 @@ public abstract class BaseHistoryFragment extends BaseFragment {
|
||||
bus.register(adapter);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
|
||||
recyclerView.setAdapter(adapter);
|
||||
recyclerView.addItemDecoration(new DividerItemDecoration(context, null));
|
||||
recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
|
||||
return view;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,8 @@
|
||||
package org.solovyev.android.calculator.plot;
|
||||
|
||||
import static android.support.v7.widget.LinearLayoutManager.VERTICAL;
|
||||
import static android.view.Menu.NONE;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
@ -9,25 +12,33 @@ import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.widget.DividerItemDecoration;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.*;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import org.solovyev.android.calculator.*;
|
||||
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.AppComponent;
|
||||
import org.solovyev.android.calculator.BaseActivity;
|
||||
import org.solovyev.android.calculator.BaseDialogFragment;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.plotter.BasePlotterListener;
|
||||
import org.solovyev.android.plotter.PlotFunction;
|
||||
import org.solovyev.android.plotter.PlotIconView;
|
||||
import org.solovyev.android.plotter.Plotter;
|
||||
import org.solovyev.android.views.DividerItemDecoration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
|
||||
import static android.support.v7.widget.LinearLayoutManager.VERTICAL;
|
||||
import static android.view.Menu.NONE;
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class PlotFunctionsFragment extends BaseDialogFragment {
|
||||
|
||||
@ -65,7 +76,7 @@ public class PlotFunctionsFragment extends BaseDialogFragment {
|
||||
@SuppressLint("InflateParams") final RecyclerView view = (RecyclerView) inflater.inflate(R.layout.fragment_plot_functions, null);
|
||||
|
||||
view.setLayoutManager(new LinearLayoutManager(context, VERTICAL, false));
|
||||
view.addItemDecoration(new DividerItemDecoration(context, null));
|
||||
view.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
|
||||
adapter = new Adapter(plotter.getPlotData().functions);
|
||||
view.setAdapter(adapter);
|
||||
return view;
|
||||
|
@ -1,175 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
*
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.views;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
|
||||
|
||||
private Drawable divider;
|
||||
private int dividerHeight;
|
||||
private int dividerWidth;
|
||||
private boolean first = false;
|
||||
private boolean last = false;
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public DividerItemDecoration(Context context, AttributeSet attrs) {
|
||||
final TypedArray a = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.listDivider});
|
||||
setDivider(a.getDrawable(0));
|
||||
a.recycle();
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public DividerItemDecoration(Context context, AttributeSet attrs, boolean showFirstDivider,
|
||||
boolean showLastDivider) {
|
||||
this(context, attrs);
|
||||
first = showFirstDivider;
|
||||
last = showLastDivider;
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public DividerItemDecoration(Drawable divider) {
|
||||
setDivider(divider);
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public DividerItemDecoration(Drawable divider, boolean showFirstDivider,
|
||||
boolean showLastDivider) {
|
||||
this(divider);
|
||||
first = showFirstDivider;
|
||||
last = showLastDivider;
|
||||
}
|
||||
|
||||
private void setDivider(Drawable divider) {
|
||||
this.divider = divider;
|
||||
this.dividerHeight = divider == null ? 0 : divider.getIntrinsicHeight();
|
||||
this.dividerWidth = divider == null ? 0 : divider.getIntrinsicWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
|
||||
RecyclerView.State state) {
|
||||
if (divider == null) {
|
||||
super.getItemOffsets(outRect, view, parent, state);
|
||||
return;
|
||||
}
|
||||
|
||||
final int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
|
||||
final boolean firstItem = position == 0;
|
||||
final boolean lastItem = position == parent.getAdapter().getItemCount() - 1;
|
||||
final boolean dividerBefore = first || !firstItem;
|
||||
final boolean dividerAfter = last && lastItem;
|
||||
|
||||
if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
|
||||
outRect.top = dividerBefore ? dividerHeight : 0;
|
||||
outRect.bottom = dividerAfter ? dividerHeight : 0;
|
||||
} else {
|
||||
outRect.left = dividerBefore ? dividerWidth : 0;
|
||||
outRect.right = dividerAfter ? dividerWidth : 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
||||
if (divider == null) {
|
||||
super.onDraw(c, parent, state);
|
||||
return;
|
||||
}
|
||||
|
||||
int left = 0;
|
||||
int right = 0;
|
||||
int top = 0;
|
||||
int bottom = 0;
|
||||
|
||||
final int orientation = getOrientation(parent);
|
||||
final int childCount = parent.getChildCount();
|
||||
|
||||
final RecyclerView.Adapter adapter = parent.getAdapter();
|
||||
final int adapterCount = adapter != null ? adapter.getItemCount() : 0;
|
||||
|
||||
final boolean vertical = orientation == LinearLayoutManager.VERTICAL;
|
||||
final int size;
|
||||
if (vertical) {
|
||||
size = dividerHeight;
|
||||
left = parent.getPaddingLeft();
|
||||
right = parent.getWidth() - parent.getPaddingRight();
|
||||
} else {
|
||||
size = dividerWidth;
|
||||
top = parent.getPaddingTop();
|
||||
bottom = parent.getHeight() - parent.getPaddingBottom();
|
||||
}
|
||||
|
||||
for (int i = 0; i < childCount; i++) {
|
||||
final View child = parent.getChildAt(i);
|
||||
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
|
||||
final int position = params.getViewLayoutPosition();
|
||||
if (position == 0 && !first) {
|
||||
continue;
|
||||
}
|
||||
if (vertical) {
|
||||
top = child.getTop() - params.topMargin - size;
|
||||
bottom = top + size;
|
||||
} else {
|
||||
left = child.getLeft() - params.leftMargin - size;
|
||||
right = left + size;
|
||||
}
|
||||
divider.setBounds(left, top, right, bottom);
|
||||
divider.draw(c);
|
||||
}
|
||||
|
||||
if (last && childCount > 0) {
|
||||
final View child = parent.getChildAt(childCount - 1);
|
||||
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
|
||||
final int position = params.getViewLayoutPosition();
|
||||
if (position == adapterCount - 1) {
|
||||
if (vertical) {
|
||||
top = child.getBottom() + params.bottomMargin;
|
||||
bottom = top + size;
|
||||
} else {
|
||||
left = child.getRight() + params.rightMargin;
|
||||
right = left + size;
|
||||
}
|
||||
divider.setBounds(left, top, right, bottom);
|
||||
divider.draw(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int getOrientation(RecyclerView parent) {
|
||||
final RecyclerView.LayoutManager lm = parent.getLayoutManager();
|
||||
if (lm instanceof LinearLayoutManager) {
|
||||
return ((LinearLayoutManager) lm).getOrientation();
|
||||
} else {
|
||||
throw new IllegalStateException("DividerItemDecoration can only be used with a LinearLayoutManager");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user