Android 为什么只有一个视图添加到片段布局?

Android 为什么只有一个视图添加到片段布局?,android,android-fragments,Android,Android Fragments,所以我试图在我的数据库中添加尽可能多的“玩家”,只是他们的名字。(下面我只是使用了一个简单的for循环,并删除了从数据库调用数据的代码)我的问题是,当我尝试将card\u player.xml添加到for循环中的fragment\u layout.xml时,它只添加了第一项,我不知道为什么。下面显示的是一个卡片视图,其中“0”为mName。它不应该显示5张卡片视图吗 fragment_layout.xml card_player.xml 这是我的fragment.java onCreat

所以我试图在我的数据库中添加尽可能多的“玩家”,只是他们的名字。(下面我只是使用了一个简单的for循环,并删除了从数据库调用数据的代码)我的问题是,当我尝试将
card\u player.xml
添加到for循环中的
fragment\u layout.xml
时,它只添加了第一项,我不知道为什么。下面显示的是一个卡片视图,其中“0”为
mName
。它不应该显示5张卡片视图吗

fragment_layout.xml


card_player.xml


这是我的fragment.java onCreate方法:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.fragment_layout, container, false);
initView();
return rootView;


}
public void initView(){
    LinearLayout mLinearLayoutContainer = (LinearLayout) rootView.findViewById(R.id.mPlayerList);
    LayoutInflater inflater = getActivity().getLayoutInflater();

for(int i = 0; i < 5; i++) {

        View mView = inflater.inflate(R.layout.card_of_player, mLinearLayoutContainer, false);

        TextView mName = (TextView) mView.findViewById(R.id.mName);

        mName.setText(i+""); // just set as name the i variable.

        mLinearLayoutContainer.addView(mView);

    }


}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
rootView=充气机。充气(R.layout.fragment\u布局,容器,false);
initView();
返回rootView;
}
公共视图(){
LinearLayout mLinearLayoutContainer=(LinearLayout)rootView.findViewById(R.id.mPlayerList);
LayoutFlater充气机=getActivity().GetLayoutFlater();
对于(int i=0;i<5;i++){
View mView=充气机。充气(播放器的R.layout.card_,mLinearLayoutContainer,false);
TextView mName=(TextView)mView.findviewbyd(R.id.mName);
mName.setText(i+“”);//只需将i变量设置为名称即可。
mLinearLayoutContainer.addView(mView);
}
}

您的cardLayout具有高度匹配父项,因此第一个选项将覆盖所有屏幕。如果添加滚动视图作为LinearLayout的父视图,则会看到上面的其他视图

更正后的代码为:

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

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="@color/rippelColor"
        card_view:cardCornerRadius="4dp">


            <TextView
                android:id="@+id/mName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Dummy Name" />

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

您的cardLayout具有高度匹配父项,因此第一个选项将覆盖所有屏幕。如果添加滚动视图作为LinearLayout的父视图,则会看到上面的其他视图

更正后的代码为:

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

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="@color/rippelColor"
        card_view:cardCornerRadius="4dp">


            <TextView
                android:id="@+id/mName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Dummy Name" />

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


…该死。。我怎么会错过呢。谢谢,等我有空的时候我会接受答案的。。我怎么会错过呢。谢谢,有空的时候我会接受的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="@color/rippelColor"
        card_view:cardCornerRadius="4dp">


            <TextView
                android:id="@+id/mName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Dummy Name" />

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