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 ListstringList; private List 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 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
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 ArrayListarray; private Button btnfinish; private TextView txtEdit; private GridView mygvChannel; private GridView othergvChannel; private List 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
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 ArrayListlist; public MyGvAdapter(Context context, ArrayList 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
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 Listlist; public OthertAdapter(Context context, List 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