家族的冲突像NavigationView拉箭头

时间:2018-04-12 08:32:51

标签: android drawerlayout navigationview

我想创建一个在侧抽屉关闭时保持可见的自定义视图,但通过拉动它,抽屉将滑动。 我在非常受欢迎的游戏Clash Of Clan中看到过如下图所示:

See the arrow on left edge Pulled side view by dragging that arrow

现在我想知道使用DrawerLayoutNavigationView是否有任何方法可以达到目的?

1 个答案:

答案 0 :(得分:1)

最后,我通过添加addDrawerListener并相应地翻译HANDLE来实现这一目标。

NavigationViewWithHandle

这是我的活动:

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

/**
 * @author Meet Vora
 */
public class PullSliderActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private View viewPuller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_slider);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        viewPuller = findViewById(R.id.viewPuller);

        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                viewPuller.setTranslationX(drawerView.getX() + drawerView.getWidth());
            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

}

这是LAYOUT XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/MyHelpActivityTheme">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:openDrawer="start">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#49fcea"
            android:gravity="center"
            android:text="This is MAIN Screen"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:textStyle="bold"/>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigationView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#223344"
                android:gravity="center"
                android:text="This is NAVIGATION View"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:textStyle="bold"/>

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

    <TextView
        android:id="@+id/viewPuller"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#5f97cf"
        android:clickable="false"
        android:gravity="center"
        android:padding="4dp"
        android:text="N\n.\nV\nI\nE\nW"
        android:textColor="@color/white"
        android:textSize="10sp"
        android:textStyle="bold"/>
</RelativeLayout>