热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

androidmainactivity刷新fragment,选择菜单栏后,如何刷新fragment选项卡?_androidactivity_开发99编程知识库...

通过使用FragmentManager替换旧的fragment来替换旧的。现在可以选择一个触发事件的ActiveView菜单项,并使活动fragment替换为当前屏幕

通过使用FragmentManager替换旧的fragment 来替换旧的。 现在可以选择一个触发事件的ActiveView菜单项,并使活动 fragment 替换为当前屏幕下面的屏幕。

629ab443bbdf171457fedbc86bd6967b.png

解决这个问题的问题主要与识别FragmentManager语法中的正确的R.id.layout 引用有关。 我已经发布了相关的XML和java代码,希望其他人可以发现它。

MainActivity.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

**android:id="@+id/fragment_placeholder"**

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

dataCapture.xml

android:tag="data_capture"

android:id="@+id/data_capture"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

tools:context=".dataCapture">

...

style="@style/colorSizeStylexml"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="10dip"

android:text="Tracklog:"/>

android:id="@+id/textViewOff"

android:layout_width="40dp"

android:layout_height="40dp"

android:gravity="center"

android:textAppearance="?android:attr/textAppearanceMedium"

android:layout_marginRight="10dip"

android:text="OFF"

android:background="#ff000000"

android:textStyle="bold"

android:focusableInTouchMode="true"

android:textColor="#ffffffff"/>

android:id="@+id/textViewOn"

android:layout_width="40dp"

android:layout_height="40dp"

android:gravity="center"

android:textAppearance="?android:attr/textAppearanceMedium"

android:layout_marginRight="10dip"

android:text="ON"

android:background="#ff58ff2d"

android:textStyle="bold"/>

.. .

Activity.javapublic class MainActivity extends Activity{

static int iTrackLogFlag = 0;//if value = (0) Tracklog is off, if (1) Tracklog is on

public dataCapture dataCapture;

...

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

. . .

//put Actionbar in tab mode

ActionBar actionBar = getActionBar();

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

//set titles for tabs

ActionBar.Tab dataCaptureTab = actionBar.newTab().setText("DataCapture");

ActionBar.Tab dataEditTab = actionBar.newTab().setText("DataEdit");

. . .

/*******************************************************************************************

* Create instances of each of the fragments. dataCapture is refreshed several times from

* fragmentManager (ActiveBar menutab for tracklog ON-OFF, and at closure of lookup table

* edit PopupWindows) hence the different format.

*******************************************************************************************/

//Fragment dataCaptureFragment = new dataCapture();

android.app.FragmentManager fragmentManager = getFragmentManager();

android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

dataCapture dataCapture = new dataCapture();

fragmentTransaction.add(dataCapture,"data_capture");

fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit();

Fragment dataEditFragment = new dataEdit();

. . .

//attach those fragment instances to their respective tabs

dataCaptureTab.setTabListener(new MyTabsListener(dataCapture));

dataEditTab.setTabListener(new MyTabsListener(dataEditFragment));

. . .

//add each tab to the ActionBar

actionBar.addTab(dataCaptureTab);

actionBar.addTab(dataEditTab);

. . .

if (savedInstanceState == null){//...do nothing

}else if (savedInstanceState!= null){

actionBar.setSelectedNavigationItem(savedInstanceState.getInt(TAB_KEY_INDEX,0));

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();

inflater.inflate(R.menu.corax, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch(item.getItemId()) {

case R.id.menuitem_tracklogOnOff:

openTracklogDialog();//opens a dialog box...trying to minimize clutter in the toolbar.

//

return true;

}

return false;

}

private void openTracklogDialog(){

AlertDialog.Builder TracklogDialog = new AlertDialog.Builder(this);

TracklogDialog.setTitle("Tracklog control");

TracklogDialog.setMessage("Press a button below to start or stop the tracklog.");

TracklogDialog.setPositiveButton("STOP",new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

iTrackLogFlag = 0;//"0" means OFF, sets button on frontend to black w/white letters OFF

}

});

TracklogDialog.setNegativeButton("START",new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

//Toast.makeText(getApplicationContext(),"Tracklog started.", Toast.LENGTH_LONG).show();

iTrackLogFlag = 1;//"1" means ON, sets button to green w/black letters

Fragment currentFragment = (dataCapture)getFragmentManager().findFragmentByTag("data_capture");

if(currentFragment == null) {

Toast.makeText(appContext,"This == NULL.", Toast.LENGTH_SHORT).show();

currentFragment = new dataCapture();

}else if(currentFragment!= null){

getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

getFragmentManager().beginTransaction().replace(R.id.fragment_placeholder, new dataCapture(),"data_capture").addToBackStack(null).commit();

Toast.makeText(appContext,"This!= NULL. currentFragment ="+currentFragment+", dataCapture ="+dataCapture+".", Toast.LENGTH_LONG).show();

}

dialog.dismiss();

}

});

AlertDialog alert = TracklogDialog.create();

alert.show();

}

}



推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 本文介绍了一款名为TimeSelector的Android日期时间选择器,采用了Material Design风格,可以在Android Studio中通过gradle添加依赖来使用,也可以在Eclipse中下载源码使用。文章详细介绍了TimeSelector的构造方法和参数说明,以及如何使用回调函数来处理选取时间后的操作。同时还提供了示例代码和可选的起始时间和结束时间设置。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 今天就跟大家聊聊有关怎么在Android应用中实现一个换肤功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根 ... [详细]
  • SmartRefreshLayout自定义头部刷新和底部加载
    1.添加依赖implementation‘com.scwang.smartrefresh:SmartRefreshLayout:1.0.3’implementation‘com.s ... [详细]
  • WPF之Binding初探
      初学wpf,经常被Binding搞晕,以下记录写Binding的基础。首先,盗用张图。这图形象的说明了Binding的机理。对于Binding,意思是数据绑定,基本用法是:1、 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文详细介绍了使用C#实现Word模版打印的方案。包括添加COM引用、新建Word操作类、开启Word进程、加载模版文件等步骤。通过该方案可以实现C#对Word文档的打印功能。 ... [详细]
  • 巧用arguments在Javascript的函数中有个名为arguments的类数组对象。它看起来是那么的诡异而且名不经传,但众多的Javascript库都使用着它强大的功能。所 ... [详细]
author-avatar
手机用户2502929415
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有