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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/btn_add_channel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:src="@drawable/btn_add" />
<android.support.design.widget.TabLayout
android:id="@+id/tbl_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btn_add_channel"></android.support.design.widget.TabLayout>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_news"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#8c5d5d"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="排序" />
<TextView
android:id="@+id/txt_finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="编辑" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="我的频道" />
<com.bwie.newsmonth06.view.MyGridView
android:id="@+id/mgv_my_channel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:verticalSpacing="5dp">
</com.bwie.newsmonth06.view.MyGridView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="点击添加栏目" />
<com.bwie.newsmonth06.view.MyGridView
android:id="@+id/mgv_recommond_channel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:verticalSpacing="5dp">
</com.bwie.newsmonth06.view.MyGridView>
</LinearLayout>
</ScrollView>
</LinearLayout>
fragment_news:
<?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">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/plv_news"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView>
</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_channel_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/bg_item_channel"
android:gravity="center" />
<ImageView
android:id="@+id/btn_delete_channel"
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" />
item_recommond_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_channel_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/bg_item_channel"
android:gravity="center" />
</RelativeLayout>
public class DBHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "channel.db";
public static final int DB_VERSION = 1;
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + ChannelDao.TABLE_NAME + "(" + ChannelDao.COLUMN_ID + " integer primary key," +
ChannelDao.COLUMN_NAME + " text," +
ChannelDao.COLUMN_PARAM + " text," +
ChannelDao.COLUMN_TYPE + " integer)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class ChannelDao {
private final SQLiteDatabase db;
public static final String TABLE_NAME = "channel";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_PARAM = "param";
public static final String COLUMN_TYPE = "type";
public ChannelDao(Context context) {
DBHelper helper = new DBHelper(context);
db = helper.getWritableDatabase();
}
/**
* 插入一条语句
*
* @param channel
* @return
*/
public long insert(Channel channel) {
if (hasId(channel.getId())) {
return 0;
}
ContentValues values = new ContentValues();
values.put(COLUMN_ID, channel.getId());
values.put(COLUMN_NAME, channel.getName());
values.put(COLUMN_PARAM, channel.getParma());
values.put(COLUMN_TYPE, channel.getType());
return db.insert(TABLE_NAME, null, values);
}
/**
* 插入一个集合的数据
*
* @param channels
* @return
*/
public int insert(List<Channel> channels) {
int count = 0;
ContentValues values = new ContentValues();
for (Channel channel : channels) {
if (!hasId(channel.getId())) {
values.put(COLUMN_ID, channel.getId());
values.put(COLUMN_NAME, channel.getName());
values.put(COLUMN_PARAM, channel.getParma());
values.put(COLUMN_TYPE, channel.getType());
long l = db.insert(TABLE_NAME, null, values);
if (l > 0) {
count++;
}
}
}
return count;
}
/**
* 更新一条数据
*
* @param channel
* @return
*/
public int update(Channel channel) {
ContentValues values = new ContentValues();
values.put(COLUMN_ID, channel.getId());
values.put(COLUMN_NAME, channel.getName());
values.put(COLUMN_PARAM, channel.getParma());
values.put(COLUMN_TYPE, channel.getType());
return db.update(TABLE_NAME, values, COLUMN_ID + "=?", new String[]{channel.getId() + ""});
}
/**
* 判断数据库中是否有这条数据
*
* @param id
* @return
*/
public boolean hasId(int id) {
Cursor cursor = db.query(TABLE_NAME, null, COLUMN_ID + "=?", new String[]{id + ""}, null, null, null);
if (cursor.moveToNext()) {
return true;
}
return false;
}
/**
* 查询所有的频道数据
*
* @return
*/
public List<Channel> queryAll() {
List<Channel> list = new ArrayList<>();
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
while (cursor.moveToNext()) {
Channel channel = new Channel();
channel.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
channel.setName(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
channel.setParma(cursor.getString(cursor.getColumnIndex(COLUMN_PARAM)));
channel.setType(cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE)));
list.add(channel);
}
return list;
}
}
public class ServerUrl {
public static final String NEWS_URL = "http://www.xieast.com/api/news/news.php?";
}
public class HttpUtils {
public static String getStringFromHttp(String urlString) {
String result = "";
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setConnectTimeout(5000);
connection.connect();
if (connection.getResponseCode() == 200) {
InputStream is = connection.getInputStream();
result = getStringFromInputStream(is);
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private static String getStringFromInputStream(InputStream is) {
String result = "";
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int length = -1;
byte[] buffer = new byte[1024];
while ((length = is.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, length);
baos.flush();
}
result = baos.toString();
baos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
previewAction();
setContentView(getContentView());
initView();
initData();
setListener();
setMoreAction();
}
protected void previewAction() {
}
protected abstract int getContentView();
protected abstract void initView();
protected void initData() {
}
protected void setListener() {
}
protected void setMoreAction() {
}
}
public abstract class BaseFragment extends Fragment {
protected Context mContext;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(getContentView(), container, false);
initView(v);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (mContext == null) {
mContext = getActivity();
}
initData();
setListener();
setMoreAction();
}
protected abstract int getContentView();
protected abstract void initView(View v);
protected void initData() {
}
protected void setListener() {
}
protected void setMoreAction() {
}
}
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 height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, height);
}
}
public class MyChannelAdapter extends BaseAdapter {
private Context context;
private List<Channel> list;
private boolean isEditable;
public interface OnItemDeleteClickListener {
void onDeleteClick(int position);
}
private OnItemDeleteClickListener listener;
public void setOnItemDeleteClickListener(OnItemDeleteClickListener listener) {
this.listener = listener;
}
public MyChannelAdapter(Context context, List<Channel> list) {
this.context = context;
this.list = list;
}
public void setEditable(boolean editable) {
isEditable = editable;
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_channel_name);
holder.imgDelete = convertView.findViewById(R.id.btn_delete_channel);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(list.get(position).getName());
if (isEditable) {
holder.imgDelete.setVisibility(View.VISIBLE);
} else {
holder.imgDelete.setVisibility(View.GONE);
}
holder.imgDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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_channel_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(list.get(position).getName());
return convertView;
}
class ViewHolder {
TextView txtName;
}
}
public class NewsAdapter extends BaseAdapter {
private Context context;
private List<News.DataBean> list;
private static final int TYPE_ONE_PIC = 0;
private static final int TYPE_TWO_PIC = 1;
private static final int TYPE_THREE_PIC = 2;
public NewsAdapter(Context context, List<News.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType(int position) {
News.DataBean bean = list.get(position);
if (!TextUtils.isEmpty(bean.getThumbnail03())) {
return TYPE_THREE_PIC;
} else if (!TextUtils.isEmpty(bean.getThumbnail02())) {
return TYPE_TWO_PIC;
} else {
return TYPE_ONE_PIC;
}
}
@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) {
ViewHolder1 holder1 = null;
ViewHolder2 holder2 = null;
ViewHolder3 holder3 = null;
int type = getItemViewType(position);
switch (type) {
case TYPE_ONE_PIC:
if (convertView == null) {
holder1 = new ViewHolder1();
convertView = View.inflate(context, R.layout.item_news_01, null);
holder1.txtTitle = convertView.findViewById(R.id.txt_title);
holder1.imgLogo1 = convertView.findViewById(R.id.img_logo);
convertView.setTag(holder1);
} else {
holder1 = (ViewHolder1) convertView.getTag();
}
holder1.txtTitle.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail01(),
holder1.imgLogo1);
break;
case TYPE_TWO_PIC:
if (convertView == null) {
holder2 = new ViewHolder2();
convertView = View.inflate(context, R.layout.item_news_02, null);
holder2.txtTitle = convertView.findViewById(R.id.txt_title);
holder2.imgLogo1 = convertView.findViewById(R.id.img_logo1);
holder2.imgLogo2 = convertView.findViewById(R.id.img_logo2);
convertView.setTag(holder2);
} else {
holder2 = (ViewHolder2) convertView.getTag();
}
holder2.txtTitle.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail01(),
holder2.imgLogo1);
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail02(),
holder2.imgLogo2);
break;
case TYPE_THREE_PIC:
if (convertView == null) {
holder3 = new ViewHolder3();
convertView = View.inflate(context, R.layout.item_news_03, null);
holder3.txtTitle = convertView.findViewById(R.id.txt_title);
holder3.imgLogo1 = convertView.findViewById(R.id.img_logo1);
holder3.imgLogo2 = convertView.findViewById(R.id.img_logo2);
holder3.imgLogo3 = convertView.findViewById(R.id.img_logo3);
convertView.setTag(holder3);
} else {
holder3 = (ViewHolder3) convertView.getTag();
}
holder3.txtTitle.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail01(),
holder3.imgLogo1);
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail02(),
holder3.imgLogo2);
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail03(),
holder3.imgLogo3);
break;
}
return convertView;
}
class ViewHolder1 {
ImageView imgLogo1;
TextView txtTitle;
}
class ViewHolder2 {
ImageView imgLogo1;
ImageView imgLogo2;
TextView txtTitle;
}
class ViewHolder3 {
ImageView imgLogo1;
ImageView imgLogo2;
ImageView imgLogo3;
TextView txtTitle;
}
}
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
File cacheFile = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File rootSD = Environment.getExternalStorageDirectory();
cacheFile = new File(rootSD, "images");
if (!cacheFile.exists()) {
cacheFile.mkdirs();
}
}
DiskCache diskCache = null;
try {
diskCache = new LruDiskCache(cacheFile, new Md5FileNameGenerator(), 50 * 1024 * 1024);
} catch (IOException e) {
e.printStackTrace();
}
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
.diskCache(diskCache)
.memoryCache(new LruMemoryCache(12 * 1024 * 1024))
.defaultDisplayImageOptions(options)
.build();
ImageLoader.getInstance().init(configuration);
initData();
}
private void initData() {
List<Channel> channelList = new ArrayList<>();
Channel channel1 = new Channel(1, "头条", "top", 1);
Channel channel2 = new Channel(2, "社会", "shehui", 1);
Channel channel3 = new Channel(3, "国内", "guonei", 1);
Channel channel4 = new Channel(4, "国际", "guoji", 1);
Channel channel5 = new Channel(5, "娱乐", "yule", 1);
Channel channel6 = new Channel(6, "体育", "tiyu", 1);
Channel channel7 = new Channel(7, "军事", "junshi", 1);
Channel channel8 = new Channel(8, "科技", "keji", 1);
Channel channel9 = new Channel(9, "财经", "caijing", 1);
Channel channel10 = new Channel(10, "时尚", "shishang", 1);
Channel channel11 = new Channel(11, "热评", "", 1);
Channel channel12 = new Channel(12, "家居", "", 1);
Channel channel13 = new Channel(13, "视频", "", 1);
Channel channel14 = new Channel(14, "美食", "", 1);
Channel channel15 = new Channel(15, "教育", "", 1);
Channel channel16 = new Channel(16, "旅游", "", 2);
Channel channel17 = new Channel(17, "健康", "", 2);
Channel channel18 = new Channel(18, "房产", "", 2);
Channel channel19 = new Channel(19, "汽车", "", 2);
Channel channel20 = new Channel(20, "亲子", "", 2);
Channel channel21 = new Channel(21, "购物", "", 2);
Channel channel22 = new Channel(22, "段子", "", 2);
Channel channel23 = new Channel(23, "美文", "", 2);
Channel channel24 = new Channel(24, "外卖", "", 2);
channelList.add(channel1);
channelList.add(channel2);
channelList.add(channel3);
channelList.add(channel4);
channelList.add(channel5);
channelList.add(channel6);
channelList.add(channel7);
channelList.add(channel8);
channelList.add(channel9);
channelList.add(channel10);
channelList.add(channel11);
channelList.add(channel12);
channelList.add(channel13);
channelList.add(channel14);
channelList.add(channel15);
channelList.add(channel16);
channelList.add(channel17);
channelList.add(channel18);
channelList.add(channel19);
channelList.add(channel20);
channelList.add(channel21);
channelList.add(channel22);
channelList.add(channel23);
channelList.add(channel24);
ChannelDao dao = new ChannelDao(this);
dao.insert(channelList);
}
}
public class NewsFragment extends BaseFragment {
private PullToRefreshListView plvNews;
public static final String ARGUMENT = "argument";
private String param;
private List<News.DataBean> list;
private NewsAdapter adapter;
private int page = 1;
private boolean isLoadMore = false;
private Handler handler = new Handler();
@Override
protected int getContentView() {
return R.layout.fragment_news;
}
@Override
protected void initView(View v) {
plvNews = v.findViewById(R.id.plv_news);
}
@Override
protected void initData() {
super.initData();
Bundle bundle = getArguments();
param = bundle.getString(ARGUMENT);
plvNews.setMode(PullToRefreshBase.Mode.BOTH);
list = new ArrayList<>();
adapter = new NewsAdapter(mContext, list);
plvNews.setAdapter(adapter);
if (TextUtils.isEmpty(param)) {
return;
}
}
@Override
protected void setListener() {
super.setListener();
plvNews.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
// 下拉刷新
page = 1;
isLoadMore = false;
getData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
page++;
isLoadMore = true;
getData();
}
});
}
@Override
protected void setMoreAction() {
super.setMoreAction();
getData();
}
private void getData() {
if (TextUtils.isEmpty(param)) {
return;
}
String url = ServerUrl.NEWS_URL + "type=" + param + "&page=" + page;
new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... strings) {
return HttpUtils.getStringFromHttp(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
handler.postDelayed(new Runnable() {
@Override
public void run() {
plvNews.onRefreshComplete();
}
}, 2000);
Gson gson = new Gson();
News news = gson.fromJson(s, News.class);
if (news != null) {
List<News.DataBean> data = news.getData();
if (data != null) {
if (!isLoadMore) {
list.clear();
}
list.addAll(data);
adapter.notifyDataSetChanged();
}
}
}
}.execute(url);
}
public static NewsFragment newInstance(String text) {
NewsFragment fragment = new NewsFragment();
Bundle bundle = new Bundle();
bundle.putString(ARGUMENT, text);
fragment.setArguments(bundle);
return fragment;
}
}
public class MainActivity extends BaseActivity implements View.OnClickListener {
private ImageView btnAddChannel;
private TabLayout tblTitle;
private ViewPager vpNews;
private List<Channel> channelList;
private ChannelDao dao;
private FragmentPagerAdapter vpAdapter;
public static final int REQUEST_CODE = 100;
@Override
protected int getContentView() {
return R.layout.activity_main;
}
@Override
protected void initView() {
btnAddChannel = findViewById(R.id.btn_add_channel);
tblTitle = findViewById(R.id.tbl_title);
vpNews = findViewById(R.id.vp_news);
}
@Override
protected void initData() {
channelList = new ArrayList<>();
dao = new ChannelDao(this);
addData();
vpAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return NewsFragment.newInstance(channelList.get(position).getParma());
}
@Override
public int getCount() {
return channelList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return channelList.get(position).getName();
}
};
vpNews.setAdapter(vpAdapter);
tblTitle.setTabMode(TabLayout.MODE_SCROLLABLE);
tblTitle.setupWithViewPager(vpNews);
}
@Override
protected void onResume() {
super.onResume();
channelList.clear();
List<Channel> channels = dao.queryAll();
for (Channel channel : channels) {
// 如果type为1,添加到我的频道中
if (channel.getType() == 1) {
channelList.add(channel);
}
}
vpAdapter.notifyDataSetChanged();
}
@Override
protected void setListener() {
super.setListener();
btnAddChannel.setOnClickListener(this);
}
private void addData() {
List<Channel> channels = dao.queryAll();
for (Channel channel : channels) {
// 如果type为1,添加到我的频道中
if (channel.getType() == 1) {
channelList.add(channel);
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add_channel:
Intent intent = new Intent(this, ChannelActivity.class);
startActivityForResult(intent, REQUEST_CODE);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
int index = data.getIntExtra("index", 0);
vpNews.setCurrentItem(index);
}
}
}
public class ChannelActivity extends BaseActivity implements View.OnClickListener {
private ChannelDao dao;
private TextView txtFinish;
private MyGridView mgvMyChannel;
private MyGridView mgvRecommondChannel;
private List<Channel> myChannels;
private List<Channel> recommondChannels;
private MyChannelAdapter myAdapter;
private RecommondChannelAdapter recommondAdapter;
// 是否是编辑状态
private boolean isEditable = false;
@Override
protected int getContentView() {
return R.layout.activity_channel;
}
@Override
protected void initView() {
txtFinish = findViewById(R.id.txt_finish);
mgvMyChannel = findViewById(R.id.mgv_my_channel);
mgvRecommondChannel = findViewById(R.id.mgv_recommond_channel);
}
@Override
protected void initData() {
super.initData();
myChannels = new ArrayList<>();
recommondChannels = new ArrayList<>();
dao = new ChannelDao(this);
addData();
myAdapter = new MyChannelAdapter(this, myChannels);
recommondAdapter = new RecommondChannelAdapter(this, recommondChannels);
myAdapter.setOnItemDeleteClickListener(new MyChannelAdapter.OnItemDeleteClickListener() {
@Override
public void onDeleteClick(int position) {
Channel channel = myChannels.get(position);
// 点击删除之后应该把类型改成推荐频道
channel.setType(2);
recommondChannels.add(channel);
myChannels.remove(position);
myAdapter.notifyDataSetChanged();
recommondAdapter.notifyDataSetChanged();
dao.update(channel);
}
});
mgvMyChannel.setAdapter(myAdapter);
mgvRecommondChannel.setAdapter(recommondAdapter);
}
@Override
protected void setListener() {
super.setListener();
txtFinish.setOnClickListener(this);
mgvMyChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.putExtra("index", position);
setResult(RESULT_OK, intent);
finish();
}
});
mgvRecommondChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Channel channel = recommondChannels.get(position);
// 点击之后添加到我的频道中,type改成1
channel.setType(1);
myChannels.add(channel);
recommondChannels.remove(position);
myAdapter.notifyDataSetChanged();
recommondAdapter.notifyDataSetChanged();
dao.update(channel);
}
});
}
private void addData() {
List<Channel> channels = dao.queryAll();
for (Channel channel : channels) {
// 如果type为1,添加到我的频道中
// 否则添加到推荐频道中
if (channel.getType() == 1) {
myChannels.add(channel);
} else {
recommondChannels.add(channel);
}
}
//
//
// Channel channel1 = new Channel(1, "头条", "top", 1);
// Channel channel2 = new Channel(2, "社会", "shehui", 1);
// Channel channel3 = new Channel(3, "国内", "guonei", 1);
// Channel channel4 = new Channel(4, "国际", "guoji", 1);
// Channel channel5 = new Channel(5, "娱乐", "yule", 1);
// Channel channel6 = new Channel(6, "体育", "tiyu", 1);
// Channel channel7 = new Channel(7, "军事", "junshi", 1);
// Channel channel8 = new Channel(8, "科技", "keji", 1);
// Channel channel9 = new Channel(9, "财经", "caijing", 1);
// Channel channel10 = new Channel(10, "时尚", "shishang", 1);
// Channel channel11 = new Channel(11, "热评", "", 1);
// Channel channel12 = new Channel(12, "家居", "", 1);
// Channel channel13 = new Channel(13, "视频", "", 1);
// Channel channel14 = new Channel(14, "美食", "", 1);
// Channel channel15 = new Channel(15, "教育", "", 1);
// Channel channel16 = new Channel(16, "旅游", "", 2);
// Channel channel17 = new Channel(17, "健康", "", 2);
// Channel channel18 = new Channel(18, "房产", "", 2);
// Channel channel19 = new Channel(19, "汽车", "", 2);
// Channel channel20 = new Channel(20, "亲子", "", 2);
// Channel channel21 = new Channel(21, "购物", "", 2);
// Channel channel22 = new Channel(22, "段子", "", 2);
// Channel channel23 = new Channel(23, "美文", "", 2);
// Channel channel24 = new Channel(24, "外卖", "", 2);
//
// 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);
// recommondChannels.add(channel17);
// recommondChannels.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_finish:
isEditable = !isEditable;
myAdapter.setEditable(isEditable);
if (isEditable) {
txtFinish.setText("完成");
} else {
txtFinish.setText("编辑");
}
break;
}
}
}