频道管理

//布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center_vertical"
        android:padding="10dp">


        <TextView
            android:id="@+id/tv_my_channel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="关注频道" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_edit_mychannel"
            android:layout_centerHorizontal="true"
            android:text="点击查看 长按拖拽排序"
            android:textSize="10sp" />


        <TextView
            android:id="@+id/tv_edit_mychannel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="编辑"
            android:textColor="@android:color/holo_red_dark" />
    </RelativeLayout>


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


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center_vertical"
        android:padding="10dp">


        <TextView
            android:id="@+id/tv_more_channel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="更多频道" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_more_channel"
            android:layout_centerHorizontal="true"
            android:text="点击添加"
            android:textSize="10sp" />


    </RelativeLayout>


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

</LinearLayout>

//频道item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/rl_channel_gray">


    <TextView
        android:id="@+id/tv_channel"
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="频道1" />


    <ImageView
        android:id="@+id/iv_operator"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignRight="@id/tv_channel"
        android:src="@drawable/ic_close_black_24dp" />

</RelativeLayout>


//程序入口

package com.bawei.channelmanager;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.TextView;


import com.bawei.channelmanager.adapter.MoreChannelAdapter;
import com.bawei.channelmanager.adapter.MyChannelAdapter;
import com.bawei.channelmanager.bean.ChannelBean;
import com.bawei.channelmanager.event.ChannelItemOpertor;
import com.bawei.channelmanager.event.MyChannelCallback;


import java.util.ArrayList;
import java.util.List;


/**
 * 频道管理
 * 需求:
 * 1. 界面
 * a) 界面包括两部分(关注的频道 和 更多频道)
 * b)频道的操作(添加、删除、拖动)
 * 2. 数据存储
 * a)界面变化后 需要持久化到数据库中
 *
 * @author zhaoliang
 * @version 1.0
 * @create 2018/6/28
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private RecyclerView rvMyChannel;   // 关注频道
    private RecyclerView rvMoreChannel; // 更多频道
    private List<ChannelBean> myChannel = new ArrayList<>();
    private List<ChannelBean> moreChannel = new ArrayList<>();
    private MyChannelAdapter myChannelAdapter;
    private MoreChannelAdapter moreChannelAdapter;


    private TextView tvEditChannel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        gernerateData();


        tvEditChannel = findViewById(R.id.tv_edit_mychannel);
        tvEditChannel.setOnClickListener(this);


        /* 关注的channel */
        rvMyChannel = findViewById(R.id.rv_my_channel);


        /* 关注 */
        rvMyChannel.setLayoutManager(new GridLayoutManager(this, 4));
        myChannelAdapter = new MyChannelAdapter(this, myChannel);
        rvMyChannel.setAdapter(myChannelAdapter);


        // 实现拖动
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new MyChannelCallback(myChannelAdapter));
        itemTouchHelper.attachToRecyclerView(rvMyChannel);


        /* 更多 */
        rvMoreChannel = findViewById(R.id.rv_more_channel);
        rvMoreChannel.setLayoutManager(new GridLayoutManager(this, 4));
        moreChannelAdapter = new MoreChannelAdapter(this, moreChannel);
        rvMoreChannel.setAdapter(moreChannelAdapter);


        /* 把更多的数据给关注的频道 */
        moreChannelAdapter.setOpertor(new ChannelItemOpertor() {
            @Override
            public void onItemClick(ChannelBean bean) {
                myChannel.add(bean);
                myChannelAdapter.notifyDataSetChanged();
            }
        });


        /* 把关注的频道给更多的频道 */
        myChannelAdapter.setOpertor(new ChannelItemOpertor() {
            @Override
            public void onItemClick(ChannelBean bean) {
                moreChannel.add(bean);
                moreChannelAdapter.notifyDataSetChanged();
            }
        });


    }


    /**
     * 生成假数据
     */
    private void gernerateData() {
        for (int i = 0; i < 20; i++) {
            if (i % 2 == 0) {
                myChannel.add(new ChannelBean("频道" + i, i, false));
            } else {
                moreChannel.add(new ChannelBean("频道" + i, i, true));
            }
        }


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_edit_mychannel:
                boolean show = myChannelAdapter.isShow();
                if (show) {
                    tvEditChannel.setText("编辑");
                } else {
                    tvEditChannel.setText("完成");
                }
                myChannelAdapter.setShow(!show);
                myChannelAdapter.notifyDataSetChanged();
                break;
        }
    }

}


