android 怎么刷新fragment页面
1个回答
展开全部
通常的做法,在onResume中发一起一个异步的请求去拿数据,通过回调,收到返回的数据,然后更新UI。
网络获取数据结束判断数据有更新,然后通过set将控件的数据更新。
举例:
这个是我的一个fragment,我把它放在一个viewpager的fragment中
public class BottomFragmentOne extends BaseFragment {
private static final String ARG_CITY = "city";
private String mCity;
private TextView tmpD;
private TextView tmpN;
private SimpleDraweeView imageD;
private SimpleDraweeView imageN;
public static BottomFragmentOne newInstanceOne(String city) {
BottomFragmentOne fragment = new BottomFragmentOne();
Bundle args = new Bundle();
args.putString(ARG_CITY, city);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mCity = getArguments().getString(ARG_CITY);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.bottom_fragment_one, container, false);
tmpD= (TextView) view.findViewById(R.id.tmp_d);
tmpN = (TextView) view.findViewById(R.id.tmp_n);
imageD= (SimpleDraweeView) view.findViewById(R.id.image_d);
imageN= (SimpleDraweeView) view.findViewById(R.id.image_n);
setUI();
return view;
}
//用来设置UI,更新UI时重新从数据库获取数据,进行设置
public void setUI(){
Forecast forecast = mFrecastDao.getForecastByCity(mCity);
tmpD.setText(forecast.getDaily_1_max());
tmpN.setText(forecast.getDaily_1_min());
imageD.setImageURI(getImageUri(forecast.getDaily_1_code_d()));
imageN.setImageURI(getImageUri(forecast.getDaily_1_code_n()));
}
}
2.除了上面那个,还有一个类似的fragment,我想实按下按钮后,在两个fragment之间进行切换。下面是按钮代码
@Override
public void onClick(View v) {
FragmentManager fm = getChildFragmentManager();
// 开启Fragment事务
FragmentTransaction transaction = fm.beginTransaction();
switch (v.getId())
{
case R.id.button_left:
if (mBottomOne == null)
{
mBottomOne = new BottomFragmentOne().newInstanceOne(mCity);
}// 使用当前Fragment的布局替代id_content的控件
transaction.replace(R.id.bottom_weather,mBottomOne);
break;
case R.id.button_right:
if (mBottomTwo == null)
{
mBottomTwo = new BottomFragmentTwo().newInstanceTwo(mCity);
}
transaction.replace(R.id.bottom_weather, mBottomTwo);
break;
}
// 事务提交
transaction.commit();
}
3.下面是viewpager中的fragment用来更新上面两个fragment数据的方法。
public void UpdateUI(){
mBottomOne.setUI();
mBottomTwo.setUI();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询