4.频道管理实现

简单实现频道管理,频道团里如图:在这里插入图片描述
实现步骤:
1.MainActivity,执行跳转界面布局:

	<?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">
	
	    <Button
	        android:id="@+id/btn_add"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:text="添加" />
	
	</LinearLayout>

2.MainActivity,执行跳转界面逻辑:

	package com.bwie.channelmanager;
	
	import android.content.Intent;
	import android.support.v7.app.AppCompatActivity;
	import android.os.Bundle;
	import android.util.Log;
	import android.view.View;
	import android.widget.Button;
	
	import com.bwie.channelmanager.activity.ChannelActivity;
	import com.bwie.channelmanager.bean.Channel;
	import com.bwie.channelmanager.dao.ChannelDao;
	import com.google.gson.Gson;
	import com.google.gson.reflect.TypeToken;
	
	import java.lang.reflect.Type;
	import java.util.List;
	
	public class MainActivity extends AppCompatActivity {
	    private static final String TAG = "MainActivity";
	    private Button btnAdd;
	
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	        btnAdd = findViewById(R.id.btn_add);
	
	        btnAdd.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	                Intent intent = new Intent(MainActivity.this, ChannelActivity.class);
	                startActivityForResult(intent, 100);
	            }
	        });
	
	    }
	
	    @Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	        super.onActivityResult(requestCode, resultCode, data);
	
	        // 1. 获取我的频道的第一种方式,频道页面回传过一个Json串
	        if (requestCode == 100 && resultCode == RESULT_OK){
	            String channels = data.getStringExtra("channels");
	            // List<Channel>
	            Gson gson = new Gson();
	            Type type = new TypeToken<List<Channel>>(){}.getType();
	            List<Channel> channelList = gson.fromJson(channels, type);
	            for (Channel channel : channelList) {
	                Log.i(TAG, "我的频道-----"+channel.getId()+"---"+channel.getName());
	            }
	        }
	    }
	
	    /**
	     * 调用数据库
	     *
	     * */
	    @Override
	    protected void onResume() {
	        super.onResume();
	        // 2.获取我的频道的第二种方式,从数据库中读出保存的数据
	        ChannelDao dao = new ChannelDao(this);
	        List<Channel> channels = dao.queryAll();
	        for (Channel channel : channels) {
	            Log.i(TAG, "我的频道-----"+channel.getId()+"---"+channel.getName());
	        }
	    }
	}

3.自定义一个频道管理容器MyGridView:

	package com.bwie.channelmanager.activity;
	import android.content.Context;
	import android.util.AttributeSet;
	import android.widget.GridView;
	
	/**
	 * Created by eric on 2018/9/18.
	 *
	 * 自定义一个频道管理容器
	 */
	
	public class MyGridView extends GridView {
	    public MyGridView(Context context) {
	        super(context);
	    }
	
	    public MyGridView(Context context, AttributeSet attrs) {
	        super(context, attrs);
	    }
	
	    public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
	        super(context, attrs, defStyleAttr);
	    }
	
	    @Override
	    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
	        super.onMeasure(widthMeasureSpec, expandSpec);
	    }
	}

