• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

Android

Android之Fragment--ListFragment的应用

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/Android/2019/0926/660.html

 
App开发培训申明】 
 
 
 
 
 
【正文】
 
 
 
一、ListFragement的说明:
 
 
 
ListFragment秉承于Fragment。因此它具备Fragment的特性,能够作为activity中的一片面,目标也是为了使页面计划加倍天真。相比Fragment,ListFragment的内容因此列表(list)的形式表现的。
 
 
 
1、ListFragment结构:
 
 
 
ListFragment的默认结构包含一个list view。因此,在ListFragment对应的结构文件中,必需指定一个 android:id 为 “@android:id/list” 的ListView控件! 若用户想点窜listview,可以在onCreateView(LayoutInflater, ViewGroup, Bundle)中进行点窜。固然,用户也能够在ListFragment的结构中包含其它的控件。
 
 
 
下面是官方文档中ListFragment对应的一个layout示例:
 
 
 
复制代码
 
<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
     android:orientation="vertical"
 
     android:layout_width="match_parent"
 
     android:layout_height="match_parent"
 
     android:paddingLeft="8dp"
 
     android:paddingRight="8dp">
 
 
 
    <ListView android:id="@id/android:list"
 
           android:layout_width="match_parent"
 
           android:layout_height="match_parent"
 
           android:background="#00FF00"
 
           android:layout_weight="1"
 
           android:drawSelectorOnTop="false"/>
 
 
 
    <TextView android:id="@id/android:empty"
 
           android:layout_width="match_parent"
 
           android:layout_height="match_parent"
 
           android:background="#FF0000"
 
           android:text="No data"/>
 
</LinearLayout>
 
复制代码
 
ListView中每一行的表现内容,是通过配置适配器ListAdapter来实现的。我们既可以自定义,也能够接纳体系默认的layout。背面的运用实例中,会分别枚举2种环境下的表现
 
 
 
2、绑定命据:
 
 
 
ListFragment绑定ListView的数据(即绑定适配器)时,必需通过ListFragment.setListAdapter()接口来绑定命据,而不是应用ListView.setAdapter() 或其它方法
 
 
 
 
 
 
 
二、通过ArrayAdapter来加载ListFragment的举例:
 
 
 
【举例】当今常州网站开发培训将平板计算机分成三片面:点击左侧的按钮,出现中心的消息题目列表(ListFragment),点击中心ListFragment的某个item,在非常右侧的fragment中表现细目。
 
 
 
新建工程文件m01_ListFragment01:
 
 
 
(1)定义activity_main.xml的结构:
 
 
 
activity_main.xml的代码如下:
 
 
 
复制代码
 
<LinearLayout 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"
 
    tools:context=".MainActivity" >
 
 
 
    <LinearLayout
 
        android:id="@+id/left"        
 
        android:layout_width="0dp"
 
        android:layout_height="match_parent"
 
        android:layout_weight="1"
 
        android:background="#cccccc"
 
        android:orientation="horizontal" >
 
 
 
        <Button
 
            android:id="@+id/button1"
 
            android:layout_width="match_parent"
 
            android:layout_height="wrap_content"
 
            android:textSize="14sp"
 
            android:text="show ListFragment" />
 
    </LinearLayout>
 
 
 
    <LinearLayout
 
        android:id="@+id/center"
 
        android:layout_width="0dp"
 
        android:layout_height="match_parent"
 
        android:layout_weight="2"
 
        android:background="#AFEEEE"
 
        android:orientation="vertical" >
 
    </LinearLayout>
 
 
 
    <LinearLayout
 
        android:id="@+id/center"
 
        android:layout_width="0dp"
 
        android:layout_height="match_parent"
 
        android:layout_weight="2"
 
        android:background="#00FFFF"
 
        android:orientation="vertical" >
 
    </LinearLayout>
 
 
 
</LinearLayout>
 
复制代码
 
现实上分派了三个线性结构,左侧表现按钮,中心表现题目,右侧表现细目。这个结构文件对应的可视化界面如下:
 
 
 
 
 
 
 
(2)定义中心的ListFragment,即新建文件ArticleListFragment.java:
 
 
 
ArticleListFragment.java的代码如下:
 
 
 
复制代码
 
 1 package com.example.m01_listfragment01;
 
 2 
 
 3 import java.util.ArrayList;
 
 4 import java.util.List;
 
 5 
 
 6 import android.app.ListFragment;
 
 7 import android.os.Bundle;
 
 8 import android.view.LayoutInflater;
 
 9 import android.view.View;
 
10 import android.view.ViewGroup;
 
