Activity和Fragment怎么刷新界面
如果现在通过网络取得了新的数据,需要通过刷新显示在界面上,应该怎么做比较好呢?
我现在只会find控件,然后用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()));
}
}
除了上面那个,还有一个类似的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();
}
下面是viewpager中的fragment用来更新上面两个fragment数据的方法。
//在这里更新UI
@Override
public void UpdateUI(){
mBottomOne.setUI();
mBottomTwo.setUI();
}
现在的问题是我刷新数据后,两个fragment的ui重叠了!!!!
Morris
9 years, 8 months ago
Answers
http://segmentfault.com/a/1190000003702775
感觉适合新手,你慢慢开发就会接触到更多优秀的框架,就可以不用那么麻烦了
transaction.replace(R.id.bottom_weather,mBottomOne);
后面添加commit()
771122
answered 9 years, 8 months ago