亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android App中使用ListFragment的實例教程

 更新時間:2016年05月11日 11:05:44   作者:聽風十年  
這篇文章主要介紹了Android App中使用ListFragment的實例教程,ListFragment的內(nèi)容是以列表(list)的形式顯示的Fragment,需要的朋友可以參考下

ListFragment繼承于Fragment。因此它具有Fragment的特性,能夠作為activity中的一部分,目的也是為了使頁面設(shè)計更加靈活。
相比Fragment,ListFragment的內(nèi)容是以列表(list)的形式顯示的。ListFragment的布局默認包含一個ListView。因此,在ListFragment對應(yīng)的布局文件中,必須指定一個 android:id 為 “@android:id/list” 的ListView控件!

ListFragment基礎(chǔ)使用
下面介紹在Activity中顯示ListFragment的步驟。

1. Activity對應(yīng)的代碼

public class FragmentTest extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 } 
}

2. Activity對應(yīng)的布局

<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"
 android:orientation="horizontal" >

 <fragment 
  android:name="com.skw.fragmenttest.MyListFragment"
  android:id="@+id/myfragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

</LinearLayout>

說明:該Activity的布局中只包行了一個Fragment。下面看看MyListFragment的內(nèi)容。

3. MyListFragment的內(nèi)容

public class MyListFragment extends ListFragment {
 private static final String TAG = "##MyListFragment##";

 private ListView selfList;

 String[] cities = {
   "Shenzhen",
   "Beijing",
   "Shanghai",
   "Guangzhou",
   "Wuhan",
   "Tianjing",
   "Changsha",
   "Xi'an",
   "Chongqing",
   "Guilin",
 };

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) {
  Log.d(TAG, "onCreateView");
  return inflater.inflate(R.layout.list_fragment, container, false);
 }


 @Override
 public void onCreate(Bundle savedInstanceState) {
  Log.d(TAG, "onCreate");
  super.onCreate(savedInstanceState);
  // 設(shè)置ListFragment默認的ListView,即@id/android:list
  this.setListAdapter(new ArrayAdapter<String>(getActivity(), 
    android.R.layout.simple_list_item_1, cities));

 }

 public void onListItemClick(ListView parent, View v, 
   int position, long id) {
  Log.d(TAG, "onListItemClick");
  Toast.makeText(getActivity(), "You have selected " + cities[position],
    Toast.LENGTH_SHORT).show();
 } 
}

說明:MyListFragment是自定義的ListFragment。它使用了list_fragment.xml作為布局,并通過android.R.layout.simple_list_item_1顯示ListView中的每一項。

4. list_fragment.xml的內(nèi)容

<?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" >

 <!-- ListFragment對應(yīng)的android:id值固定為"@id/android:list" -->
 <ListView
  android:id="@id/android:list"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:drawSelectorOnTop="false"
  />

</LinearLayout>

"Activity的布局以及代碼"和前面一樣,這里就不再重復說明。

5. MyListFragment的內(nèi)容

public class MyListFragment extends ListFragment {
 private static final String TAG = "##MyListFragment##";

 private ListView selfList;

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) {
  Log.d(TAG, "onCreateView");
  return inflater.inflate(R.layout.list_fragment, container, false);
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
  final String[] from = new String[] {"title", "info"};
  final int[] to = new int[] {R.id.text1, R.id.text2};

  Log.d(TAG, "onCreate");
  super.onCreate(savedInstanceState);
  // 建立SimpleAdapter,將from和to對應(yīng)起來
  SimpleAdapter adapter = new SimpleAdapter(
    this.getActivity(), getSimpleData(), 
    R.layout.item, from, to);
  this.setListAdapter(adapter);
 }

 public void onListItemClick(ListView parent, View v, 
   int position, long id) {
  Log.d(TAG, "onListItemClick");
  Toast.makeText(getActivity(), 
    "You have selected " + position,
    Toast.LENGTH_SHORT).show();
 }

 private List<Map<String, Object>> getSimpleData() {
  List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

  Map<String, Object> map = new HashMap<String, Object>();
  map.put("title", "Ferris wheel");
  map.put("info", "Suzhou Ferris wheel");
  list.add(map);

  map = new HashMap<String, Object>();
  map.put("title", "Flower");
  map.put("info", "Roser");
  list.add(map);

  map = new HashMap<String, Object>();
  map.put("title", "Disk");
  map.put("info", "Song Disk");
  list.add(map);

  return list;
 }
}

