频道管理的简单使用

点击编辑”-- 后“我的频道”里面的内容可以删掉回到下面“其他频道里面”,,点击“其他频道”里的内容可以到上面的“我的频道”里。
下面是频道管理主acitivityxml布局的代码,根布局为LinearLayout
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center|left"
        android:paddingLeft="20dp"
        android:text="我的频道" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical|left"
        android:paddingLeft="10dp"
        android:text="长按拖拽替换位置"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/redsharp"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="编辑"
        android:textSize="15sp" />


</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/hui" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/mine_recycleView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/hui" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center|left"
        android:paddingLeft="20dp"
        android:text="其他频道" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical|left"
        android:paddingLeft="10dp"
        android:text="点击添加替换我的频道中"
        android:textSize="15sp" />


</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/hui" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/other_recycleView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

//这是适配器中list_view的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/channel_rl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/sharp"

    >

    <TextView
        android:id="@+id/channel_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/delete_tv"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:background="@drawable/delete" />
</RelativeLayout>

在drawable中
创建一个redsharp.xml的文件,用来显示编辑框的形状
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"

    >
<solid android:color="@color/white" /> <!-- 定义填充的颜色值 -->
<padding
    android:bottom="2dp"
    android:left="2dp"
    android:right="2dp"
    android:top="2dp" />
<!-- 设置圆角矩形 -->
<corners android:radius="100dp" />
<stroke android:width="2dp" android:color="@color/red" />
</shape>

创建一个sharp.xml的文件,用来显示频道中的分割条
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />
    <!-- 设置圆角矩形 -->
    <corners android:radius="1dp" />
    <stroke android:width="0.5dp" android:color="#000000" />
</shape>
在valus目录下,在colors.xml文件中加入以下属性,配合上文使用
<color name="red">#DF3234</color>
<color name="black">#000000</color>
<color name="white">#FFFFFF</color>
<color name="hui">#999999</color>
最后,放上一个名为delete.png的文件,这个文件就是你点击编辑按钮,显示的文件,点击图片进行删除

然后进行代码需要的类

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener{
    private View childView;
    private RecyclerView touchView;
    private RecyclerView.ViewHolder viewHolder;
    GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, final OnItemClickListener mListener) {
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onSingleTapUp(MotionEvent ev) {
                if (childView != null && mListener != null) {
                    mListener.onItemClick(childView, touchView.getChildPosition(childView),viewHolder);
                }
                return true;
            }
            @Override
            public void onLongPress(MotionEvent ev) {
                if (childView != null && mListener != null) {
                    mListener.onLongClick(childView, touchView.getChildPosition(childView),viewHolder);
                }
            }
        });
    }



    public interface OnItemClickListener {
        public void onItemClick(View view, int position, RecyclerView.ViewHolder viewHolder);
        public void onLongClick(View view, int posotion, RecyclerView.ViewHolder viewHolder);
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
        mGestureDetector.onTouchEvent(motionEvent);
        childView = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
        touchView = recyclerView;
        if (childView!=null){
            viewHolder = recyclerView.getChildViewHolder(childView);
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

bean类
public class ItemBean {

    public ItemBean(String text, boolean select) {
        this.text = text;
        this.select = select;
    }

    private String text;
    private boolean select;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public boolean isSelect() {
        return select;
    }

    public void setSelect(boolean select) {
        this.select = select;
    }

}

上面都是准备工作,下面就是主代码,直接粘贴,各种导包就能使用了


private RecyclerView mineView;
private RecyclerView otherView;
private List<ItemBean> mineData = new ArrayList<ItemBean>();
private List<ItemBean> otherData = new ArrayList<ItemBean>();
private MyrecyAdapter otherAdapter;
private TextView edit;
boolean isediting = false;
private MyrecyAdapter mineAdapter;


//此
方法在oncreate中调用

private void initView() {
    {
        mineView = (RecyclerView) findViewById(R.id.mine_recycleView);
        edit = (TextView) findViewById(R.id.edit);
        mineAdapter = new MyrecyAdapter(this, mineData, 0);

        mineView.setLayoutManager(new GridLayoutManager(TwoActivity.this, 3));
        mineView.setAdapter(mineAdapter);

        otherView = (RecyclerView) findViewById(R.id.other_recycleView);
        otherView.setLayoutManager(new GridLayoutManager(TwoActivity.this, 3));
        otherAdapter = new MyrecyAdapter(this, otherData, 1);
        otherView.setAdapter(otherAdapter);

        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isediting) {
                    edit.setText("编辑");
                    for (int i = 0; i < mineData.size(); i++) {
                        mineData.get(i).setSelect(false);
                    }
                } else {
                    edit.setText("完成");
                    for (int i = 0; i < mineData.size(); i++) {
                        mineData.get(i).setSelect(true);
                    }
                }
                isediting = !isediting;
                mineAdapter.notifyDataSetChanged();

            }
        });
        //下面的条目触摸事件
        otherView.addOnItemTouchListener(new RecyclerItemClickListener(TwoActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, final int position, RecyclerView.ViewHolder childViewViewHolder) {

                final ImageView moveImageView = getView(view);

                if (moveImageView != null) {
                    TextView newTextView = (TextView) view.findViewById(R.id.channel_tv);
                    final int[] startLocation = new int[2];
                    newTextView.getLocationInWindow(startLocation);
                    final ItemBean channel = otherAdapter.getItem(position);
                    if (isediting) {
                        channel.setSelect(true);
                    } else {
                        channel.setSelect(false);
                    }
                    mineAdapter.addItem(channel);
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            try {
                                int[] endLocation = new int[2];
                                RecyclerView.LayoutManager mine_layoutManager = mineView.getLayoutManager();
                                if (mine_layoutManager instanceof LinearLayoutManager) {
                                    LinearLayoutManager linearManager = (LinearLayoutManager) mine_layoutManager;
                                    int lastItemPosition = linearManager.findLastVisibleItemPosition();
                                    mineView.getChildAt(lastItemPosition).getLocationInWindow(endLocation);
                                    otherAdapter.deleteItem(position);
                                }
                            } catch (Exception localException) {
                            }
                        }
                    }, 50L);
                }
            }

            @Override
            public void onLongClick(View view, int posotion, RecyclerView.ViewHolder childViewViewHolder) {

            }
        }));

    }


}
//此方法在oncreate中调用
private void initData() {
    for (int i = 0; i < 15; i++) {
        mineData.add(new ItemBean("我的" + i, false));

    }
    for (int i = 15; i < 30; i++) {
        otherData.add(new ItemBean("编辑" + i, false));
    }

}


