微信公众号:李斯特先生
###简单封装BaseFragment titleBar
1、BaseFragment 加载layout 初始化fragment布局
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
contentView = initView();
if (contentView == null) {
contentView = inflater.inflate(bindLayout(), null, false);
}
if (fagment_title_layout != 0 && contentView instanceof ViewGroup) {
titleBar = (ViewGroup) LayoutInflater.from(mActivity).inflate(fagment_title_layout, null);
titleBar.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.title_height)));
((ViewGroup) contentView).addView(titleBar);
}
initView();
initData();
return contentView;
}
2、简单封装title 按钮
/**
* 设置标题 左边按钮
* @param resId
* @param resourceId
* @param onClickListener
*/
public void setTitleLeftImage(int resId,int resourceId,View.OnClickListener onClickListener){
if(titleBar== null){
ImageView ivLeft = contentView.findViewById(resId);
if(resourceId != 0){
ivLeft.setImageResource(resourceId);
}
ivLeft.setOnClickListener(onClickListener);
ivLeft.setVisibility(View.VISIBLE);
}
}
/**
* 设置标题 名称
* @param resId
* @param title
*/
public void setTitleText(int resId,String title){
if(titleBar == null){
TextView tvTitle = contentView.findViewById(resId);
tvTitle.setSelected(true);
tvTitle.setText(title);
tvTitle.setVisibility(View.VISIBLE);
}
}
/***
* 设置标题 右边按钮
* @param resId
* @param resourceId
* @param onClickListener
*/
public void setTitleRightImage(int resId,int resourceId, View.OnClickListener onClickListener){
if(titleBar == null){
Button buRight = contentView.findViewById(resId);
if(resourceId != 0){
buRight.setBackgroundResource(resourceId);
}
buRight.setOnClickListener(onClickListener);
buRight.setVisibility(View.VISIBLE);
}
}
3、public_fragment_title.xml
<ImageView
android:id="@+id/back"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:visibility="gone"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:visibility="gone"
android:layout_centerInParent="true"
android:textSize="@dimen/font_size_big"/>
<Button
android:id="@+id/add"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:visibility="gone"
android:layout_marginRight="10dp"/>
</RelativeLayout>
4、复写BaseFragment
public class HomeFragment extends BaseFragment {
@Override
public View initView() {
return null;
}
@Override
public int bindLayout() {
return R.layout.fragment_home;
}
@Override
public void initData() {
//title布局
setTitleLayoutId(R.layout.public_fragment_title);
//title栏左边按钮
setTitleLeftImage(R.id.back, R.mipmap.back_gray, this);
//title名称
setTitleText(R.id.title, "标题");
//title栏右边按钮
setTitleRightImage(R.id.add, R.mipmap.add_contact, this);
}
@Override
public void onViewClick(View view) {
switch (view.getId()) {
case R.id.back:
Toast.makeText(getActivity(), "okoko", Toast.LENGTH_SHORT).show();
break;
case R.id.add:
Toast.makeText(getActivity(), "ppppppp", Toast.LENGTH_SHORT).show();
break;
}
}
}