4.进行频道管理页布局:

	<?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">
	
	    <ScrollView
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:scrollbars="none">
	
	        <LinearLayout
	            android:layout_width="match_parent"
	            android:layout_height="wrap_content"
	            android:orientation="vertical">
	
	            <RelativeLayout
	                android:layout_width="match_parent"
	                android:layout_height="wrap_content">
	
	                <Button
	                    android:id="@+id/btn_finish"
	                    android:layout_width="wrap_content"
	                    android:layout_height="wrap_content"
	                    android:layout_alignParentRight="true"
	                    android:text="完成" />
	            </RelativeLayout>
	
	            <RelativeLayout
	                android:layout_width="match_parent"
	                android:layout_height="wrap_content"
	                android:padding="10dp">
	
	                <TextView
	                    android:layout_width="wrap_content"
	                    android:layout_height="wrap_content"
	                    android:text="我的频道" />
	
	                <TextView
	                    android:id="@+id/txt_edit"
	                    android:layout_width="wrap_content"
	                    android:layout_height="wrap_content"
	                    android:layout_alignParentRight="true"
	                    android:text="编辑" />
	
	            </RelativeLayout>
	
	            <com.bwie.channelmanager.activity.MyGridView
	                android:id="@+id/gv_my_channel"
	                android:layout_width="match_parent"
	                android:layout_height="wrap_content"
	                android:layout_marginLeft="10dp"
	                android:layout_marginRight="10dp"
	                android:horizontalSpacing="10dp"
	                android:numColumns="4"
	                android:verticalSpacing="10dp"></com.bwie.channelmanager.activity.MyGridView>
	
	            <TextView
	                android:layout_width="match_parent"
	                android:layout_height="wrap_content"
	                android:padding="10dp"
	                android:text="频道推荐" />
	
	            <com.bwie.channelmanager.activity.MyGridView
	                android:id="@+id/gv_recommond_channel"
	                android:layout_width="match_parent"
	                android:layout_height="wrap_content"
	                android:layout_marginLeft="10dp"
	                android:layout_marginRight="10dp"
	                android:horizontalSpacing="10dp"
	                android:numColumns="4"
	                android:verticalSpacing="10dp"></com.bwie.channelmanager.activity.MyGridView>
	        </LinearLayout>
	    </ScrollView>
	</LinearLayout>

5.Bean类Channel对象:

	package com.bwie.channelmanager.bean;
	
	/**
	 * Created by eric on 2018/9/18.
	 * 
	 * 数据封装类
	 */
	
	public class Channel {
	    private int id;
	    private String name;
	
	    public Channel() {
	    }
	
	    public Channel(int id, String name) {
	        this.id = id;
	        this.name = name;
	    }
	
	    public int getId() {
	        return id;
	    }
	
	    public void setId(int id) {
	        this.id = id;
	    }
	
	    public String getName() {
	        return name;
	    }
	
	    public void setName(String name) {
	        this.name = name;
	    }
	}

6.数据库之db:

	package com.bwie.channelmanager.db;
	import android.content.Context;
	import android.database.sqlite.SQLiteDatabase;
	import android.database.sqlite.SQLiteOpenHelper;
	/**
	 * Created by eric on 2018/9/18.
	 * 
	 * 数据库创建
	 */
	
	public class DBHelper extends SQLiteOpenHelper {
	
	    public DBHelper(Context context) {
	        super(context, "channel.db", null, 1);
	    }
	
	    @Override
	    public void onCreate(SQLiteDatabase db) {
	        String sql = "create table channel(id integer primary key, name text)";
	        db.execSQL(sql);
	    }
	
	    @Override
	    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	
	    }
	}

7.数据库之dao:

	package com.bwie.channelmanager.dao;
	
	import android.content.ContentValues;
	import android.content.Context;
	import android.database.Cursor;
	import android.database.sqlite.SQLiteDatabase;
	
	import com.bwie.channelmanager.bean.Channel;
	import com.bwie.channelmanager.db.DBHelper;
	
	import java.util.ArrayList;
	import java.util.List;
	
	/**
	 * Created by eric on 2018/9/18.
	 * 
	 * 数据库表
	 */
	
	public class ChannelDao {
	
	    private final SQLiteDatabase db;
	
	    public ChannelDao(Context context) {
	        DBHelper helper = new DBHelper(context);
	        db = helper.getWritableDatabase();
	    }
	
	    public int insert(List<Channel> list) {
	        // 插入集合之前先把之前的数据清空
	        clear();
	        int length = 0;
	        ContentValues values = new ContentValues();
	        for (Channel channel : list) {
	            values.put("id", channel.getId());
	            values.put("name", channel.getName());
	            long l = db.insert("channel", null, values);
	            if (l > 0) {
	                length++;
	            }
	        }
	        return length;
	    }
	
	    public List<Channel> queryAll() {
	        List<Channel> list = new ArrayList<>();
	        Cursor cursor = db.query("channel", null, null, null, null, null, null);
	        while (cursor.moveToNext()) {
	            Channel channel = new Channel();
	            channel.setId(cursor.getInt(cursor.getColumnIndex("id")));
	            channel.setName(cursor.getString(cursor.getColumnIndex("name")));
	            list.add(channel);
	        }
	        return list;
	    }
	
	    public int clear() {
	        int i = db.delete("channel", "", new String[]{});
	        return i;
	    }
	}

