2015-04-02 48 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