web-dev-qa-db-ja.com

フラグメント内のビューを膨張させる方法

フラグメント内のビューを膨らまそうとすると、NULLになります。例えば:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Here I will inflate my view using the layout ID & return.
    return view;
}

ボタンがクリックされるたびに、動的なビューを作成する必要があります(例:button&LinearLayoutに追加します)。次のようにフラグメントクラス内でこの操作を実行したいと思います。

public void addPlaces() {    
    Button button = new Button(null);
    button.setText("button name");
    // e.g. like adding button to enter code here linear layout
    linearLayout.addView(button); 
}

したがって、onCreateView内でLinearLayoutを膨らませて、addクラスで使用すると、NULLになります。達成方法

39
Naruto

変数をインスタンス変数として宣言し、線形レイアウトを初期化します

LinearLayout linearLayout;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment1, container, false);
    linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
    return rootView;
}

それから

public void addPlaces() {
    Button button = new Button(getActivity());
    // needs activity context
    // fragment hosted by a activity. use getActivity() to get the context of the hosting activity. 
    button.setText("button name");
    linearlayout.addView(button);
}

例:要件に応じて以下を変更します。

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent" >

    <Button
        Android:id="@+id/button1"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_alignParentBottom="true"
        Android:layout_centerHorizontal="true"
        Android:text="Button" />

    <LinearLayout
        Android:layout_width="fill_parent"
        Android:id="@+id/linearlayout"
        Android:layout_height="fill_parent"
        Android:layout_above="@+id/button1"
        Android:layout_alignParentTop="true"
        Android:layout_centerHorizontal="true"
        Android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

Myfragment.Java

public class Myfragment extends Fragment {

    LinearLayout linearLayout;
    View rootView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Button b = (Button) rootView.findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                addPlaces();
            }

        });
        linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment1, container, false);
        return rootView;
    }

    public void addPlaces() {
        Button button = new Button(getActivity()); // needs activity context
        button.setText("button name");
        linearLayout.addView(button);
    }
}

エミュレーターのスナップショット

enter image description here

編集:

activity-main.xml

<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:paddingBottom="@dimen/activity_vertical_margin"
    Android:paddingLeft="@dimen/activity_horizontal_margin"
    Android:paddingRight="@dimen/activity_horizontal_margin"
    Android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

     <fragment Android:name="com.example.fragments.Myfragment"
            Android:id="@+id/frag"
            Android:layout_above="@+id/button1"
            Android:layout_width="fill_parent"
            Android:layout_height="fill_parent" />

      <Button
          Android:id="@+id/button1"
          Android:layout_width="wrap_content"
          Android:layout_height="wrap_content"
          Android:layout_alignParentBottom="true"
          Android:layout_centerHorizontal="true"
          Android:text="Button" />

</RelativeLayout>

MainActivity.Java

public class MainActivity extends FragmentActivity {
    Button b;
    Myfragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragment = new Myfragment();
        fragmentTransaction.add(R.id.frag, fragment);
        fragmentTransaction.commit();
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                fragment.addPlaces();
            }

        });
    }
}

Myfragment.Java

public class Myfragment extends Fragment {

    LinearLayout linearLayout;
    View rootView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment1, container, false);
        return rootView;
    }

    public void addPlaces() {
        Button button = new Button(getActivity()); // needs activity context
        button.setText("button name");
        linearLayout.addView(button);
    }
}
66
Raghunandan