首页 > Activity和Fragment怎么刷新界面

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重叠了!!!!


网络获取数据结束判断数据有更新,然后通过set将控件的数据更新。


无论是activity,还是fragment,不都有自己的生命周期函数吗,虽然fragment的特殊点。

通常的做法,在onResume中发一起一个异步的请求去拿数据,通过回调,收到返回的数据,然后更新UI。


如果你用的是Android studio 1.3版本以上的话,你可以去看下databinding,并不是之前常用的MVC结构,是MVVM结构,用viewmodel代替了之前的controller一层来做view的数据刷新


http://.com/a/1190000003702775
感觉适合新手,你慢慢开发就会接触到更多优秀的框架,就可以不用那么麻烦了

transaction.replace(R.id.bottom_weather,mBottomOne);
后面添加commit()

【热门文章】
【热门文章】