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

Android 每次单击CardView按钮时复制该按钮,android,android-linearlayout,cardview,Android,Android Linearlayout,Cardview,我的活动中有一个cardview。它由两个EditTexts、一个图像按钮和一个回收器视图组成。我希望每当我点击实际cardview下方的按钮时,它都应该复制一个以上的cardview。我尝试过使用recycler视图,但没有得到所需的输出,因为我必须发布每个cardview中的所有数据 mRecyclerView = (RecyclerView) findViewById(R.id.rv); addmore=(Button)findViewById(R.id.addmore

我的活动中有一个cardview。它由两个
EditText
s、一个图像按钮和一个回收器视图组成。我希望每当我点击实际cardview下方的按钮时,它都应该复制一个以上的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);

        }
    });
mRecyclerView=(RecyclerView)findViewById(R.id.rv);
addmore=(按钮)findViewById(R.id.addmore\u btn);
最终字符串[]动物={
};
//从数组初始化数组列表
final List animalsList=新的ArrayList(Arrays.asList(animals));
//定义RecyclerView的布局
mLayoutManager=(新的LinearLayoutManager(本));
mLayoutManager.setStackFromEnd(true);
mLayoutManager.setReverseLayout(true);
//mLayoutManager=新的GridLayoutManager(mContext);
mRecyclerView.setLayoutManager(mllayoutmanager);
//初始化RecyclerView适配器实例的新实例
mAdpter=新的addMoreAddter(此,动物列表);
//为RecyclerView设置适配器
mRecyclerView.setAdapter(mAdpter);
addmore.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//指定位置
int位置=0;
字符串itemlab=”“+mRandom.nextInt(100);
添加(位置“+”项标签);
mAdpter.notifyItemInserted(位置);
mRecyclerView.scrollToPosition(位置);
}
});
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();
    }


}
public类addmoreadter扩展了RecyclerView.Adapter{
私有列表数据集;
//私人语境;
private Random mRandom=new Random();
活动a;
public addmoreaddter(活动a,列表){
mDataSet=列表;
//上下文=上下文;
这个a=a;
}
公共静态类ViewHolder扩展了RecyclerView.ViewHolder{
公共文本视图mTextView;
公共图像按钮mRemoveButton;
公共关系布局;
编辑文本文档标题;
编辑文本到期日;
ImageView onclick_id;
公共视图持有者(视图v){
超级(五);
mTextView=(TextView)v.findViewById(R.id.tv);
mRemoveButton=(ImageButton)v.findViewById(R.id.ib_移除);
mrrelativelayout=(RelativeLayout)v.findViewById(R.id.rl);
文档标题=(编辑文本)v.findviewbyd(R.id.document\u title);
到期日=(编辑文本)v.findViewById(R.id.expiration\u date\u edit);
onclick_id=(ImageView)v.findviewbyd(R.id.click_id);
}
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
//创建新视图
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.test,parent,false);
视窗支架vh=新视窗支架(v);
返回vh;
}
@凌驾
公共无效onBindViewHolder(ViewHolder,最终int位置){
holder.mTextView.setText((String)mDataSet.get(position));
holder.mTextView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
字符串animal=mDataSet.get(位置);
}
});
holder.mRemoveButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//获取单击的项目标签
String itemLabel=mDataSet.get(位置);
mDataSet.remove(位置);
已移除(位置)的项目;
notifyItemRangeChanged(位置,mDataSet.size());
}
});
holder.onclick\u id.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(a,“…”,Toast.LENGTH_LONG).show();
}
});
}
@凌驾
public int getItemCount(){
返回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>


添加您尝试过的代码?您在“回收者”视图中遇到了哪些问题?因为这是最好的方法之一。在你的测试中显示一些代码!我的布局与recyclerview配合得很好,但我必须使用摄像头和gallery上传文档,然后如何从适配器类提交数据,因为submit按钮位于activity类中。还有一些方法,如onRequestPermissionsResult、resultactivity正在给出错误显示代码,编辑你的问题并添加代码,否则这个问题很难回答way@Deepakadd您尝试过的代码?在回收者视图中您面临哪些问题?因为这是最好的方法之一。在你的测试中显示一些代码!我的布局与recyclerview配合得很好,但我必须使用摄像头和gallery上传文档,然后如何从适配器类提交数据,因为submit按钮位于activity类中。还有一些方法,如onRequestPermissionsResult、resultactivity正在给出错误显示代码,编辑你的问题并添加代码,否则这个问题很难回答way@Deepak