Skip to content
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

@dSukmanova Сукманова Дарья #9

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import ru.yandex.yamblz.R;

class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ContentHolder> {
class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ContentHolder> implements MyItemTouchHelperAdapter {

private final Random rnd = new Random();
private final List<Integer> colors = new ArrayList<>();
Expand All @@ -40,6 +41,27 @@ private Integer createColorForPosition(int position) {
return colors.get(position);
}

@Override
public void onItemDissmiss(int position) {
removeItem(position);
notifyItemRemoved(position);
}

@Override
public void onItemMove(int fromPosition, int toPosition) {
int startPos = fromPosition;
int endPos = toPosition;
if (fromPosition > toPosition) {
startPos = toPosition;
endPos = fromPosition;
}
for (int i = startPos; i < endPos; i++) {
Collections.swap(colors, i, i + 1);
}
notifyItemMoved(fromPosition,toPosition);
return;
}

static class ContentHolder extends RecyclerView.ViewHolder {
ContentHolder(View itemView) {
super(itemView);
Expand All @@ -50,4 +72,13 @@ void bind(Integer color) {
((TextView) itemView).setText("#".concat(Integer.toHexString(color).substring(2)));
}
}

public void removeItem(int index) {
this.colors.remove(index);
}

public void setColor(List<Integer> colors) {
this.colors.clear();
this.colors.addAll(colors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,37 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import butterknife.BindView;
import ru.yandex.yamblz.R;

public class ContentFragment extends BaseFragment {
private boolean bordersOn = false;
private int bordersWidth = 3;
private MyItemDecoration myItemDecoration;
private ItemsChangedDecorator itemsChangedDecorator;
private GridLayoutManager gridLayoutManager;
private int columnsNum = 1;


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
columnsNum = savedInstanceState.getInt("columnsNum");
}
setHasOptionsMenu(true);
}

@BindView(R.id.rv)
RecyclerView rv;
Expand All @@ -23,10 +44,64 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
return inflater.inflate(R.layout.fragment_content, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId){
case R.id.borders:
switchBordersMode();
break;
case R.id.plusColumns:
columnsNum ++;
gridLayoutManager.setSpanCount(columnsNum);
break;
case R.id.minusColumns:
columnsNum--;
gridLayoutManager.setSpanCount(columnsNum);
break;
}
rv.getAdapter().notifyDataSetChanged();
return super.onOptionsItemSelected(item);
}

private void switchBordersMode (){
if (bordersOn){
rv.removeItemDecoration(myItemDecoration);
bordersOn = false;
} else {
rv.addItemDecoration(myItemDecoration);
bordersOn = true;
}
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
rv.setAdapter(new ContentAdapter());
ContentAdapter contentAdapter = new ContentAdapter();
rv.setAdapter(contentAdapter);

itemsChangedDecorator = new ItemsChangedDecorator();
ItemTouchHelper.Callback callback = new MyItemTochHelperCallback(contentAdapter,itemsChangedDecorator);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(rv);

myItemDecoration = new MyItemDecoration(bordersWidth);
if (bordersOn) rv.addItemDecoration(myItemDecoration);

rv.addItemDecoration(itemsChangedDecorator);
gridLayoutManager = new GridLayoutManager(getContext(), columnsNum);
rv.setLayoutManager(gridLayoutManager);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("columnsNum",columnsNum);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.yandex.yamblz.ui.fragments;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
* Created by dsukmanova on 31.07.16.
*/

public class ItemsChangedDecorator extends RecyclerView.ItemDecoration {
private int fromPos = -1;
private int toPos = -1;
private int CIRCLE_RADIUS = 15;
private int CIRCLE_OFFSET = 30;

private Paint paint;

ItemsChangedDecorator() {
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(6);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

значение в пикселях приводит к раку чешуи

}

public void setFromPos(int fromPos) {
this.fromPos = fromPos;
}

public void setToPos(int toPos) {
this.toPos = toPos;
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (fromPos > -1) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

View child = layoutManager.findViewByPosition(fromPos);
if (child != null) {
drawCircle(c, layoutManager, child);
}
child = layoutManager.findViewByPosition(toPos);
if (child != null) {
drawCircle(c, layoutManager, child);
}
}
}

private void drawCircle(Canvas c, RecyclerView.LayoutManager lm, View child) {
int heigth = Math.abs(lm.getDecoratedTop(child) - lm.getDecoratedBottom(child));
c.drawCircle(lm.getDecoratedRight(child) - CIRCLE_OFFSET, lm.getDecoratedTop(child) + heigth / 2, CIRCLE_RADIUS, paint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ru.yandex.yamblz.ui.fragments;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
* Created by dsukmanova on 31.07.16.
*/

public class MyItemDecoration extends RecyclerView.ItemDecoration {
private int offset;
Paint paint;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=(


MyItemDecoration(int offset) {
this.offset = offset;
this.paint = new Paint();
this.paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2 * offset);
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int count = parent.getChildCount();
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
for (int i = 0; i < count; i++) {
View child = parent.getChildAt(i);
int position = parent.getChildAdapterPosition(child);
if (position % 2 == 0) {
c.drawRect(layoutManager.getDecoratedLeft(child) + offset,
layoutManager.getDecoratedTop(child) + offset,
layoutManager.getDecoratedRight(child) - offset,
layoutManager.getDecoratedBottom(child) - offset,
paint
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ru.yandex.yamblz.ui.fragments;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;


/**
* Created by dsukmanova on 31.07.16.
*/

public class MyItemTochHelperCallback extends ItemTouchHelper.Callback {
MyItemTouchHelperAdapter myItemTouchHelperAdapter;
ItemsChangedDecorator itemsChangedDecorator;

MyItemTochHelperCallback(MyItemTouchHelperAdapter myItemTouchHelperAdapter) {
this.myItemTouchHelperAdapter = myItemTouchHelperAdapter;
}

MyItemTochHelperCallback(MyItemTouchHelperAdapter myItemTouchHelperAdapter,ItemsChangedDecorator decorator) {
this.myItemTouchHelperAdapter = myItemTouchHelperAdapter;
this.itemsChangedDecorator = decorator;
}

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int moveFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.RIGHT;
return makeMovementFlags(moveFlags, swipeFlags);
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
myItemTouchHelperAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
myItemTouchHelperAdapter.onItemDissmiss(position);
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
View itemView = viewHolder.itemView;
int itemViewWidth = itemView.getWidth();
Paint paint = new Paint();
paint.setColor(Color.RED);
int maxAlpha = 255;
double kAlpha = 0.75;
paint.setAlpha(Math.min(maxAlpha, (int) (kAlpha * maxAlpha * Math.abs(dX) / itemViewWidth)));
float rectLeft = dX > 0 ? itemView.getLeft() : (itemView.getLeft() + dX);
float rectRight = dX > 0 ? dX : itemView.getRight();
c.drawRect(rectLeft, itemView.getTop(), rectRight, itemView.getBottom(), paint);

}
}

@Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
itemsChangedDecorator.setFromPos(fromPos);
itemsChangedDecorator.setToPos(toPos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.yandex.yamblz.ui.fragments;

/**
* Created by dsukmanova on 31.07.16.
*/

public interface MyItemTouchHelperAdapter {
void onItemDissmiss(int position);

void onItemMove(int fromPosition, int toPosition);
}
9 changes: 9 additions & 0 deletions app/src/main/res/menu/menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Рамки вкл./выкл."
android:id="@+id/borders"/>
<item android:title="Колонки +1"
android:id="@+id/plusColumns"/>
<item android:title="Колонки -1"
android:id="@+id/minusColumns"/>
</menu>
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ext.versions = [
compileSdk : 23,
buildTools : '23.0.3',

androidGradlePlugin : '2.2.0-alpha4',
androidGradlePlugin : '2.2.0-alpha5',
aptGradlePlugin : '1.8',
retrolambdaGradlePlugin : '3.2.5',
lombokGradlePlugin : '0.2.3.a2',
Expand Down