11 import android.widget.ArrayAdapter;
 
12 
 
13 public class ArticleListFragment extends ListFragment {
 
14 
 
15     private ArrayAdapter<String> adapter;
 
16 
 
17     @Override
 
18     public void onCreate(Bundle savedInstanceState) {
 
19         // TODO Auto-generated method stub
 
20         super.onCreate(savedInstanceState);
 
21         
 
22         //定义一个数组
 
23         List<String> data = new ArrayList<String>();
 
24         for (int i = 0; i < 30; i++) {
 
25             data.add("smyh" + i);
 
26         }
 
27         //将数组加到ArrayAdapter中心
 
28         adapter = new ArrayAdapter<String>(getActivity(),
 
29                 android.R.layout.simple_list_item_1, data);
 
30         //绑定适配器时,必需通过ListFragment.setListAdapter()接口,而不是ListView.setAdapter()或其它方法
 
31         setListAdapter(adapter);
 
32     }
 
33 
 
34     @Override
 
35     public View onCreateView(LayoutInflater inflater, ViewGroup container,
 
36             Bundle savedInstanceState) {
 
37         // TODO Auto-generated method stub
 
38         return super.onCreateView(inflater, container, savedInstanceState);
 
39     }
 
40 
 
41     @Override
 
42     public void onPause() {
 
43         // TODO Auto-generated method stub
 
44         super.onPause();
 
45     }
 
46 }
 
复制代码
 
焦点代码是22至32行:我们让这个Fragment秉承ListFragment,而后在onCreate()方法中定义一个ArrayAdapter,将数据放进入,非常后绑定适配器就行了。需求留意的是,因为我们秉承的是ListFragment,这个Fragment默认自带了一个结构,以是我们不需求从新新建结构文件了。
 
 
 
(3)将中心的ListFragment加载到Activity中心去。当我们点击按钮时,就开始加载这个Fragment:
 
 
 
MainActivity.java的代码如下:
 
 
 
复制代码
 
 1 package com.example.m01_listfragment01;
 
 2 
 
 3 import android.app.Activity;
 
 4 import android.app.FragmentManager;
 
 5 import android.app.FragmentTransaction;
 
 6 import android.os.Bundle;
 
 7 import android.view.Menu;
 
 8 import android.view.View;
 
 9 import android.view.View.OnClickListener;
 
10 import android.widget.Button;
 
11 
 
12 public class MainActivity extends Activity {
 
13 
 
14     private FragmentManager manager;
 
15     private FragmentTransaction transaction;
 
16     @Override
 
17     protected void onCreate(Bundle savedInstanceState) {
 
18         super.onCreate(savedInstanceState);
 
19         setContentView(R.layout.activity_main);
 
20         Button button = (Button) findViewById(R.id.button1);
 
21         button.setOnClickListener(new OnClickListener() {
 
22             
 
23             //点击按钮,加载ListFragment
 
24             @Override
 
25             public void onClick(View v) {
 
26                 // TODO Auto-generated method stub
 
27                 manager = getFragmentManager();
 
28                 transaction = manager.beginTransaction();
 
29                 ArticleListFragment articleListFragment = new ArticleListFragment();
 
30                 transaction.add(R.id.center, articleListFragment, "article");
 
31                 transaction.co毫米it();
 
32             }
 
33         });
 
34 
 
35     }
 
36 
 
37     @Override
 
38     public boolean onCreateOptionsMenu(Menu menu) {
 
39         // Inflate the menu; this adds items to the action bar if it is present.
 
40         getMenuInflater().inflate(R.menu.main, menu);
 
41         return true;
 
42     }
 
43 }
 
复制代码
 
这个代码对照简单,就未几注释了。
 
 
 
当今运转程序,初始界面如下:
 
 
 
 
 
 
 
点击左侧的按钮后,表现如下:
 
 
 
 
 
 
 
注:要是想实现:点击中心的某个item,弹出吐司表现那个item中的内容,可以在上方的ArticleListFragment.java中的监听事件里增加如下代码:
 
 
 
(代码放置的位置是:让它和Fragment的性命周期方法并列就行了)
 
 
 
复制代码
 
1     @Override
 
2     public void onListItemClick(ListView l, View v, int position, long id) {
 
3         // TODO Auto-generated method stub
 
4         super.onListItemClick(l, v, position, id);
 
5         String item = adapter.getItem(position);
 
6         Toast.makeText(getActivity(), item, 1).show();
 
7     } 
 
复制代码
 
由此我们常州软件技术培训可以看到,监听事件的函数为onListItemClick(),可以干脆写,不需求set。
 
 
 
