您的位置:首页 > 编程语言

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 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息