ChannelActivity
package com.example.demo_1121;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import com.google.gson.Gson;
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);
initView();
initData();
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();
}
});
gvMyChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 获取到当前点击的条目
Channel channel = myChannels.get(position);
// 添加到我的频道中
recommondChannels.add(channel);
recommondChannelAdapter.notifyDataSetChanged();
// 移除下面的条目
myChannels.remove(position);
myChannelAdapter.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;*/
}
}
}
Channel
package com.example.demo_1121;
public class Channel {
private int id;
private String name;
public Channel(String id, String name) {
}
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;
}
}
DBHelper
package com.example.demo_1121;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(@Nullable Context context) {
super(context, "Channe.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table channel(id integer primary key autoincrement," +
"name text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MyChannelAdapter
package com.example.demo_1121;
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 java.util.List;
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);
holder.imgDelete.setVisibility(View.GONE);
} else {
holder.imgDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG,"onClick:点击了删除");
listener.onDeleteClick(position);
}
});
}
return convertView;
/*} else {
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;
}
}
MyGridView
package com.example.demo_1121;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
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);
}
}
RecommondChannelAdapter
package com.example.demo_1121;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
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;
}
}
activity_channel
<?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="添加" />-->
<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.example.demo_1121.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.example.demo_1121.MyGridView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="频道推荐" />
<com.example.demo_1121.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.example.demo_1121.MyGridView>
</LinearLayout>
</ScrollView>
</LinearLayout>
item_my_channel
<?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:gravity="center" />
<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:visibility="gone" />
</RelativeLayout>
item_recommond_channel
<?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:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/txt_recommond_channel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo_1121">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ChannelActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>