如何在按钮单击时刷新片段选项卡内容

How to refresh fragment tab content on button click(如何在按钮单击时刷新片段选项卡内容)
本文介绍了如何在按钮单击时刷新片段选项卡内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 的新手,我正在实施一个小项目,其中我有来自一个 Drupal 网站的新闻.这个应用程序有 3 个标签,每个标签都有一个包含特定内容的文章列表.文章列表由以线性布局排列的 textviews 和 imageviews 创建.当我点击标题时,文章正在以详细信息打开.这些文章是通过 HTTP POST 和 ViewJSON 模块从 Drupal 站点加载的.选项卡是使用 FragmentActivity 和 TabHost 创建的.一切都很好,直到这里都可以正常工作.

I'm really new in Android and I'm implementing a small project in which I have news from one Drupal website. This app is with 3 tabs and every tab have a list of articles with specific content. The list of articles is created by textviews and imageviews arranged in linearlayout. When I click on the title, the article is opening with details. Those articles are loaded from Drupal site throught HTTP POST and ViewJSON module. Tabs are created with FragmentActivity and TabHost. Everything's ok and works fine until here.

我的问题是我想制作一个刷新按钮,放置在标签上,当我按下它时,文章列表必须刷新或重新加载并保持当前标签打开.我试图替换标签片段内容并重新打开它,但是当我点击一篇文章的标题打开它时,标签内容保留在所有片段下的堆栈中.我提到当我点击文章标题时,会打开一个新片段.我发送了文章的 ID 作为参数,并在同一个片段中打开了文章的内容.

My problem is that I want to make one refresh button, to be placed over tabs and when I press it, the list of articles must refresh or reload and keep the current tab opened. I tried to replace tab fragment content and re-open it, but when I click on the title of one article to open it, the tab content remain in stack under all fragments. I mention that when I click on article's title, one new fragment is opening. I sent an id of the articles as an argument and in the same fragment I open the article's content.

我正在尝试使用此代码,但没有成功.

I was trying with this code but no succes.

Button btn_refresh = (Button) findViewById(R.id.btn_refresh);
        btn_refresh.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                 String current_tab = mTabHost.getCurrentTabTag(); 
                 if(current_tab.contains("POPULARE")){

                     Fragment f;
                     f = new PopulareActivity(); 
                     FragmentTransaction ft =   getSupportFragmentManager().beginTransaction(); 
                     ft.replace(R.id.tot,f);   
                     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
                     ft.commit();
               }
                 if(current_tab.contains("RECOMANDATE")){

                     Fragment f;
                     f = new RecomandateActivity(); 
                     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
                     ft.replace(R.id.tot,f);   
                     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
                     ft.commit(); 
                 } 

更确切地说,我在应用程序上输入,按 Tab2(显示来自 tab2 的文章列表),按刷新按钮,打开一篇文章,按移动设备的后退按钮并显示 tab2 的内容.现在,我在 tab1 上输入(并显示来自 tab1 的文章列表),从 tab1 列表中打开一篇文章,显示我需要的内容,然后在移动设备上按下后退按钮.此时显示的是 tab2 的内容(我按下刷新按钮的 tab2 中的文章列表.).

More exactly I enter on app, press Tab2 (show the list of articles from tab2), press refresh button, open one article, press the back button of mobile device and show me the content of tab2. Now, I enter on tab1 (and show the list of articles from tab1), open one article from the list of tab1, show me what I need, and press the back button from mobile. In this moment is shown the tab2 content (the list of articles form tab2 from where I pressed the refresh button.).

要打开我使用的文章的详细信息:

To open details of an article I use:

           tv.setOnClickListener(new View.OnClickListener() { 
                    public void onClick(View v) { 

                             Fragment f;
                             f = new DetaliiActivity();
                             Bundle data = new Bundle();  
                             data.putInt("id", nid);
                             f.setArguments(data);
                             FragmentTransaction ft = getFragmentManager().beginTransaction();

                             ft.replace(R.id.tot,f);

                             ft.addToBackStack(null); 
                             ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
                             ft.commit(); 
                    }
                });

提前感谢您的帮助!

推荐答案

要刷新片段的内容,可以保留对片段的引用,并调用公共(例如)refresh() 方法.示例:

To refresh the contents of a fragment, you could keep a reference to the fragment, and call a public (for example) refresh() method in that fragment. Example:

public class ArticleTabFragmentActivity extends FragmentActivity{
  private PopulareFragment populareFragment;
  private RecomandateFragment recomandateFragment;

  <code>

  private void bindView(){
    Button btn_refresh = (Button) findViewById(R.id.btn_refresh);

    btn_refresh.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                 String current_tab = mTabHost.getCurrentTabTag(); 
                 if(current_tab.contains("POPULARE")){
                   populareFragment.refresh();
                 } else if(current_tab.contains("RECOMANDATE")){
                   recomandateFragment.refresh();
                 } 
    });
  }



}

public class PopulareFragment extends Fragment{  

  public void refresh(){
    <refresh the contents of the fragment>
  }

}

<Same for other fragment>

现在,当您创建标签片段时,就像 PupulareFragment 一样,使用 pupulareFragment 实例变量来存储片段.所以在刷新按钮的onClick方法中是可用的.

Now when you create the tab fragment, like the PupulareFragment, use the pupulareFragment instance variable to store the fragment. So it is available in the refresh button onClick method.

这样刷新时不会替换/创建任何新片段.因此,在转到文章详细信息活动后返回 tabActivity 可能也可以正常工作.

This way you are not replacing/creating any new fragments when refreshing. So going back to tabActivity after going to de article details activity, would probably work fine aswell.

这篇关于如何在按钮单击时刷新片段选项卡内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android This feature requires ASM7(Android此功能需要ASM7)
How to make Eclipse compile patched modules on JDK 9+ by passing the --patch-module javac option to my project compiler? (see pic)(如何通过向我的项目编译器传递--patch-MODULE javac选项,让Eclipse在JDK 9+上编译打了补丁的模块?(见图))
Cannot resolve symbol Theme.MaterialComponents.Light.NoActionBar (Android Studio)(无法解析符号Theme.MaterialComponents.Light.NoActionBar(Android Studio))
Drawing emojis on Android canvas using unicode values(使用Unicode值在Android画布上绘制表情符号)
I#39;m unable to get the right timezone offset with daylight(我无法获得正确的时区与日光的偏移)
SimpleDateFormat getHours() return incorrect value(SimpleDateFormat getHors()返回不正确的值)