ListFragment代码案例
2016-07-01 13:38
337 查看
由于以前是在新浪发过的版本,所以代码片段会有的有异常,大家尽量看一下,在此声明一下。
01 public class MainActivity extends Activity {
02
03 @Override
04 protected void onCreate(Bundle savedInstanceState) {
05 super.onCreate(savedInstanceState);
06 setContentView(R.layout.activity_main);
07 }
08 }
09
10
11
12 public class MyFragment extends ListFragment {
13
14 private String[] datas = {"小奇","小强","小贝","小花"};
15 private ArrayAdapter<</span>String> adapter;
16 @Override
17 public View onCreateView(LayoutInflater inflater, ViewGroup container,
18 Bundle savedInstanceState) {
19 View view = inflater.inflate(R.layout.listfragment, container, false);
20 adapter = new ArrayAdapter<</span>String>(getActivity(),android.R.layout.simple_list_item_1,datas);
21 setListAdapter(adapter);
22 return view;
23 }
24
25 @Override
26 public void onListItemClick(ListView l, View v, int position, long id) {
27 // TODO Auto-generated method stub
28 Toast.makeText(getActivity(), "您选择的是"+datas[position], Toast.LENGTH_LONG).show();
29 super.onListItemClick(l, v, position, id);
30 }
布局文件
Activity布局
01 xmlns:android="http://schemas.android.com/apk/res/android"
02 xmlns:tools="http://schemas.android.com/tools"
03 android:layout_width="match_parent"
04 android:layout_height="match_parent"
05 tools:context=".MainActivity" >
06
07
08 android:name="com.xiaobei.day13course01.MyFragment"
09 android:id="@+id/myfrag"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 />
13
14
Fragment布局
17
18 xmlns:android="http://schemas.android.com/apk/res/android"
19 xmlns:tools="http://schemas.android.com/tools"
20 android:layout_width="match_parent"
21 android:layout_height="match_parent"
22 tools:context=".MainActivity" >
23
24
25 android:id="@android:id/list"
26 android:layout_width="match_parent"
27 android:layout_height="wrap_content"
28 />
29
30 android:id="@android:id/empty"
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:layout_centerInParent="true"
34 android:text="暂无数据" />
35
36
01 public class MainActivity extends Activity {
02
03 @Override
04 protected void onCreate(Bundle savedInstanceState) {
05 super.onCreate(savedInstanceState);
06 setContentView(R.layout.activity_main);
07 }
08 }
09
10
11
12 public class MyFragment extends ListFragment {
13
14 private String[] datas = {"小奇","小强","小贝","小花"};
15 private ArrayAdapter<</span>String> adapter;
16 @Override
17 public View onCreateView(LayoutInflater inflater, ViewGroup container,
18 Bundle savedInstanceState) {
19 View view = inflater.inflate(R.layout.listfragment, container, false);
20 adapter = new ArrayAdapter<</span>String>(getActivity(),android.R.layout.simple_list_item_1,datas);
21 setListAdapter(adapter);
22 return view;
23 }
24
25 @Override
26 public void onListItemClick(ListView l, View v, int position, long id) {
27 // TODO Auto-generated method stub
28 Toast.makeText(getActivity(), "您选择的是"+datas[position], Toast.LENGTH_LONG).show();
29 super.onListItemClick(l, v, position, id);
30 }
布局文件
Activity布局
01 xmlns:android="http://schemas.android.com/apk/res/android"
02 xmlns:tools="http://schemas.android.com/tools"
03 android:layout_width="match_parent"
04 android:layout_height="match_parent"
05 tools:context=".MainActivity" >
06
07
08 android:name="com.xiaobei.day13course01.MyFragment"
09 android:id="@+id/myfrag"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 />
13
14
Fragment布局
17
18 xmlns:android="http://schemas.android.com/apk/res/android"
19 xmlns:tools="http://schemas.android.com/tools"
20 android:layout_width="match_parent"
21 android:layout_height="match_parent"
22 tools:context=".MainActivity" >
23
24
25 android:id="@android:id/list"
26 android:layout_width="match_parent"
27 android:layout_height="wrap_content"
28 />
29
30 android:id="@android:id/empty"
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:layout_centerInParent="true"
34 android:text="暂无数据" />
35
36
相关文章推荐
- 10个C#程序员经常用到的实用代码片段
- 46 个非常有用的 PHP 代码片段
- 直接拿来用的15个jQuery代码片段
- 12个超实用的JQuery代码片段
- 程序员必知35个jQuery 代码片段
- 分享12个实用的jQuery代码片段
- 一些实用的jQuery代码片段收集
- 非常实用的12个jquery代码片段
- 10个很棒的jQuery代码片段
- 9个比较实用的php代码片段
- 7个有用的jQuery代码片段分享
- 6个超实用的PHP代码片段
- 必须收藏的23个php实用代码片段
- 又十个超级有用的PHP代码片段
- 必须收藏的php实用代码片段
- 9个实用的PHP代码片段分享
- 分享8个最佳的代码片段在线测试网站
- JavaScript的漂亮的代码片段
- 你一定会收藏的Nodejs代码片段
- 18个非常棒的jQuery代码片段