package com.goach.tabdemo.adapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.goach.tabdemo.APPConst;
import com.goach.tabdemo.R;
import com.goach.tabdemo.activity.MyChannelHeaderWidget;
import com.goach.tabdemo.activity.MyChannelWidget;
import com.goach.tabdemo.activity.RecChannelHeaderWidget;
import com.goach.tabdemo.activity.RecChannelWidget;
import com.goach.tabdemo.base.EditModeHandler;
import com.goach.tabdemo.base.IChannelType;
import com.goach.tabdemo.base.ItemDragListener;
import com.goach.tabdemo.base.ItemDragVHListener;
import com.goach.tabdemo.bean.ChannelBean;
import com.goach.tabdemo.view.helper.ItemDragHelperCallback;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import java.util.List;
/**
* Created by goach on 2016/9/28.
*/
public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ChannelViewHolder> implements
ItemDragListener {
private List<ChannelBean> mMyChannelItems;
private List<ChannelBean> mOtherChannelItems;
private int mMyHeaderCount;
private int mRecHeaderCount;
private LayoutInflater mInflater;
private SparseArray<IChannelType> mTypeMap = new SparseArray();
private boolean isEditMode;
private ItemTouchHelper mItemTouchHelper;
// touch 点击开始时间
private long startTime;
// touch 间隔时间 用于分辨是否是 "点击"
private static final long SPACE_TIME = 100;
private ChannelItemClickListener channelItemClickListener;
public ChannelAdapter(Context context,RecyclerView recyclerView, List<ChannelBean> myChannelItems,List<ChannelBean> otherChannelItems,
int myHeaderCount,int recHeaderCount){
this.mItemTouchHelper = new ItemTouchHelper(new ItemDragHelperCallback(this));
mItemTouchHelper.attachToRecyclerView(recyclerView);
this.mMyChannelItems = myChannelItems;
this.mOtherChannelItems = otherChannelItems;
this.mMyHeaderCount = myHeaderCount;
this.mRecHeaderCount = recHeaderCount ;
mInflater = LayoutInflater.from(context);
mTypeMap.put(IChannelType.TYPE_MY_CHANNEL_HEADER,new MyChannelHeaderWidget(new EditHandler()));
mTypeMap.put(IChannelType.TYPE_MY_CHANNEL,new MyChannelWidget(new EditHandler()));
mTypeMap.put(IChannelType.TYPE_REC_CHANNEL_HEADER,new RecChannelHeaderWidget());
mTypeMap.put(IChannelType.TYPE_REC_CHANNEL,new RecChannelWidget(new EditHandler()));
}
@Override
public ChannelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return mTypeMap.get(viewType).createViewHolder(mInflater,parent);
}
@Override
public void onBindViewHolder(ChannelViewHolder holder, int position) {
if(getItemViewType(position)==IChannelType.TYPE_MY_CHANNEL){
int myPosition = position-mMyHeaderCount;
myPosition = myPosition<0||myPosition>=mMyChannelItems.size()?0:myPosition;
mTypeMap.get(getItemViewType(position)).bindViewHolder(holder,position,mMyChannelItems.get(myPosition));
return;
}
if(getItemViewType(position)==IChannelType.TYPE_REC_CHANNEL){
int otherPosition = position-mMyChannelItems.size()-mMyHeaderCount-mRecHeaderCount;
otherPosition = otherPosition<0||otherPosition>=mOtherChannelItems.size()?0:otherPosition;
mTypeMap.get(getItemViewType(position)).bindViewHolder(holder,position,mOtherChannelItems.get(otherPosition));
return;
}
mTypeMap.get(getItemViewType(position)).bindViewHolder(holder,position,null);
}
@Override
public int getItemViewType(int position) {
if(position<mMyHeaderCount)
return IChannelType.TYPE_MY_CHANNEL_HEADER;
if(position>=mMyHeaderCount&&position<mMyChannelItems.size()+mMyHeaderCount)
return IChannelType.TYPE_MY_CHANNEL;
if(position>=mMyChannelItems.size()+mMyHeaderCount&&position<mMyChannelItems.size()+mMyHeaderCount+mRecHeaderCount)
return IChannelType.TYPE_REC_CHANNEL_HEADER;
return IChannelType.TYPE_REC_CHANNEL;
}
@Override
public int getItemCount() {
return mMyChannelItems.size()+mOtherChannelItems.size()+mMyHeaderCount+mRecHeaderCount;
}
@Override
public void onItemMove(int fromPosition, int toPosition) {
if(toPosition > 2){
ChannelBean item = mMyChannelItems.get(fromPosition - mMyHeaderCount);
mMyChannelItems.remove(fromPosition - mMyHeaderCount);
mMyChannelItems.add(toPosition - mMyHeaderCount, item);
notifyItemMoved(fromPosition, toPosition);
}
}
@Override
public void onItemSwiped(int position) {
}
public static class ChannelViewHolder extends RecyclerView.ViewHolder implements ItemDragVHListener {
public ChannelViewHolder(View itemView) {
super(itemView);
}
@Override
public void onItemSelected() {
scaleItem(1.0f , 1.2f , 0.5f);
}
@Override
public void onItemFinished() {
scaleItem(1.2f , 1.0f , 1.0f);
}
public void scaleItem(float start , float end , float alpha) {
ObjectAnimator anim1 = ObjectAnimator.ofFloat(itemView, "scaleX",
start, end);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(itemView, "scaleY",
start, end);
ObjectAnimator anim3 = ObjectAnimator.ofFloat(itemView, "alpha",
alpha);
AnimatorSet animSet = new AnimatorSet();
animSet.setDuration(200);
animSet.setInterpolator(new LinearInterpolator());
animSet.playTogether(anim1, anim2 ,anim3);
animSet.start();
}
}
private class EditHandler extends EditModeHandler{
@Override
public void startEditMode(RecyclerView mRecyclerView) {
doStartEditMode(mRecyclerView);
}
@Override
public void cancelEditMode(RecyclerView mRecyclerView) {
doCancelEditMode(mRecyclerView);
}
@Override
public void clickMyChannel(RecyclerView mRecyclerView,ChannelAdapter.ChannelViewHolder holder) {
RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
int position = holder.getAdapterPosition();
if(isEditMode){
View targetView = layoutManager.findViewByPosition(mMyChannelItems.size()
+mMyHeaderCount+mRecHeaderCount);
View currentView = mRecyclerView.getLayoutManager().findViewByPosition(position);
int targetX ;
int targetY;
if(mRecyclerView.indexOfChild(targetView)>=0){
int spanCount = ((GridLayoutManager)layoutManager).getSpanCount();
targetX = targetView.getLeft();
targetY = targetView.getTop();
if ((mMyChannelItems.size()) % spanCount == 1) {
View preTargetView = la
没有合适的资源?快使用搜索试试~ 我知道了~
高仿新闻类APP频道管理功能,ItemTouchHelper的实践
共122个文件
xml:48个
java:24个
bin:16个
5星 · 超过95%的资源 需积分: 10 146 下载量 16 浏览量
2016-10-21
18:58:49
上传
评论 2
收藏 2.25MB ZIP 举报
温馨提示
高仿新闻类APP频道管理功能,参考博客http://blog.csdn.net/Iamzgx/article/details/52843653
资源推荐
资源详情
资源评论
收起资源包目录
高仿新闻类APP频道管理功能,ItemTouchHelper的实践 (122个子文件)
gradlew.bat 2KB
fileSnapshots.bin 6.28MB
fileSnapshots.bin 5.22MB
localClassSetAnalysis.bin 876KB
localClassSetAnalysis.bin 280KB
fileHashes.bin 244KB
fileHashes.bin 145KB
taskArtifacts.bin 136KB
taskArtifacts.bin 111KB
fileSnapshotsToTreeSnapshotsIndex.bin 31KB
localJarClasspathSnapshot.bin 23KB
localJarClasspathSnapshot.bin 22KB
outputFileStates.bin 20KB
localJarClasspathSnapshot.bin 20KB
localJarClasspathSnapshot.bin 19KB
localClassSetAnalysis.bin 18KB
localClassSetAnalysis.bin 18KB
.gitignore 105B
.gitignore 8B
build.gradle 903B
build.gradle 521B
settings.gradle 16B
gradlew 5KB
app.iml 12KB
TabDemo.iml 938B
gradle-wrapper.jar 52KB
ChannelAdapter.java 17KB
ZTabLayout.java 14KB
MainActivity.java 4KB
AddChannelFragment.java 4KB
IndicationTabLayout.java 4KB
MyChannelWidget.java 4KB
ItemDragHelperCallback.java 3KB
MyChannelHeaderWidget.java 2KB
RecChannelWidget.java 2KB
TwoFragment.java 2KB
OneFragment.java 1KB
RecChannelHeaderWidget.java 993B
AppUtils.java 987B
GridItemDecoration.java 865B
EditModeHandler.java 806B
IChannelType.java 728B
ChannelBean.java 671B
TabApplication.java 427B
ApplicationTest.java 360B
LazyFragment.java 330B
ExampleUnitTest.java 324B
ItemDragListener.java 223B
ItemDragVHListener.java 187B
APPConst.java 145B
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis.lock 17B
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis.lock 17B
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis.lock 17B
cache.properties.lock 17B
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis.lock 17B
cache.properties.lock 17B
ic_launcher.png 10KB
ic_launcher.png 8KB
ic_launcher.png 5KB
ic_launcher.png 3KB
ic_launcher.png 2KB
ic_close_normal.png 817B
channel_close.png 432B
add_channel_icon.png 159B
proguard-rules.pro 657B
gradle.properties 872B
local.properties 423B
gradle-wrapper.properties 239B
cache.properties 31B
cache.properties 31B
workspace.xml 185KB
misc.xml 3KB
activity_channel_my_header.xml 1KB
activity_simple_tab.xml 1KB
activity_channel_my.xml 1KB
support_media_compat_24_2_0.xml 968B
support_fragment_24_2_0.xml 940B
support_core_ui_24_2_0.xml 933B
support_compat_24_2_0.xml 926B
activity_channel_rec_header.xml 788B
attrs.xml 785B
recyclerview_v7_24_2_0.xml 774B
support_core_utils_24_2_0.xml 772B
appcompat_v7_24_2_0.xml 756B
activity_channel_rec.xml 756B
AndroidManifest.xml 725B
design_24_2_0.xml 720B
compiler.xml 686B
animated_vector_drawable_24_2_0.xml 640B
support_vector_drawable_24_2_0.xml 635B
gradle.xml 626B
activity_tab_edit.xml 606B
cardview_v7_24_2_0.xml 575B
runConfigurations.xml 564B
hamcrest_core_1_3.xml 526B
library_2_4_0.xml 520B
共 122 条
- 1
- 2
iGoach
- 粉丝: 99
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页