8.适配器布局:

  8.1>.我的频道适配器布局          
		<?xml version="1.0" encoding="utf-8"?>
		<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
		    android:layout_width="match_parent"
		    android:layout_height="wrap_content">
		
		    <TextView
		        android:id="@+id/txt_my_channel"
		        android:layout_width="match_parent"
		        android:layout_height="wrap_content"
		        android:layout_centerInParent="true"
		        android:layout_margin="10dp"
		        android:background="@drawable/bg_gray"
		        android:gravity="center" />
		
		    //img_delete ×号 可以找资源替换
		    <ImageView
		        android:id="@+id/btn_delete"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:layout_alignParentRight="true"
		        android:layout_alignParentTop="true"
		        android:src="@drawable/img_delete"
		        android:visibility="gone" />
		
		</RelativeLayout>
		
  8.2>.推荐频道适配器布局
		<?xml version="1.0" encoding="utf-8"?>
		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		    android:id="@+id/ll_add"
		    android:layout_width="match_parent"
		    android:layout_height="wrap_content"
		    android:background="@drawable/bg_white"
		    android:gravity="center"
		    android:orientation="horizontal">
		
		    //@drawable/btn_add 加号 可找资源替换
		    <ImageView
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:src="@drawable/btn_add" />
		
		    <TextView
		        android:id="@+id/txt_recommond_channel"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content" />
		
		</LinearLayout>

9.频道适配器逻辑:

 8.1>.我的频道适配器逻辑 
	    package com.bwie.channelmanager.adapter;
		import android.content.Context;
		import android.util.Log;
		import android.view.View;
		import android.view.ViewGroup;
		import android.widget.BaseAdapter;
		import android.widget.ImageView;
		import android.widget.TextView;
		
		import com.bwie.channelmanager.bean.Channel;
		import com.bwie.channelmanager.R;
		
		import java.util.List;
		
		/**
		 * Created by eric on 2018/9/18.
		 *
		 * 我的频道适配器
		 *
		 */
		
		public class MyChannelAdapter extends BaseAdapter {
		    private static final String TAG = "MyChannelAdapter";
		    // 1. 提供一个接口
		    public interface OnDeleteItemClickListener {
		        void onDeleteClick(int position);
		    }
		
		    // 2. 提供一个接口的实例
		    private OnDeleteItemClickListener listener;
		
		    // 3. 提供一个初始化实例的方法
		    public void setOnDeleteItemClickListener(OnDeleteItemClickListener listener) {
		        this.listener = listener;
		    }
		
		    private Context context;
		    private List<Channel> list;
		
		    private boolean isEdited = false;
		
		    public MyChannelAdapter(Context context, List<Channel> list) {
		        this.context = context;
		        this.list = list;
		    }
		
		    public void setEdited(boolean edited) {
		        isEdited = edited;
		        notifyDataSetChanged();
		    }
		
		    @Override
		    public int getCount() {
		        return list.size();
		    }
		
		    @Override
		    public Object getItem(int position) {
		        return list.get(position);
		    }
		
		    @Override
		    public long getItemId(int position) {
		        return position;
		    }
		
		    @Override
		    public View getView(final int position, View convertView, ViewGroup parent) {
		        ViewHolder holder = null;
		        if (convertView == null) {
		            holder = new ViewHolder();
		            convertView = View.inflate(context, R.layout.item_my_channel, null);
		            holder.txtName = convertView.findViewById(R.id.txt_my_channel);
		            holder.imgDelete = convertView.findViewById(R.id.btn_delete);
		            convertView.setTag(holder);
		        } else {
		            holder = (ViewHolder) convertView.getTag();
		        }
		        holder.txtName.setText(list.get(position).getName());
		        if (isEdited) {
		            holder.imgDelete.setVisibility(View.VISIBLE);
		        } else {
		            holder.imgDelete.setVisibility(View.GONE);
		        }
		        holder.imgDelete.setOnClickListener(new View.OnClickListener() {
		            @Override
		            public void onClick(View v) {
		                Log.i(TAG, "onClick: 点击了删除");
		                // 6.合适的时机触发接口回调
		                listener.onDeleteClick(position);
		            }
		        });
		        return convertView;
		    }
		
		    class ViewHolder {
		        TextView txtName;
		        ImageView imgDelete;
		    }
		}

 8.2>.推荐频道适配器逻辑
	   package com.bwie.channelmanager.adapter;
		import android.content.Context;
		import android.view.View;
		import android.view.ViewGroup;
		import android.widget.BaseAdapter;
		import android.widget.TextView;
		
		import com.bwie.channelmanager.bean.Channel;
		import com.bwie.channelmanager.R;
		
		import java.util.List;
		
		/**
		 * Created by eric on 2018/9/18.
		 *
		 * 推荐频道适配器
		 *
		 */
		
		public class RecommondChannelAdapter extends BaseAdapter {
		    private Context context;
		    private List<Channel> list;
		
		    public RecommondChannelAdapter(Context context, List<Channel> list) {
		        this.context = context;
		        this.list = list;
		    }
		
		    @Override
		    public int getCount() {
		        return list.size();
		    }
		
		    @Override
		    public Object getItem(int position) {
		        return list.get(position);
		    }
		
		    @Override
		    public long getItemId(int position) {
		        return position;
		    }
		
		    @Override
		    public View getView(int position, View convertView, ViewGroup parent) {
		        ViewHolder holder = null;
		        if (convertView == null) {
		            holder = new ViewHolder();
		            convertView = View.inflate(context, R.layout.item_recommond_channel, null);
		            holder.txtName = convertView.findViewById(R.id.txt_recommond_channel);
		            convertView.setTag(holder);
		        } else {
		            holder = (ViewHolder) convertView.getTag();
		        }
		        holder.txtName.setText(list.get(position).getName());
		        return convertView;
		    }
		
		    class ViewHolder {
		        TextView txtName;
		    }
		}

