4

我的抽屜中出現點擊式點擊效果時出現問題。帶有NavigationView的DrawerLayout - 在點擊時不顯示連鎖效果

我有活性的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" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer"/> 

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

正如你可以看到我把NavigationView到我DrawerLayout,然後使用菜單中的XML文件填充NavigationView

activity_main_drawer.xml如下所示:

<menu 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 
<group> 
    <item 
     android:id="@+id/id1" 
     android:title="Item 1"/> 
    <item 
     android:id="@+id/id2" 
     android:title="Item 2"/> 
    <item 
     android:id="@+id/id3" 
     android:title="Item 3"/> 
    <item 
     android:id="@+id/id4" 
     android:title="Item 4"/> 
</group> 

不幸的是,當我這樣做的,我沒有點擊項目在抽屜菜單之後的任何視覺效果。我想要的是所謂的「漣漪效應」。我該怎麼做?

+0

什麼版本的操作系統,你的測試呢? – azizbekian

+0

@azizbekian我使用Android 7.1。2 – michalsol

回答

1

紋波效應可以通過以下方式添加。

添加itemBackground到NavigationView裏面你的XML文件中的以下屬性:

應用:itemBackground = 「@繪製/ ripple_navigation_selector」

此外,繪製-V21夾內,加ripple_navigation_selector。 xml

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
     android:color="@color/ripplecolor" > 
     <item android:drawable="@color/accentColor" /> 
     <item 
      android:id="@android:id/mask" 
      android:drawable="@android:color/white" /> 
</ripple> 
+0

不幸的是,漣漪效應仍然無法解決,此解決方案無效 – michalsol

1

如果您使用的是appcompat,您可以將其添加到導航抽屜項目的XML中。

上述解決方案取自此answer

希望這有助於你。

+0

它不起作用。它只是使抽屜變得透明,但連鎖效果仍然不可見 – michalsol

0

這可能是由於性能下降,嘗試在onClick上做任何事情之前添加200-300毫秒的延遲。

public void itemClicked(MenuItem item) { 
    Handler theHandler = new Handler(); 
    theHandler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        //whatever you wish to do 
       } 
      }, 200); 
} 

不用擔心這麼多延遲不會對用戶可見,但會順利顯示連鎖反應。

+0

這不是原因。解決方案沒有幫助。 – michalsol

2

我也面臨這個問題

請刪除此屬性:

點擊=真

focusableOnTouch =真

然後應用連鎖反應XML

<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:color="@android:color/transparent" 
tools:targetApi="lollipop"> <!-- ripple color --> 

<item android:drawable="@android:color/transparent" /> <!-- normal color --> 

我希望這將有助於你

+1

刪除'clickable = true'和'focusableOnTouch = true'是什麼意思?我沒有這些屬性。 但是,您的解決方案也行不通 - 仍然沒有連鎖反應:( – michalsol

+0

爲什麼這個答案收到了兩個upvotes和25個賞金點??這沒有任何意義。 –

相關問題