說明:MyListFragment使用了R.layout.list_fragment作為布局,并且對于ListView中的每一項都使用了R.layout.item作為布局。

6. list_fragment.xml的內(nèi)容

<!-- ListFragment對應(yīng)的android:id值固定為"@id/android:list" -->
<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawSelectorOnTop="false"
    />

7. item.xml的內(nèi)容

<?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/text1"
  android:textSize="12sp"
  android:textStyle="bold"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

 <TextView android:id="@+id/text2"
  android:textSize="24sp"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

</LinearLayout>

ListFragment實例
應(yīng)用實例說明:建立一個activity,包括2個ListFragment。第1個ListFragment采用中ListView每一行的內(nèi)容通過android自帶的android.R.layout.simple_list_item_1布局來顯示;第2個ListFragment每一行的內(nèi)容通過自定義的layout文件來顯示,每一行顯示兩個文本。

activity對應(yīng)的layout文件代碼:

<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"
 android:orientation="horizontal" >

 <fragment 
  android:name="com.skywang.app.ListFragmentImpl"
  android:id="@+id/fragment1" 
  android:layout_weight="1"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

 <fragment 
  android:name="com.skywang.app.ListFragmentSelf"
  android:id="@+id/fragment2" 
  android:layout_weight="1"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

</LinearLayout>

說明:
(01) 該layout布局包含兩個fragment。
activity的代碼:

package com.skywang.app;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;

public class ListFragmentTest extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.list_fragment_test);  
 }
}

說明:
(01) 在 onCreateView()中,調(diào)用list_fragment_impl作為該ListFragment的布局文件。
(02) 在 onCreate()中,通過setListAdapter() 設(shè)置android.R.layout.simple_list_item_1為ListView每一行的布局文件,設(shè)置cities為其中數(shù)據(jù)的每一項內(nèi)容。

ListFragmentImpl.java的代碼:

package com.skywang.app;

import android.app.ListFragment;
import android.widget.ListView; 
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.util.Log;
import android.widget.Toast;
import android.widget.SimpleAdapter;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

public class ListFragmentImpl extends ListFragment{
 private static final String TAG = "ListFragmentImpl";
 
 private ListView selfList;
 
 String[] cities = {
   "Shenzhen",
   "Beijing",
   "Shanghai",
   "Guangzhou",
   "Wuhan",
   "Tianjing",
   "Changsha",
   "Xi'an",
   "Chongqing",
   "Guilin",
 };

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) {
  Log.d(TAG, "onCreateView");
  return inflater.inflate(R.layout.list_fragment_impl, container, false);
 }
 

 @Override
 public void onCreate(Bundle savedInstanceState) {
  Log.d(TAG, "onCreate");
  super.onCreate(savedInstanceState);
  // 設(shè)置ListFragment默認的ListView,即@id/android:list
  this.setListAdapter(new ArrayAdapter<String>(getActivity(), 
    android.R.layout.simple_list_item_1, cities));
  
 }
 
 public void onListItemClick(ListView parent, View v, 
   int position, long id) {
  Log.d(TAG, "onListItemClick");
  Toast.makeText(getActivity(), 
    "You have selected " + cities[position],
    Toast.LENGTH_SHORT).show();
 } 
}

list_fragment_impl.xml的內(nèi)容:

<?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" >
 
 <!-- ListFragment對應(yīng)的android:id值固定為"@id/android:list" -->
 <ListView
  android:id="@id/android:list"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:drawSelectorOnTop="false"
  />
 
</LinearLayout>

ListFragmentSelf.java的代碼:

package com.skywang.app;

import android.app.ListFragment;
import android.widget.ListView; 
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.util.Log;
import android.widget.Toast;
import android.widget.SimpleAdapter;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

public class ListFragmentSelf extends ListFragment{
 private static final String TAG = "ListFragmentImpl";
 
