- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我需要使用 google placeAutocomplete
小部件在 dropdown
中显示地点列表。在这里,我正在根据我的查询显示位置的对话框,但我需要为该搜索结果对话框提供自定义设计,如在 Uber、Ola 应用程序中。在这里,我想要一个如下图所示的设计。如果有人以前这样做过,请给我您的建议,提前谢谢。
最佳答案
您需要自定义适配器。我已经在我的项目中实现了这个功能。你可以按照这个。
activity_search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/search_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:background="@drawable/searchbar_bg"
android:layout_marginLeft="@dimen/activity_margin_10"
android:layout_marginRight="@dimen/activity_margin_10"
android:layout_marginTop="@dimen/activity_margin_10"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/search_et"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Search"
android:singleLine="true"
android:layout_toLeftOf="@+id/clear"
android:imeOptions="actionSearch"
android:background="@null"
android:drawableLeft="@drawable/ic_action_search"
android:drawablePadding="@dimen/activity_margin_10"
android:paddingLeft="@dimen/activity_margin_10"
android:paddingRight="@dimen/activity_margin_10"/>
<ImageView
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_clear"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:padding="@dimen/activity_margin_16"
android:visibility="gone"/>
</RelativeLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/list_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/search_layout"
android:layout_above="@+id/powered_by_google"
android:background="@color/white"
android:layout_marginTop="@dimen/activity_margin_10"/>
<ImageView
android:id="@+id/powered_by_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:padding="@dimen/activity_margin_10"
android:layout_marginBottom="@dimen/activity_margin_10"
android:src="@drawable/powered_by_google_light"/>
</RelativeLayout>
</LinearLayout>
SearchActivity.java
package com.android.dezi.views.rider.Activities;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.android.dezi.BaseActivity;
import com.android.dezi.R;
import com.android.dezi.adapters.PlaceAutocompleteAdapter;
import com.android.dezi.adapters.PlaceAutocompleteAdapter.PlaceAutoCompleteInterface;
import com.android.dezi.adapters.PlaceSavedAdapter;
import com.android.dezi.adapters.PlaceSavedAdapter.SavedPlaceListener;
import com.android.dezi.beans.SavedAddress;
import com.android.dezi.views.rider.Fragments.SearchFragment;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.PlaceBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.location.places.ui.PlaceAutocomplete;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import java.util.ArrayList;
import java.util.List;
/**
* Created by anuj.sharma on 4/6/2016.
*/
public class SearchActivity extends BaseActivity implements PlaceAutoCompleteInterface, GoogleApiClient.OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks,OnClickListener,SavedPlaceListener {
Context mContext;
GoogleApiClient mGoogleApiClient;
LinearLayout mParent;
private RecyclerView mRecyclerView;
LinearLayoutManager llm;
PlaceAutocompleteAdapter mAdapter;
List<SavedAddress> mSavedAddressList;
PlaceSavedAdapter mSavedAdapter;
private static final LatLngBounds BOUNDS_INDIA = new LatLngBounds(
new LatLng(-0, 0), new LatLng(0, 0));
EditText mSearchEdittext;
ImageView mClear;
@Override
public void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
public void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_search);
mContext = SearchActivity.this;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0 /* clientId */, this)
.addApi(Places.GEO_DATA_API)
.build();
initViews();
}
/*
Initialize Views
*/
private void initViews(){
mRecyclerView = (RecyclerView)findViewById(R.id.list_search);
mRecyclerView.setHasFixedSize(true);
llm = new LinearLayoutManager(mContext);
mRecyclerView.setLayoutManager(llm);
mSearchEdittext = (EditText)findViewById(R.id.search_et);
mClear = (ImageView)findViewById(R.id.clear);
mClear.setOnClickListener(this);
mAdapter = new PlaceAutocompleteAdapter(this, R.layout.view_placesearch,
mGoogleApiClient, BOUNDS_INDIA, null);
mRecyclerView.setAdapter(mAdapter);
mSearchEdittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count > 0) {
mClear.setVisibility(View.VISIBLE);
if (mAdapter != null) {
mRecyclerView.setAdapter(mAdapter);
}
} else {
mClear.setVisibility(View.GONE);
if (mSavedAdapter != null && mSavedAddressList.size() > 0) {
mRecyclerView.setAdapter(mSavedAdapter);
}
}
if (!s.toString().equals("") && mGoogleApiClient.isConnected()) {
mAdapter.getFilter().filter(s.toString());
} else if (!mGoogleApiClient.isConnected()) {
// Toast.makeText(getApplicationContext(), Constants.API_NOT_CONNECTED, Toast.LENGTH_SHORT).show();
Log.e("", "NOT CONNECTED");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public void onClick(View v) {
if(v == mClear){
mSearchEdittext.setText("");
if(mAdapter!=null){
mAdapter.clearList();
}
}
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onPlaceClick(ArrayList<PlaceAutocompleteAdapter.PlaceAutocomplete> mResultList, int position) {
if(mResultList!=null){
try {
final String placeId = String.valueOf(mResultList.get(position).placeId);
/*
Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place.
*/
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeId);
placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if(places.getCount()==1){
//Do the things here on Click.....
Intent data = new Intent();
data.putExtra("lat",String.valueOf(places.get(0).getLatLng().latitude));
data.putExtra("lng", String.valueOf(places.get(0).getLatLng().longitude));
setResult(SearchActivity.RESULT_OK, data);
finish();
}else {
Toast.makeText(getApplicationContext(),"something went wrong",Toast.LENGTH_SHORT).show();
}
}
});
}
catch (Exception e){
}
}
}
@Override
public void onSavedPlaceClick(List<SavedAddress> mResponse, int position) {
if(mResponse!=null){
try {
Intent data = new Intent();
data.putExtra("lat",String.valueOf(mResponse.get(position).getLatitude()));
data.putExtra("lng", String.valueOf(mResponse.get(position).getLongitude()));
setResult(SearchActivity.RESULT_OK, data);
finish();
}
catch (Exception e){
}
}
}
}
PlaceAutocompleteAdapter.java
这是定制的适配器。所有重要的部分都在里面。
package com.android.dezi.adapters;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.style.CharacterStyle;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.dezi.R;
import com.android.dezi.beans.TripHistoryBean;
import com.android.dezi.views.rider.Fragments.SearchFragment;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.common.data.DataBufferUtils;
import com.google.android.gms.location.places.AutocompleteFilter;
import com.google.android.gms.location.places.AutocompletePrediction;
import com.google.android.gms.location.places.AutocompletePredictionBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.model.LatLngBounds;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Created by anuj.sharma on 4/6/2016.
*/
public class PlaceAutocompleteAdapter extends RecyclerView.Adapter<PlaceAutocompleteAdapter.PlaceViewHolder> implements Filterable{
public interface PlaceAutoCompleteInterface{
public void onPlaceClick(ArrayList<PlaceAutocomplete> mResultList, int position);
}
Context mContext;
PlaceAutoCompleteInterface mListener;
private static final String TAG = "PlaceAutocompleteAdapter";
private static final CharacterStyle STYLE_BOLD = new StyleSpan(Typeface.BOLD);
ArrayList<PlaceAutocomplete> mResultList;
private GoogleApiClient mGoogleApiClient;
private LatLngBounds mBounds;
private int layout;
private AutocompleteFilter mPlaceFilter;
public PlaceAutocompleteAdapter(Context context, int resource, GoogleApiClient googleApiClient,
LatLngBounds bounds, AutocompleteFilter filter){
this.mContext = context;
layout = resource;
mGoogleApiClient = googleApiClient;
mBounds = bounds;
mPlaceFilter = filter;
this.mListener = (PlaceAutoCompleteInterface)mContext;
}
/*
Clear List items
*/
public void clearList(){
if(mResultList!=null && mResultList.size()>0){
mResultList.clear();
}
}
/**
* Sets the bounds for all subsequent queries.
*/
public void setBounds(LatLngBounds bounds) {
mBounds = bounds;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// Skip the autocomplete query if no constraints are given.
if (constraint != null) {
// Query the autocomplete API for the (constraint) search string.
mResultList = getAutocomplete(constraint);
if (mResultList != null) {
// The API successfully returned results.
results.values = mResultList;
results.count = mResultList.size();
}
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
// The API returned at least one result, update the data.
notifyDataSetChanged();
} else {
// The API did not return any results, invalidate the data set.
//notifyDataSetInvalidated();
}
}
};
return filter;
}
private ArrayList<PlaceAutocomplete> getAutocomplete(CharSequence constraint) {
if (mGoogleApiClient.isConnected()) {
Log.i("", "Starting autocomplete query for: " + constraint);
// Submit the query to the autocomplete API and retrieve a PendingResult that will
// contain the results when the query completes.
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, mPlaceFilter);
// This method should have been called off the main UI thread. Block and wait for at most 60s
// for a result from the API.
AutocompletePredictionBuffer autocompletePredictions = results
.await(60, TimeUnit.SECONDS);
// Confirm that the query completed successfully, otherwise return null
final Status status = autocompletePredictions.getStatus();
if (!status.isSuccess()) {
// Toast.makeText(mContext, "Error contacting API: " + status.toString(),
// Toast.LENGTH_SHORT).show();
Log.e("", "Error getting autocomplete prediction API call: " + status.toString());
autocompletePredictions.release();
return null;
}
Log.i("", "Query completed. Received " + autocompletePredictions.getCount()
+ " predictions.");
// Copy the results into our own data structure, because we can't hold onto the buffer.
// AutocompletePrediction objects encapsulate the API response (place ID and description).
Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
while (iterator.hasNext()) {
AutocompletePrediction prediction = iterator.next();
// Get the details of this prediction and copy it into a new PlaceAutocomplete object.
resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
prediction.getDescription()));
}
// Release the buffer now that all data has been copied.
autocompletePredictions.release();
return resultList;
}
Log.e("", "Google API client is not connected for autocomplete query.");
return null;
}
@Override
public PlaceViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = layoutInflater.inflate(layout, viewGroup, false);
PlaceViewHolder mPredictionHolder = new PlaceViewHolder(convertView);
return mPredictionHolder;
}
@Override
public void onBindViewHolder(PlaceViewHolder mPredictionHolder, final int i) {
mPredictionHolder.mAddress.setText(mResultList.get(i).description);
mPredictionHolder.mParentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onPlaceClick(mResultList,i);
}
});
}
@Override
public int getItemCount() {
if(mResultList != null)
return mResultList.size();
else
return 0;
}
public PlaceAutocomplete getItem(int position) {
return mResultList.get(position);
}
/*
View Holder For Trip History
*/
public class PlaceViewHolder extends RecyclerView.ViewHolder {
// CardView mCardView;
public RelativeLayout mParentLayout;
public TextView mAddress;
public PlaceViewHolder(View itemView) {
super(itemView);
mParentLayout = (RelativeLayout)itemView.findViewById(R.id.predictedRow);
mAddress = (TextView)itemView.findViewById(R.id.address);
}
}
/**
* Holder for Places Geo Data Autocomplete API results.
*/
public class PlaceAutocomplete {
public CharSequence placeId;
public CharSequence description;
PlaceAutocomplete(CharSequence placeId, CharSequence description) {
this.placeId = placeId;
this.description = description;
}
@Override
public String toString() {
return description.toString();
}
}
}
view_placesearch.xml
您想在适配器中显示的自定义 View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/predictedRow"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:gravity="center_vertical">
<ImageView
android:id="@+id/image"
android:layout_width="22dp"
android:layout_height="22dp"
android:src="@drawable/ic_action_navigate"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
/>
<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="15sp"
android:layout_toRightOf="@+id/image"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/light_gray"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
希望对你也有帮助。
注意:不要忘记在 manifest
文件中添加 API key 。
输出如下:
我找到了一些有用的链接,它们也可以帮助你。
关于android - 如何自定义放置自动完成小部件对话框设计以列出位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36999647/
我想做的是,如果鼠标位于“下一个”按钮上,它会以慢速向右滚动,如果鼠标没有位于“下一个”按钮上,它会停止滚动? 这是我的尝试http://jsfiddle.net/mdanz/nCCRy/14/ $(
StyleCop 是一个很棒的视觉工作室小插件。但它不会向您显示实时提示或提供任何自动修复。 随之而来的是 reSharper 和 StyleCop for reSharper,这是理想的解决方案,但
我为我的MatchQuery使用了模糊性选项,但是我想将模糊性值设置为auto。有什么办法吗? 另外,对于完成建议程序,您可以将其设置为支持unicode,对于我的MatchQuery,有什么方法可以
我想从表中获取一行[字符串名称,字符串密码,int 某些内容]并将其映射到一个 User 对象,该对象具有 3 个属性,如上面的 getter 和 setter有什么方法可以自动完成吗?我考虑过反射,
我有一个像这样的方法:void m1(string str) 并且有一个像这样的类: public class MyClass { public bool b1 { set; get; }
我正在尝试使用 $rootScope 从一个 Controller 向另一个 Controller $broadcast 一些数据。 如果我使用像 ng-click 这样的触发器来运行将广播的功能,它
我考虑了很多关于是要使用完全自动化的缓存还是手动缓存。 我们的自动方法是一种解决方案,它可以挖掘数据库、查询和格式化每个潜在和 future 的数据请求,并将其保存到适当的缓存存储(内存缓存或基于磁盘
我的 CSS 必须使用过渡来更改,直到现在我都使用 div:hover 来实现。 当您单击另一个 div 时需要激活过渡,而不是当您将鼠标悬停在必须移动/更改的 div 上时。 我该怎么做? 谢谢 永
在我的应用程序中,我需要一些动画,但如果它已经设置了动画,则不需要持续时间。但我的问题是它会自动添加持续时间。 在这里你可以看到 2 个函数,第二个没有持续时间但它确实有持续时间(可能从 1 秒开始)
两年前,我需要制作一个工具,通过 POST 自动将 txt/csv 文件上传到我的 Web 服务器,然后使用 cronjob 通过 PHP 对其进行解析。 这有两次在每天午夜自动发生。尽管这行得通,但
请阅读下面程序中的评论: #include void test(char c[]) { c=c+2; //why does this work ? c--; printf("%
也许是个幼稚的问题,但是...... 确认或拒绝: 自动和静态存储持续时间的对象/变量的内存的存在是在编译时确定的,程序运行时失败的可能性绝对为零,因为没有足够的内存用于自动对象。 自然地,当自动对象
有没有什么方法可以自动获得类中属性更改的通知,而不必在每个 setter 中都编写 OnPropertyChanged? (我有数百个属性,我想知道它们是否已更改)。 安东建议 dynamic pro
我们在使用 Azure DevOps 的项目中采用了 gitflow 流程。我有以下场景: 当功能分支合并到 Develop 时,我想在完成拉取请求的同时执行压缩合并策略 当 Release 分支定期
我的网站上有一个评论部分,我将 html 编码的评论保存在我的数据库中。所以我添加了这条评论- "testing" `quotes` \and backslashes\ and html 并将其保存在
是否存在“ checkin 前 TFS 自动 checkout ”这样的功能,以便在我说“ checkin ”之前我不会 checkout 任何文件,例如以防我只是临时更改文件 - 这一直发生。 换句
我有一个运行在 Linux/Apache/Tomcat 堆栈上的网站,它需要每隔几个月自动脱机以进行服务器维护,这将持续任意时间。有哪些选项可以让 Apache 建立和取消“服务器维护”页面? 我需要
我经常在工作中创建文档,在公司内部,由于我们使用的首字母缩写词和缩写词的数量,我们几乎拥有自己的语言。因此,我厌倦了在发布文档之前手动创建首字母缩写词和缩写表,并且快速的谷歌搜索发现了一个可以有效地为
我希望在用户或宏将计算模式从自动更改为手动或手动更改为自动时运行代码。是否有为此触发的事件? (属性是 Application.Calculation 在 Excel 互操作中。) 使用 Excel
这个问题在这里已经有了答案: Repeat command automatically in Linux (13 个回答) 6年前关闭。 我想创建一个脚本来获取另一个文件夹中的所有文件夹名称。并为这些
我是一名优秀的程序员,十分优秀!