1.MainActivity
package com.example.lenovo.pingdao;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TabLayout tbl;
private ImageView showAll;
private ViewPager vp;
private List<String> stringList;
private List<Fragment> fragmentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tbl = findViewById(R.id.tbl);
showAll = findViewById(R.id.show_all);
vp = findViewById(R.id.vp);
stringList = new ArrayList<>();
stringList.add("头条");
stringList.add("社会");
stringList.add("国内");
stringList.add("国际");
stringList.add("娱乐");
stringList.add("体育");
stringList.add("军事");
stringList.add("科技");
stringList.add("财经");
stringList.add("时尚");
tbl.setTabMode(TabLayout.MODE_SCROLLABLE);
tbl.setTabTextColors(ColorStateList.valueOf(Color.BLACK));
fragmentList = new ArrayList<>();
for (String s : stringList) {
tbl.addTab(tbl.newTab().setText(s));
NewsFragment newsFragment = new NewsFragment();
fragmentList.add(newsFragment);
}
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return stringList.get(position);
}
});
tbl.setupWithViewPager(vp);
final ArrayList<String> arrayList = new ArrayList<>();
arrayList.addAll(stringList);
//点击加号 跳转频道管理页面
showAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,ChannelActivity.class);
Bundle bundle = new Bundle();
bundle.putStringArrayList("array",arrayList);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
Item main_activity
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<android.support.design.widget.TabLayout
android:id="@+id/tbl"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp" >
</android.support.design.widget.TabLayout>
<ImageView
android:id="@+id/show_all"
android:src="@drawable/feedback_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
2.ChannelActivity
import android.content.Intent;
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.GridView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ChannelActivity extends AppCompatActivity implements View.OnClickListener{
private ArrayList<String> array;
private Button btnfinish;
private TextView txtEdit;
private GridView mygvChannel;
private GridView othergvChannel;
private List<String> recommond;
private boolean isEdited = false;
private MyGvAdapter madapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_channel);
//找控件
btnfinish = findViewById(R.id.btn_finish);
txtEdit = findViewById(R.id.txt_edit);
mygvChannel = findViewById(R.id.mygvChannel);
othergvChannel = findViewById(R.id.othergvChannel);
//接收值
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
array = bundle.getStringArrayList("array");
//给频道管理写适配器
madapter = new MyGvAdapter(this, array);
mygvChannel.setAdapter(madapter);
//给推荐频道写死数据
recommond = new ArrayList<>();
recommond.add("特卖");
recommond.add("直播");
recommond.add("房产");
recommond.add("历史");
recommond.add("旅游");
//给推荐频道添加设置适配器
final OthertAdapter othertAdapter = new OthertAdapter(this,recommond);
othergvChannel.setAdapter(othertAdapter);
MyGvAdapter.OnDeleteItemClick listener = new MyGvAdapter.OnDeleteItemClick() {
@Override
public void OnDeleteItemClick(int position) {
recommond.add(array.get(position));
othertAdapter.notifyDataSetChanged();
array.remove(position);
madapter.notifyDataSetChanged();
}
};
madapter.setOnDeleteItemClick(listener);
// 推荐频道里 添加到 我的频道
othergvChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
array.add(recommond.get(i));
madapter.notifyDataSetChanged();
recommond.remove(i);
othertAdapter.notifyDataSetChanged();
}
});
//点击事件
txtEdit.setOnClickListener(this);
btnfinish.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.txt_edit:
isEdited = !isEdited;
if (isEdited){
txtEdit.setText("完成");
}else {
txtEdit.setText("编辑");
}
madapter.setEdited(isEdited);
break;
case R.id.btn_finish:
finish();
break;
}
}
}
Item channel_activity
<?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_finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="我的频道"/>
<TextView
android:id="@+id/txt_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="20dp"
android:textColor="#000000"
android:text="编辑"/>
</RelativeLayout>
<GridView
android:id="@+id/mygvChannel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="10dp"
android:numColumns="4"
></GridView>
<TextView
android:text="推荐频道"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<GridView
android:id="@+id/othergvChannel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="4"></GridView>
</LinearLayout>
3.MyGvAdapter
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by lenovo on
*/
public class MyGvAdapter extends BaseAdapter{
//提供一个接口
public interface OnDeleteItemClick{
void OnDeleteItemClick(int position);
// void OnDeleteItemClick(int i);
}
//接口实例化
private OnDeleteItemClick listener;
//提供初始化接口
public void setOnDeleteItemClick(OnDeleteItemClick listener){
this.listener=listener;
}
private Context context;
private ArrayList<String> list;
public MyGvAdapter(Context context, ArrayList<String> list) {
this.context = context;
this.list = list;
}
//boolean
private boolean isEdited =false;
public void setEdited(boolean edited){
isEdited = edited;
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null){
holder = new ViewHolder();
view = View.inflate(context,R.layout.item_mygv,null);
holder.txtChannel = view.findViewById(R.id.txt_channel);
holder.del = view.findViewById(R.id.del);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
holder.txtChannel.setText(list.get(i));
//判断是否 显示和隐藏
if (isEdited){
holder.del.setVisibility(View.VISIBLE);
}else {
holder.del.setVisibility(View.GONE);
}
holder.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.OnDeleteItemClick(i);
}
});
return view;
}
class ViewHolder{
private TextView txtChannel;
private ImageView del;
}
}
Item_myGv
<?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="horizontal">
<TextView
android:id="@+id/txt_channel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="删除"
android:textColor="#000000"/>
<ImageView
android:id="@+id/del"
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@drawable/delete_icon_night"/>
</LinearLayout>
4.OtherGvAdapter
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by lenovo on
*/
public class OthertAdapter extends BaseAdapter{
private boolean isEdited = false;
private Context context;
private List<String> list;
public OthertAdapter(Context context, List<String> 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 i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null){
holder = new ViewHolder();
view = View.inflate(context,R.layout.item_other,null);
holder.txtChannel = view.findViewById(R.id.txt_channel);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
holder.txtChannel.setText(list.get(i));
return view;
}
class ViewHolder{
private TextView txtChannel;
}
}
Item_other
<?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="horizontal">
<ImageView
android:id="@+id/del"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:src="@drawable/detail_subscribe_night"/>
<TextView
android:id="@+id/txt_channel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="删除"
android:textColor="#000000"/>
</LinearLayout>
本博客详细介绍了如何在安卓应用中实现新闻阅读应用的Tab布局与频道管理功能,包括MainActivity中TabLayout和ViewPager的使用,以及ChannelActivity中频道的编辑与添加。通过自定义适配器实现了频道的动态添加和删除。

1万+

被折叠的 条评论
为什么被折叠?



