Android--custom toolbar (Fragment)

public class ContactsFragment extends BaseFragment {
    /**
     * Flag bit, the flag has been initialized
     */
    private boolean isPrepared;
    /**
     * Whether it has been loaded once, and will not request data for the second time
     */
    private boolean mHasLoadedOnce;
    private TextView textView;
    private Toolbar toolbar;
    SimpleToolbar simpleToolbar;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            // Need to inflate a layout file to fill Fragment
            mView = inflater.inflate(R.layout.fragment_contacts, container, false);
            initView();
            isPrepared = true;
// implement lazy loading
            lazyLoad();
        }
        //The cached mView needs to determine whether a parent has been added. If there is a parent, it needs to be deleted from the parent, otherwise an error will occur that the mView already has a parent.
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }

        return mView;
    }
    /**
     * Initialize the control
     */
    private void initView() {
        simpleToolbar = find(R.id.toolbar_contacts);
        simpleToolbar.setMainTitle("Contact");

        simpleToolbar.setRightTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MyApplication.getContext(),"plus",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        // fill in the data of each control

        mHasLoadedOnce = true;
    }

    public static ContactsFragment newInstance(String param1) {
        ContactsFragment fragment = new ContactsFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }

}

public class SimpleToolbar extends Toolbar {
    /**
     * Intermediate Title
     */
    private TextView mTxtMiddleTitle;
    /**
     * Right Title
     */
    private TextView mTxtRightTitle;

    public SimpleToolbar(Context context) {
        super(context);
    }

    public SimpleToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SimpleToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTxtMiddleTitle = (TextView) findViewById(R.id.middle_title);
        mTxtRightTitle = (TextView) findViewById(R.id.right_title);
    }

    //Set the content of the middle title
    public void setMainTitle(String text) {
        this.setTitle(" ");
        mTxtMiddleTitle.setVisibility(View.VISIBLE);
        mTxtMiddleTitle.setText(text);
    }

    //Set the color of the content text of the middle title
    public void setMainTitleColor(int color) {
        mTxtMiddleTitle.setTextColor(color);
    }

    //Set the text to the right of the title
    public void setRightTitleText(String text) {
        mTxtRightTitle.setVisibility(View.VISIBLE);
        mTxtRightTitle.setText(text);
    }

    //Set the text color on the right side of the title
    public void setRightTitleColor(int color) {
        mTxtRightTitle.setTextColor(color);
    }

    //Set the icon on the right side of the title
    public void setRightTitleDrawable(int res) {
        Drawable dwRight = ContextCompat.getDrawable(getContext(), res);
        dwRight.setBounds(0, 0, dwRight.getMinimumWidth(), dwRight.getMinimumHeight());
        mTxtRightTitle.setCompoundDrawables (null, null, dwRight, null);
    }

    //Set the click event on the right side of the title
    public void setRightTitleClickListener(OnClickListener onClickListener){
        mTxtRightTitle.setOnClickListener(onClickListener);
    }

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <view.SimpleToolbar
        android:id="@+id/toolbar_contacts"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" >
        <TextView
            android:id="@+id/middle_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="20sp"
            android:textColor="@color/white"
            />
        <TextView
            android:id="@+id/right_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"
            android:drawableRight="@drawable/plus"
            android:gravity="center"
            android:textColor="#ffffff"
            android:textSize="16sp"
            android:visibility="visible"
            />
    </view.SimpleToolbar>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="contacts"
        />
</LinearLayout>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325887613&siteId=291194637