如何在元素内添加和删除Recycler listview项

时间:2016-05-31 05:39:10

标签: android listview android-recyclerview

enter image description here

This is my Adapter class

    public class NewFeePlanAdapter extends RecyclerView.Adapter<NewFeePlanAdapter.MyViewHolder> {

    Context context;
    ArrayList<FeeTypeModel> feeTypeModelArrayList;


    public NewFeePlanAdapter(Context context, ArrayList<FeeTypeModel> feeTypeModelArrayList){
        this.context = context;
        this.feeTypeModelArrayList = feeTypeModelArrayList;
    }




    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.new_fee_list_row, parent,false);

        MyViewHolder myViewHolder = new MyViewHolder(itemView,new MyCustomEditTextListener());

        return myViewHolder;
    }

    FeeTypeModel feeTypeModel;

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        feeTypeModel = feeTypeModelArrayList.get(position);
        holder.fee_type_name_tv.setText(feeTypeModel.getFeeType().toString());


        if (feeTypeModel.getLinearLayout()!=null) {
            holder.new_plan_main_ll.addView(feeTypeModel.getLinearLayout());
        }

        holder.myCustomEditTextListener.updatePosition(position);
        holder.fee_amount_et.setText( feeTypeModelArrayList.get(position).getAmount());

        holder.fee_type_cb.setOnCheckedChangeListener(null);
        holder.fee_type_cb.setChecked(feeTypeModel.isSelectPlan());


        holder.fee_type_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                feeTypeModelArrayList.get(position).setSelectPlan(isChecked);

            }
        });


        holder.add_installments_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.fee_amount_et.getText().length() == 0){
                    Toast.makeText(context,"Please Enter Amount",Toast.LENGTH_LONG).show();
                }else {
                    showDialogForNumberOfInstallments(v,position,Long.parseLong(holder.fee_amount_et.getText().toString()));

                }
            }
        });


    }

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




    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView fee_type_name_tv;
        EditText fee_amount_et;
        ImageView add_installments_iv;
        CheckBox fee_type_cb;
        public MyCustomEditTextListener myCustomEditTextListener;
        LinearLayout new_plan_main_ll;

        public MyViewHolder(View view, MyCustomEditTextListener myCustomEditTextListener) {
            super(view);
            new_plan_main_ll = (LinearLayout)view.findViewById(R.id.new_plan_main_ll);
            fee_type_cb = (CheckBox) view.findViewById(R.id.fee_type_cb);
            fee_type_name_tv = (TextView) view.findViewById(R.id.fee_type_name_tv);
            fee_amount_et = (EditText) view.findViewById(R.id.fee_amount_et);
            add_installments_iv = (ImageView)view.findViewById(R.id.add_installments_iv);
            this.myCustomEditTextListener = myCustomEditTextListener;
            this.fee_amount_et.addTextChangedListener(myCustomEditTextListener);

        }
    }

    private class MyCustomEditTextListener implements TextWatcher {
        private int position;

        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            feeTypeModelArrayList.get(position).setAmount( charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // no op
        }
    }



    AutoCompleteTextView new_fee_plan_name_act;
    EditText numberOfInstallments_et;
    int selectedPosition;
    private void showDialogForNumberOfInstallments(View v,int position, final long amount){
        selectedPosition = position;
        // Create custom dialog object
        final Dialog dialog = new Dialog(context);
        // Include dialog.xml file
        dialog.setContentView(R.layout.new_plan_name_dialog);

        dialog.setTitle("Installments");

        new_fee_plan_name_act = (AutoCompleteTextView) dialog.findViewById(R.id.new_fee_plan_name_act);
        new_fee_plan_name_act.setVisibility(View.GONE);

        numberOfInstallments_et = (EditText)dialog.findViewById(R.id.numberOfInstallments_et);
        numberOfInstallments_et.setVisibility(View.VISIBLE);

        new_fee_plan_name_act.setHint("Enter Number Of Installments");

        dialog.setCancelable(false);
        dialog.show();

        Button feePlan_cancel_bt = (Button) dialog.findViewById(R.id.feePlan_cancel_bt);
        // if decline button is clicked, close the custom dialog
        feePlan_cancel_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Close dialog
                dialog.dismiss();
            }
        });

        Button FeePlan_create_bt = (Button) dialog.findViewById(R.id.FeePlan_create_bt);
        FeePlan_create_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (numberOfInstallments_et.getText().toString().length()==0){
                    Toast.makeText(context,"Please Enter New number 0f Installments",Toast.LENGTH_LONG).show();
                }else {

                    FeeTypeModel feeTypeModel11 = feeTypeModelArrayList.get(selectedPosition);
                    if (feeTypeModel11.getLinearLayout()!=null) {
                        feeTypeModel11.getLinearLayout().removeView(v);
                    }

                    LinearLayout layout = new LinearLayout(context);
                    layout.setLayoutParams(new LinearLayout.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
                    layout.setOrientation(LinearLayout.VERTICAL);



//                    Toast.makeText(context,"Entered Installments "+numberOfInstallments_et.getText().toString(),Toast.LENGTH_LONG).show();
                    int WrapWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
                    int WrapHeight = LinearLayout.LayoutParams.WRAP_CONTENT;

                    int length = Integer.parseInt(numberOfInstallments_et.getText().toString());
                    long instAmount = amount/length;
                    for (int i=0;i<length;i++){
                        TextView t = new TextView(context);
                        t.setText("Installment"+i+"      "+instAmount);
                        layout.addView(t,WrapWidth,WrapHeight);
                    }

                    feeTypeModel11.setLinearLayout(layout);
                    dialog.dismiss();

                    notifyDataSetChanged();


                }

            }
        });

    }


}


