如何动态地向片段添加视图?

时间:2018-10-26 02:48:50

标签: java android android-fragments android-fragmentactivity

  

我正在尝试向左片段添加动态视图。代码如下所示:

public class WordFragmentActivity extends AppCompatActivity {
TextView tab1,tab2;
FrameLayout container;
Databasehelper databasehelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_word_fragment);
    tab1=findViewById(R.id.tab1);
    tab2=findViewById(R.id.tab2);
    tab2.setBackgroundColor(Color.WHITE);
    tab1.setBackgroundResource(R.drawable.customborder);


getSupportFragmentManager().beginTransaction().replace(R.id.wcontainer,new 
LeftFragment()).commit();
    container=findViewById(R.id.container);
    databasehelper=new Databasehelper(this);
    displayword_info();

}

/* *container=findViewById(R.id.container);* with this line i am trying to link *container* to the container that is present inside the leftfragment */

public void TabClick(View view)
{
    if (view.getId() == R.id.tab1)
    {
        tab2.setBackgroundColor(Color.WHITE);
        tab1.setBackgroundResource(R.drawable.customborder);

        getSupportFragmentManager().beginTransaction().
        replace(R.id.wcontainer,new LeftFragment()).commit();
        displayword_info();
    }
    else 
    {
        tab1.setBackgroundColor(Color.WHITE);
        tab2.setBackgroundResource(R.drawable.customborder);

        getSupportFragmentManager().beginTransaction().
        replace(R.id.wcontainer,new RightFragment()).commit();
    }
}
public void displayword_info()
{

    container.removeAllViews();
    ArrayList<WordInfo> list=databasehelper.getword_info();

    for(int i=0;i<list.size();i++)
    {
        final WordInfo info=list.get(i);
        View view= LayoutInflater.from(getApplicationContext()).
                   inflate(R.layout.cardfront_layout,null);

        TextView wordfront=view.findViewById(R.id.wordtext);

        wordfront.setText(info.word);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
            Intent i=new Intent(getApplicationContext(),
                                FlippingActivity.class);
            i.putExtra("id",info.id);
            startActivity(i);
            }
        });
        container.addView(view);



    }
}

}

问题是,当我在手机上运行该应用程序时,该应用程序会自动关闭。在 displayword_info() 内,我正在尝试向容器添加视图认为我在那里做错了。 这是WordFragmentActivity的xml代码:

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tab1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:onClick="TabClick"
        android:text="Tab1"
        android:textSize="20dp" />

    <View
        android:layout_width="1dp"
        android:layout_height="25dp"
        android:background="#333" />

    <TextView
        android:id="@+id/tab2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:onClick="TabClick"
        android:text="Tab2"
        android:textSize="20dp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#333" />
    <FrameLayout
        android:id="@+id/wcontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    </LinearLayout>

这是左片段的xml代码:

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

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

    <LinearLayout
        android:layout_marginTop="0dp"
        android:orientation="vertical"
        android:id="@+id/container"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        >

    </LinearLayout>

</ScrollView>

</RelativeLayout>

0 个答案:

没有答案