Xml
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
></android.support.v7.widget.RecyclerView>
MainActivity
private RecyclerView recyclerView;
private ChannelAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recycle);
//优化
recyclerView.setHasFixedSize(true);
//adapter
adapter = new ChannelAdapter(this);
//布局的管理
// recyclerView.setLayoutManager(new LinearLayoutManager(this));
GridLayoutManager layout=new GridLayoutManager(this,3);
//跨列显示
layout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int i) {
return adapter.isTitle(i)?3:1;
}
});
recyclerView.setLayoutManager(layout);
adapter.setOnItemClickListener(new ChannelAdapter.OnItemClickListener() {
@Override
public void onItemClick(View itemView, int position) {
//移除
ChannelBean channelBean = adapter.removeItem(position);
channelBean.setSelect(!channelBean.isSelect());
//添加
adapter.addItem(channelBean);
}
});
recyclerView.setAdapter(adapter);
initData();
}
private void initData() {
ArrayList<ChannelBean> selDatas = new ArrayList<>();
ArrayList<ChannelBean> norDatas = new ArrayList<>();
selDatas.add(new ChannelBean("关注1", true, false));
selDatas.add(new ChannelBean("关注2", true, false));
selDatas.add(new ChannelBean("关注3", true, false));
selDatas.add(new ChannelBean("关注4", true, false));
selDatas.add(new ChannelBean("关注5", true, false));
selDatas.add(new ChannelBean("关注6", true, false));
selDatas.add(new ChannelBean("关注7", true, false));
selDatas.add(new ChannelBean("关注8", true, false));
norDatas.add(new ChannelBean("关注9", false, false));
norDatas.add(new ChannelBean("关注10", false, false));
norDatas.add(new ChannelBean("关注11", false, false));
norDatas.add(new ChannelBean("关注12", false, false));
norDatas.add(new ChannelBean("关注13", false, false));
norDatas.add(new ChannelBean("关注14", false, false));
norDatas.add(new ChannelBean("关注15", false, false));
norDatas.add(new ChannelBean("关注16", false, false));
norDatas.add(new ChannelBean("关注17", false, false));
norDatas.add(new ChannelBean("关注18", false, false));
adapter.setChannelDatas(selDatas, norDatas);
}
Bean
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 void setName(String name) {
this.name = name;
}
public void setSelect(boolean select) {
isSelect = select;
}
public void setCurrent(boolean current) {
isCurrent = current;
}
public void setFixted(boolean fixted) {
isFixted = fixted;
}
public String getName() {
return name;
}
public boolean isSelect() {
return isSelect;
}
public boolean isCurrent() {
return isCurrent;
}
public boolean isFixted() {
return isFixted;
}
Adapter
public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ViewHolder> {
private Context context;
private List<ChannelBean> selelctDatas;
private List<ChannelBean> normalDatas;
private ChannelBean channelBean;
public ChannelAdapter(Context context) {
this.context = context;
selelctDatas=new ArrayList<>();
normalDatas=new ArrayList<>();
}
//当前position是否是选中的频道item
public boolean isSelectChannel(int position){
return position<=selelctDatas.size();
}
//当前item是否是标题
public boolean isTitle(int position){
return isSelTitle(position)||isNomTitle(position);
}
//是否为选中的频道标题
public boolean isSelTitle(int position){
return position==0;
}
//是否为普通的标题
public boolean isNomTitle(int position){
return position==selelctDatas.size()+1;
}
@Override
public int getItemViewType(int position) {
if(isTitle(position)){
return R.layout.item_channle_title;
}
if(isSelectChannel(position)){
return R.layout.item_channle_selected;
}
return R.layout.item_channle_normal;
}
public ChannelBean getItem(int position){
if(isTitle(position)){
return null;
}
// 第一个选中频道列表的数据
if(isSelectChannel(position)){
return selelctDatas.get(position-1);
}
//第二个未选中频道列表的数据
return normalDatas.get(position-selelctDatas.size()-2);
}
public void setChannelDatas(List<ChannelBean> selectedChannel, List<ChannelBean> normalChannel) {
this.selelctDatas = selectedChannel;
this.normalDatas = normalChannel;
notifyDataSetChanged();
}
public ChannelBean removeItem(int position){
ChannelBean channelBean=null;
if(isSelectChannel(position)){
channelBean = selelctDatas.remove(position - 1);
}else{
channelBean=normalDatas.remove(position-selelctDatas.size()-2);
}
notifyItemRemoved(position);
return channelBean;
}
public void addItem(ChannelBean item){
if(item.isSelect()){
selelctDatas.add(item);
notifyItemInserted(selelctDatas.size());
}else{
normalDatas.add(0,item);
notifyItemInserted(selelctDatas.size()+2);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == R.layout.item_channle_title) {
return new ViewHolder(LayoutInflater.from(context).inflate(i, viewGroup, false));
} else {
return new ViewHolder(LayoutInflater.from(context).inflate(i, viewGroup, false),listener);
}
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
if(isTitle(i)){
viewHolder.text.setText("我的频道");
return;
}
if(isNomTitle(i)){
viewHolder.text.setText("频道推荐");
return;
}
viewHolder.text.setText(getItem(i).getName());
}
private OnItemClickListener listener;
public ChannelAdapter setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
return this;
}
public interface OnItemClickListener{
void onItemClick(View itemView,int position);
}
@Override
public int getItemCount() {
return selelctDatas.size()+normalDatas.size()+2;
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView text;
public ViewHolder(@NonNull View itemView) {
super(itemView);
text=itemView.findViewById(R.id.text);
}
public ViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
this(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener==null){
return;
}
listener.onItemClick(ViewHolder.this.itemView,getAdapterPosition());
}
});
}
}
加载布局的xml文件
选中的样式
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
android:paddingBottom="4dp">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="频道名称"
android:gravity="center_horizontal"
android:background="#4ff"
/>
</FrameLayout>
未选中的样式
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
android:paddingBottom="4dp">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="频道名称"
android:gravity="center_horizontal"
android:background="#009000"
/>
</FrameLayout>
标题
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingTop="8dp"
android:paddingRight="16dp"
android:paddingBottom="8dp"
android:text="标题"
android:textColor="#44f" />
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>