单击按钮打开片段?

时间:2014-06-08 00:58:26

标签: java android xml android-fragments

我有一个ViewPager,我可以在其中查看我创建的4个不同的标签(它们是片段)。在我的一个片段中,我想要一个按钮,在我点击该页面上的按钮后,用另一个片段替换该视图。现在我不想滑动到另一个标签,我只想在按下该按钮时替换当前片段的视图。这是我的尝试,但我不确定为什么当我点击我的按钮时,没有任何反应。如果有人能帮助我那会很棒。谢谢!

这是我打电话来创建视图和我要按的按钮。

ManualSelection.java

public class ManualSelection extends Fragment {

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

        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.activity_manual_selection, container, false);
        intent = new Intent(getActivity(), ManualSelectionListView.class);
        return rootView;
    }

    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Button listViewGenerated = (Button) getActivity().findViewById(R.id.manual_generate);
        listViewGenerated.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onClick(View v) {
                FragmentManager fm = getActivity().getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.listView_manual, new ManualSelectionListView());
                ft.commit();
            }

        });
    }
}

这是我要创建的片段

ManualSelectionListView.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ManualSelectionListView extends Fragment {

static final String[] MOBILE_OS =
        new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.listview, container, false);

    ListView lv = (ListView)rootView.findViewById(R.id.listview);
    lv.setAdapter(new MobileArrayAdapter(getActivity(),MOBILE_OS));

    return rootView;
}
}

这是我的XML文件:

activity_manual_selection.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    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="com.ecs160.homestay.ManualSelection"
    android:id="@+id/fragmentTest"> 

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Generate"
    android:id="@+id/manual_generate"
    android:layout_centerHorizontal="true"
    android:onClick="generateListView"/>

这是名为listview.xml的XML文件,其中包含R.id.listView_manual

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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/listView_manual">

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

当我点击&#34; Generate时,我什么都没显示。&#34;有任何想法吗?谢谢!

2 个答案:

答案 0 :(得分:0)

在您的活动中,您应该创建:

public void generateListView(View v){
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.listView_manual, new ManualSelectionListView());
    ft.commit();
}

答案 1 :(得分:0)

问题是您正在尝试从主活动中获取Button id,该ID将返回null

        Button listViewGenerated = (Button) getActivity().findViewById(R.id.manual_generate);

按钮manual_generate位于您的activity_manual_selection布局中,而不在您的主要活动的布局中,您无法从不同的布局获取视图,否则它将返回null

<强>溶液

从片段的布局中获取Button的ID

public class ManualSelection extends Fragment {

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

    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.activity_manual_selection, container, false);
    intent = new Intent(getActivity(), ManualSelectionListView.class);

   Button listViewGenerated = (Button) rootView.findViewById(R.id.manual_generate);
    listViewGenerated.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onClick(View v) {
            FragmentManager fm = getActivity().getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.listView_manual, new ManualSelectionListView());
            ft.commit();
        }

    });

    return rootView;
}

删除onActivityCreated