10.频道管理页逻辑:

		package com.bwie.channelmanager.activity;
		import android.support.v7.app.AppCompatActivity;
		import android.os.Bundle;
		import android.view.View;
		import android.widget.AdapterView;
		import android.widget.Button;
		import android.widget.TextView;
		
		import com.bwie.channelmanager.R;
		import com.bwie.channelmanager.adapter.MyChannelAdapter;
		import com.bwie.channelmanager.adapter.RecommondChannelAdapter;
		import com.bwie.channelmanager.bean.Channel;
		import com.bwie.channelmanager.dao.ChannelDao;
		
		import java.util.ArrayList;
		import java.util.List;
		
		public class ChannelActivity extends AppCompatActivity implements View.OnClickListener {
		    private TextView txtEdit;
		    private MyGridView gvMyChannel;
		    private MyGridView gvRecommondChannel;
		    private Button btnFinish;
		
		    // 我的频道
		    private List<Channel> myChannels;
		    // 推荐频道
		    private List<Channel> recommondChannels;
		
		    private MyChannelAdapter myChannelAdapter;
		    private RecommondChannelAdapter recommondChannelAdapter;
		
		    // 当前是否是编辑状态
		    private boolean isEdited = false;
		
		
		    @Override
		    protected void onCreate(Bundle savedInstanceState) {
		        super.onCreate(savedInstanceState);
		        setContentView(R.layout.activity_channel);
		        //1 初始化控件
		        initView();
		        //2 初始化数据
		        initData();
		        //3 点击事件
		        setListener();
		    }
		
		    private void setListener() {
		        txtEdit.setOnClickListener(this);
		        // 推荐列表的点击事件
		        gvRecommondChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		            @Override
		            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		                // 获取到当前点击的条目
		                Channel channel = recommondChannels.get(position);
		                // 添加到我的频道中
		                myChannels.add(channel);
		                myChannelAdapter.notifyDataSetChanged();
		                // 移除下面的条目
		                recommondChannels.remove(position);
		                recommondChannelAdapter.notifyDataSetChanged();
		            }
		        });
		
		        btnFinish.setOnClickListener(this);
		    }
		
		    private void initView() {
		        txtEdit = findViewById(R.id.txt_edit);
		        gvMyChannel = findViewById(R.id.gv_my_channel);
		        gvRecommondChannel = findViewById(R.id.gv_recommond_channel);
		        btnFinish = findViewById(R.id.btn_finish);
		    }
		
		    private void initData() {
		        myChannels = new ArrayList<>();
		        recommondChannels = new ArrayList<>();
		
		        addData();
		
		        myChannelAdapter = new MyChannelAdapter(this, myChannels);
		        // 4. 初始化接口实例方法
		        MyChannelAdapter.OnDeleteItemClickListener listener = new MyChannelAdapter.OnDeleteItemClickListener() {
		            @Override
		            public void onDeleteClick(int position) {
		                // 一点击删除按钮就会回调到这个方法并且传回点击的删除按钮的position
		                // 获取点击的条目
		                Channel channel = myChannels.get(position);
		                // 添加到推荐的列表中
		                recommondChannels.add(channel);
		                recommondChannelAdapter.notifyDataSetChanged();
		                // 我的频道中删除这个条目
		                myChannels.remove(position);
		                myChannelAdapter.notifyDataSetChanged();
		            }
		        };
		        // 5. 调用初始化接口实例的方法
		        myChannelAdapter.setOnDeleteItemClickListener(listener);
		
		
		        gvMyChannel.setAdapter(myChannelAdapter);
		
		        recommondChannelAdapter = new RecommondChannelAdapter(this, recommondChannels);
		        gvRecommondChannel.setAdapter(recommondChannelAdapter);
		
		    }
		
		    private void addData() {
		        Channel channel1 = new Channel(1, "关注");
		        Channel channel2 = new Channel(2, "推荐");
		        Channel channel3 = new Channel(3, "热点");
		        Channel channel4 = new Channel(4, "科技");
		        Channel channel5 = new Channel(5, "视频");
		        Channel channel6 = new Channel(6, "数码");
		        Channel channel7 = new Channel(7, "汽车");
		        Channel channel8 = new Channel(8, "问答");
		        Channel channel9 = new Channel(9, "图片");
		        Channel channel10 = new Channel(10, "段子");
		        Channel channel11 = new Channel(11, "电影");
		        Channel channel12 = new Channel(12, "美文");
		        Channel channel13 = new Channel(13, "社会");
		        Channel channel14 = new Channel(14, "娱乐");
		        Channel channel15 = new Channel(15, "军事");
		        Channel channel16 = new Channel(16, "国际");
		        Channel channel17 = new Channel(17, "体育");
		        Channel channel18 = new Channel(18, "财经");
		        Channel channel19 = new Channel(19, "特卖");
		        Channel channel20 = new Channel(20, "直播");
		        Channel channel21 = new Channel(21, "房产");
		        Channel channel22 = new Channel(22, "时尚");
		        Channel channel23 = new Channel(23, "历史");
		        Channel channel24 = new Channel(24, "旅游");
		
		        myChannels.add(channel1);
		        myChannels.add(channel2);
		        myChannels.add(channel3);
		        myChannels.add(channel4);
		        myChannels.add(channel5);
		        myChannels.add(channel6);
		        myChannels.add(channel7);
		        myChannels.add(channel8);
		        myChannels.add(channel9);
		        myChannels.add(channel10);
		        myChannels.add(channel11);
		        myChannels.add(channel12);
		        myChannels.add(channel13);
		        myChannels.add(channel14);
		        myChannels.add(channel15);
		        myChannels.add(channel16);
		        myChannels.add(channel17);
		        myChannels.add(channel18);
		        recommondChannels.add(channel19);
		        recommondChannels.add(channel20);
		        recommondChannels.add(channel21);
		        recommondChannels.add(channel22);
		        recommondChannels.add(channel23);
		        recommondChannels.add(channel24);
		    }
		
		    @Override
		    public void onClick(View v) {
		        switch (v.getId()) {
		            case R.id.txt_edit:
		                isEdited = !isEdited;
		                if (isEdited) {
		                    txtEdit.setText("完成");
		                } else {
		                    txtEdit.setText("编辑");
		                }
		
		                myChannelAdapter.setEdited(isEdited);
		                break;
		            case R.id.btn_finish:
		//                1.第一种方式,使用Json传值
		//                Gson gson = new Gson();
		//                String json = gson.toJson(myChannels);
		//                Intent intent = new Intent();
		//                intent.putExtra("channels", json);
		//                setResult(RESULT_OK, intent);
		//                finish();
		
		                // 2. 第二种方式,点击完成时把集合添加到数据库中
		                ChannelDao dao = new ChannelDao(this);
		                dao.insert(myChannels);
		                finish();
		                break;
		        }
		    }
		}

//资源文件:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值