【Android】android Fragment的使用
来源:互联网 发布:网络学会计推荐 编辑:程序博客网 时间:2024/06/02 08:55
Fragment使用很简单,注意android3.0之前版本的使用方法,下面是android4.0以上的方法。
上图是应用效果,效果就是当点击上面的listview时,底部textview进行更新。
控件布局:上面是一个listview,下面就是一个textview,不过他们各自都是一个Fragment。看主界面布局xml,分别是两个Fragment,MainActivity直接加载布局xml就行,不需要其他的。
<LinearLayout 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" tools:context=".MainActivity" android:orientation="vertical" android:background="#7ecef4"> <fragment android:name="com.example.fragmenttest.FragmentList" android:id="@+id/frag_list" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="2"/> <fragment android:name="com.example.fragmenttest.FragmentDetails" android:id="@+id/frag_detail" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"/></LinearLayout>
接着是FragmentDetails.java和FragmentList.java
package com.example.fragmenttest;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.ListFragment;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ListView;import android.widget.SimpleAdapter;public class FragmentList extends ListFragment { private String[] values = new String[] { "侏儒", "人类", "暗夜精灵", "矮人", "德莱尼", "狼人" }; private int[] images = new int[] { R.drawable.gnome, R.drawable.gnome, R.drawable.gnome, R.drawable.gnome, R.drawable.gnome, R.drawable.gnome }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.frag_list, container, false);//需要注意的就是继承ListFragment的类的布局文件中listview的id一定要为list } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < values.length; i++) { Map<String, Object> listItem = new HashMap<String, Object>(); listItem.put("values", values[i]); listItem.put("images", images[i]); listItems.add(listItem); } SimpleAdapter adapter = new SimpleAdapter(getActivity(), listItems, R.layout.list_item, new String[] { "values", "images" }, new int[] { R.id.txt_title, R.id.img }); setListAdapter(adapter); } @Override public void onListItemClick(ListView l, View v, int position, long id) { // String item = (String) getListAdapter().getItem(position); FragmentDetails frag = (FragmentDetails) getFragmentManager() .findFragmentById(R.id.frag_detail); if (frag != null && frag.isInLayout()) { switch (position) { case 0: frag.setText(getString(R.string.Gnome)); break; case 1: frag.setText(getString(R.string.Human)); break; case 2: frag.setText(getString(R.string.NightElf)); break; case 3: frag.setText(getString(R.string.Dwarf)); break; case 4: frag.setText(getString(R.string.Draenei)); break; case 5: frag.setText(getString(R.string.Werewolf)); break; } } Log.i("PDA", "position = " + position); }}
package com.example.fragmenttest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class FragmentDetails extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.frag_detail, container,false); } public void setText(String item){ TextView txt = (TextView) getView().findViewById(R.id.txt_detail); txt.setText(item); }}
0 0
- Android Fragment的使用
- Android-Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- android Fragment 的使用
- Android Fragment的使用
- Android Fragment的使用
- Android---fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- EF6+MySql code first
- HDU3035-平面图最小割转最短路
- 每周一读-《书都不会读,你还想成功》
- JavaScript字符集编码与解码_2
- Android新布局组件ConstainLayout了解使用
- 【Android】android Fragment的使用
- 蓝桥杯基础训练--区间K大数查询
- Android线程池使用终结版
- 某控件.performClick()实现点击
- Java中的文件和目录管理----Path、Files
- 广告印刷
- qt 中英文翻译
- 深度学习卷积算法的GPU加速实现方法
- 听说你要找前端工作,写一个酷炫的动画的简历呗