动态添加/删除RecyclerView

先贴代码,说明以后再说。。。懒癌

参考

http://blog.csdn.net/ak341221/article/details/53116181

Activity:

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.recycleView)
    RecyclerView recyclerView;
    @Bind(R.id.button)
    Button button;
    private Activity context;
    private List testModels;
    private TestAdapter testAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        context = MainActivity.this;

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        testModels = new ArrayList<>();
        testModels.add(new TestModel("", "", true));


        testAdapter = new TestAdapter(context, testModels);
        recyclerView.setAdapter(testAdapter);


        testAdapter.setMyItemClickListener(new ClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                if (testModels.get(postion).isPlus()) {
                    testModels.get(postion).setPlus(false);
                    testModels.add(new TestModel("", "", true));
                    testAdapter.notifyDataSetChanged();
                } else {
                    testModels.remove(postion);
                    testAdapter.notifyDataSetChanged();
                }
            }
        });
        testAdapter.setEditListener(new EditListener() {
            @Override
            public void onItemClick(int position, String str) {
                testModels.get(position).setWhat(str);
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, JSONObject.toJSONString(testModels), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

TestModel:

public class TestModel {
    private String num;
    private String what;
    private boolean plus;

    public TestModel(String num, String what, boolean plus) {
        this.num = num;
        this.what = what;
        this.plus = plus;
    }


    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public String getWhat() {
        return what;
    }

    public void setWhat(String what) {
        this.what = what;
    }

    public boolean isPlus() {
        return plus;
    }

    public void setPlus(boolean plus) {
        this.plus = plus;
    }
}

plus的用处是用来判断此条的按钮是需要增加还是删除

TestAdapter:

public class TestAdapter extends RecyclerView.Adapter {


    private List rowsBeen;
    private Context context;
    private ClickListener clickListener;
    private EditListener editListener;

    public TestAdapter(Context context, List rowsBeen) {
        this.context = context;
        this.rowsBeen = rowsBeen;
    }

    @Override
    public TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_initiate, parent, false);
        TestViewHolder vh = new TestViewHolder(itemView, clickListener, editListener);
        return vh;
    }

    @Override
    public int getItemCount() {
        return rowsBeen.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ((TestViewHolder)holder).bindHolder(rowsBeen.get(position));
    }

    public void setMyItemClickListener(ClickListener clickListener){
        this.clickListener = clickListener;
    }

    public void setEditListener(EditListener listener){
        this.editListener = listener;
    }
}

接口:

public interface EditListener {
    void onItemClick(int position, String str);
}


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

一个是监听EditText的输入情况
一个是增加/删除点击事件
通过两个接口所有的操作都在activity进行

ViewHolder:

public class TestViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, TextWatcher {
    @Bind(R.id.tvType)
    TextView tvType;
    @Bind(R.id.lNum)
    RelativeLayout lNum;
    @Bind(R.id.editText)
    EditText editText;
    @Bind(R.id.plusOrMinus)
    ImageView plusOrMinus;
    private ClickListener mClickListener;
    private EditListener mEditListener;


    public TestViewHolder(View itemView, ClickListener clickListener, EditListener editListener) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.mClickListener = clickListener;
        this.mEditListener = editListener;
        plusOrMinus.setOnClickListener(this);
        editText.addTextChangedListener(this);


    }

    public void bindHolder(TestModel testModel) {
        if (testModel.isPlus()) {
            plusOrMinus.setImageResource(R.mipmap.icon_zengjia);
        } else {
            plusOrMinus.setImageResource(R.mipmap.icon_jianshao);
        }
        editText.setText(testModel.getWhat());
    }

    @Override
    public void onClick(View view) {
        if (mClickListener != null) {
            mClickListener.onItemClick(view, getPosition());
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if (mEditListener != null) {
            mEditListener.onItemClick(getPosition(), editable.toString());
        }
    }
}

item_initiate:




    

        

            

            

        

        

        

    



你可能感兴趣的:(动态添加/删除RecyclerView)