这内部环节代码在第05行,通过getItem()汲取那个item,而后用字符串来汲取。
 
 
 
我们先去掉这片面的监听事件代码,继续往下看。
 
 
 
(4)点击中心ListFragment的item,加载右侧的DetailFragment:
 
 
 
我们在中心ListFragment中增加一个按钮的监听事件,监听事件的函数为onListItemClick(),ArticleListFragment.java在上头代码的底子之上,增加的代码如下:
 
 
 
(代码放置的位置是:让它和Fragment的性命周期方法并列就行了)
 
 
 
复制代码
 
 1     //点击按钮,加载非常右侧的Fragment
 
 2     @Override
 
 3     public void onListItemClick(ListView l, View v, int position, long id) {
 
 4         // TODO Auto-generated method stub
 
 5         super.onListItemClick(l, v, position, id);
 
 6         
 
 7         //点击按钮后,加载右侧的Fragment
 
 8         FragmentManager manager = getFragmentManager();
 
 9         FragmentTransaction transaction = manager.beginTransaction();
 
10         DetailFragment detailFragment = new DetailFragment();
 
11         //记着:这个处所必需用replace,而不是用add
 
12         transaction.replace(R.id.right, detailFragment, "detailFragment");
 
13         
 
14         //将中心的item的内容放到Bundle工具中心,而后放到非常右侧Frament的参数中心
 
15         String item = adapter.getItem(position);
 
16         Bundle args = new Bundle();
 
17         args.putString("item",item);
 
18         detailFragment.setArguments(args);
 
19         //Toast.makeText(getActivity(), item, 1).show();        
 
20         
 
21         transaction.co毫米it();
 
22     }
 
复制代码
 
上头的代码中,我们是在中心的Fragment中点击按钮,而后加载右侧的Fragment,而后要留意14至18行的焦点代码,看一下它是若何通过bundle来传递数据的。
 
 
 
需求留意的是,第12行代码必需用replace的方法加载右侧的fragment,而不是add;要是用add,运转的错误稍后将展现出来。
 
 
 
(5)定义右侧的DetailFragment:
 
 
 
先定义结构文件,在内部加一个TextView,fragment_detail.xml的代码如下:
 
 
 
复制代码
 
<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
    android:layout_width="match_parent"
 
    android:layout_height="match_parent"
 
    android:orientation="vertical" >
 
 
 
    <TextView
 
        android:id="@+id/textView1"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:text="TextView" />
 
</LinearLayout>
 
复制代码
 
而后新建文件,DetailFragment.java的代码如下: 
 
 
 
复制代码
 
 1 package com.example.m01_listfragment01;
 
 2 
 
 3 import android.app.Fragment;
 
 4 import android.os.Bundle;
 
 5 import android.view.LayoutInflater;
 
 6 import android.view.View;
 
 7 import android.view.ViewGroup;
 
 8 import android.widget.TextView;
 
 9 
 
10 public class DetailFragment extends Fragment {
 
11 
 
12 
 
13     @Override
 
14     public void onCreate(Bundle savedInstanceState) {
 
15         // TODO Auto-generated method stub
 
16         super.onCreate(savedInstanceState);
 
17     }
 
18 
 
19     @Override
 
20     public View onCreateView(LayoutInflater inflater, ViewGroup container,
 
21             Bundle savedInstanceState) {
 
22         // TODO Auto-generated method stub
 
23         View view = inflater.inflate(R.layout.fragment_detail, null);
 
24         TextView textView = (TextView)view.findViewById(R.id.textView1);
 
25         textView.setText(""+getArguments().getString("item"));
 
26         return view;
 
27     }
 
28 
 
29     @Override
 
30     public void onPause() {
 
31         // TODO Auto-generated method stub
 
32         super.onPause();
 
33     }    
 
34 }
 
复制代码
 
焦点代码是第25行,周密看一下我们是怎么通过键值对来拿到中心的Fragment传递过来的item的内容。
 
 
 
当今运转程序,一次点击左边的按钮和中心的item,结果如下:
 
 
 
 
 
 
 
要是我们在中心的Fragment中错误地通过add方法加载右侧的Fragment,而不是通过replace方法,非常终错误的结果如下:
 
 
 
 
 
 
 
也即是说,每点击一次中心的item,就会在右侧继续加载一个文本,而不是替代的方法,很鲜明,这种方法不是我们常州平台运营想要的。
 
 
 
 
 

上篇:上一篇:Android微信抢红包插件说明和开辟实现
下篇:下一篇:AccessibilityService实现自动遍历点赞功效