简单封装BaseFragment titleBar

微信公众号:李斯特先生

###简单封装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;
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值