listFragment
来源:互联网 发布:javascript php 混合 编辑:程序博客网 时间:2024/05/20 20:21
转自:http://blog.csdn.net/kakaxi1o1/article/details/29368645
1,ListFragment的布局默认包含一个list view。因此,在ListFragment对应的布局文件中,必须指定一个 android:id “@android:id/list” 的ListView控件! 若用户向修改list view的,可以在onCreateView(LayoutInflater, ViewGroup, Bundle)中进行修改。当然,用户也可以在ListFragment的布局中包含其它的控件。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView <strong> android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view=inflater.inflate(R.layout.list_fragment, null);return view;}
2,ListFragment绑定ListView的数据,必须通过ListFragment.setListAdapter()接口来绑定数据,而不是使用ListView.setAdapter() 或其它方法!
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, cities)); }3,,监听事件
@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id); Toast.makeText(getActivity(), "You have selected " + cities[position], Toast.LENGTH_SHORT).show(); showSelectedDetails(cities[position]);}
private void showSelectedDetails(String city) {translateContentListener.translate(city);}4,向acitivty传递数据
interface TranslateContentListener{void translate(String city);}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);translateContentListener=(TranslateContentListener) activity;}private void showSelectedDetails(String city) {translateContentListener.translate(city);}5,activity只负责控制fragment,实现上面定义的接口
public class FragmentActivity extends BaseActivity implements TranslateContentListener{RightFragement rightFragment;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.fragment_activity);FragmentManager manager=getSupportFragmentManager();rightFragment=(RightFragement) manager.findFragmentById(R.id.right_fragment);}@Overridepublic void translate(String city) {rightFragment.showContent(city);} //向fragment传递数据 interface ShowContentListener{void showContent(String title);}}6,接受内容的fragment 负责展示
public class RightFragement extends Fragment implements ShowContentListener{TextView textView;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view=inflater.inflate(R.layout.text_view_itme, null);textView=(TextView) view.findViewById(R.id.text);return view;}@Overridepublic void showContent(String title) {textView.setText(title);}}
当然,也可以通过 fragment的getActivity方法,实现fragment向activity传递数据
1 0
- ListFragment
- ListFragment
- listFragment
- ListFragment
- ListFragment实例
- Android ListFragment
- Messages ListFragment
- android Listfragment
- Android ListFragment
- ListFragment+CheckedTextView和ListFragment+CheckBox两种多选效果
- 【Android 开发教程】ListFragment
- ListFragment setDivider的问题
- Android ListFragment实例Demo
- Android ListFragment使用
- ListFragment的使用
- 使用ListFragment显示列表
- Android自定义ListFragment
- ListFragment的用法
- 关于dom 事件冒泡、捕获 dom事件三个阶段
- hdu1233最短路问题使用
- 项目经理应该知道的97件事--剔除多余的流程
- Unity3D的Time类(UnityEngine.Time)详解
- HTML5 FileReader
- listFragment
- 计算两点之间的距离
- jquery 基本操作
- android studio 获取sha1值和MD5值
- 禁用cookie后session是如何设置的
- C语言学习——动态内存
- 2016春晚的烧钱营销:阿里与腾讯
- 类似新浪微博帖子显示话题、@好友、表情解析等
- hdu1863