频道管理

前提

  依赖 implementation 'com.android.support:recyclerview-v7:28.0.0'

ContentBean

public class ContentBean {
    private String name;
    private boolean isSelected;
    private boolean isCurrent;
    private boolean isFixed;

    public ContentBean(String name, boolean isSelected, boolean isCurrent) {
        this.name = name;
        this.isSelected = isSelected;
        this.isCurrent = isCurrent;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    public boolean isCurrent() {
        return isCurrent;
    }

    public void setCurrent(boolean current) {
        isCurrent = current;
    }

    public boolean isFixed() {
        return isFixed;
    }

    public void setFixed(boolean fixed) {
        isFixed = fixed;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    private RecyclerView listener;
    private ContensAdapter contensAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listener=findViewById(R.id.recyclerview);
        listener.setHasFixedSize(true);
        contensAdapter = new ContensAdapter(this);
        GridLayoutManager layoutManager=new GridLayoutManager(this,4);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                return contensAdapter.isTitle(i)?4:1;
            }
        });
        listener.setLayoutManager(layoutManager);
        //设置监听
        contensAdapter.setOnItemClickListener(new ContensAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                ContentBean bean=contensAdapter.removeItem(position);
                bean.setSelected(!bean.isSelected());
                contensAdapter.addItem(bean);
            }
        });
        listener.setAdapter(contensAdapter);
        initData();
    }
//添加数据
    private void initData() {
        List<ContentBean> selDatas=new ArrayList<>();
        List<ContentBean> norDatas=new ArrayList<>();

        selDatas.add(new ContentBean("关注1", true, false));
        selDatas.add(new ContentBean("关注2", true, false));
        selDatas.add(new ContentBean("关注3", true, false));
        selDatas.add(new ContentBean("关注4", true, false));
        selDatas.add(new ContentBean("关注5", true, false));
        selDatas.add(new ContentBean("关注6", true, false));
        selDatas.add(new ContentBean("关注7", true, false));
        selDatas.add(new ContentBean("关注8", true, false));

        norDatas.add(new ContentBean("关注9", false, false));
        norDatas.add(new ContentBean("关注10", false, false));
        norDatas.add(new ContentBean("关注11", false, false));
        norDatas.add(new ContentBean("关注12", false, false));
        norDatas.add(new ContentBean("关注13", false, false));
        norDatas.add(new ContentBean("关注14", false, false));
        norDatas.add(new ContentBean("关注15", false, false));
        norDatas.add(new ContentBean("关注16", false, false));
        norDatas.add(new ContentBean("关注17", false, false));
        norDatas.add(new ContentBean("关注18", false, false));
        contensAdapter.setData(selDatas,norDatas);
        //adapter.setChannel(selDatas,norDatas);
    }
}

ContentsAdapter 适配器

public class ContensAdapter extends RecyclerView.Adapter<ContensAdapter.Viewholder> {

    private Context context;
    private List<ContentBean> selectedList;
    private List<ContentBean> normalList;
    private OnItemClickListener listener;

    public ContensAdapter(Context context) {
        this.context = context;
        selectedList=new ArrayList<>();
        normalList=new ArrayList<>();
    }

    public void setData(List<ContentBean> selected,List<ContentBean> normal){
        selectedList=selected;
        normalList=normal;
        notifyDataSetChanged();
    }
    //是否是我的频道的标题
    public boolean  isSelTitle(int position){
        return position==0;
    }
    //是否是频道推荐的标题
    public boolean isNorTitle(int position){
        return position==selectedList.size()+1;
    }
    //是否是标题
    public boolean isTitle(int position){
        return isSelTitle(position)||isNorTitle(position);
    }

    @Override
    public int getItemViewType(int position) {
        if(isTitle(position)){//标题条目
            return R.layout.item_channle_title;
        }
        if(isSelChannel(position)){//
            return R.layout.item_channle_selected;
        }
        //
        return R.layout.item_channle_normal;

    }
    public boolean isSelChannel(int position){
        return position<=selectedList.size();
    }

    public ContensAdapter setOnItemClickListener(OnItemClickListener listener){
        this.listener=listener;
        return this;
    }

    public void addItem(ContentBean item){
        if(item.isSelected()){
            selectedList.add(item);
            notifyItemChanged(selectedList.size());
        }else{
            normalList.add(0,item);
            notifyItemChanged(selectedList.size()+2);//可以自己设置值 自己去调试
        }
    }
    public ContentBean removeItem(int position){
        ContentBean bean=null;
        if(isSelChannel(position)){
            bean=selectedList.remove(position-1);
        }else{
            bean=normalList.remove(position-selectedList.size()-2);
        }
        notifyItemChanged(position);
        return bean;
    }

    public interface OnItemClickListener{
        void onItemClick(View itemView,int position);
    }

    public ContentBean getItem(int position){
        if(isTitle(position)){
            return null;
        }
        if(isSelChannel(position)){
            return selectedList.get(position-1);
        }
        return normalList.get(position-selectedList.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(isSelTitle(i)){
            viewholder.textView.setText("我的频道");
            return;
        }
        if(isNorTitle(i)){
            viewholder.textView.setText("频道推荐");
            return;
        }
        viewholder.textView.setText(getItem(i).getName());
    }

    @Override
    public int getItemCount() {
        return selectedList.size()+normalList.size()+2;
    }

    class Viewholder extends RecyclerView.ViewHolder{
        TextView textView;
        public Viewholder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.text);
        }

        public Viewholder(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.textView,getAdapterPosition());
                }
            });
        }
    }
}

activity_main.xml

<?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=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />
</android.support.constraint.ConstraintLayout>

item_view.xml

<?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:text="16dp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="4dp"
         />

</android.support.constraint.ConstraintLayout>

item_channle_title.xml

<?xml version="1.0" encoding="utf-8"?>
<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="@android:color/black" />

item_channle_selected.xml

<?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: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="#ccc"
        />
</FrameLayout>

item_channle_normal.xml

<?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: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="#ccc"
        />
</FrameLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值