我正在创造某种星座运势。我有一个带有所有星座符号的片段,所以当用户点击一个星座符号时,我会将该符号名称发送到网络服务,并且我正在为该符号获取带有星座内容的JSON。该内容分为4个部分:每日,每周,每月和每年。为此,我创建了4个选项卡并使用JSON中的内容进行归档。 但问题是,当我回到所有星座标志时,4个标签仍在那里。我不想要标签显示。所以我需要以某种方式隐藏4个标签,并在用户点击另一个(或相同的)星座标志时再次显示它们。此外,当我点击另一个符号时,会创建4个新选项卡,但是旧的传递符号,而不是新的。有时整个屏幕都是白色的,没有内容。这是我的全部代码:
FragmentHoroscopePage.java (带有所有星座图的片段):
public class FragmentHoroscopePage extends Fragment implements OnClickListener {
private FragmentItemHoroscopePage itemHoroscope;
private FragmentManager manager;
private FragmentTransaction transaction;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_horoscope, container, false);
Log.d("FRAGMENT", "FragmentHoroscope");
manager = getFragmentManager();
ImageView ivOven = (ImageView) view.findViewById(R.id.ivOven);
ImageView ivBik = (ImageView) view.findViewById(R.id.ivBik);
ImageView ivBliznaci = (ImageView) view.findViewById(R.id.ivBliznaci);
ImageView ivRak = (ImageView) view.findViewById(R.id.ivRak);
ImageView ivLav = (ImageView) view.findViewById(R.id.ivLav);
ImageView ivDevica = (ImageView) view.findViewById(R.id.ivDevica);
ImageView ivVaga = (ImageView) view.findViewById(R.id.ivVaga);
ImageView ivSkorpija = (ImageView) view.findViewById(R.id.ivSkorpija);
ImageView ivStrelec = (ImageView) view.findViewById(R.id.ivStrelec);
ImageView ivJarec = (ImageView) view.findViewById(R.id.ivJarec);
ImageView ivVodolija = (ImageView) view.findViewById(R.id.ivVodolija);
ImageView ivRibi = (ImageView) view.findViewById(R.id.ivRibi);
ivOven.setOnClickListener(this);
ivBik.setOnClickListener(this);
ivBliznaci.setOnClickListener(this);
ivRak.setOnClickListener(this);
ivLav.setOnClickListener(this);
ivDevica.setOnClickListener(this);
ivVaga.setOnClickListener(this);
ivSkorpija.setOnClickListener(this);
ivStrelec.setOnClickListener(this);
ivJarec.setOnClickListener(this);
ivVodolija.setOnClickListener(this);
ivRibi.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
itemHoroscope = new FragmentItemHoroscopePage();
transaction = manager.beginTransaction();
Bundle bundle = new Bundle();
String sign = null;
switch(v.getId()) {
case R.id.ivOven:
sign = "oven";
break;
case R.id.ivBik:
sign = "bik";
break;
case R.id.ivBliznaci:
sign = "bliznaci";
break;
case R.id.ivRak:
sign = "rak";
break;
case R.id.ivLav:
sign = "lav";
break;
case R.id.ivDevica:
sign = "devica";
break;
case R.id.ivVaga:
sign = "vaga";
break;
case R.id.ivSkorpija:
sign = "skorpija";
break;
case R.id.ivStrelec:
sign = "strelec";
break;
case R.id.ivJarec:
sign = "jarec";
break;
case R.id.ivVodolija:
sign = "vodilija";
break;
case R.id.ivRibi:
sign = "ribi";
break;
}
bundle.putString("horoscope_sign", sign);
itemHoroscope.setArguments(bundle);
transaction.replace(R.id.mainContainer, itemHoroscope, "ItemHoroscopePage");
transaction.addToBackStack("ItemHoroscopePage");
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}
}
FragmentItemHoroscopePage.java - 这里我创建了4个标签,ViewPager和适配器
public class FragmentItemHoroscopePage extends Fragment implements TabListener {
private ActionBar actionBar;
private ViewPager viewPager;
private String sign;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_layout_horoscope, container, false);
Log.d("FRAGMENT", "FragmentItemHoroscope");
actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
FragmentManager manager = getFragmentManager();
sign = getArguments().getString("horoscope_sign");
viewPager = (ViewPager) view.findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(manager));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
}
@Override
public void onPageScrolled(int i, float from, int pix) { }
@Override
public void onPageScrollStateChanged(int i) { }
});
actionBar.addTab(actionBar.newTab().setText(R.string.dneven).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.nedelen).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.mesecen).setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.godisen).setTabListener(this));
return view;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) { }
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) { }
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Bundle bundle = new Bundle();
bundle.putString("horoscope_sign", sign);
Fragment fragment = null;
switch (i) {
case 0:
fragment = new FragmentDaily();
break;
case 1:
fragment = new FragmentWeekly();
break;
case 2:
fragment = new FragmentMonthly();
break;
case 3:
fragment = new FragmentYearly();
break;
}
fragment.setArguments(bundle);
return fragment;
}
@Override
public int getCount() { return 4; }
}
}
FragmentDaily.java - 四个标签中的一个,其他标签全部相同,但内容不同。在这里,我获得了传递的horoscope_sign,在AsyncTask中,我向Web服务发送了POST请求,并获得了JSON。任务完成后,我创建第一个标签的内容(每日标签)
public class FragmentDaily extends Fragment {
private Context context;
private ItemDaily itemDaily;
private ImageView ivZnak;
private TextView tvZnak;
private TextView tvDay;
private TextView tvText;
private FragmentManager manager;
private FragmentTransaction transaction;
private Bundle bundle;
private FragmentItemHoroscopePage itemHoroscopePage;
private List<NameValuePair> pair;
private String horoscope_sign;
private String[] znak = new String[12];
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dneven, container, false);
Log.d("HOROSCOPE FRAGMENT", "FragmentDaily");
context = getActivity();
manager = getFragmentManager();
bundle = new Bundle();
itemHoroscopePage = new FragmentItemHoroscopePage();
znak = context.getResources().getStringArray(R.array.horoscope);
horoscope_sign = getArguments().getString("horoscope_sign");
pair = new ArrayList<NameValuePair>();
pair.add(new BasicNameValuePair("horoscope_sign", horoscope_sign));
System.out.println("UNUTRA" + horoscope_sign);
new GetHoroscopeTask().execute();
ivZnak = (ImageView) view.findViewById(R.id.ivDailyZnak);
tvZnak = (TextView) view.findViewById(R.id.tvDailyZnak);
tvDay = (TextView) view.findViewById(R.id.tvDailyDay);
tvText = (TextView) view.findViewById(R.id.tvDailyText);
return view;
}
class GetHoroscopeTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(context);
dialog.setMessage("Please wait, Colecting data!");
dialog.setCancelable(false);
dialog.show();
}
@Override
protected Boolean doInBackground(String... params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Variables.URL_HOROSCOPE);
httpPost.setEntity(new UrlEncodedFormEntity(pair));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String data = EntityUtils.toString(httpEntity);
int status = httpResponse.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
itemDaily = new ItemDaily();
JSONObject jObject = new JSONObject(data);
JSONObject jRealObject = jObject.getJSONObject("daily");
itemDaily.setText(jRealObject.getString("text").toString());
itemDaily.setDay(jRealObject.getString("day").toString());
return true;
} else {
Toast.makeText(context, Variables.ERROR_MESSAGE, Toast.LENGTH_LONG).show();
((Activity) context).finish();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if(result == false) {
Toast.makeText(context, Variables.ERROR_MESSAGE, Toast.LENGTH_LONG).show();
} else {
if(dialog.isShowing()) {
dialog.dismiss();
}
tvDay.setText(getResources().getString(R.string.dneven_horoskop) + " " + itemDaily.getDay());
tvText.setText(Html.fromHtml(itemDaily.getText()));
}
}
}
}
答案 0 :(得分:0)
然后创建一个新的意图。只是不做完();在标签活动中。如果您希望再次转到标签,请调用finish();