利用RecyclerView实现动态添加RadioButton多行显示

最近在做一个项目需要动态添加RadioButton并且多行显示(行数不确定),找了很多网上的资源,感觉都多少不够好,自由度不是很高,往往还不能动态添加.经过一番思索,决定利用RecyclerView代替RadioGroup作为容器,这样不仅解决了布局问题,添加起来也更加方便,不用不断的new RadioButton,只需要在适配器中加入即可,废话不多说,贴上代码:


先上适配器代码:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;


import com.cd.education.R;
import com.cd.education.activity.board.bean.Classes;
import com.cd.education.activity.home.bean.Child;


import java.util.List;


/**
 * Created by chunzhang on 2016/9/30.
 */
public class ClassesListAdapter extends RecyclerView.Adapter {
    List mClassesList;
    private Context mContext;


    MyItemClickListener listener;
    public ClassesListAdapter(List classesList, Context context, MyItemClickListener listener1){
        this.mClassesList = classesList;
        this.mContext = context;
        this.listener = listener1;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.classes_list, null);
        MenuViewHolder holder = new MenuViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (mClassesList != null){
            MenuViewHolder menuViewHolder = (MenuViewHolder) holder;
            menuViewHolder.bindData(mClassesList.get(position), position);
        }
    }


    @Override
    public int getItemCount() {
        if (mClassesList != null){
            return mClassesList.size();
        }
        return 0;
    }


    class MenuViewHolder extends RecyclerView.ViewHolder{
        private Classes mClasses;
        private RadioButton rb;
        private TextView classes_name;


        public MenuViewHolder(View itemView) {
            super(itemView);
            rb = (RadioButton) itemView.findViewById(R.id.rb_select);
            classes_name = (TextView) itemView.findViewById(R.id.tv_classes_name);
        }


        public void bindData(final Classes classes, final int postion) {
            this.mClasses = classes;
            classes_name.setText(classes.classesName);
            if (classes.ischecked == false){
                rb.setBackgroundResource(R.mipmap.ic_choose);
            }else {
                rb.setBackgroundResource(R.mipmap.ic_nochoose);
            }
            rb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(postion);
                    }
                }
            });
        }
    }


    public interface MyItemClickListener {
        public void onItemClick(int postion);
    }
}


没什么好说的,一个简单的布局,只有一个RadioButton和一个TextView,设置了一个点击监听,用于改变状态和记录点击的位置.


下面是RecyclerView的一些设定及适配器绑定:


private void setRecyclerView(RecyclerView itemRv) {
        GridLayoutManager manager = new GridLayoutManager(this, 4);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        itemRv.setLayoutManager(manager);
        int spacingH = (int) AppUtil.dip2px(this, 5);
        int spacingV = 0;
        itemRv.addItemDecoration(new RecyclerViewItemSpaces(spacingH, spacingV, spacingH, spacingV));
        classesAdapter = new ClassesListAdapter(mPersenter.mClassesList, this, listener);
        itemRv.setAdapter(classesAdapter);
    }


    private ClassesListAdapter.MyItemClickListener listener = new ClassesListAdapter.MyItemClickListener() {
        @Override
        public void onItemClick(int postion) {
            if (mPersenter.mClassesList.get(postion).ischecked == false){
                for (int i = 0; i < mPersenter.mClassesList.size(); i++){
                    mPersenter.mClassesList.get(i).ischecked = false;
                }
                mPersenter.mClassesList.get(postion).ischecked = true;
            }
            classesAdapter.notifyDataSetChanged();
            index = postion;
            Log.e("position", postion+"");
        }
    };
为了方便,我在Classes这个bean中加入了一个ischeck的(boolean)字段用于处理每次点击后的状态变化
(代码中部分布局文件,资源等未贴出来.)


最后的实现效果图还是不错的,可以适应任意个数的RadioButton的添加,抛砖引玉,希望有大神给出更好的解决方法<^^>.

你可能感兴趣的:(Android)