Android:GoogleMaps SearchView片段

时间:2016-04-02 10:17:34

标签: android google-maps android-fragments android-studio searchview

我昨天开始构建一个在ViewPager中使用片段的应用程序。我目前有一个使用ViewPager的主要活动,两个片段(加载到主活动寻呼机)和一个导航抽屉。内容是空的,我刚开始处理第一个片段。我想要它做的是有一个谷歌地图的背景,以及它上面的SearchView。这就是它的样子(链接):

IMG - The first fragment with the map and searchview

我从未使用Google Maps API,所以这对我来说有点困难。我只是想通过在选择后建议地点并在地图上显示地点来使搜索功能起作用(谷歌地图具有类似的功能)。

我检查并尝试了来自stackoverflow和互联网其他部分的8个解决方案,我终于达到了我没有得到任何例外或致命错误的地步,但建议和搜索并没有工作了。下面我发布我的代码来自与它有关的文件,希望它能帮助你找到我的代码有什么问题。我打赌它不仅仅是一条线,所以我想提前感谢那些愿意花时间帮助我的人。

的AndroidManifest.xml

<uses-permission android:name="com.example.marto.findparking.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="...hidden..." />
    <provider
        android:name=".gmaps.PlaceProvider"
        android:authorities="com.example.marto.findparking.gmaps.search_suggestion_provider"
        android:exported="false" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

searchable.xml

<searchable ...

android:searchSuggestAuthority="com.example.marto.findparking.gmaps.search_suggestion_provider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestSelection=" ?"
android:searchSuggestThreshold="2" >
</searchable>

fragment_activity_home_parameter.xml(我正在处理的片段)

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="horizontal">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <fragment
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center"
        android:layout_marginTop="11dip"
        android:layout_marginLeft="9dip"
        android:layout_marginRight="9dip"
         >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#fff"
            >
            <ImageButton
                android:id="@+id/button1"
                android:layout_width="50dip"
                android:layout_height="50dip"
                android:scaleType="fitXY"
                android:background="@drawable/show_menu_icon"/>

            <SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true" >
            </SearchView>

            <TextView
                android:id="@+id/searchViewResult"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/searchView"
                android:layout_centerHorizontal="true"
                />

        </LinearLayout>
    </android.support.v7.widget.CardView>
    </FrameLayout>
</LinearLayout>

ActivityHome.java(主要活动)

public class ActivityHome extends AppCompatActivity {
public static FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_home);
    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));

    View inflatedView = getLayoutInflater().inflate(R.layout.fragment_activity_home_parameter, null);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) inflatedView.findViewById(R.id.searchView);
    // Tells your app's SearchView to use this activity's searchable configuration
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
}

}

我使用的最后一件事是本教程中的文件: http://wptrafficanalyzer.in/blog/android-searchview-widget-with-google-places-api-using-actionbarsherlock-library/ 我们正在谈论名为PlaceDetailsJSONParser,PlaceJSONParser和PlaceProvider的类。问题是,我非常确定到目前为止我将很多东西混合在一起,所以代码的某些部分可能不属于它们所在的位置。我还读到,没有办法在片段内部使用searchview功能,并且需要通过编写SearchActivity类并将数据从此处传递到片段来完成不同的操作。有谁知道如何使这项工作?

0 个答案:

没有答案