以编程方式从xml添加cardview

时间:2016-09-09 08:50:33

标签: android xml android-studio android-cardview cardview

我正在编写一个Android应用程序,并且有一个从列表中提取的注释列表,但我想通过在edittext中编写并使用按钮发送它来动态添加它。是否可以添加新的cardView并输入我在edittext中写入的数据? 任何帮助都会得到满足。感谢。

这是我的活动代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/linearLayout">

    <LinearLayout
        android:id="@+id/top_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#ffffff"
        android:orientation="horizontal"
        android:gravity="center_horizontal">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="volver"
                android:id="@+id/backBut"
                android:backgroundTint="#ffffff" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="COMENTARIOS"
                android:id="@+id/ventas"
                android:gravity="center_vertical"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                android:textStyle="bold|italic"
                android:typeface="sans"
                android:textColor="#000000" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/refreshbutton"
                android:layout_marginRight="10dp"
                android:background="@drawable/reload"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />


        </RelativeLayout>
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/reciclador"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_bar"
        android:padding="3dp"
        android:scrollbars="vertical"
        android:layout_marginBottom="50dp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/commentlayout">

        <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#ffffff"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
            <EditText
                android:id="@+id/commenttext"
                android:hint="Escribe tu comentario"
                android:layout_weight="0.9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            />

            <ImageButton
                android:layout_weight="0.1"
                android:id="@+id/sendButton"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_toRightOf="@id/commenttext"
                android:background="@drawable/send" />
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>

为了刷新我的数据,我使用了:

void refresh(List<Comment> commentsList){
        mAdapter = new CommentsAdapter(commentsList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
}

但这不是自动的。我想要像facebook这样的东西。

感谢。

1 个答案:

答案 0 :(得分:-1)

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:card_view="http://schemas.android.com/apk/res-auto"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="2dp"
    card_view:contentPadding="10dp">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <EditText
            android:id="@+id/editext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_alignParentTop="true"/>

        <EditText
            android:id="@+id/edittext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/edittext"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

或在Gradle中添加

compile 'com.android.support:cardview-v7:22.0.0'