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