如何在按钮点击时刷新片段标签内容

时间:2022-08-23 22:40:48

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.

我是Android的新手,我正在执行一个小项目,其中有一个Drupal网站的新闻。这个应用程序有3个选项卡,每个选项卡都有特定内容的文章列表。文章列表由线性布局中的textview和imageviews创建。当我点击标题时,文章的开头就有了细节。这些文章通过HTTP POST和ViewJSON模块从Drupal站点加载。选项卡是用FragmentActivity和TabHost创建的。一切正常,直到这里。

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.

我的问题是,我想做一个refresh按钮,放在制表符上,当我按它时,文章列表必须刷新或重新加载,并保持当前制表符打开。我试图替换标签片段内容并重新打开它,但是当我点击一篇文章的标题来打开它时,标签内容仍然在所有的片段下的堆栈中。我提到当我点击文章标题时,一个新的片段正在打开。我将文章的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. 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.).

更确切地说,我输入app,按Tab2(显示列表,从Tab2),点击刷新按钮,打开一篇文章,按下移动设备的后退按钮,并显示Tab2的内容。现在,我输入tab1(并显示来自tab1的文章列表),打开来自tab1列表的一篇文章,显示我需要什么,然后按下mobile的back按钮。此时显示的是tab2内容(文章列表从我按下refresh按钮的位置显示为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!

谢谢你的帮助!

2 个解决方案

#1


6  

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:

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

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. So it is available in the refresh button onClick method.

现在,当您创建选项卡片段(如PupulareFragment)时,使用PupulareFragment实例变量来存储片段。所以它在刷新按钮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,可能也会运行得很好。

#2


-1  

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.

试试这个,标签下的所有东西都将被再次执行。

#1


6  

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:

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

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. So it is available in the refresh button onClick method.

现在,当您创建选项卡片段(如PupulareFragment)时,使用PupulareFragment实例变量来存储片段。所以它在刷新按钮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,可能也会运行得很好。

#2


-1  

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.

试试这个,标签下的所有东西都将被再次执行。