zoukankan      html  css  js  c++  java
  • 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>
      
  • 相关阅读:
    android-exploitme(六):基础加密
    错误:error libGL.so: cannot open shared object file: No such file or directory
    android-exploitme(五):不安全的数据存储
    android-exploitme(四):参数篡改
    android-exploitme(三):安全连接
    android-exploitme(二):安装apk熟悉测试环境
    android-exploitme(一):生成apk
    Ubuntu rsync同步
    phantomjs + selenium headless test
    Fatal error: cannot allocate memory for the buffer pool
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152246.html
Copyright © 2011-2022 走看看