未通过Fragment调用ListView适配器



当我通过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调用时,我看不到任何列表查看

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

这是对的吗?

片段需要附加到活动才能发挥作用。您需要使用xml(静态)或使用片段管理器(动态)将片段添加到活动中。

静态地,您可以简单地添加片段,方法是将其放在活动的布局文件中:

<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应该指向您的片段类。

使用片段管理器,您可以按如下方式添加片段:在您的活动的onCreate()中,只需使用以下内容:

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

有关更多详细信息,请参阅FragmentManager和FragmentTransaction。您也可以将replace方法与容器视图一起使用,顾名思义,容器视图将替换片段。

您需要使用FragmentTransaction,并且必须在主活动布局中具有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();

最新更新