关于ViewPager使用FragmentPagerAdapter的删除问题

陈小缘 2015-10-28 08:59:07
删除非最后一个页面原来的数据还在。比如我用它来显示天气数据,现有北京,上海,广州三个城市,删除上海后,剩下北京和广州是没错,关键是广州显示的天气数据是原来上海的天气数据,然后添加一个城市,比如说天津,天津显示的就是之前广州的天气数据(重启应用后可以正常显示)各种能想到的都试过了,实在解决不了,各路大神有碰到过类似的吗
...全文
464 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈小缘 2016-05-03
  • 打赏
  • 举报
回复
引用 14 楼 tom1365412836 的回复:
楼主解决没...同求方法
你解决了吗?我是将FragmentPagerAdapter改为FragmentStatePagerAdapter就可以了,好久没逛论坛
lovatom1314 2016-03-07
  • 打赏
  • 举报
回复
楼主解决没...同求方法
猴子写代码 2015-10-30
  • 打赏
  • 举报
回复
首先,你代码贴的太多,来回答问题的人看到这么多代码都不想看。 把你的工程上传上来,我给你导入看一下
水的川 2015-10-30
  • 打赏
  • 举报
回复
引用 8 楼 u011387817 的回复:
引用 5 楼 u013377714 的回复:
引用 4 楼 u011387817 的回复:
[quote=引用 1 楼 u013377714 的回复:] 相关代码呢?这应该根据你的page页数和数据绑定吧,你再删除后要对相应地页面逻辑作出相应地修改。
我已经做过修改了,还是不行
你不给我们看代码怎么知道你错哪了。。。
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); HorizontalScrollView.LayoutParams lp = (HorizontalScrollView.LayoutParams) rotationListener.getLayoutParams(); //切换为竖屏 if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { if (lp.gravity != Gravity.NO_GRAVITY) { lp.gravity = Gravity.NO_GRAVITY; } //切换为横屏 } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) if (lp.gravity != Gravity.CENTER) lp.gravity = Gravity.CENTER; rotationListener.setLayoutParams(lp); } } [/quote]看了你的代码感觉有点乱,说下我的实现思路吧,看你的代码是通过城市名请求得到数据,那你就把你添加的城市名按顺序存在一个数组或者list里面,当你在viewpager切换的时候判断当前的页面position是数组的哪一个城市,然后加载显示。
陈小缘 2015-10-30
  • 打赏
  • 举报
回复
引用 9 楼 u013377714 的回复:
引用 8 楼 u011387817 的回复:
引用 5 楼 u013377714 的回复:
[quote=引用 4 楼 u011387817 的回复:] [quote=引用 1 楼 u013377714 的回复:] 相关代码呢?这应该根据你的page页数和数据绑定吧,你再删除后要对相应地页面逻辑作出相应地修改。
我已经做过修改了,还是不行
你不给我们看代码怎么知道你错哪了。。。
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); HorizontalScrollView.LayoutParams lp = (HorizontalScrollView.LayoutParams) rotationListener.getLayoutParams(); //切换为竖屏 if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { if (lp.gravity != Gravity.NO_GRAVITY) { lp.gravity = Gravity.NO_GRAVITY; } //切换为横屏 } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) if (lp.gravity != Gravity.CENTER) lp.gravity = Gravity.CENTER; rotationListener.setLayoutParams(lp); } } [/quote]看了你的代码感觉有点乱,说下我的实现思路吧,看你的代码是通过城市名请求得到数据,那你就把你添加的城市名按顺序存在一个数组或者list里面,当你在viewpager切换的时候判断当前的页面position是数组的哪一个城市,然后加载显示。 [/quote]自己解决不了,注释我已经写好了
陈小缘 2015-10-30
  • 打赏
  • 举报
