我是初学者,我正尝试从此库中实施浮动搜索视图:Floating Search View
我需要将这个汉堡包按钮连接到我的抽屉视图,并使用操作连接右按钮。
听汉堡包按钮点击:
mSearchView.attachNavigationDrawerToMenuButton(mDrawerLayout);
快速将NavigationDrawer连接到汉堡包按钮:
mSearchView.setOnMenuItemClickListener(new FloatingSearchView.OnMenuItemClickListener() {
@Override
public void onMenuItemSelected(MenuItem item) {
}
});
收听项目选择
{{1}}
有一些例子,但是当我把它放在我的代码上时,给我一个错误。也许问题是我不知道应该在哪里写它。
请帮助我,我一直在寻找和尝试数周。
答案 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);
}
}
结果如下: