Fragment里添加ListView不要用ListFragment

起始的想法是将Fragment和ViewPager结合起来,

然后突发奇想,在第一个Fragment里添加了ListView,

依照网上的建议,extends了ListFragment,接着各种报错。

仔细看了下,原来是MainActivity这里:

代码如下:

//构造适配器
   List<Fragment> fragments=new ArrayList<Fragment>();
  fragments.add(new Fragment ());
  fragments.add(new Fragment ());
  fragments.add(new Fragment ());
  FPAdapter adapter = new FPAdapter(getSupportFragmentManager(), fragments);

因为是

List<Fragment>

Fragment1用ListFragment自然会报错。

修改Fragment1里代码,添加ListView的方法如下:

public class Fragment extends Fragment {
    private ListView listView;
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      View view= inflater.inflate(R.layout.layout , container, false);
      listView = (ListView)view.findViewById(R.id.lv);
      ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
          android.R.layout.simple_list_item_ ,getData());
      listView.setAdapter(arrayAdapter);
      return view;
    }
    private List<String> getData(){
      List<String> data = new ArrayList<String>();
      for(int i = ;i < ;i++) {
        data.add(i+"");
      }
      return data;
    }
  }

其中

android.R.layout.simple_list_item_1

是自带的,不用定义。

这样ListView便能正常显示了。

以上就是本文的全部内容,希望大家喜欢。

(0)

相关推荐

  • Android中fragment嵌套fragment问题解决方法

    都说fragment好用,duang~~,又遇到问题了,记录一下,分享给遇到这个问题的同学! 1.fragment嵌套fragment时出现getActivity()为null activity A嵌套fragment B,B嵌套fragment C,C跳转到activity D,当activity D被finish掉之后,C中容易爆出getActivity为空.如果你的activity被回收了,那你需要在bundle中保存一下fragment信息,我的解决方法:fragment实例化之后会到a

  • Android中使用ListView实现漂亮的表格效果

    在这里我们要使用Android ListView来实现显示股票行情,效果图如下,红色表示股票价格上涨,绿色表示股票价格下跌. 第一步.定义color.xml如下: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <resources>     <color name="color_dark_grey">#808080</color>     <

  • android动态布局之动态加入TextView和ListView的方法

    本文实例讲述了android动态布局之动态加入TextView和ListView的方法.分享给大家供大家参考.具体实现方法如下: package org.guoshi; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.guoshi.adapter.ImageAndTextAdapter; import android.app.

  • android开发教程之实现滑动关闭fragment示例

    主要代码:(有注释) 复制代码 代码如下: package com.example.checkboxtest; import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.os.Handler;import android.os.Message;import andr

  • Android Map新用法:MapFragment应用介绍

    1.MapView ,MapActivity 这种的局限在于,必须要继承MapActivity,否则无法使用MapView.纠结就在于此.但是,最新官网上已经弃用了这糟粕的MapActivity. Version 1 of the Google Maps Android API as been officially deprecated as of December 3rd, 2012. This means that from March 3rd, 2013 you will no longe

  • Android基础之Fragment与Activity交互详解

    今天继续讲解Fragment组件的特性,主要是跟Activity的交互和生命周期的关系,我们前面已经说过Fragment是依赖于Activity的,而且生命周期也跟Activity绑定一起.下面我们看看Fragment跟Activity的关系. 1.为Activity创建事件回调方法在一些情况下, 你可能需要一个fragment与activity分享事件. 一个好的方法是在fragment中定义一个回调的interface, 并要求宿主activity实现它.当activity通过interfa

  • Android中ExpandableListView的用法实例

    本文实例讲述了Android中ExpandableListView的用法,ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件中定义一个ExpandableListView 复制代码 代码如下: <LinearLayout       android:id="@+id/linearLayout"      android:layout_width="fill_parent"

  • Android基础之使用Fragment控制切换多个页面

    今天讲解一下Fragment的控制,主要是切换View和页面替换等操作.还有就是如何获取Fragment的管理对象,以及与Activity的通信方式.1.管理Fragment要在activity中管理fragment,需要使用FragmentManager. 通过调用activity的getFragmentManager()取得它的实例. •可以通过FragmentManager做一些事情, 包括: 使用findFragmentById()(用于在activity layout中提供一个UI的f

  • Android提高之ListView实现自适应表格的方法

    前面有文章介绍了使用GridView实现表格的方法,本文就来说说如何用ListView实现自适应的表格.GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(主要由于格单元大小不一).此外,GridView实现的表格可以定位在具体某个格单元,而ListView实现的表格则只能定位在表格行.因此还是那句老话:根据具体的使用环境而选择GridView 或者 ListV

  • Android基础之使用Fragment适应不同屏幕和分辨率(分享)

    最近事情很忙,一个新项目赶着出来,但是很多功能都要重新做,一直在编写代码.Debug.今天因为一个新程序要使用Fragment来做,虽然以前也使用过Fragment,不过没有仔细研究,今天顺道写篇文章记录一下Fragment的使用.这文章主要参考了Android官网的介绍. Fragment是Android3.0后增加的新控件,有点类似于Activity组件,也是用来承载各种View元素.Google增加这个玩意的目的是为了平板电脑里面可以复用部分显示的View,只要写好一个View,可以同时在

随机推荐