先贴代码,说明以后再说。。。懒癌
参考
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<TestModel> 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<RecyclerView.ViewHolder> {
private List<TestModel> rowsBeen;
private Context context;
private ClickListener clickListener;
private EditListener editListener;
public TestAdapter(Context context, List<TestModel> 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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:paddingLeft="30dp">
<RelativeLayout
android:id="@+id/lNum"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:background="@drawable/edittext_bg"
android:gravity="center">
<TextView
android:id="@+id/tvType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="0"
android:textSize="16dp" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:layout_toLeftOf="@+id/plusOrMinus"
android:layout_toRightOf="@+id/lNum"
android:background="@drawable/edittext_bg"
android:gravity="center" />
<ImageView
android:id="@+id/plusOrMinus"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
</LinearLayout>