問題描述
我是 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()該片段中的 code> 方法.示例:
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.
這篇關于如何在按鈕單擊時刷新片段選項卡內容的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!