回复
引用 9 楼 u013377714 的回复:
引用 8 楼 u011387817 的回复:
引用 5 楼 u013377714 的回复:
[quote=引用 4 楼 u011387817 的回复:] [quote=引用 1 楼 u013377714 的回复:] 相关代码呢?这应该根据你的page页数和数据绑定吧,你再删除后要对相应地页面逻辑作出相应地修改。
我已经做过修改了,还是不行
你不给我们看代码怎么知道你错哪了。。。
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); HorizontalScrollView.LayoutParams lp = (HorizontalScrollView.LayoutParams) rotationListener.getLayoutParams(); //切换为竖屏 if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { if (lp.gravity != Gravity.NO_GRAVITY) { lp.gravity = Gravity.NO_GRAVITY; } //切换为横屏 } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) if (lp.gravity != Gravity.CENTER) lp.gravity = Gravity.CENTER; rotationListener.setLayoutParams(lp); } } [/quote]看了你的代码感觉有点乱,说下我的实现思路吧,看你的代码是通过城市名请求得到数据,那你就把你添加的城市名按顺序存在一个数组或者list里面,当你在viewpager切换的时候判断当前的页面position是数组的哪一个城市,然后加载显示。 [/quote]源码地址:https://github.com/wuyr/JwhkWeather能帮我看下吗
陈小缘 2015-10-30
  • 打赏
  • 举报
回复
引用 10 楼 jeabo 的回复:
首先,你代码贴的太多,来回答问题的人看到这么多代码都不想看。 把你的工程上传上来,我给你导入看一下
找不到在哪上传附件,这是github的地址https://github.com/wuyr/JwhkWeather重新写了下注释
陈小缘 2015-10-29
  • 打赏
  • 举报