private ImageView getView(View view) {
    view.destroyDrawingCache();
    view.setDrawingCacheEnabled(true);
    Bitmap cache = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);
    ImageView iv = new ImageView(this);
    iv.setImageBitmap(cache);
    return iv;
}


//适配器类
 public class MyrecyAdapter extends RecyclerView.Adapter<MyrecyAdapter.MyViewHolder>  {

        private List<ItemBean> mData;
        private int type;
        private Context context;

        public MyrecyAdapter(Context context, List<ItemBean> list, int type) {
            this.context = context;
            this.mData = list;
            this.type = type;
        }


        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(context, R.layout.list_view, null);
            MyViewHolder holder = new MyViewHolder(view);

            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, final int position) {
            //给每一个View设置内容
            holder.channel_tv.setText(mData.get(position).getText());
            //48-51  去掉other前面的图片
            if (type == 1) {
                holder.delete_tv.setVisibility(View.GONE);
            }
            //52-56去掉mine前面的图片
            else if (type == 0) {
                if (position == 0 || position == 1) {
                    holder.delete_tv.setVisibility(View.GONE);
                } else if (mData.get(position).isSelect()) {
                    holder.delete_tv.setVisibility(View.VISIBLE);
                } else {
                    holder.delete_tv.setVisibility(View.GONE);
                }
            }

            holder.delete_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (type == 0) {
                        ItemBean positionItemBean = mineAdapter.getItem(position);
                        positionItemBean.setSelect(false);
                        mineAdapter.deleteItem(position);
                        otherAdapter.addItem(positionItemBean);
                    }
                }
            });

        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        public ItemBean getItem(int position) {
            return mData.get(position);
        }

        public void addItem(ItemBean channel) {
            mData.add(channel);
            notifyDataSetChanged();
        }


        public void deleteItem(int position) {
            mData.remove(position);
            notifyDataSetChanged();
        }


        class MyViewHolder extends RecyclerView.ViewHolder {
            private TextView channel_tv;
            private TextView delete_tv;
            private RelativeLayout channel_rl;

            public MyViewHolder(View itemView) {
                super(itemView);

                channel_tv = (TextView) itemView.findViewById(R.id.channel_tv);
                delete_tv = (TextView) itemView.findViewById(R.id.delete_tv);
                channel_rl = (RelativeLayout) itemView.findViewById(R.id.channel_rl);
            }


        }
    }








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值