Use ListView in fragment:
1 Create a list view in fragment.xml (You must create the id for it):
在fragment的xml文件中創建一個<ListView>
,賦上id
<ListView
android:id="@+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
while the fragment must have the <FrameLayout>
to be the root view:
fragment的根視圖要是<FrameLayout>
<FrameLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.sunshine.MainActivityFragment"
tools:showIn="@layout/activity_main">
<ListView
android:id="@+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</FrameLayout>
2 Create a new xml file (Such as:list_item_forecast) to contain the textview (show the data).
It will offer a space to adapter in order to generate listview automatically.
For example(You must create the id for it):
創建一個新的xml文件(比如 list_item_forecast) 包含 <TextView>
(賦id)
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:id="@+id/list_item_forecast_textiew"/>
3 Create a arraylist to contain the data.
創建一個數組列表ArrayList
String [] forecastArray = {
"Today-Sunny-88/63",
"Tomorrow-Foggy-70/46",
"Weds-Cloudy-72/63",
"Thurs-Rainy-64/51",
"Fri-Foggy-70/46",
"Sat-Sunny-76/68"
};
// Assign to the ArrayList
List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
**4 Initialize the adapter.
設定轉換器**
// Create the ArrayAdapter to set the parameter
// It will take data from the resource and populate the ListView
ArrayAdapter<String> mForecaseAdapter =
new ArrayAdapter <String> (
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textiew,
weekForecast
);
5 Set the adapter to listview.
將轉換器配給ListView
/*
* Calling findViewById() on the Activity object will only work if
* the current Activity layout is set by setContentView().
* You need a View object of the layout to call the findViewById() in not MainActivity
*/
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView weather = (ListView) rootView.findViewById(R.id.listview_forecast);
weather.setAdapter(mForecaseAdapter);
So, in the public onCreateView(), the return statement becomes: return rootView.
findViewById()這個方法在非MainActivity裏面不能調用,只有當前佈局中設置了setContentView()的纔可以調用。所以我們需要一個視圖對象來調用
然後把返回值直接改成rootView就好了。
關於轉換器(不知道中文書翻譯是什麼樣,我自己叫它轉換器):
原始數據需要先傳給轉換器,轉換器會將原始數據進行排列,當listview發出相應請求時,再合適的放置在listview裏面。如圖
設定轉換器:
它有四個參數 (parameters):
1. 環境 (context)
2. 列表項佈局的id (ID of list item layout)
3. 文本視圖的id (ID of text view)
4. 一列數據 (list of data)
如圖:
Reference: https://www.udacity.com/course/viewer#!/c-ud853/l-1395568821/e-1395668598/m-1402878728