##BindView ButterKnife 優勢 綁定組件方便,使用簡單 處理點擊事件方便,如adapter中的viewholder 同時父組件綁定後子組件無需綁定 註意 在setcontentview之後使用,且子空間不可再使用static final屬性 ##在不改變按鈕圖片大小的情況,擴大點 ...
BindView ButterKnife
優勢
綁定組件方便,使用簡單
處理點擊事件方便,如adapter中的viewholder
同時父組件綁定後子組件無需綁定
註意
在setcontentview之後使用,且子空間不可再使用static final屬性
在不改變按鈕圖片大小的情況,擴大點擊事件,
在較低Android版本此方法可能有問題,即src和setbackground的區別,同樣可以設置背景圖片,但src僅將圖片資源載入,不做其他處理,而setbackground會使圖片自適應與按鈕大小,但也有具體屬性設置,imagebutton相比常規button對圖片設置更加詳細,但對字處理較弱。
recyclerview設置滾動條自動將點擊項滾動到中間方法##
- 重寫linerlayout類,如下代碼:
public CenterLayoutManager(Context context) {
super(context);
}
public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CenterLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private static class CenterSmoothScroller extends LinearSmoothScroller {
CenterSmoothScroller(Context context) {
super(context);
}
@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
}
}
註意重寫lanerlayoutmanager類的方法,與繪製組件較相似
- 在點擊事件中設置centersmoothscroller的方法
- 同時在記得為recycler適配器設置上你的自定義佈局
Popwindow配合recycler使用設置
popwindow一般不直接使用,配合recycler可以有更加多變的使用方法,popwindow與dialog傳統對話框區別可以在任意位置顯示,更加靈活,
與recycler結合
View view = LayoutInflater.from(getContext()).inflate(R.layout.pop_classifyfragment,null);
RecyclerView recyclerView =(RecyclerView) view.findViewById(R.id.pop_classifyfragment_list);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(),4));
Classify_PopAdapter popAdapter =new Classify_PopAdapter(getContext(),mList);
recyclerView.setAdapter(popAdapter);
popAdapter.setClickListener(new AdapterClickListener() {
@Override
public void click(int flag, int position) {
}
});
mPopWindow = new PopupWindow(view,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
mPopWindow.setContentView(view);
mPopWindow.showAtLocation(pop_btn, Gravity.NO_GRAVITY, 187, 72);
MWindowManager.init(getActivity()).lightoff();
mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//在dismiss中恢復透明度
public void onDismiss() {
MWindowManager.init(getActivity()).lighton();
}
});
}
原理為計算每一項的中心線位置,與視窗的中心線位置相減,計算出偏移量。而中心線位置由 (尾-頭)/2+頭 計算得出。
一般為額外寫一個類,不直接在activity中寫,會造成冗餘。
同時還需建立adapter類,便於設置數據,layoutparams還可設置popwindow的大小,展示時通過showatlocation後兩個屬性設置與原點偏移量,如第三個屬性為gravity.no_gravity預設會在左上角顯示原點,所以應根據實際情況設置參數
MWindow.init 方法
activity並不直接控制視圖,控制視圖的為window類,它有介面MWindow供使用,使用MWindow.init.lightoff() 方法和類似lighton()方法可以實現popwindwo打開關閉時主activity狀態的明暗變化,此時註意界面層次為activity、popwindow的父佈局如linerlayout、popwindow的子控制項。