回复
引用 5 楼 u013377714 的回复:
引用 4 楼 u011387817 的回复:
引用 1 楼 u013377714 的回复:
相关代码呢?这应该根据你的page页数和数据绑定吧,你再删除后要对相应地页面逻辑作出相应地修改。
我已经做过修改了,还是不行
你不给我们看代码怎么知道你错哪了。。。
SubWeatherFragment源码: public class SubWeatherFragment extends android.support.v4.app.Fragment { private final String cityKey = "cityKey"; public static SubWeatherFragment fragment; private SwipeRefreshLayout swipeRefreshLayout; private View view; private LinearLayout rotationListener; private TextView updateTime, temperature, weather, humidity, windDirection, windPower, sunRise, sunSet; private List<Map<String, TextView>> forecasts; private RecommendView comfort, uvPower, apparel, cold, drying, travel, umbrella; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Map<String, String> baseInfo = (Map) ((List) msg.obj).get(0); List<Map<String, String>> forecast = (List) ((List) msg.obj).get(1), recommend = (List) ((List) msg.obj).get(2); updateTime.setText(baseInfo.get("updatetime")); temperature.setText(baseInfo.get("wendu")); humidity.setText(baseInfo.get("shidu")); windDirection.setText(baseInfo.get("fengxiang")); windPower.setText(baseInfo.get("fengli")); sunRise.setText(baseInfo.get("sunrise_1")); sunSet.setText(baseInfo.get("sunset_1")); weather.setText(forecast.get(0).get(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) > 18 ? "type2" : "type")); comfort.setValue(recommend.get(1).get("value").trim()); comfort.setInfo(recommend.get(1).get("detail").trim()); uvPower.setValue(recommend.get(6).get("value").trim()); uvPower.setInfo(recommend.get(6).get("detail").trim()); apparel.setValue(recommend.get(2).get("value").trim()); apparel.setInfo(recommend.get(2).get("detail").trim()); cold.setValue(recommend.get(3).get("value").trim()); cold.setInfo(recommend.get(3).get("detail").trim()); drying.setValue(recommend.get(4).get("value").trim()); drying.setInfo(recommend.get(4).get("detail").trim()); travel.setValue(recommend.get(5).get("value").trim()); travel.setInfo(recommend.get(5).get("detail").trim()); umbrella.setValue(recommend.get(10).get("value").trim()); umbrella.setInfo(recommend.get(10).get("detail").trim()); long currentTime; for (int i = 0; i < forecasts.size(); i++) { currentTime = System.currentTimeMillis() + ((86400 * 1000) * i); forecasts.get(i).get("data").setText(new SimpleDateFormat("M月dd日").format(new Date(currentTime))); forecasts.get(i).get("week").setText(new SimpleDateFormat("E").format(new Date(currentTime))); forecasts.get(i).get("lowHigh").setText((forecast.get(i).get("low")). substring(3, forecast.get(i).get("low").length() - 1) + "~" + (forecast.get(i).get("high")). substring(3, forecast.get(i).get("high").length())); forecasts.get(i).get("day").setText(forecast.get(i).get("type")); forecasts.get(i).get("night").setText(forecast.get(i).get("type2")); } if (swipeRefreshLayout.isRefreshing()) { swipeRefreshLayout.setRefreshing(false); Toast toast = Toast.makeText(WeatherActivity.activity, "更新天气成功", Toast.LENGTH_SHORT); View view = toast.getView(); view.setBackgroundResource(R.color.toast_background); toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 15); toast.setView(view); toast.show(); } } }; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view = inflater.inflate(R.layout.weather_info, container, false); initSwipeRefreshLayout(); initModules(); if (getArguments().getString(cityKey) != null) { Log.e("onCreateView", getArguments().getString(cityKey)); refreshData(getArguments().getString(cityKey)); } fragment = this; return view; } private void initSwipeRefreshLayout() { swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.weather_refresh); swipeRefreshLayout.setColorScheme(android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light, android.R.color.holo_blue_light); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refreshData(getArguments().getString(cityKey)); } }); } @Override public void onDestroyView() { Log.e("onDestroyView", getArguments().getString(cityKey)); super.onDestroyView(); } @Override public void onDestroy() { Log.e("onDestroy", getArguments().getString(cityKey)); super.onDestroy(); } @Override public void onDetach() { Log.e("onDestroy", getArguments().getString(cityKey)); super.onDetach(); } private void initModules() { /* 初始化rotationListener 主要用来当屏幕切换为横屏时对齐方式为中部居中 */ rotationListener = (LinearLayout) view.findViewById(R.id.listener_orientation); updateTime = (TextView) view.findViewById(R.id.update_time); //实例化主天气界面下的各种组件 updateTime = (TextView) view.findViewById(R.id.update_time); temperature = (TextView) view.findViewById(R.id.temperature); weather = (TextView) view.findViewById(R.id.weather); humidity = (TextView) view.findViewById(R.id.humidity); windDirection = (TextView) view.findViewById(R.id.wind_direction); windPower = (TextView) view.findViewById(R.id.wind_power); sunRise = (TextView) view.findViewById(R.id.sun_rise); sunSet = (TextView) view.findViewById(R.id.sun_set); comfort = (RecommendView) view.findViewById(R.id.comfort); uvPower = (RecommendView) view.findViewById(R.id.uv_power); apparel = (RecommendView) view.findViewById(R.id.apparel); cold = (RecommendView) view.findViewById(R.id.cold); drying = (RecommendView) view.findViewById(R.id.drying); travel = (RecommendView) view.findViewById(R.id.travel); umbrella = (RecommendView) view.findViewById(R.id.umbrella); initForecast(); } private void initForecast() { forecasts = new ArrayList<>(); TextView date, week, lowHigh, day, night; int id; for (int i = 0; i < 5; i++) { Map<String, TextView> tmp = new HashMap<>(); switch (i) { case 0: id = R.id.day_1; break; case 1: id = R.id.day_2; break; case 2: id = R.id.day_3; break; case 3: id = R.id.day_4; break; case 4: id = R.id.day_5; break; default: id = -1; } if (id != -1) { date = (TextView) view.findViewById(id).findViewById(R.id.date); tmp.put("data", date); week = (TextView) view.findViewById(id).findViewById(R.id.week); tmp.put("week", week); lowHigh = (TextView) view.findViewById(id).findViewById(R.id.low_high); tmp.put("lowHigh", lowHigh); day = (TextView) view.findViewById(id).findViewById(R.id.day); tmp.put("day", day); night = (TextView) view.findViewById(id).findViewById(R.id.night); tmp.put("night", night); forecasts.add(tmp); } } } public void refreshData(String cityKey) { HttpUtil.sendGet("http://wthrcdn.etouch.cn/WeatherApi?citykey=" + cityKey, new HttpCallBackListener() { @Override public void onFinish(String data) { Message message = new Message(); message.what = 0; message.obj = new ParseXML().parse(data); handler.sendMessage(message); } @Override public void onError(Exception e) { Log.e("setGet", "onError", e); e.printStackTrace(); Looper.prepare(); Toast toast = Toast.makeText(WeatherActivity.activity, "更新天气失败,请检查网络", Toast.LENGTH_LONG); View view = toast.getView(); view.setBackgroundResource(R.color.toast_background); toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 15); toast.setView(view); toast.show(); Looper.loop(); } }); }
陈小缘 2015-10-29
  • 打赏
  • 举报
回复
引用 5 楼 u013377714 的回复:
引用 4 楼 u011387817 的回复:
引用 1 楼 u013377714 的回复:
相关代码呢?这应该根据你的page页数和数据绑定吧,你再删除后要对相应地页面逻辑作出相应地修改。
我已经做过修改了,还是不行
你不给我们看代码怎么知道你错哪了。。。
续上,超字数了 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); HorizontalScrollView.LayoutParams lp = (HorizontalScrollView.LayoutParams) rotationListener.getLayoutParams(); //切换为竖屏 if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { if (lp.gravity != Gravity.NO_GRAVITY) { lp.gravity = Gravity.NO_GRAVITY; } //切换为横屏 } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) if (lp.gravity != Gravity.CENTER) lp.gravity = Gravity.CENTER; rotationListener.setLayoutParams(lp); } }
陈小缘 2015-10-29
  • 打赏
  • 举报
