每次单击按钮时复制CardView按钮

时间:2017-11-20 09:09:18

标签: android android-linearlayout cardview

我的活动中有一张cardview。它包含两个EditText,一个图像按钮和一个回收者视图。我希望每当我点击实际卡片视图下方的按钮时,它应该再复制一次如上所述的cardview。我已经尝试使用Recycler视图,但我没有得到所需的输出,因为我必须发布每个cardview的所有数据。

   mRecyclerView = (RecyclerView) findViewById(R.id.rv);
       addmore=(Button)findViewById(R.id.addmore_btn);

    final String[] animals = {


    };

    // Intilize an array list from array
    final List<String> animalsList = new ArrayList(Arrays.asList(animals));

    // Define a layout for RecyclerView
    mLayoutManager=(new LinearLayoutManager(this));
    mLayoutManager.setStackFromEnd(true);
    mLayoutManager.setReverseLayout(true);
 //   mLayoutManager = new GridLayoutManager(mContext);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // Initialize a new instance of RecyclerView Adapter instance
    mAdpter = new AddmoreAdpter(this,animalsList);

    // Set the adapter for RecyclerView
    mRecyclerView.setAdapter(mAdpter);
    addmore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Specify the position
            int position = 0;
            String itemLabel = "" + mRandom.nextInt(100);
            animalsList.add(position,"" + itemLabel);
            mAdpter.notifyItemInserted(position);
        mRecyclerView.scrollToPosition(position);

        }
    });

Adpter课程:

public class AddmoreAdpter extends RecyclerView.Adapter<AddmoreAdpter.ViewHolder>{
    private List<String> mDataSet;
//    private Context context;
    private Random mRandom = new Random();
Activity a;
    public AddmoreAdpter(Activity a,List<String> list){
        mDataSet = list;
     //   context = context;
       this.a=a;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView mTextView;
        public ImageButton mRemoveButton;
        public RelativeLayout mRelativeLayout;
        EditText doc_title;
        EditText expiry_date;
        ImageView onclick_id;

        public ViewHolder(View v){
            super(v);
             mTextView = (TextView) v.findViewById(R.id.tv);
             mRemoveButton = (ImageButton) v.findViewById(R.id.ib_remove);
             mRelativeLayout = (RelativeLayout) v.findViewById(R.id.rl);
             doc_title=(EditText)v.findViewById(R.id.document_title);
             expiry_date=(EditText)v.findViewById(R.id.expiry_date_edit);
            onclick_id=(ImageView)v.findViewById(R.id.click_id);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        // Create a new View
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.test,parent,false);
        ViewHolder vh = new ViewHolder(v);
        return vh;


    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position){
        holder.mTextView.setText((String)mDataSet.get(position));
        holder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String animal = mDataSet.get(position);
            }
        });
        holder.mRemoveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Get the clicked item label
                String itemLabel = mDataSet.get(position);
                mDataSet.remove(position);
                notifyItemRemoved(position);

                notifyItemRangeChanged(position,mDataSet.size());

            }
        });

        holder.onclick_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(a, "................", Toast.LENGTH_LONG).show();
            }
        });
    }

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


}

活动xml:

 <android.support.v7.widget.CardView
                android:id="@+id/card_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:clickable="true"
                android:backgroundTint="#ffffff"
                card_view:cardPreventCornerOverlap="false">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp"
                    android:gravity="center">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">


                        <LinearLayout
                            android:id="@+id/upload_text"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@color/colorAccent"

                            >

                            <TextView
                                android:id="@+id/idproof"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Upload Documents"
                                android:padding="10dp"
                                android:textStyle="bold"
                                android:layout_gravity="center"
                                android:textAllCaps="true"
                                android:textSize="16dp"
                                android:textColor="#ffffff"
                                android:layout_weight="1"
                                />


                            <!--<ImageView-->
                            <!--android:layout_width="wrap_content"-->
                            <!--android:layout_height="wrap_content"-->
                            <!--android:background="@drawable/ic_deletebutton"-->
                            <!--android:layout_gravity="center"-->
                            <!--android:layout_marginRight="10dp"-->
                            <!--/>-->
                        </LinearLayout>

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="8dp"
                            android:layout_marginBottom="8dp"
                            android:textColorHint="@color/colorPrimary"
                            >

                            <EditText android:id="@+id/document_title"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:inputType="textEmailAddress"
                                android:hint="Document Title"
                                android:singleLine="true"
                                android:textColorHint="@color/colorPrimary"
                                />
                        </android.support.design.widget.TextInputLayout>
                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="8dp"
                            android:layout_marginBottom="8dp"
                            android:textColorHint="@color/colorPrimary"
                            >
                            <EditText android:id="@+id/expiry_date_edit"

                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:inputType="textEmailAddress"
                                android:hint="Date Of Expiry"
                                android:singleLine="true"
                                android:textColorHint="@color/colorPrimary"
                                />
                        </android.support.design.widget.TextInputLayout>
                        <RelativeLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            >
                            <android.support.v7.widget.RecyclerView
                                android:id="@+id/recycler_view_id"
                                android:layout_width="wrap_content"
                                android:layout_height="fill_parent"
                                android:visibility="visible"
                                />
                            <ImageView
                                android:clickable="true"
                                android:focusable="true"
                                android:id="@+id/click_id"
                                android:layout_alignParentEnd="true"
                                android:layout_width="40dp"
                                android:layout_height="40dp"
                                android:layout_marginTop="20dp"
                                android:layout_marginLeft="10dp"
                                android:layout_marginBottom="10dp"
                                android:background="@drawable/ic_uplodicon"
                                android:layout_alignParentRight="true" />

                        </RelativeLayout>


                    </LinearLayout>

                </RelativeLayout>

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

0 个答案:

没有答案