0

我想知道。有没有办法通过按下按钮将 CardViews 添加到 RecyclerView?

我的意思是我可以用这个 XML 添加一个 CardView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cardrl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$MainFragment">
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:foreground="?android:attr/selectableItemBackground"
    android:id="@+id/CardView_1"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:elevation="2dp">
    <TextView
        android:layout_gravity="top|right"
        android:text="@string/card_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</android.support.v7.widget.CardView>

并让它在创建之前我可以用数据填充 TextView 吗?

我知道很多人会说做这样的事情:

RelativeLayout rl = (RelativeLayout) findById(R.id.rl);

LayoutInflater layoutInflater = (LayoutInflater) 
    this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) ); 

但由于某种原因它不起作用,因为它说Cannot resolve method Inflate(int, activity, boolean)

任何帮助是极大的赞赏!谢谢!

4

1 回答 1

0

那是因为没有inflate(int, activity, boolean)方法之类的东西。它应该是inflate(int resId, Viewgroup parentContainer, boolean attachToParent)

于 2014-08-26T00:20:21.803 回答