activity_main:
<?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>
Acticity_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">
<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.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.MyGridView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="频道推荐" />
<com.bwie.channelmanager.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.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:background="@drawable/bg_gray"
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:src="@drawable/img_delete"
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:background="@drawable/bg_white"
android:gravity="center"
android:orientation="horizontal">
<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>
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) {
}
}
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;
}
}
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);
}
}
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;
}
}
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;
}
}
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());
}
}
}
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();
}
});
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;
}
}
}