如何刷新按钮点击片段选项卡内容 [英] How to refresh fragment tab content on button click

查看:148
本文介绍了如何刷新按钮点击片段选项卡内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很新的Andr​​oid和我采取一个小的项目中,我从一个Drupal的网站新闻。这个程序是有3个标签,每个标签有具体内容的文章列表。由textviews和imageviews排列的LinearLayout创建的文章列表。当我点击标题,文章是开放细节。这些物品是从Drupal站点throught HTTP POST和ViewJSON模块加载。标签与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.

我的问题是,我希望做一个刷新按钮,放置了标签,当我preSS它,文章列表必须刷新或重新加载,并保持当前选项卡中打开。 我试图替换选项卡的内容片断,然后重新打开它,但是当我点击一篇文章,打开它的标题,标签内容留在堆栈下的所有片段。我提到,当我点击文章的标题,一是新的片段打开。我把文章的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.

我试图用这个code,但没有更迭。

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

更确切地说我输入的应用程序,preSS TAB2(显示从TAB2文章列表),preSS刷新按钮,打开一文中,preSS的移动设备的背面按钮,告诉我TAB2的内容。现在,我对TAB1输入(并显示从tab1的文章列表),打开一文从TAB1的名单,告诉我我需要什么,和preSS从移动后退按钮。在这一刻显示的TAB2内容(文章列表形成了从我的pressed刷新按钮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(); 
                    }
                });

预先感谢帮助!

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. 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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