2015-04-02 36 views
0

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

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; 
    } 
} 

但是,当我经过片段致电下面我看不出有任何的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" 
    /> 

这是正确的?

+0

您可以将代码在其中添加ListViewFragment的活动? – Naveed 2015-04-02 19:31:17

+0

我在MainActivity中添加了这行'ListViewFragment listViewFragment = new ListViewFragment();'这不行? – user2661518 2015-04-02 19:34:05

+0

不是。我将添加一个答案 – Naveed 2015-04-02 19:35:44

回答

1

片段需要附加到一个活动才能正常工作。您需要使用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(); 

详情请参阅FragmentManagerFragmentTransaction。您也可以使用replace方法与容器视图,其名称暗示将替换片段。

+0

所以在activity_main.xml中我添加了' <片段机器人:layout_width =“ WRAP_CONTENT” 机器人:layout_height = “WRAP_CONTENT” 机器人:ID =“@ + ID /片段” 机器人:名称= “com.app.fragment.ListViewFragment” 工具:布局= “@布局/ listviewfragment”/> ' – user2661518 2015-04-02 20:09:15

+0

见上文 – Naveed 2015-04-02 20:23:31

1

您需要使用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"/> 

MainActivity:

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

评论我编辑处理XML文件问题我得到inflateException。我需要为ListView添加id吗? – user2661518 2015-04-02 20:12:40

+0

尝试'View view = inflater.inflate(R.layout。listviewfragment,container,false);' – itechevo 2015-04-02 20:39:31

相关问题