简体   繁体   English

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

[英]How to refresh fragment tab content on button click

I'm really new in Android and I'm implementing a small project in which I have news from one Drupal website. 我是Android的新手,我正在实施一个小项目,我从一个Drupal网站获得新闻。 This app is with 3 tabs and every tab have a list of articles with specific content. 此应用程序有3个选项卡,每个选项卡都有一个包含特定内容的文章列表。 The list of articles is created by textviews and imageviews arranged in linearlayout. 文章列表由在linearlayout中排列的textviews和imageview创建。 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. 这些文章是通过HTTP POST和ViewJSON模块从Drupal站点加载的。 Tabs are created with FragmentActivity and TabHost. 使用FragmentActivity和TabHost创建选项卡。 Everything's ok and works fine until here. 一切都很好,直到这里工作正常。

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. 我发送了一篇文章的id作为论据,并在同一篇文章中打开了文章的内容。

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(); 
                 } 

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. 更确切地说,我进入应用程序,按Tab2(显示tab2中的文章列表),按刷新按钮,打开一篇文章,按移动设备的后退按钮,并显示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. 现在,我进入tab1(并显示tab1中的文章列表),从tab1列表中打开一篇文章,告诉我我需要什么,然后按移动后退按钮。 In this moment is shown the tab2 content (the list of articles form tab2 from where I pressed the refresh button.). 在这一刻显示了tab2内容(从我按下刷新按钮的tab2文章列表。)。

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(); 
                    }
                });

Thanks in advance for help! 在此先感谢您的帮助!

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. 要刷新片段的内容,您可以保留对片段的引用,并在该片段中调用公共(例如) refresh()方法。 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>

Now when you create the tab fragment, like the PupulareFragment, use the pupulareFragment instance variable to store the fragment. 现在,当您创建选项卡片段(如PupulareFragment)时,请使用pupulareFragment实例变量来存储片段。 So it is available in the refresh button onClick method. 因此它可以在onClick方法的刷新按钮中使用。

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. 因此,在参加de article details活动之后返回tabActivity,可能也会正常工作。

FragmentTabHost tabHost3 = (FragmentTabHost) findViewById(android.R.id.tabhost);
String current_tab = tabHost3.getCurrentTabTag();
if (current_tab.equals("abc")) { 
    ClassFragment ni = (ClassFragment) getSupportFragmentManager().findFragmentByTag("abc");
    ni.refresh();
}

Try this, everything that is present under tab will be executed again. 试试这个,tab下的所有内容都会再次执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM