在Fragment中添加ListView
在Fragment中,几个主要生命周期的创建过程
onCreate 创造页面
onCreateView 创造View
onStart 开始
三种获取LayoutInflater的方式
LayoutInflater inflater=LayoutInflater.from(getContext());
LayoutInflater inflater=getLayoutInflater();
LayoutInflater localinflater =
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
其中的onCreate负责创建Fragment的View,必须要进行重写
将页面的初始化放在onCreateView中
示例代码:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("TAG","onCreateView"); //创建界面MsgFragment contentView=inflater.inflate(R.layout.fragment_msg,container,false); //界面的初始化 myListAdapter=new MyListAdapter(); ListView listView=(ListView)contentView.findViewById(R.id.id_listview_msgfragment); listView.setAdapter(myListAdapter); return contentView; }
重写方法onStart()
@Override public void onStart() { Log.d("TAG","onStart"); demo(); super.onStart(); } /////////////////////添加数据 private void demo() { listData.add(new Con("日前,国家卫健委有关负责人表示,我国正采取多")); listData.add(new Con("日前,国家卫健委有关负责人表示,我国正采取多")); listData.add(new Con("日前,国家卫健委有关负责人表示,我国正采取多")); listData.add(new Con("日前,国家卫健委有关负责人表示,我国正采取多")); listData.add(new Con("日前,国家卫健委有关负责人表示,我国正采取多")); }
MsgFragment.java
1 package com.example.senior0302_test2; 2 3 4 import android.os.Bundle; 5 import android.support.annotation.Nullable; 6 import android.support.v4.app.Fragment; 7 import android.util.Log; 8 import android.view.LayoutInflater; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.widget.BaseAdapter; 12 import android.widget.ListView; 13 import android.widget.TextView; 14 15 import java.util.ArrayList; 16 17 18 /** 19 * A simple {@link Fragment} subclass. 20 */ 21 public class MsgFragment extends Fragment { 22 23 ArrayList<Con>listData=new ArrayList<>(); 24 MyListAdapter myListAdapter; 25 View contentView; 26 27 28 29 30 public MsgFragment() { 31 32 } 33 34 @Override 35 public void onStart() { 36 Log.d("TAG","onStart"); 37 demo(); 38 super.onStart(); 39 } 40 /////////////////////添加数据 41 private void demo() 42 { 43 listData.add(new Con("日前,国家卫健委有关负责人表示,我国正采取多")); 44 listData.add(new Con("日前,国家卫健委有关负责人表示,我国正采取多")); 45 listData.add(new Con("日前,国家卫健委有关负责人表示,我国正采取多")); 46 listData.add(new Con("日前,国家卫健委有关负责人表示,我国正采取多")); 47 listData.add(new Con("日前,国家卫健委有关负责人表示,我国正采取多")); 48 } 49 50 @Override 51 public void onCreate(@Nullable Bundle savedInstanceState) { 52 Log.d("TAG","onCreate"); 53 super.onCreate(savedInstanceState); 54 } 55 56 @Override 57 public View onCreateView(LayoutInflater inflater, ViewGroup container, 58 Bundle savedInstanceState) 59 { 60 Log.d("TAG","onCreateView"); 61 //创建界面MsgFragment 62 contentView=inflater.inflate(R.layout.fragment_msg,container,false); 63 64 //界面的初始化 65 myListAdapter=new MyListAdapter(); 66 ListView listView=(ListView)contentView.findViewById(R.id.id_listview_msgfragment); 67 listView.setAdapter(myListAdapter); 68 return contentView; 69 70 } 71 72 /////////////////////该有的数据 73 public class Con 74 { 75 public String content; 76 public Con(String content) 77 { 78 this.content=content; 79 } 80 } 81 /////////////////////适配器 82 public class MyListAdapter extends BaseAdapter 83 { 84 85 @Override 86 public int getCount() { 87 return listData.size(); 88 } 89 90 @Override 91 public Object getItem(int position) { 92 return listData.get(position); 93 } 94 95 @Override 96 public long getItemId(int position) { 97 return position; 98 } 99 100 @Override 101 public View getView(int position, View converView, ViewGroup parent) { 102 103 if(converView==null) 104 { 105 converView=getLayoutInflater().inflate(R.layout.layout_fragment_msg,parent,false); 106 } 107 Con c= (Con) getItem(position); 108 TextView textView=(TextView)converView.findViewById(R.id.id_textview_msgfragment); 109 textView.setText(c.content); 110 111 return converView; 112 } 113 } 114 115 }