//接口回调MyChannelCallback

package com.bawei.channelmanager.event;


import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;


import com.bawei.channelmanager.R;


public class MyChannelCallback extends ItemTouchHelper.Callback {


    private TouchListener touchListener;


    public MyChannelCallback(TouchListener touchListener) {
        this.touchListener = touchListener;
    }


    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {


        int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        return makeMovementFlags(dragFlag, 0);
    }


    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int currentPostion = viewHolder.getLayoutPosition();
        int targetPostion = target.getLayoutPosition();


        touchListener.move(currentPostion, targetPostion);
        return true;
    }


    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {


    }


    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            viewHolder.itemView.setBackgroundResource(R.drawable.rl_channel_popurl);
        }


        super.onSelectedChanged(viewHolder, actionState);
    }


    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        viewHolder.itemView.setBackgroundResource(R.drawable.rl_channel_gray);


        super.clearView(recyclerView, viewHolder);
    }

}


//拖动接口  标签互换接口

package com.bawei.channelmanager.event;


public interface TouchListener {


    void move(int currentPostion, int targetPostion);

}


package com.bawei.channelmanager.event;


import com.bawei.channelmanager.bean.ChannelBean;


public interface ChannelItemOpertor {


    void onItemClick(ChannelBean bean);
}

//my适配器

package com.bawei.channelmanager.adapter;


import android.content.Context;
import android.media.Image;
import android.support.annotation.NonNull;
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 com.bawei.channelmanager.R;
import com.bawei.channelmanager.bean.ChannelBean;
import com.bawei.channelmanager.event.ChannelItemOpertor;
import com.bawei.channelmanager.event.TouchListener;


import java.util.Collections;
import java.util.List;


/**
 * 我的频道的适配器
 *
 * @author zhaoliang
 * @version 1.0
 * @create 2018/6/28
 */
public class MyChannelAdapter extends RecyclerView.Adapter<MyChannelAdapter.ViewHolder> implements TouchListener {


    private Context context;
    private List<ChannelBean> beanList;
    private ChannelItemOpertor opertor;
    private boolean isShow;


    public boolean isShow() {
        return isShow;
    }


    public void setShow(boolean show) {
        isShow = show;
    }


    public void setOpertor(ChannelItemOpertor opertor) {
        this.opertor = opertor;
    }


    public MyChannelAdapter(Context context) {
        this.context = context;
    }


    public MyChannelAdapter(Context context, List<ChannelBean> beanList) {
        this.context = context;
        this.beanList = beanList;
    }


    public MyChannelAdapter(Context context, List<ChannelBean> beanList, ChannelItemOpertor opertor) {
        this.context = context;
        this.beanList = beanList;
        this.opertor = opertor;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.channel_item, parent, false));
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        // 绑定数据
        holder.tvChannel.setText(beanList.get(position).getChannel());
        holder.ivOperator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChannelBean channelBean = beanList.remove(position);
                notifyDataSetChanged();
                if (opertor != null) {
                    opertor.onItemClick(channelBean);
                }
            }
        });
        if (isShow) {
            holder.ivOperator.setVisibility(View.VISIBLE);
        } else {
            holder.ivOperator.setVisibility(View.GONE);
        }
    }


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


    @Override
    public void move(int currentPostion, int targetPostion) {
        Collections.swap(beanList, currentPostion, targetPostion);
        notifyItemMoved(currentPostion, targetPostion);
    }


    class ViewHolder extends RecyclerView.ViewHolder {


        private TextView tvChannel;
        private ImageView ivOperator;


        public ViewHolder(View itemView) {
            super(itemView);
            tvChannel = itemView.findViewById(R.id.tv_channel);
            ivOperator = itemView.findViewById(R.id.iv_operator);
        }
    }

}


