ListView 适配器未通过 Fragment 调用

ListView adapter not getting called through Fragment

当我通过 MainActivity 调用 ListViewAdpater 时,它工作正常。但是如果我创建 ListViewFragment 它不会被调用。我在 MainActivity 中添加了 ListViewFragment listViewFragment = new ListViewFragment();。我有 ListViewAdapter 作为

public class ListViewAdapter extends ArrayAdapter<String> {
    public ListViewAdapter(Context context, String[] sensorArray) {
        super(context, R.layout.listview_adapter, sensorArray);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View view = layoutInflater.inflate(R.layout.listview_adapter, parent, false);
        ImageView imageView = (ImageView)  view.findViewById(R.id.imageView);
        TextView textView = (TextView) view.findViewById(R.id.textView);
        imageView.setImageResource(R.drawable.image);
        textView.setText(getItem(position));
        return view;
    }
}

但是当我如下调用 Fragment 时,我没有看到任何 listView

public class ListViewFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_main, container, false);

        String[] sensorArray = getResources().getStringArray(R.array.sensor_array);

        ListView listView = (ListView) view.findViewById(R.id.listView);
        listView.setAdapter(new ListViewAdapter(getActivity(),sensorArray));
        return view;
    }
}

当我 运行 调试器调用 ListViewFragment 但它没有进入 onCreate 方法

我更改了 xml 个文件如下 所以在 activity_main.xml 中我添加了`

<fragment  android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fragment"
    android:name="com.app.fragment.ListViewFragment"
    tools:layout="@layout/listviewfragment"/>

and created listviewfragment.xml as'

<ListView 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"
    />

这样对吗?

片段需要附加到 activity 才能运行。您需要使用 xml(静态)或使用片段管理器(动态)将片段添加到 activity。

静态地,您可以通过将其放入 activity 的布局文件中来简单地添加片段:

<fragment android:name="com.example.android.fragments.MyFragment"
              android:id="@+id/my_fragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />

android:name 应该指向您的片段 class。

使用片段管理器,您可以按如下方式添加片段: 在你的 activity 的 onCreate() 中使用这个:

    ListViewFragment listViewFragment = new ListViewFragment();
    getFragmentManager().beginTransaction().add(R.id.container_view, listViewFragment).commit();

有关详细信息,请参阅 FragmentManager and FragmentTransaction。您也可以对容器视图使用 replace 方法,顾名思义,它将替换片段。

您需要使用 FragmentTransaction 并且必须在主 activity 布局中包含 FrameLayout 元素。

activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"/>

主要活动:

FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container,new ListViewFragment(), "sensorArray");
ft.commit();