diff --git a/build.gradle b/build.gradle index 9632370..e894cea 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.2' + classpath 'com.android.tools.build:gradle:2.2.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } diff --git a/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/DragRecyclerView.java b/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/DragRecyclerView.java index 897d4a7..d7f9eab 100644 --- a/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/DragRecyclerView.java +++ b/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/DragRecyclerView.java @@ -14,17 +14,13 @@ import android.view.animation.ScaleAnimation; /** - * Desc : 可拖动recyclerView - * User : Cyan(baocq@maritech.com) - * New : 2016/9/21 14:27 + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2016/9/28 */ public class DragRecyclerView extends RecyclerView { - /* 适配器 */ private OnItemChangeListener adapter; - /* 是否可拖动 */ private boolean dragEnable; - /* 是否显示拖动动画 */ private boolean showDragAnimation; @@ -50,9 +46,6 @@ private void init(Context context, AttributeSet attrs) { } } - /////////////////////////////////////////////////////////////////////////// - // 拖动监听 - /////////////////////////////////////////////////////////////////////////// ItemTouchHelper touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() { @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { @@ -71,9 +64,7 @@ public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder v @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { - // 原位置 int oldPosition = viewHolder.getAdapterPosition(); - // 目标位置 int targetPosition = target.getAdapterPosition(); adapter.onItemMoved(oldPosition, targetPosition); return false; @@ -91,14 +82,12 @@ public boolean isItemViewSwipeEnabled() { @Override public boolean isLongPressDragEnabled() { - // 禁用,使用自定义触摸监听 return false; } @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { - //滑动时改变Item的透明度 final float alpha = 1 - Math.abs(dX) / (float) viewHolder.itemView.getWidth(); viewHolder.itemView.setAlpha(alpha); viewHolder.itemView.setTranslationX(dX); @@ -110,10 +99,7 @@ public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHold @Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) { - /** item已经被拽起(托起状态) */ - // 设置背景 viewHolder.itemView.setBackgroundColor(Color.LTGRAY); - // 放大动画 if (showDragAnimation) zoomView(viewHolder.itemView); } super.onSelectedChanged(viewHolder, actionState); @@ -122,52 +108,31 @@ public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionStat @Override public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) { super.clearView(recyclerView, viewHolder); - // 还原item的样式 viewHolder.itemView.setAlpha(1.0f); viewHolder.itemView.setBackgroundColor(Color.WHITE); - // 缩放动画 if (showDragAnimation) revertView(viewHolder.itemView); } @Override public boolean canDropOver(RecyclerView recyclerView, ViewHolder current, ViewHolder target) { - /** 被禁用的位置不会被挤兑 */ - return adapter.onItemDrag(target.getAdapterPosition()); + return adapter.onItemDrop(target.getAdapterPosition()); } }); - /* 放大动画 */ private ScaleAnimation zoomAnimation = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); - /* 还原动画 */ private ScaleAnimation revertAnimation = new ScaleAnimation(1.1f, 1.0f, 1.1f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); - /** - * 放大 - * - * @param v 视图 - */ private void zoomView(final View v) { v.setAnimation(zoomAnimation); - // 动画执行完停留位置 zoomAnimation.setFillAfter(true); - // 动画持续时间 zoomAnimation.setDuration(200); - // 开始 zoomAnimation.start(); } - /** - * 还原 - * - * @param v 视图 - */ private void revertView(final View v) { v.setAnimation(revertAnimation); - // 动画执行完停留位置 revertAnimation.setFillAfter(true); - // 动画持续时间 revertAnimation.setDuration(400); - // 执行监听 revertAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { @@ -175,7 +140,6 @@ public void onAnimationStart(Animation animation) { @Override public void onAnimationEnd(Animation animation) { - /** 动画结束后清除效果 */ v.clearAnimation(); } @@ -183,7 +147,6 @@ public void onAnimationEnd(Animation animation) { public void onAnimationRepeat(Animation animation) { } }); - // 开始 revertAnimation.start(); } @@ -197,17 +160,19 @@ public DragRecyclerView showDragAnimation(boolean showDragAnimation) { return this; } + public DragRecyclerView setDragAdapter(OnItemChangeListener dragBaseAdapter) { if (dragBaseAdapter instanceof Adapter) { this.adapter = dragBaseAdapter; touchHelper.attachToRecyclerView(this); - setAdapter((Adapter) adapter); + super.setAdapter((Adapter) adapter); } else { throw new IllegalArgumentException(); } return this; } + public DragRecyclerView bindEvent(HoldTouchHelper.OnItemTouchEvent onItemTouchEvent) { HoldTouchHelper.bind(this, onItemTouchEvent); return this; diff --git a/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/HoldTouchHelper.java b/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/HoldTouchHelper.java index 8665d13..6e1c3bc 100644 --- a/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/HoldTouchHelper.java +++ b/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/HoldTouchHelper.java @@ -7,11 +7,9 @@ import android.view.View; /** - * Desc : 拦截 recyclerView 单击/长按事件 - * User : Cyan(baocq@maritech.cn) - * New : 2016/11/21 13:09 + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2016/9/28 */ - public class HoldTouchHelper { private RecyclerView recyclerView; diff --git a/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/OnItemChangeListener.java b/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/OnItemChangeListener.java index 28381cf..7b48bdd 100644 --- a/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/OnItemChangeListener.java +++ b/dragrecyclerview/src/main/java/cn/cyan/dragrecyclerview/OnItemChangeListener.java @@ -1,15 +1,18 @@ package cn.cyan.dragrecyclerview; + /** - * Desc : item改变 - * User : Cyan(baocq@maritech.cn) - * New : 2016/9/28 8:41 + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2016/9/28 */ public interface OnItemChangeListener { - /* 是否可拖动 */ + /* item can be dragged */ boolean onItemDrag(int position); - /* item移动 */ + /* item moved */ void onItemMoved(int form, int target); + + /* item can be dropped */ + boolean onItemDrop(int position); } diff --git a/sample/src/androidTest/java/cn/cyan/dragtab/ExampleInstrumentedTest.java b/sample/src/androidTest/java/cn/cyan/sample/ExampleInstrumentedTest.java similarity index 96% rename from sample/src/androidTest/java/cn/cyan/dragtab/ExampleInstrumentedTest.java rename to sample/src/androidTest/java/cn/cyan/sample/ExampleInstrumentedTest.java index 255e2fa..38fa911 100644 --- a/sample/src/androidTest/java/cn/cyan/dragtab/ExampleInstrumentedTest.java +++ b/sample/src/androidTest/java/cn/cyan/sample/ExampleInstrumentedTest.java @@ -1,4 +1,4 @@ -package cn.cyan.dragtab; +package cn.cyan.sample; import android.content.Context; import android.support.test.InstrumentationRegistry; diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index a058b4e..3aeccec 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -1,14 +1,15 @@ + package="cn.cyan.sample"> - + diff --git a/sample/src/main/java/cn/cyan/dragtab/MainActivity.java b/sample/src/main/java/cn/cyan/dragtab/MainActivity.java deleted file mode 100644 index 3e70eb3..0000000 --- a/sample/src/main/java/cn/cyan/dragtab/MainActivity.java +++ /dev/null @@ -1,76 +0,0 @@ -package cn.cyan.dragtab; - -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.widget.Toast; - -import java.util.ArrayList; -import java.util.List; - -import cn.cyan.dragrecyclerview.DragRecyclerView; -import cn.cyan.dragrecyclerview.HoldTouchHelper; -import cn.cyan.dragtab.tab.DividerGridItemDecoration; -import cn.cyan.dragtab.tab.Tab; -import cn.cyan.dragtab.tab.TabAdapter; - -public class MainActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - initTabs(); - } - - private void initTabs() { - DragRecyclerView dragRecyclerView = (DragRecyclerView) findViewById(R.id.drvTab); - // 网格线 - dragRecyclerView.addItemDecoration(new DividerGridItemDecoration()); - // 不固定大小 - dragRecyclerView.setHasFixedSize(false); - // 布局管理器 - dragRecyclerView.setLayoutManager(new GridLayoutManager(this, 3)); - - /** 自定义属性 */ - dragRecyclerView - .dragEnable(true) - .showDragAnimation(true) - .setDragAdapter(new TabAdapter(this, getTabs())) - .bindEvent(onItemTouchEvent); - } - - HoldTouchHelper.OnItemTouchEvent onItemTouchEvent = new HoldTouchHelper.OnItemTouchEvent() { - @Override - public void onLongPress(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position) { - if (((TabAdapter) recyclerView.getAdapter()).onItemDrag(position)) { - ((DragRecyclerView) recyclerView).startDrag(position); - } - } - - @Override - public void onItemClick(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position) { - String tab = ((TabAdapter) recyclerView.getAdapter()).getUsing().get(position).getName(); - Toast.makeText(MainActivity.this, tab, Toast.LENGTH_SHORT).show(); - } - }; - - - private List getTabs() { - List result = new ArrayList<>(); - String[] set = getResources().getStringArray(R.array.tabs); - for (Object tab : set) { - Tab item = new Tab(); - item.setName(tab.toString()); - item.setDragEnable(true); - result.add(item); - } - // 添加更多 - Tab addMore = new Tab(); - addMore.setName("添加更多"); - addMore.setDragEnable(false); - result.add(addMore); - return result; - } -} diff --git a/sample/src/main/java/cn/cyan/dragtab/tab/FindIconHelper.java b/sample/src/main/java/cn/cyan/dragtab/tab/FindIconHelper.java deleted file mode 100644 index 03e070d..0000000 --- a/sample/src/main/java/cn/cyan/dragtab/tab/FindIconHelper.java +++ /dev/null @@ -1,52 +0,0 @@ -package cn.cyan.dragtab.tab; - -import java.util.HashMap; -import java.util.Map; - -import cn.cyan.dragtab.R; - -/** - * Desc : 获取图标的辅助 - * User : Cyan(baocq@maritech.cn) - * New : 2016/11/4 14:56 - */ - -public class FindIconHelper { - - private static FindIconHelper instance; - - private Map icons; - - public static FindIconHelper getInstance() { - if (instance == null) { - instance = new FindIconHelper(); - } - return instance; - } - - private FindIconHelper() { - icons = new HashMap<>(); - icons.put("今日热点",R.mipmap.hot); - icons.put("国际新闻",R.mipmap.news_international); - icons.put("及时快讯",R.mipmap.news); - icons.put("美女图片",R.mipmap.woman); - icons.put("政经要闻",R.mipmap.politics); - icons.put("体育赛事",R.mipmap.sports); - icons.put("茶余饭后",R.mipmap.tea); - icons.put("添加更多",R.mipmap.add); - } - - - /** - * 根据按钮名称获取按钮的icon - * - * @param name 按钮名 - * @return 按钮的resId - */ - public int find(String name) { - if (icons.keySet().contains(name)) { - return icons.get(name); - } - return R.mipmap.ic_launcher; - } -} diff --git a/sample/src/main/java/cn/cyan/dragtab/tab/Tab.java b/sample/src/main/java/cn/cyan/dragtab/tab/Tab.java deleted file mode 100644 index 8a150cc..0000000 --- a/sample/src/main/java/cn/cyan/dragtab/tab/Tab.java +++ /dev/null @@ -1,29 +0,0 @@ -package cn.cyan.dragtab.tab; - -/** - * Desc : 按钮 - * User : Cyan(baocq@maritech.cn) - * New : 2016/11/21 14:23 - */ - -public class Tab { - - private String name; - private boolean dragEnable; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public boolean isDragEnable() { - return dragEnable; - } - - public void setDragEnable(boolean dragEnable) { - this.dragEnable = dragEnable; - } -} diff --git a/sample/src/main/java/cn/cyan/dragtab/tab/TabAdapter.java b/sample/src/main/java/cn/cyan/dragtab/tab/TabAdapter.java deleted file mode 100644 index c8c57cb..0000000 --- a/sample/src/main/java/cn/cyan/dragtab/tab/TabAdapter.java +++ /dev/null @@ -1,89 +0,0 @@ -package cn.cyan.dragtab.tab; - -import android.content.Context; -import android.support.v7.widget.RecyclerView; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; - -import java.util.Collections; -import java.util.List; - -import cn.cyan.dragrecyclerview.OnItemChangeListener; -import cn.cyan.dragtab.R; - -/** - * Desc : 适配器 - * User : Cyan(baocq@maritech.cn) - * New : 2016/11/21 14:21 - */ - -public class TabAdapter extends RecyclerView.Adapter implements OnItemChangeListener { - private Context context; - private volatile List list; - - public TabAdapter(Context context, List list) { - this.context = context; - this.list = list; - } - - @Override - public TabsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - View itemView = LayoutInflater.from(context).inflate(R.layout.item_tab, parent, false); - return new TabsViewHolder(itemView); - } - - @Override - public void onBindViewHolder(TabsViewHolder holder, int position) { - holder.setName(list.get(position).getName()); - } - - @Override - public int getItemCount() { - return list.size(); - } - - @Override - public boolean onItemDrag(int position) { - return list.get(position).isDragEnable(); - } - - @Override - public void onItemMoved(int form, int target) { - if (form < target) { - // 向后 - for (int i = form; i < target; i++) { - Collections.swap(list, i, i + 1); - } - } else { - // 向前 - for (int i = form; i > target; i--) { - Collections.swap(list, i, i - 1); - } - } - notifyItemMoved(form, target); - } - - class TabsViewHolder extends RecyclerView.ViewHolder { - ImageView ivIcon; - TextView tvTab; - - TabsViewHolder(View itemView) { - super(itemView); - ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon); - tvTab = (TextView) itemView.findViewById(R.id.tvTab); - } - - void setName(String name) { - ivIcon.setImageResource(FindIconHelper.getInstance().find(name)); - tvTab.setText(name); - } - - } - - public List getUsing() { - return list; - } -} diff --git a/sample/src/main/java/cn/cyan/sample/base/BaseFragment.java b/sample/src/main/java/cn/cyan/sample/base/BaseFragment.java new file mode 100644 index 0000000..8f41dc0 --- /dev/null +++ b/sample/src/main/java/cn/cyan/sample/base/BaseFragment.java @@ -0,0 +1,74 @@ +package cn.cyan.sample.base; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Toast; + +import java.util.List; + +import cn.cyan.dragrecyclerview.DragRecyclerView; +import cn.cyan.dragrecyclerview.HoldTouchHelper; +import cn.cyan.sample.R; + +/** + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2017/1/4 + */ + +public abstract class BaseFragment extends Fragment { + + private List data; + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + + View view = inflater.inflate(R.layout.fragment_sample, container, false); + initView(view); + return view; + } + + private void initView(View view) { + + DragRecyclerView dragRecyclerView = (DragRecyclerView) view.findViewById(R.id.drv); + dragRecyclerView.addItemDecoration(new DividerGridItemDecoration()); + dragRecyclerView.setHasFixedSize(false); + dragRecyclerView.setLayoutManager(layoutManager()); + data = initData(); + /** custom setting */ + dragRecyclerView + .dragEnable(true) + .showDragAnimation(true) + .setDragAdapter(adapter(data)) + .bindEvent(onItemTouchEvent); + } + + + protected abstract SampleAdapter adapter(List data); + + protected abstract RecyclerView.LayoutManager layoutManager(); + + HoldTouchHelper.OnItemTouchEvent onItemTouchEvent = new HoldTouchHelper.OnItemTouchEvent() { + @Override + public void onLongPress(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position) { + if (((SampleAdapter) recyclerView.getAdapter()).onItemDrag(position)) { + ((DragRecyclerView) recyclerView).startDrag(position); + } + } + + @Override + public void onItemClick(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int position) { + String text = data.get(position).getText(); + Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show(); + } + }; + + + protected abstract List initData(); + +} diff --git a/sample/src/main/java/cn/cyan/dragtab/tab/DividerGridItemDecoration.java b/sample/src/main/java/cn/cyan/sample/base/DividerGridItemDecoration.java similarity index 89% rename from sample/src/main/java/cn/cyan/dragtab/tab/DividerGridItemDecoration.java rename to sample/src/main/java/cn/cyan/sample/base/DividerGridItemDecoration.java index 181d3d7..9d2cda1 100644 --- a/sample/src/main/java/cn/cyan/dragtab/tab/DividerGridItemDecoration.java +++ b/sample/src/main/java/cn/cyan/sample/base/DividerGridItemDecoration.java @@ -1,4 +1,4 @@ -package cn.cyan.dragtab.tab; +package cn.cyan.sample.base; import android.content.Context; import android.content.res.TypedArray; @@ -11,9 +11,8 @@ import android.view.View; /** - * Desc : 网格线 - * User : Cyan(baocq@maritech.cn) - * New : 2016/9/28 8:45 + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2017/1/4 */ public class DividerGridItemDecoration extends RecyclerView.ItemDecoration { @@ -27,7 +26,7 @@ public DividerGridItemDecoration(Context context) { a.recycle(); } - public DividerGridItemDecoration(int color) { + private DividerGridItemDecoration(int color) { mDivider = new ColorDrawable(color); } @@ -41,7 +40,7 @@ public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { drawVertical(c, parent); } - public void drawHorizontal(Canvas c, RecyclerView parent) { + private void drawHorizontal(Canvas c, RecyclerView parent) { int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); @@ -57,7 +56,7 @@ public void drawHorizontal(Canvas c, RecyclerView parent) { } } - public void drawVertical(Canvas c, RecyclerView parent) { + private void drawVertical(Canvas c, RecyclerView parent) { final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); diff --git a/sample/src/main/java/cn/cyan/sample/base/MainActivity.java b/sample/src/main/java/cn/cyan/sample/base/MainActivity.java new file mode 100644 index 0000000..b89bbe6 --- /dev/null +++ b/sample/src/main/java/cn/cyan/sample/base/MainActivity.java @@ -0,0 +1,48 @@ +package cn.cyan.sample.base; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v7.app.AppCompatActivity; +import android.view.Menu; +import android.view.MenuItem; + +import cn.cyan.sample.R; +import cn.cyan.sample.grid.GridFragment; +import cn.cyan.sample.list.ListFragment; + +/** + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2017/1/4 + */ +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + setFragment(new GridFragment()); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.main_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.grid: + setFragment(new GridFragment()); + break; + case R.id.list: + setFragment(new ListFragment()); + break; + } + return super.onOptionsItemSelected(item); + } + + private void setFragment(Fragment fragment) { + getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit(); + } +} diff --git a/sample/src/main/java/cn/cyan/sample/base/SampleAdapter.java b/sample/src/main/java/cn/cyan/sample/base/SampleAdapter.java new file mode 100644 index 0000000..f423d45 --- /dev/null +++ b/sample/src/main/java/cn/cyan/sample/base/SampleAdapter.java @@ -0,0 +1,54 @@ +package cn.cyan.sample.base; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; + +import java.util.Collections; +import java.util.List; + +import cn.cyan.dragrecyclerview.OnItemChangeListener; + +/** + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2017/1/4 + */ +public abstract class SampleAdapter extends RecyclerView.Adapter implements OnItemChangeListener { + protected Context context; + protected volatile List data; + + public SampleAdapter(Context context, List list) { + this.context = context; + this.data = list; + } + + @Override + public void onItemMoved(int form, int target) { + if (form < target) { + // after + for (int i = form; i < target; i++) { + Collections.swap(data, i, i + 1); + } + } else { + // before + for (int i = form; i > target; i--) { + Collections.swap(data, i, i - 1); + } + } + notifyItemMoved(form, target); + } + + @Override + public int getItemCount() { + return data.size(); + } + + @Override + public boolean onItemDrag(int position) { + return data.get(position).isDragEnable(); + } + + @Override + public boolean onItemDrop(int position) { + return data.get(position).isDropEnable(); + } +} diff --git a/sample/src/main/java/cn/cyan/sample/base/SampleApplication.java b/sample/src/main/java/cn/cyan/sample/base/SampleApplication.java new file mode 100644 index 0000000..c63f412 --- /dev/null +++ b/sample/src/main/java/cn/cyan/sample/base/SampleApplication.java @@ -0,0 +1,22 @@ +package cn.cyan.sample.base; + +import android.app.Application; +import android.content.Context; + +/** + * User : Cyan(baocq@maritech.cn) + * Date : 2017/1/4 + */ +public class SampleApplication extends Application { + + private static Context context; + + @Override + public void onCreate() { + context=getApplicationContext(); + } + + public static Context getContext() { + return context; + } +} diff --git a/sample/src/main/java/cn/cyan/sample/base/SampleEntity.java b/sample/src/main/java/cn/cyan/sample/base/SampleEntity.java new file mode 100644 index 0000000..ad2815d --- /dev/null +++ b/sample/src/main/java/cn/cyan/sample/base/SampleEntity.java @@ -0,0 +1,37 @@ +package cn.cyan.sample.base; + +/** + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2017/1/4 + */ +public class SampleEntity { + + private String text; + private boolean dragEnable; + private boolean dropEnable; + + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public boolean isDragEnable() { + return dragEnable; + } + + public void setDragEnable(boolean dragEnable) { + this.dragEnable = dragEnable; + } + + public boolean isDropEnable() { + return dropEnable; + } + + public void setDropEnable(boolean dropEnable) { + this.dropEnable = dropEnable; + } +} diff --git a/sample/src/main/java/cn/cyan/sample/grid/FindIconHelper.java b/sample/src/main/java/cn/cyan/sample/grid/FindIconHelper.java new file mode 100644 index 0000000..ee149a2 --- /dev/null +++ b/sample/src/main/java/cn/cyan/sample/grid/FindIconHelper.java @@ -0,0 +1,52 @@ +package cn.cyan.sample.grid; + +import android.support.annotation.StringRes; + +import java.util.HashMap; +import java.util.Map; + +import cn.cyan.sample.R; +import cn.cyan.sample.base.SampleApplication; + +/** + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2017/1/4 + */ + +public class FindIconHelper { + + private static FindIconHelper instance; + + private Map icons; + + public static FindIconHelper getInstance() { + if (instance == null) { + instance = new FindIconHelper(); + } + return instance; + } + + private FindIconHelper() { + icons = new HashMap<>(); + icons.put(getStr(R.string.hot),R.mipmap.hot); + icons.put(getStr(R.string.news_international),R.mipmap.news_international); + icons.put(getStr(R.string.news),R.mipmap.news); + icons.put(getStr(R.string.beauty),R.mipmap.beauty); + icons.put(getStr(R.string.politics),R.mipmap.politics); + icons.put(getStr(R.string.sports),R.mipmap.sports); + icons.put(getStr(R.string.gossip),R.mipmap.gossip); + icons.put(getStr(R.string.more),R.mipmap.more); + } + + private String getStr(@StringRes int resId){ + return SampleApplication.getContext().getString(resId); + } + + + public int find(String name) { + if (icons.keySet().contains(name)) { + return icons.get(name); + } + return R.mipmap.ic_launcher; + } +} diff --git a/sample/src/main/java/cn/cyan/sample/grid/GridAdapter.java b/sample/src/main/java/cn/cyan/sample/grid/GridAdapter.java new file mode 100644 index 0000000..e7a1e21 --- /dev/null +++ b/sample/src/main/java/cn/cyan/sample/grid/GridAdapter.java @@ -0,0 +1,55 @@ +package cn.cyan.sample.grid; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import java.util.List; + +import cn.cyan.sample.R; +import cn.cyan.sample.base.SampleAdapter; +import cn.cyan.sample.base.SampleEntity; + +/** + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2017/1/4 + */ +public class GridAdapter extends SampleAdapter { + + public GridAdapter(Context context, List list) { + super(context, list); + } + + @Override + public GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View itemView = LayoutInflater.from(context).inflate(R.layout.item_grid, parent, false); + return new GridViewHolder(itemView); + } + + @Override + public void onBindViewHolder(GridViewHolder holder, int position) { + holder.setText(data.get(position).getText()); + } + + class GridViewHolder extends RecyclerView.ViewHolder { + ImageView imageView; + TextView textView; + + GridViewHolder(View itemView) { + super(itemView); + imageView = (ImageView) itemView.findViewById(R.id.imageView); + textView = (TextView) itemView.findViewById(R.id.textView); + } + + void setText(String name) { + imageView.setImageResource(FindIconHelper.getInstance().find(name)); + textView.setText(name); + } + + } + +} diff --git a/sample/src/main/java/cn/cyan/sample/grid/GridFragment.java b/sample/src/main/java/cn/cyan/sample/grid/GridFragment.java new file mode 100644 index 0000000..79b3d3a --- /dev/null +++ b/sample/src/main/java/cn/cyan/sample/grid/GridFragment.java @@ -0,0 +1,50 @@ +package cn.cyan.sample.grid; + +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.List; + +import cn.cyan.sample.R; +import cn.cyan.sample.base.BaseFragment; +import cn.cyan.sample.base.SampleAdapter; +import cn.cyan.sample.base.SampleEntity; + +/** + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2017/1/4 + */ +public class GridFragment extends BaseFragment { + @Override + protected SampleAdapter adapter(List data) { + return new GridAdapter(getActivity(),data); + } + + @Override + protected RecyclerView.LayoutManager layoutManager() { + return new GridLayoutManager(getActivity(),3); + } + + @Override + protected List initData() { + List result = new ArrayList<>(); + String[] strings = getResources().getStringArray(R.array.grid_array); + for (String s : strings) { + SampleEntity item = new SampleEntity(); + item.setText(s); + item.setDragEnable(true); + item.setDropEnable(true); + result.add(item); + } + // add more + SampleEntity addMore = new SampleEntity(); + addMore.setText(getString(R.string.more)); + addMore.setDragEnable(false); + addMore.setDragEnable(false); + result.add(addMore); + return result; + } + + +} diff --git a/sample/src/main/java/cn/cyan/sample/list/ListAdapter.java b/sample/src/main/java/cn/cyan/sample/list/ListAdapter.java new file mode 100644 index 0000000..cca73ce --- /dev/null +++ b/sample/src/main/java/cn/cyan/sample/list/ListAdapter.java @@ -0,0 +1,50 @@ +package cn.cyan.sample.list; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import java.util.List; + +import cn.cyan.dragrecyclerview.OnItemChangeListener; +import cn.cyan.sample.R; +import cn.cyan.sample.base.SampleAdapter; +import cn.cyan.sample.base.SampleEntity; + +/** + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2017/1/4 + */ +public class ListAdapter extends SampleAdapter implements OnItemChangeListener { + public ListAdapter(Context context, List list) { + super(context, list); + } + + @Override + public ListAdapter.ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View itemView = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false); + return new ListAdapter.ListViewHolder(itemView); + } + + @Override + public void onBindViewHolder(ListAdapter.ListViewHolder holder, int position) { + holder.setText(data.get(position).getText()); + } + + class ListViewHolder extends RecyclerView.ViewHolder { + TextView textView; + + ListViewHolder(View itemView) { + super(itemView); + textView = (TextView) itemView.findViewById(R.id.textView); + } + + void setText(String name) { + textView.setText(name); + } + + } +} diff --git a/sample/src/main/java/cn/cyan/sample/list/ListFragment.java b/sample/src/main/java/cn/cyan/sample/list/ListFragment.java new file mode 100644 index 0000000..e44b4b6 --- /dev/null +++ b/sample/src/main/java/cn/cyan/sample/list/ListFragment.java @@ -0,0 +1,42 @@ +package cn.cyan.sample.list; + +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.List; + +import cn.cyan.sample.R; +import cn.cyan.sample.base.BaseFragment; +import cn.cyan.sample.base.SampleAdapter; +import cn.cyan.sample.base.SampleEntity; + +/** + * User : Cyan(newbeeeeeeeee@gmail.com) + * Date : 2017/1/4 + */ +public class ListFragment extends BaseFragment { + @Override + protected SampleAdapter adapter(List data) { + return new ListAdapter(getActivity(),data); + } + + @Override + protected RecyclerView.LayoutManager layoutManager() { + return new LinearLayoutManager(getActivity()); + } + + @Override + protected List initData() { + List result = new ArrayList<>(); + String[] strings = getResources().getStringArray(R.array.list_array); + for (String s : strings) { + SampleEntity item = new SampleEntity(); + item.setText(s); + item.setDragEnable(true); + item.setDropEnable(true); + result.add(item); + } + return result; + } +} diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 2f4c2b6..165e653 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -4,12 +4,12 @@ android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="cn.cyan.dragtab.MainActivity"> + tools:context="cn.cyan.sample.base.MainActivity"> - - + diff --git a/sample/src/main/res/layout/fragment_sample.xml b/sample/src/main/res/layout/fragment_sample.xml new file mode 100644 index 0000000..2a5d934 --- /dev/null +++ b/sample/src/main/res/layout/fragment_sample.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/sample/src/main/res/layout/item_tab.xml b/sample/src/main/res/layout/item_grid.xml similarity index 92% rename from sample/src/main/res/layout/item_tab.xml rename to sample/src/main/res/layout/item_grid.xml index 839d091..996f60c 100644 --- a/sample/src/main/res/layout/item_tab.xml +++ b/sample/src/main/res/layout/item_grid.xml @@ -15,13 +15,13 @@ android:padding="20dp"> + + + + + + + + + \ No newline at end of file diff --git a/sample/src/main/res/menu/main_menu.xml b/sample/src/main/res/menu/main_menu.xml new file mode 100644 index 0000000..07fd193 --- /dev/null +++ b/sample/src/main/res/menu/main_menu.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/sample/src/main/res/mipmap-xhdpi/woman.png b/sample/src/main/res/mipmap-xhdpi/beauty.png similarity index 100% rename from sample/src/main/res/mipmap-xhdpi/woman.png rename to sample/src/main/res/mipmap-xhdpi/beauty.png diff --git a/sample/src/main/res/mipmap-xhdpi/tea.png b/sample/src/main/res/mipmap-xhdpi/gossip.png similarity index 100% rename from sample/src/main/res/mipmap-xhdpi/tea.png rename to sample/src/main/res/mipmap-xhdpi/gossip.png diff --git a/sample/src/main/res/mipmap-xhdpi/add.png b/sample/src/main/res/mipmap-xhdpi/more.png similarity index 100% rename from sample/src/main/res/mipmap-xhdpi/add.png rename to sample/src/main/res/mipmap-xhdpi/more.png diff --git a/sample/src/main/res/values/array.xml b/sample/src/main/res/values/array.xml index 7504177..868e7df 100644 --- a/sample/src/main/res/values/array.xml +++ b/sample/src/main/res/values/array.xml @@ -1,13 +1,23 @@ - - 今日热点 - 国际新闻 - 及时快讯 - 美女图片 - 政经要闻 - 体育赛事 - 茶余饭后 + + @string/hot + @string/news_international + @string/news + @string/beauty + @string/politics + @string/sports + @string/gossip + + + + + A day is a miniature of eternity. + Miracles sometimes occur, but one has to work terribly for them. + It takes two to make a quarrel. + Beware beginning. + Miss the songs have a one and alone with in. + all or nothing, now or never. \ No newline at end of file diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index 756aa0e..886820c 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -1,3 +1,13 @@ - DragTab + DragRecyclerView + Grid + List + 今日热点 + 国际新闻 + 及时快讯 + 美女图片 + 政经要闻 + 体育赛事 + 茶余饭后 + 添加更多 diff --git a/sample/src/test/java/cn/cyan/dragtab/ExampleUnitTest.java b/sample/src/test/java/cn/cyan/sample/ExampleUnitTest.java similarity index 93% rename from sample/src/test/java/cn/cyan/dragtab/ExampleUnitTest.java rename to sample/src/test/java/cn/cyan/sample/ExampleUnitTest.java index 5e85773..8a1c992 100644 --- a/sample/src/test/java/cn/cyan/dragtab/ExampleUnitTest.java +++ b/sample/src/test/java/cn/cyan/sample/ExampleUnitTest.java @@ -1,4 +1,4 @@ -package cn.cyan.dragtab; +package cn.cyan.sample; import org.junit.Test;