2017-07-25 32 views
0

我是初学者,我试图从该库实行浮动搜索查看浮动搜索查看:Floating Search View我不能把一个监听汉堡包按钮和左右键点击使用阿里莫蒂库

我需要将这个汉堡包按钮连接到我的抽屉视图和正确的按钮。

听汉堡包按钮点击:

mSearchView.setOnLeftMenuClickListener(
     new FloatingSearchView.OnLeftMenuClickListener() { ...}); 

要快速您NavigationDrawer连接到汉堡包按钮:

mSearchView.attachNavigationDrawerToMenuButton(mDrawerLayout); 

倾听项选择

mSearchView.setOnMenuItemClickListener(new FloatingSearchView.OnMenuItemClickListener() { 
     @Override 
     public void onMenuItemSelected(MenuItem item) {     

     } 
    }); 

有些例子,但是当我把它放在我的代码上时,给我带来一个错误。也许问题是我不知道应该在哪里写。

请帮助我,我一直在寻找并尝试数周。

enter image description here

+0

试图实现这种行为,或者你只是使用这个库? – j2ko

+0

也发布你正在得到的错误 – j2ko

+0

我写这文学(例子),但我不知道在哪里如何使这项工作,这是错误。 –

回答

0

所以在这里完成例如:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.j2ko.webviewapp"> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".FloatingSearchActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

FloatingSearchActivity布局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- The main content view --> 
    <RelativeLayout 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:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     tools:context="com.j2ko.webviewapp.FloatingSearchActivity" 
     tools:showIn="@layout/activity_floating_search"> 

     <com.arlib.floatingsearchview.FloatingSearchView 
      android:id="@+id/floating_search_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:floatingSearch_searchBarMarginLeft="10dp" 
      app:floatingSearch_searchBarMarginTop="10dp" 
      app:floatingSearch_searchBarMarginRight="10dp" 
      app:floatingSearch_searchHint="Search..." 
      app:floatingSearch_suggestionsListAnimDuration="250" 
      app:floatingSearch_showSearchKey="false" 
      app:floatingSearch_leftActionMode="showHamburger" 
      app:floatingSearch_close_search_on_keyboard_dismiss="true"/> 
    </RelativeLayout> 

    <!-- The navigation drawer --> 
    <LinearLayout android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:orientation="vertical" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#c46ec4"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Drower item 1"/> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Drower item 2"/> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Drower item 3"/> 
    </LinearLayout> 
</android.support.v4.widget.DrawerLayout> 

最后FloatingSearchActivity

package com.j2ko.webviewapp; 

import android.os.Bundle; 
import android.os.Parcel; 
import android.os.Parcelable; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.AppCompatActivity; 

import com.arlib.floatingsearchview.FloatingSearchView; 
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion; 

import java.util.ArrayList; 
import java.util.List; 

public class FloatingSearchActivity extends AppCompatActivity { 
    FloatingSearchView mSearchView; 
    DrawerLayout mDrawerLayout; 

    //Dummy data for search suggestions 
    private static final List<String> SOME_HARDCODED_DATA; 
    static { 
     SOME_HARDCODED_DATA = new ArrayList<>(); 
     SOME_HARDCODED_DATA.add("One"); 
     SOME_HARDCODED_DATA.add("Two"); 
     SOME_HARDCODED_DATA.add("Three"); 
     SOME_HARDCODED_DATA.add("Four"); 
    } 

    //This just for illustration how to populate suggestions 
    private static class SimpleSuggestions implements SearchSuggestion { 
     private final String mData; 
     public SimpleSuggestions(String string) { 
      mData = string; 
     } 
     @Override 
     public String getBody() { 
      return mData; 
     } 

     @Override 
     public int describeContents() { 
      return 0; 
     } 

     @Override 
     public void writeToParcel(Parcel dest, int flags) { 
      dest.writeString(mData); 
     } 

     public static final Parcelable.Creator<SimpleSuggestions> CREATOR 
       = new Parcelable.Creator<SimpleSuggestions>() { 
      public SimpleSuggestions createFromParcel(Parcel in) { 
       return new SimpleSuggestions(in); 
      } 

      public SimpleSuggestions[] newArray(int size) { 
       return new SimpleSuggestions[size]; 
      } 
     }; 

     private SimpleSuggestions(Parcel in) { 
      mData = in.readString(); 
     } 
    } 

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

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mSearchView = (FloatingSearchView) findViewById(R.id.floating_search_view); 
     mSearchView.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() { 
      @Override 
      public void onSearchTextChanged(String oldQuery, final String newQuery) { 
       List<SearchSuggestion> list = new ArrayList<SearchSuggestion>(); 
       //emulating search on dummy data 
       for (String item : SOME_HARDCODED_DATA) { 
        if (item.contains(newQuery)) { 
         list.add(new SimpleSuggestions(item)); 
        } 
       } 
       mSearchView.swapSuggestions(list); 
      } 
     }); 

     //now search view controlling drawer open/closer 
     mSearchView.attachNavigationDrawerToMenuButton(mDrawerLayout); 
    } 

} 

结果如下:

enter image description hereenter image description here

+0

谢谢!你是最棒的!!!!它非常完美,非常感谢你。 –