常用UI细节

2016-09-17  本文已影响57人  CatDog118

TextView

Bitmap&Factory

ImageView

ListView

ViewPager

startUpdate()
destroyItem()           // 先删除一个废弃的
instantiateItem()       // 创建一个即将使用的
setPrimaryItem()        // 多次调用,可以用来保存当前对象
finishUpdate()          // 多次调用
saveState()
restoreState()

Fragment

  1. FragmentActivity防止重复创建

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        MyFragment myFragment;
        if (savedInstanceState == null) {
            myFragment = MyFragment.newInstance();
            // add fragment
        }
        
        // 或者
        MyFragment myFragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (myFragment == null) {
            myFragment = MyFragment.newInstance();
            // add fragment
        }
    }
    
  2. Fragment声明周期

    public class MyFragment extends BaseFragment {
        
        private Persenter mPersenter;
        private Listener mListener;
        
        public MyFragment() {
            setRetainInstance(true);
        }
        
        public static MyFragment newInstance() {
            return new MyFragment();
        }
        
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            if (activity instanceof Listener) {
                mListener = (Listener) activity;
            }
        }
        
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
        
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.xxx, container, false);
            initView(); // ButterKnife.inject(this, view);
            return rootView;
        }
        
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            if (savedInstanceState == null) {
                this.loadUserDetails();
            }
        }
        
        public void onDestroyView() {
            super.onDestroyView();
            listview.setAdapter(null);
            ButterKnife.reset(this);
        }
        
        public void onDestroy() {
            super.onDestroy();
            this.mPresenter.destroy();  // view = null; unsubscribe();
        }
        
        public void onDetach() {
            super.onDetach();
            mPersenter = null;
        }
    }
    

android:configChanges="orientation|screenSize"

上一篇 下一篇

猜你喜欢

热点阅读