今日頭條頻道管理細緻程式碼
//佈局點選進入頻道管理頁面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwei.www.channelonly.MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <android.support.design.widget.TabLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="50dp" android:id="@+id/tbl" ></android.support.design.widget.TabLayout> <ImageView android:id="@+id/show_all" android:src="@drawable/btn_add" android:layout_width="30dp" android:layout_height="30dp" /> </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>
//Activity頁面
import android.content.Intent; 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); fragmentList = new ArrayList<>(); for (String s : stringList) { tbl.addTab(tbl.newTab().setText(s)); } 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 super.getPageTitle(position); } }); tbl.setupWithViewPager(vp); final ArrayList<String> arrayList = new ArrayList<>(); arrayList.addAll(stringList); //點選加號 跳轉頻道管理頁面 showAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,ChannelActivity.class); Bundle bundle = new Bundle(); bundle.putStringArrayList("array",arrayList); intent.putExtras(bundle); startActivity(intent); } }); } }
//建立一個佈局頁面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwei.www.channelonly.ChannelActivity"> <Button android:text="完成" android:textSize="20sp" android:id="@+id/btn_finish" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:padding="20dp" android:textSize="20sp" android:text="我的頻道" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txt_edit" android:textSize="20sp" android:gravity="center" android:layout_alignParentRight="true" android:textColor="#000000" android:layout_width="wrap_content" android:text="編輯" android:paddingRight="15sp" android:layout_height="wrap_content" /> </RelativeLayout> <GridView android:padding="20dp" android:id="@+id/mygvChannel" android:horizontalSpacing="10dp" android:numColumns="4" android:layout_width="wrap_content" android:layout_height="wrap_content"> </GridView> <TextView android:text="頻道推薦" android:textSize="20sp" android:padding="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <GridView android:padding="20dp" android:id="@+id/othergvChannel" android:numColumns="4" android:layout_width="wrap_content" android:layout_height="wrap_content"></GridView> </LinearLayout>
//寫兩個介面卡
//我的
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 Button btnFinish;
private TextView txtEdit;
private GridView myChannel;
private GridView otherChannel;
private MyChannelAdapter madapter;
private List<String> recommond;
private OtherAdapter otherAdapter;
private ArrayList<String> array;
private boolean isEdited = false;
@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);
myChannel = findViewById(R.id.mygvChannel);
otherChannel = findViewById(R.id.othergvChannel);
//接收值
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
array = bundle.getStringArrayList("array");
//給頻道管理寫介面卡
madapter = new MyChannelAdapter(this, array);
myChannel.setAdapter(madapter);
recommond = new ArrayList<>();
//給推薦頻道寫資料
recommond.add("特賣");
recommond.add("直播");
recommond.add("房產");
recommond.add("歷史");
recommond.add("旅遊");
//給推薦頻道新增設定介面卡
otherAdapter = new OtherAdapter(this,recommond);
otherChannel.setAdapter(otherAdapter);
MyChannelAdapter.OnDeleteItemClick listener = new MyChannelAdapter.OnDeleteItemClick() {
@Override
public void OnDeleteItemClick(int position) {
recommond.add(array.get(position));
array.remove(position);
madapter.notifyDataSetChanged();
otherAdapter.notifyDataSetChanged();
}
};
madapter.setOnDeleteItemClick(listener);
// 推薦頻道里 新增到 我的頻道
otherChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
array.add(recommond.get(position));
madapter.notifyDataSetChanged();
recommond.remove(position);
otherAdapter.notifyDataSetChanged();
}
});
//點選事件
txtEdit.setOnClickListener(this);
btnFinish.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.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;
}
}
}
//另一個介面卡
package com.example.lenovo.pingdao;
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 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;
}
}
//我的介面卡
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 asus on 2018/9/20.
*/
public class MyChannelAdapter extends BaseAdapter{
//提供一個介面
public interface OnDeleteItemClick{
void OnDeleteItemClick(int position);
}
//介面例項化
private OnDeleteItemClick listener;
//提供初始化介面
public void setOnDeleteItemClick(OnDeleteItemClick listener){
this.listener=listener;
}
private Context context;
private ArrayList<String> list;
public MyChannelAdapter(Context context, ArrayList<String> list) {
this.context = context;
this.list = list;
}
private boolean isEdited = false;
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_mychannel,null);
holder.txtChannel = convertView.findViewById(R.id.txt_channel);
holder.del = convertView.findViewById(R.id.del);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtChannel.setText(list.get(position));
//判斷是否 顯示和隱藏 刪除圖片
if (isEdited){
holder.del.setVisibility(View.VISIBLE);
}else {
holder.del.setVisibility(View.GONE);
}
holder.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.OnDeleteItemClick(position);
}
});
return convertView;
}
class ViewHolder{
private TextView txtChannel;
private ImageView del;
}
}
//佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:paddingTop="5dp"
android:id="@+id/del"
android:src="@drawable/btn_add"
android:layout_width="25dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/channel"
android:textColor="#000000"
android:textSize="22sp"
android:text="刪除"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<?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">
<TextView
android:id="@+id/txt_channel"
android:text="刪除"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/del"
android:src="@drawable/img_delete"
android:layout_width="18dp"
android:layout_height="18dp" />
</LinearLayout>
//佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwei.www.channelonly.ChannelActivity">
<Button
android:text="完成"
android:textSize="20sp"
android:id="@+id/btn_finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="20dp"
android:textSize="20sp"
android:text="我的頻道"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_edit"
android:textSize="20sp"
android:gravity="center"
android:layout_alignParentRight="true"
android:textColor="#000000"
android:layout_width="wrap_content"
android:text="編輯"
android:paddingRight="15sp"
android:layout_height="wrap_content" />
</RelativeLayout>
<GridView
android:padding="20dp"
android:id="@+id/mygvChannel"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</GridView>
<TextView
android:text="頻道推薦"
android:textSize="20sp"
android:padding="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<GridView
android:padding="20dp"
android:id="@+id/othergvChannel"
android:numColumns="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></GridView>
</LinearLayout>