抽屉手柄更换手柄背景脱落

时间:2019-03-02 04:23:34

标签: android drawer

这个问题有很多次,但是我没有得到有关如何更改手柄背景的答案。

到目前为止,我得到的是:

Close Drawer

关闭抽屉

Open Drawer

打开抽屉

打开“抽屉”后,把手进入抽屉内部,我一直将抽屉保持在全屏状态。

现在的问题是,当抽屉打开时,手柄应将其布局从圆形android标志更改为方形android标志。

这是代码:

MainActivty:

public class MainActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
ListView mDrawerList;
LinearLayout mDrawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    mDrawerLayout = (DrawerLayout) findViewById( R.id.drawer_layout );
    mDrawer = findViewById( R.id.drawer );
    mDrawerList = (ListView) findViewById( R.id.drawer_list );

    String[] list = {"hello", "One"};

    if (mDrawerLayout.isDrawerOpen( GravityCompat.END )){
        DrawerHandle.attach( mDrawer, R.layout.layout_handle, 0.5f );
    }else {
        DrawerHandle.attach( mDrawer, R.layout.layout_handle_2, 0.5f );
    }
  }
}

DrawerHandle.class:

public class DrawerHandle implements DrawerLayout.DrawerListener {
public static final String TAG = "DrawerHandle";

private ViewGroup mRootView;
private DrawerLayout mDrawerLayout;
private View mHandle;
private View mDrawer;

private float mVerticalOffset;
private int mGravity;
private WindowManager mWM;
private Display mDisplay;
private Point mScreenDimensions = new Point();

private OnClickListener mHandleClickListener = new OnClickListener(){

    @Override
    public void onClick(View v) {
        if(!mDrawerLayout.isDrawerOpen(mGravity)) mDrawerLayout.openDrawer(mGravity);
        else mDrawerLayout.closeDrawer(mGravity);
    }

};

private OnTouchListener mHandleTouchListener = new OnTouchListener() {
    private static final int MAX_CLICK_DURATION = 200;
    private long startClickTime;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                startClickTime = System.currentTimeMillis();
                break;
            }
            case MotionEvent.ACTION_UP: {
                if(System.currentTimeMillis() - startClickTime < MAX_CLICK_DURATION) {
                    v.performClick();
                    return true;
                }
            }
        }
        MotionEvent copy = MotionEvent.obtain(event);
        copy.setEdgeFlags(ViewDragHelper.EDGE_ALL);
        copy.setLocation(event.getRawX() + (mGravity == Gravity.LEFT || mGravity == GravityCompat.START ? -mHandle.getWidth()/2 : mHandle.getWidth() / 2), event.getRawY());
        mDrawerLayout.onTouchEvent(copy);
        copy.recycle();
        return true;
    }
};

private int getDrawerViewGravity(View drawerView) {
    final int gravity = ((DrawerLayout.LayoutParams) drawerView.getLayoutParams()).gravity;
    return GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(drawerView));
}

private float getTranslation(float slideOffset){
    return (mGravity == GravityCompat.START || mGravity == Gravity.LEFT) ? slideOffset*mDrawer.getWidth() : -slideOffset*mDrawer.getWidth();
}

private void updateScreenDimensions() {

    if (Build.VERSION.SDK_INT >= 13) {
        mDisplay.getSize(mScreenDimensions);
    } else {
        mScreenDimensions.x = mDisplay.getWidth();
        mScreenDimensions.y = mDisplay.getHeight();
    }
}

public DrawerHandle(DrawerLayout drawerLayout, View drawer, int handleLayout, float handleVerticalOffset) {
    mDrawer = drawer;
    mGravity = getDrawerViewGravity(mDrawer);
    mDrawerLayout = drawerLayout;
    mRootView = (ViewGroup)mDrawerLayout.getRootView();
    LayoutInflater inflater = (LayoutInflater) mDrawerLayout.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    mHandle = inflater.inflate(handleLayout, mRootView, false);
    mWM = (WindowManager) mDrawerLayout.getContext().getSystemService(Context.WINDOW_SERVICE);
    mDisplay = mWM.getDefaultDisplay();

    mHandle.setOnClickListener(mHandleClickListener);
    mHandle.setOnTouchListener(mHandleTouchListener);
    mRootView.addView(mHandle, new FrameLayout.LayoutParams(mHandle.getLayoutParams().width, mHandle.getLayoutParams().height, mGravity));
    setVerticalOffset(handleVerticalOffset);
    mDrawerLayout.setDrawerListener(this);
}

public static DrawerHandle attach(View drawer, int handleLayout, float verticalOffset) {
    if (!(drawer.getParent() instanceof DrawerLayout)) throw new IllegalArgumentException("Argument drawer must be direct child of a DrawerLayout");
    return new DrawerHandle((DrawerLayout)drawer.getParent(), drawer, handleLayout, verticalOffset);
}

public static DrawerHandle attach(View drawer, int handleLayout) {
    return attach(drawer, handleLayout, 0);
}

@Override
public void onDrawerClosed(View arg0) {
}

@Override
public void onDrawerOpened(View arg0) {

    mHandle.setX( 10 );
}

@Override
public void onDrawerSlide(View arg0, float slideOffset) {
    float translationX = getTranslation(slideOffset);
    mHandle.setTranslationX(translationX);
}

@Override
public void onDrawerStateChanged(int arg0) {

}

public View getView(){
    return mHandle;
}

public View getDrawer() {
    return mDrawer;
}

public void setVerticalOffset(float offset) {
    updateScreenDimensions();
    mVerticalOffset = offset;
    mHandle.setY(mVerticalOffset*mScreenDimensions.y);
  }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:orientation="vertical"
    android:padding="20dp"
    android:layout_marginRight="-67dp"
    android:background="#CCFF0000"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Collection"
        android:paddingBottom="20dp"
        />
    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:layout_weight="1"
        />
</LinearLayout>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.v4.widget.DrawerLayout>

layout_handle.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@mipmap/ic_launcher_square" />
</android.support.constraint.ConstraintLayout>

layout_handle_2.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@mipmap/ic_launcher_round" />
</android.support.constraint.ConstraintLayout>

我坚持这个问题,只是帮帮我。

请告诉我您的建议。

先谢谢了。

0 个答案:

没有答案