• 14 ListFragment 代码案例


    • MainActivity.java:
    package com.qf.day14_fragment_demo9_list;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    
    • MyListFragment对象:

      package com.qf.day14_fragment_demo9_list.fragment;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import com.qf.day14_fragment_demo9_list.R;
      
      import android.annotation.SuppressLint;
      import android.app.ListFragment;
      import android.os.Bundle;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.ArrayAdapter;
      import android.widget.ListView;
      import android.widget.Toast;
      
      @SuppressLint("NewApi")
      public class MyListFragment extends ListFragment{
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              // TODO Auto-generated method stub
      
              View view = inflater.inflate(R.layout.layout, container, false);
      
              List<String> list = new ArrayList<String>();
              for(int i=0;i<20;i++){
                  list.add("G20"+i);
              }
      
      
              ArrayAdapter<String> adapter =  new ArrayAdapter<String>
              (getActivity(), android.R.layout.simple_list_item_1, list);
      
              //设置数据源
              setListAdapter(adapter);
      
      
              return view;
          }
      
          //ListView 的点击事件
          @Override
          public void onListItemClick(ListView l, View v, int position, long id) {
              // TODO Auto-generated method stub
              super.onListItemClick(l, v, position, id);
      
              Toast.makeText(getActivity(), "===>"+position, 0).show();
          }
      
      }
      
    • activity_main.xml:

      <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context=".MainActivity" >
      
          <fragment 
              android:id="@+id/ft"
              android:name="com.qf.day14_fragment_demo9_list.fragment.MyListFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              />
      
      </RelativeLayout>
      
    • listfragment的填充对象(layout.xml ):

      <?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">
      <!--必须设定id为@id/android:list 这样可以在listfragme中直接设置setadapter-->
      <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>
      
  • 相关阅读:
    .NET 请求和接收FormData的值
    span可编辑 属性 html 可编辑td 文字不可 选中
    mvc关于pots请求 哪个函数 出现bug研究
    C#的split函数分割
    HBase入门
    Labview学习之波形图表的历史数据
    VC++学习之GDI概述
    如何撰写项目计划书
    VC++学习之VC中常见问题
    Labview学习之程序Web发布
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152246.html
Copyright © 2020-2023  润新知