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

"NetJorka #23

Open
wants to merge 5 commits 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.yandex.yamblz.ui.fragments;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
Expand All @@ -8,41 +10,68 @@
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> {
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ContentHolder> {

private final Random rnd = new Random();
private final List<Integer> colors = new ArrayList<>();

@Override
public ContentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.content_item, parent, false));
return new ContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.content_item, parent, false), this);
}

private Integer changeColor(int pos) {
colors.set(pos, Color.rgb(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255)));
return colors.get(pos);
}

private Integer getColorByPos(int pos) {
return colors.get(pos);
}

@Override
public void onBindViewHolder(ContentHolder holder, int position) {
holder.bind(createColorForPosition(position));
}

public void removeItem(int position) {
colors.remove(position);
notifyItemRemoved(position);
}

public void moveItem(int from, int to) {
Collections.swap(colors, from, to);
notifyItemMoved(from, to);
}

@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}

private Integer createColorForPosition(int position) {
if (position >= colors.size()) {
while (position >= colors.size()) {
colors.add(Color.rgb(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255)));
}
return colors.get(position);
}

static class ContentHolder extends RecyclerView.ViewHolder {
ContentHolder(View itemView) {
ContentHolder(View itemView, ContentAdapter contentAdapter) {
super(itemView);
itemView.setOnClickListener(view -> {
int from = contentAdapter.getColorByPos(getAdapterPosition());
int to = contentAdapter.changeColor(getAdapterPosition());
ObjectAnimator.ofObject(itemView, "backgroundColor", new ArgbEvaluator(), from, to)
.start();
((TextView) itemView).setText("#".concat(Integer.toHexString(to).substring(2)));
});
}

void bind(Integer color) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,99 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.GridLayoutManager;
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;
import ru.yandex.yamblz.ui.other.BorderItemDecoration;
import ru.yandex.yamblz.ui.other.ItemTouhHelperSimpleCallback;

public class ContentFragment extends BaseFragment {
private int spanCount = 1;
private BorderItemDecoration borderItemDecoration;

@BindView(R.id.rv)
RecyclerView rv;
private GridLayoutManager grid;
static final String SPAN_COUNT = "SPAN_COUNT";
static boolean borderFlag = true;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_content, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
rv.setAdapter(new ContentAdapter());
if (savedInstanceState != null) {
spanCount = savedInstanceState.getInt(SPAN_COUNT, 1);
}
grid = new GridLayoutManager(getContext(), spanCount);
rv.setLayoutManager(grid);

ContentAdapter adapter = new ContentAdapter();
adapter.setHasStableIds(true);
rv.setAdapter(adapter);
borderItemDecoration = new BorderItemDecoration(getContext());
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouhHelperSimpleCallback(adapter, borderItemDecoration));
helper.attachToRecyclerView(rv);

rv.addItemDecoration(borderItemDecoration);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.add_column) {
spanCount++;
grid.setSpanCount(spanCount);
rv.requestLayout();
rv.getAdapter().notifyDataSetChanged();
return true;
}
if (id == R.id.del_column) {
spanCount--;
spanCount = Math.max(1, spanCount);
grid.setSpanCount(spanCount);
rv.requestLayout();
rv.getAdapter().notifyDataSetChanged();
return true;
}
if (id == R.id.change_border) {
if (borderFlag) {
rv.removeItemDecoration(borderItemDecoration);
borderFlag = false;
} else {
rv.addItemDecoration(borderItemDecoration);
borderFlag = true;
}
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SPAN_COUNT, spanCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ru.yandex.yamblz.ui.other;

/**
* Created by GEORGY on 08.08.2016.
*/

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import ru.yandex.yamblz.R;

public class BorderItemDecoration extends RecyclerView.ItemDecoration {

private Paint paint;
private int strokeWidth = 5;
private boolean draw = true;
private int firstItem = -1;
private int secondItem = -1;
private Context context;
private Drawable drawable;


public BorderItemDecoration(Context context) {
super();
this.context = context;
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(strokeWidth * 2);
drawable = ContextCompat.getDrawable(context, R.drawable.mark);
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (!draw) return;
final RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
for (int i = 0; i < parent.getChildCount(); i++) {

final View child = parent.getChildAt(i);
int id = parent.getChildAdapterPosition(child) % 2;
if (parent.getChildAdapterPosition(child) % 2 == 0) {
c.drawRect(
layoutManager.getDecoratedLeft(child),
layoutManager.getDecoratedTop(child),
layoutManager.getDecoratedRight(child),
layoutManager.getDecoratedBottom(child),
paint);
}

}
if (firstItem != -1 && secondItem != -1) {

RecyclerView.ViewHolder child1Holder = parent.findViewHolderForAdapterPosition(firstItem);
RecyclerView.ViewHolder child2Holder = parent.findViewHolderForAdapterPosition(secondItem);

if (child1Holder != null && child2Holder != null) {
onMarkSelectedItem(c, parent, child1Holder.itemView);
onMarkSelectedItem(c, parent, child2Holder.itemView);
}
}
}

public void setIndexSelectedItem(int firstIndex, int secondIndex) {
firstItem = firstIndex;
secondItem = secondIndex;
}

private void onMarkSelectedItem(Canvas canvas, RecyclerView parent, View child) {
final RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
drawable.setBounds(layoutManager.getDecoratedRight(child) - drawable.getMinimumWidth(),
layoutManager.getDecoratedTop(child),
layoutManager.getDecoratedRight(child),
layoutManager.getDecoratedTop(child) + drawable.getMinimumHeight());
drawable.draw(canvas);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ru.yandex.yamblz.ui.other;

/**
* Created by GEORGY on 07.08.2016.
*/

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

import ru.yandex.yamblz.ui.fragments.ContentAdapter;

public class ItemTouhHelperSimpleCallback extends ItemTouchHelper.SimpleCallback {
private ContentAdapter contentAdapter;
Paint paint;
private BorderItemDecoration borderItemDecoration;

public ItemTouhHelperSimpleCallback(ContentAdapter contentAdapter, BorderItemDecoration borderItemDecoration) {
super(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT
, ItemTouchHelper.RIGHT);
this.contentAdapter = contentAdapter;
paint = new Paint();
this.borderItemDecoration = borderItemDecoration;
}

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

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
contentAdapter.moveItem(viewHolder.getAdapterPosition(), target.getAdapterPosition());
borderItemDecoration.setIndexSelectedItem(viewHolder.getAdapterPosition(), target.getAdapterPosition());

return true;
}


@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
contentAdapter.removeItem(viewHolder.getAdapterPosition());
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
View itemView = viewHolder.itemView;
if (dX > 0) {
paint.setARGB((int) (255 * Math.min(dX, itemView.getWidth()) / itemView.getWidth()), 255, 0, 0);
c.drawRect(itemView.getLeft(),
(float) itemView.getTop(),
itemView.getLeft() + dX,
(float) itemView.getBottom(), paint);
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}

Binary file added app/src/main/res/drawable/mark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions app/src/main/res/menu/menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/add_column"
android:orderInCategory="100"
android:title="add column"/>
<item
android:id="@+id/del_column"
android:orderInCategory="200"
android:title="del column"/>
<item
android:id="@+id/change_border"
android:orderInCategory="300"
android:title="change border"/>
</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-alpha7',
aptGradlePlugin : '1.8',
retrolambdaGradlePlugin : '3.2.5',
lombokGradlePlugin : '0.2.3.a2',
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon May 30 20:17:48 ICT 2016
#Sun Aug 07 19:43:45 MSK 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip