RecycleView頻道管理
阿新 • • 發佈:2018-11-22
1.MainActivity頁面
package com.example.recycleview01.ui.activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import com.example.recycleview01.Bean.ChannelBean; import com.example.recycleview01.R; import com.example.recycleview01.ui.adaper.RecycleAdaper; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private RecycleAdaper adaper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取資源id recyclerView = findViewById(R.id.recyele); //優化 recyclerView.setHasFixedSize(true); //建立介面卡 adaper = new RecycleAdaper(this); //佈局管理器 GridLayoutManager layoutManager = new GridLayoutManager(this,4); //item如何款列顯示 layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int i) { //是標題就要跨4列 return adaper.isTitle(i)?4:1; } }); //將頻道管理器設定給recycleview recyclerView.setLayoutManager(layoutManager); adaper.setOnItemCllickListener(new RecycleAdaper.OnItemCllickListener() { @Override public void onItemClick(View itemView, int position) { if(position<0){ return; } /*if (!isEditMode && adaper.isSelectChannel(position)) { Toast.makeText(MainActivity.this, "選中了條目, 返回: " + position, Toast.LENGTH_SHORT).show(); return; }*/ //移除 ChannelBean removeBean = adaper.removeItem(position); removeBean.setSelect(!removeBean.isSelect()); //新增 adaper.addItem(removeBean); } }); //設定介面卡 recyclerView.setAdapter(adaper); initData(); } private void initData() { //我的頻道資料 List<ChannelBean> selectDatas = new ArrayList<>(); selectDatas.add(new ChannelBean("關注",true,false)); selectDatas.add(new ChannelBean("推薦",true,false)); selectDatas.add(new ChannelBean("科技",true,false)); selectDatas.add(new ChannelBean("美食",true,false)); selectDatas.add(new ChannelBean("養生",true,false)); selectDatas.add(new ChannelBean("電影",true,false)); selectDatas.add(new ChannelBean("生活",true,false)); selectDatas.add(new ChannelBean("搞笑",true,false)); selectDatas.add(new ChannelBean("懂車帝",true,false)); selectDatas.add(new ChannelBean("軍事",true,false)); selectDatas.add(new ChannelBean("黨媒推薦",true,false)); selectDatas.add(new ChannelBean("歷史",true,false)); selectDatas.add(new ChannelBean("體育",true,false)); //頻道推薦資料 List<ChannelBean> normalDatas = new ArrayList<>(); normalDatas.add(new ChannelBean("寵物",false,false)); normalDatas.add(new ChannelBean("娛樂",false,false)); normalDatas.add(new ChannelBean("財經",false,false)); normalDatas.add(new ChannelBean("直播",false,false)); normalDatas.add(new ChannelBean("特賣",false,false)); normalDatas.add(new ChannelBean("房產",false,false)); normalDatas.add(new ChannelBean("精品課",false,false)); normalDatas.add(new ChannelBean("小說",false,false)); normalDatas.add(new ChannelBean("時尚",false,false)); normalDatas.add(new ChannelBean("音訊",false,false)); normalDatas.add(new ChannelBean("育兒",false,false)); normalDatas.add(new ChannelBean("數碼",false,false)); normalDatas.add(new ChannelBean("健康",false,false)); adaper.setChannelDatas(selectDatas,normalDatas); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); return true; } private boolean isEditMode = false; @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.e_action) { isEditMode = !isEditMode; item.setTitle(isEditMode ? "完成" : "編輯"); return true; } return super.onOptionsItemSelected(item); } }
2.介面卡頁面
package com.example.recycleview01.ui.adaper; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.example.recycleview01.Bean.ChannelBean; import com.example.recycleview01.R; import java.util.ArrayList; import java.util.List; public class RecycleAdaper extends RecyclerView.Adapter<RecycleAdaper.ViewHolder> { private List<ChannelBean> selectChannel; private List<ChannelBean> normalChannel; private Context mContext; public RecycleAdaper(Context mContext) { this.mContext = mContext; //初始化 selectChannel = new ArrayList<>(); normalChannel = new ArrayList<>(); } public ChannelBean getItem(int position){ if(isTitle(position)){ return null; } //第一個選中頻道列表的資料 if(isSelectChannel(position)){ return selectChannel.get(position-1); } //第二個未選中頻道列表的資料 return normalChannel.get(position-selectChannel.size()-2); } //判斷當前item是否是標題 public boolean isTitle(int position){ return isSelectTitle(position)||isNorlamTitle(position); } //是否是選中的頻道標題 public boolean isSelectTitle(int position){ return position == 0; } //是否是普通的頻道標題 public boolean isNorlamTitle(int position){ return position == selectChannel.size()+1; } //當前position是否是選中的頻道item public boolean isSelectChannel(int position){ return position<=selectChannel.size(); } public void setChannelDatas(List<ChannelBean> selectChannel,List<ChannelBean> normalChannel){ this.selectChannel = selectChannel; this.normalChannel = normalChannel; notifyDataSetChanged(); } @Override public int getItemViewType(int position) { if(isTitle(position)){ return R.layout.item_channel_title; } if(isSelectChannel(position)){ return R.layout.item_channel_select; } return R.layout.item_channel_normal; } public ChannelBean removeItem(int position){ ChannelBean channelBean = null; if(isSelectChannel(position)){ channelBean = selectChannel.remove(position-1); }else{ channelBean = normalChannel.remove(position-selectChannel.size()-2); } ////////////////////////////// notifyItemRemoved(position); return channelBean; } public void addItem(ChannelBean item){ if(item.isSelect()){ //新增到選中頻道的尾部 selectChannel.add(item); notifyItemInserted(selectChannel.size()); }else{ //新增到未選中頻道的頭部 normalChannel.add(0,item); notifyItemInserted(selectChannel.size()+2); } } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { if(i== R.layout.item_channel_title){ return new ViewHolder(LayoutInflater.from(mContext).inflate(i,viewGroup,false)); }else{ return new ViewHolder(LayoutInflater.from(mContext).inflate(i,viewGroup,false),listener); } } @Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) { if(isSelectTitle(i)){ //我的頻道 viewHolder.text.setText("我的頻道"); return; } if(isNorlamTitle(i)){ //頻道推薦 viewHolder.text.setText("頻道推薦"); return; } viewHolder.text.setText(getItem(i).getName()); } @Override public int getItemCount() { return selectChannel.size()+normalChannel.size()+2; } private OnItemCllickListener listener; public RecycleAdaper setOnItemCllickListener(OnItemCllickListener listener){ this.listener = listener; return this; } //定義介面 public interface OnItemCllickListener{ void onItemClick(View itemView,int position); } class ViewHolder extends RecyclerView.ViewHolder{ TextView text; public ViewHolder(View itemView){ super(itemView); text = itemView.findViewById(R.id.text); } public ViewHolder(@NonNull View itemView, final OnItemCllickListener listener) { this(itemView); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(listener==null){ return; } listener.onItemClick(ViewHolder.this.itemView,getAdapterPosition()); } }); } } }
3.主頁面佈局
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.activity.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyele" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
4.標題的本佈局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="標題"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
/>
</android.support.constraint.ConstraintLayout>
5.選中的頻道的佈局
在這裡插入程式碼片<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="頻道名稱"
android:background="@drawable/shape_text"
/>
</FrameLayout>
6.未選中的頻道佈局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="頻道名稱"
android:padding="10dp"
android:gravity="center_horizontal"
android:background="@drawable/shape_text"
/>
</FrameLayout>
7.方框用shape做
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<size
android:width="50dp"
android:height="20dp"
/>
<stroke
android:color="#ccc"
android:width="1dp"
/>
</shape>
8.頭部編輯和完成切換
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/e_action" android:title="編輯" app:showAsAction="always"/>
</menu>
9.bean類
package com.example.recycleview01.Bean;
public class ChannelBean {
//名稱
private String name;
//是否選擇的頻道
private boolean isSelect;
//是否當前頻道
private boolean isCurrent;
//是否固定不能更改的
private boolean isFixted;
public ChannelBean(String name, boolean isSelect, boolean isCurrent) {
this.name = name;
this.isSelect = isSelect;
this.isCurrent = isCurrent;
}
public String getName() {
return !isSelect?"+"+name:name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
public boolean isCurrent() {
return isCurrent;
}
public void setCurrent(boolean current) {
isCurrent = current;
}
public boolean isFixted() {
return isFixted;
}
public void setFixted(boolean fixted) {
isFixted = fixted;
}
}