//Other适配器

package com.bawei.channelmanager.adapter;


import android.content.Context;
import android.support.annotation.NonNull;
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 com.bawei.channelmanager.R;
import com.bawei.channelmanager.bean.ChannelBean;
import com.bawei.channelmanager.event.ChannelItemOpertor;


import java.util.List;


public class MoreChannelAdapter extends RecyclerView.Adapter<MoreChannelAdapter.MoreViewHodler> {


    private Context context;
    private List<ChannelBean> channelBeans;
    private ChannelItemOpertor opertor;


    public void setOpertor(ChannelItemOpertor opertor) {
        this.opertor = opertor;
    }


    public MoreChannelAdapter(Context context, List<ChannelBean> channelBeans) {
        this.context = context;
        this.channelBeans = channelBeans;
    }


    public MoreChannelAdapter(Context context, List<ChannelBean> channelBeans, ChannelItemOpertor opertor) {
        this.context = context;
        this.channelBeans = channelBeans;
        this.opertor = opertor;
    }


    @NonNull
    @Override
    public MoreViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MoreViewHodler(LayoutInflater.from(context).inflate(R.layout.channel_item, parent, false));
    }


    @Override
    public void onBindViewHolder(@NonNull MoreViewHodler holder, final int position) {
        holder.itemView.setBackgroundResource(R.drawable.rl_channel_popurl);
        holder.tvChannel.setText(channelBeans.get(position).getChannel());
        holder.ivOperator.setImageResource(R.drawable.ic_add_black_24dp);


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChannelBean channelBean = channelBeans.remove(position);
                notifyDataSetChanged();
                if (opertor != null) {
                    opertor.onItemClick(channelBean);
                }
            }
        });
    }


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


    class MoreViewHodler extends RecyclerView.ViewHolder {


        private TextView tvChannel;
        private ImageView ivOperator;


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


            tvChannel = itemView.findViewById(R.id.tv_channel);
            ivOperator = itemView.findViewById(R.id.iv_operator);
        }
    }

}


//数据库存取数据

package com.bawei.channelmanager.db;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;


public class DBOpenHelper extends SQLiteOpenHelper {


    public static final String DB_NAME = "eplus";
    public static final int DB_VERSION = 1;


    public DBOpenHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table channel(_id Integer primary key autoincrement, channel varchar(50), sort Integer)");
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }

}

//数据库帮助类

package com.bawei.channelmanager.bean;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import com.bawei.channelmanager.db.DBOpenHelper;


import java.util.ArrayList;
import java.util.List;


public class ChannelDao {


    private Context context;
    private DBOpenHelper helper;
    private SQLiteDatabase database;


    public ChannelDao(Context context) {
        helper = new DBOpenHelper(context);
        database = helper.getWritableDatabase();
    }


    /**
     * 插入操作
     *
     * @param bean
     */
    public void insert(ChannelBean bean) {
        database.execSQL("insert into channel(channel, sort) values(?, ?)", new String[]{bean.getChannel(), String.valueOf(bean.getSort())});
    }


    public void update(ChannelBean bean) {
        database.execSQL("update channel set channel = ?, sort = ? where _id = ?", new String[]{bean.getChannel(), String.valueOf(bean.getSort()), String.valueOf(bean.get_id())});
    }


    public List<ChannelBean> query() {
        List<ChannelBean> beans = new ArrayList<>();
        ChannelBean bean1;
        Cursor cursor = database.rawQuery("select * from channel", null);
        while (cursor.moveToNext()) {
            // String isMore = cursor.getString(cursor.getColumnIndex("isMore"));
            bean1 = new ChannelBean(
                    cursor.getString(cursor.getColumnIndex("channel")),
                    cursor.getInt(cursor.getColumnIndex("sort")),
                    true
            );
            beans.add(bean1);
        }


        return beans;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值