 private ListView selfList;
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) {
  Log.d(TAG, "onCreateView");
  return inflater.inflate(R.layout.list_fragment_self, container, false);
 }
 

 @Override
 public void onCreate(Bundle savedInstanceState) {
  final String[] from = new String[] {"title", "info"};
  final int[] to = new int[] {R.id.text1, R.id.text2};
  
  Log.d(TAG, "onCreate");
  super.onCreate(savedInstanceState);
  // 建立SimpleAdapter,將from和to對應(yīng)起來
  SimpleAdapter adapter = new SimpleAdapter(
    this.getActivity(), getSimpleData(), 
    R.layout.two_textview, from, to);
  this.setListAdapter(adapter);
 }
 
 public void onListItemClick(ListView parent, View v, 
   int position, long id) {
  Log.d(TAG, "onListItemClick");
  Toast.makeText(getActivity(), 
    "You have selected " + position,
    Toast.LENGTH_SHORT).show();
 }
 
 private List<Map<String, Object>> getSimpleData() {
  List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("title", "Ferris wheel");
  map.put("info", "Suzhou Ferris wheel");
  list.add(map);

  map = new HashMap<String, Object>();
  map.put("title", "Flower");
  map.put("info", "Roser");
  list.add(map);

  map = new HashMap<String, Object>();
  map.put("title", "Disk");
  map.put("info", "Song Disk");
  list.add(map);
  
  return list;
 }
}

說明:

(01) 在 onCreateView()中,調(diào)用list_fragment_self作為該ListFragment的布局文件。
(02) 在 onCreate()中,通過setListAdapter() 設(shè)置R.layout.two_textview為ListView每一行的布局文件。


list_fragment_self.xml的內(nèi)容:

<?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" >
 
 <!-- ListFragment對應(yīng)的android:id值固定為"@id/android:list" -->
 <ListView
  android:id="@id/android:list"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:drawSelectorOnTop="false"
  />
 
</LinearLayout>

two_textview.xml的內(nèi)容:

<?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/text1"
  android:textSize="12sp"
  android:textStyle="bold"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

 <TextView android:id="@+id/text2"
  android:textSize="24sp"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>
  
</LinearLayout>

效果圖:

2016511110549079.png (400×683)

相關(guān)文章

  • Android Jni的簡單使用詳解

    Android Jni的簡單使用詳解

    這篇文章主要介紹了Android Jni的簡單使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別

    淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別

    這篇文章主要介紹了淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別 ,getMeasuredWidth()獲取的是view原始的大小,getWidth()獲取的是這個view最終顯示的大小,具體區(qū)別介紹大家參考下本文
    2018-04-04
  • Android圖片加載利器之Picasso源碼解析

    Android圖片加載利器之Picasso源碼解析

    這篇文章主要為大家詳細解析了Android圖片加載利器之Picasso源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android使用Messenger實現(xiàn)service與activity交互

    Android使用Messenger實現(xiàn)service與activity交互

    這篇文章主要介紹了android使用Messenger實現(xiàn)service與activity交互的相關(guān)資料,需要的朋友可以參考下
    2016-06-06
  • Android編譯的注意事項

    Android編譯的注意事項

    今天小編就為大家分享一篇關(guān)于Android編譯的注意事項,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Android應(yīng)用?;顚嵺`詳解

    Android應(yīng)用?;顚嵺`詳解

    這篇文章主要介紹了Android應(yīng)用?;顚嵺`詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • Android創(chuàng)建Alert框的方法

    Android創(chuàng)建Alert框的方法

    這篇文章主要介紹了Android創(chuàng)建Alert框的方法,實例分析了Android創(chuàng)建alert彈出窗口的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • Android使用自定義屬性實現(xiàn)圖片自動播放滾動的功能

    Android使用自定義屬性實現(xiàn)圖片自動播放滾動的功能

    這篇文章主要介紹了Android使用自定義屬性實現(xiàn)圖片自動播放滾動的功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Android自定義view之太極圖的實現(xiàn)教程

    Android自定義view之太極圖的實現(xiàn)教程

    這篇文章主要給大家介紹了關(guān)于Android自定義view之太極圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • flutter 微信聊天輸入框功能實現(xiàn)

    flutter 微信聊天輸入框功能實現(xiàn)

    這篇文章主要介紹了flutter 微信聊天輸入框功能實現(xiàn),本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03

最新評論