Android技能树 — Fragment总体小结,android实验报告总结

老样子,先上脑图:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

我们就按照脑图的顺序一样样来看Fragment的基础知识。


正文:

1.Fragment的添加

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

我们知道Fragment是一个"碎片(或者片段)",添加在Activity中。如果我现在问你,Activity要显示一个按钮Button,你会怎么做?

1. 直接在Layout.xml中添加<Button/>

<?xml version="1.0" encoding="utf-8"?>


2. 在代码中动态添加,比如我们添加到一个LinearLayout中:

Button button = new Button(mActivity);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
container.addView(button);

所以Fragment也很简单,就把它当做一个简单的View(但其实更像是“子 Activity”),然后添加方式也是一样。

1. 直接在Layout.xml中添加:

<?xml version="1.0" encoding="utf-8"?>

2. 直接在代码中添加:

Fragment one = new FragmentOne();//自定义的Fragment类
//要先获取FragmentManager对象
FragmentManager fragmentManager = getSupportFragmentManager();
//开启一个FragmentTransaction事务
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.framelayout_view,one).commit();

其中添加到R.id.framelayout_view的这个idViewGourp可以是<FrameLayout/>,也可以是其他的比如<LinearLayout/>等。


2. Fragment的基本操作

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

看到我们上面的动态代码添加的时候需要获取FragmentTransactionFragmentManager

2.1 FragmentManager相关

1. getFragmentManager():

获取Fragment父容器的管理器,但是现在该方法在Activity中已经被标记不推荐使用了。

/**

  • Return the FragmentManager for interacting with fragments associated
  • with this activity.
  • @deprecated Use {@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()}
    */
    @Deprecated
    public FragmentManager getFragmentManager() {
    return mFragments.getFragmentManager();
    }

2. getSupportFragmentManager():

v4包下的这个方法,与上一个效果一样,不过是Android推荐使用的方法(毕竟可以兼容Android所有版本)

3. getChildFragmentManager():

  • 28
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android天气预报实验报告模板 public class SetCityActivity extends Activity { //定义的一个自动定位的列表 private ListView gpsView; //定义的一个省份可伸缩性的列表 private ExpandableListView provinceList; //定义的用于过滤的文本输入框 private TextView filterText; //定义的一个记录城市码的SharedPreferences文件名 public static final String CITY_CODE_FILE="city_code"; //城市的编码 private String[][] cityCodes; //省份 private String[] groups; //对应的城市 private String[][] childs; //自定义的伸缩列表适配器 private MyListAdapter adapter; //记录应用程序widget的ID private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.set_city); gpsView = (ListView)findViewById(R.id.gps_view); provinceList= (ExpandableListView)findViewById(R.id.provinceList); //设置自动定位的适配器 gpsView.setAdapter(new GPSListAdapter(SetCityActivity.this)); //==============================GPS================================= //当单击自动定位时 gpsView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView localeCity = (TextView)view.findViewById(R.id.locateCityText); localeCity.setText("正在定位..."); final LocateHandler handler = new LocateHandler(localeCity); //添加一个线程来处理定位 new Thread(){ public void run() { Map<Integer, String> cityMap= getLocationCityInfo(); //记录匹配的城市的索引 int provinceIndex = -1; int cityIndex = -1; //传给处理类的数据封装对象 Bundle bundle = new Bundle(); if(cityMap!=null) { //得到图家名 String country = cityMap.get(LocationXMLParser.COUNTRYNAME); //只匹配中国地区的天气 if(country!=null&&country.equals("中国")){ //得到省 String province = cityMap.get(LocationXMLParser.ADMINISTRATIVEAREANAME); //得到市 String city = cityMap.get(LocationXMLParser.LOCALITYNAME); //得到区县 String towns = cityMap.get(LocationXMLParser.DEPENDENTLOCALITYNAME); Log.i("GPS", "============"+province+"."+city+"."+towns+"=============="); //将GPS定位的城市与提供能查天气的城市进行匹配 StringBuilder matchCity = new StringBuilder(city); matchCity.append("."); matchCity.append(towns); //找到省份 for(int i=0; i<groups.length; i++) { if(groups[i].equals(province)) { provinceIndex = i; break; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值