片段类具有的ListView? [英] Fragment Class with ListView?

查看:135
本文介绍了片段类具有的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能有自定义的ListView 片段类里面呢?

这是我的片段类化合物,其中 fragment_find_people 只是一个空 XML

 公共类FindPeopleFragment扩展片段{公共FindPeopleFragment(){}
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    查看rootView = inflater.inflate(R.layout.fragment_find_people,集装箱,FALSE);    返回rootView;
}

}

现在,我已经有一个 customListView ,但它仅与活动的作品类。我怎么可以转移,为片段

我listview_main XML

 < RelativeLayout的
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:ID =@ + ID / listmain>< ListView控件
    机器人:ID =@ + ID /列表视图
    机器人:layout_marginTop =10dp
    机器人:背景=@绘制/ BG
    机器人:分=#9c9c9c
    机器人:dividerHeight =5DP
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT/>< / RelativeLayout的>


解决方案

   - 类语境与getActivity取代那唯一的区别
- 并且更换观点不活动       查看rootView = inflater.inflate(R.layout.fragment_find_people,集装箱,FALSE);
       //现在你必须初始化列表视图
       列表视图列表视图=(列表视图)rootView .findViewById(R.id.your_listview);       // EDITED code
       的String []项目=新的String [] {项目1,项目2,3项};
       ArrayAdapter<串GT;适配器=
       新ArrayAdapter<串GT;(getActivity(),android.R.layout.simple_list_item_1,项目);        listview.setAdapter(适配器);        //要具有自定义列表视图中使用这样的:你必须定义CustomeAdapter类
        // listview.setadapter(新CustomeAdapter(getActivity()));
       // getActivty使用语境,而不是
     返回视图。

Is it possible to have custom ListView inside Fragment class?

This is my fragment class wherein the fragment_find_people is just an empty XML:

public class FindPeopleFragment extends Fragment {

public FindPeopleFragment(){}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);

    return rootView;
}

}

Now, I've already have a customListView but it only works with the Activity class. How can I transfer that to Fragment?

my listview_main xml

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/listmain" >

<ListView
    android:id="@+id/listview"
    android:layout_marginTop="10dp"
    android:background="@drawable/bg"
    android:divider="#9c9c9c"
    android:dividerHeight="5dp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" /></RelativeLayout>

解决方案

--Class context replace with getActivity thats only difference 
--and replace view not activity          

       View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
       //now you must initialize your list view
       Listview listview =(Listview)rootView .findViewById(R.id.your_listview);

       //EDITED Code 
       String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
       ArrayAdapter<String> adapter =
       new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items); 

        listview.setAdapter(adapter);  

        //To have custom list view use this : you must define CustomeAdapter class
        // listview.setadapter(new CustomeAdapter(getActivity()));
       //getActivty is used instead of Context
     return view;

这篇关于片段类具有的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