回复
引用 5 楼 u013377714 的回复:
引用 4 楼 u011387817 的回复:
引用 1 楼 u013377714 的回复:
相关代码呢?这应该根据你的page页数和数据绑定吧,你再删除后要对相应地页面逻辑作出相应地修改。
我已经做过修改了,还是不行
你不给我们看代码怎么知道你错哪了。。。
CustomFragmentPagerAdapter源码: import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import com.jwhk.weather.activity.WeatherActivity; import com.jwhk.weather.fragment.SubWeatherFragment; import com.jwhk.weather.utils.DataBasesUtil; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CustomFragmentPagerAdapter extends FragmentPagerAdapter { public List<String> addedCityName; public Map<String, SubWeatherFragment> pages; public CustomFragmentPagerAdapter(FragmentManager fm) { super(fm); addedCityName = new ArrayList<>(); pages = new HashMap<>(); } @Override public Fragment getItem(int position) { return pages.get(addedCityName.get(position)); } @Override public int getCount() { return pages.size(); } public synchronized boolean addPage(final String cityName) { if (addedCityName.contains(cityName)) return false; addedCityName.add(cityName); SubWeatherFragment fragment = new SubWeatherFragment(); Bundle bundle = new Bundle(); bundle.putString("cityKey", DataBasesUtil.queryCode(cityName).get(0)); //bundle.putString("cityName", cityName); fragment.setArguments(bundle); pages.put(cityName, fragment); return true; } public synchronized boolean removePage(String cityName) { if (addedCityName.size() > 1) { WeatherActivity.removeCity(cityName); addedCityName.remove(cityName); pages.remove(cityName); return true; } return false; } @Override public int getItemPosition(Object object) { return POSITION_NONE; } } ---------------------------------------------------------------------------------- ---------------------------------------------------------------------------------- WeatherFragment源码: import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.jwhk.weather.R; import com.jwhk.weather.activity.WeatherActivity; import com.jwhk.weather.adapter.CustomFragmentPagerAdapter; import com.jwhk.weather.custom.TopView; public class WeatherFragment extends android.support.v4.app.Fragment { public static CustomFragmentPagerAdapter adapter; public static ViewPager pager; private static View view; static { adapter = new CustomFragmentPagerAdapter(WeatherActivity.activity.getSupportFragmentManager()); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view = inflater.inflate(R.layout.weather_fragment_view, container, false); initViewPager(); return view; } public static void initViewPager(){ pager = (ViewPager) view.findViewById(R.id.view_pager); pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { TopView.setTitleName(WeatherFragment.adapter.addedCityName.get(position)); } @Override public void onPageScrollStateChanged(int state) { } }); pager.setAdapter(adapter); } }
水的川 2015-10-29
  • 打赏
  • 举报
回复
引用 4 楼 u011387817 的回复:
引用 1 楼 u013377714 的回复:
相关代码呢?这应该根据你的page页数和数据绑定吧,你再删除后要对相应地页面逻辑作出相应地修改。
我已经做过修改了,还是不行
你不给我们看代码怎么知道你错哪了。。。
shangguansb 2015-10-29
  • 打赏
  • 举报
回复
pager上套的是什么容器?
陈小缘 2015-10-29
  • 打赏
  • 举报
回复
引用 2 楼 u012608587 的回复:
pager上套的是什么容器?
pager上面是fragment,fragment上面是activity
陈小缘 2015-10-29
  • 打赏
  • 举报
回复
引用 1 楼 u013377714 的回复:
相关代码呢?这应该根据你的page页数和数据绑定吧,你再删除后要对相应地页面逻辑作出相应地修改。
我已经做过修改了,还是不行
水的川 2015-10-28
  • 打赏
  • 举报
回复
相关代码呢?这应该根据你的page页数和数据绑定吧,你再删除后要对相应地页面逻辑作出相应地修改。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