this is my new_fee_list_row.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:elevation="6dp"
    android:layout_marginTop="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginBottom="@dimen/activity_horizontal_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/new_plan_main_ll"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="wrap_content">

            <CheckBox
                android:id="@+id/fee_type_cb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/fee_type_name_tv"
                android:text="fsdfdsf"
                android:layout_marginLeft="@dimen/activity_horizontal_margin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_marginTop="@dimen/activity_horizontal_margin"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_toLeftOf="@+id/add_installments_iv"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/fee_amount_et"
                    android:layout_width="130dp"
                    android:layout_height="wrap_content"
                    android:imeActionId="@+id/login"
                    android:imeActionLabel="@string/action_sign_in_short"
                    android:imeOptions="actionUnspecified"
                    android:maxLines="1"
                    android:hint="Enter Amount"
                    android:inputType="number"
                    android:singleLine="true"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

            </android.support.design.widget.TextInputLayout>
            <ImageView
                android:id="@+id/add_installments_iv"
                android:layout_width="wrap_content"
                android:layout_marginLeft="@dimen/activity_horizontal_margin"
                android:layout_alignParentRight="true"

                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/activity_horizontal_margin"
                android:src="@android:drawable/ic_input_add"
                android:layout_height="wrap_content" />
        </RelativeLayout>



    </LinearLayout>


</android.support.v7.widget.CardView>


//This is my main Activity class


    public class NewFeePlanActivity extends AppCompatActivity {

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_fee_plan);
        RecyclerView fee_plans_rv = (RecyclerView)findViewById(R.id.fee_plans_rv);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
            fee_plans_rv.setLayoutManager(mLayoutManager);  
    NewFeePlanAdapter    newFeePlanAdapter = new NewFeePlanAdapter(NewFeePlanActivity.this, feeTypeModelArrayList);
                    fee_plans_rv.setAdapter(newFeePlanAdapter);
}}

我正在使用上面的代码,但我的问题是在列表中添加特定位置

**如上图所示,我需要动态添加和删除每个项目位置在回收者视图中的视图,任何人都可以用代码指导我,     您的回复将不胜感激..... **

0 个答案:

没有答案