mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Add fragment_nearby.xml layout file
This commit is contained in:
parent
5ca298dd4f
commit
e2d3f65aef
2 changed files with 13 additions and 177 deletions
|
|
@ -1,202 +1,32 @@
|
||||||
package fr.free.nrw.commons.nearby;
|
package fr.free.nrw.commons.nearby;
|
||||||
|
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.location.Location;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
|
||||||
import android.support.v7.widget.RecyclerView;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.ImageView;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
import com.bumptech.glide.Glide;
|
import fr.free.nrw.commons.R;
|
||||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
|
||||||
import com.example.android.xyztouristattractions.R;
|
|
||||||
import com.example.android.xyztouristattractions.common.Attraction;
|
|
||||||
import com.example.android.xyztouristattractions.common.Constants;
|
|
||||||
import com.example.android.xyztouristattractions.common.Utils;
|
|
||||||
import com.example.android.xyztouristattractions.provider.TouristAttractions;
|
|
||||||
import com.example.android.xyztouristattractions.service.UtilityService;
|
|
||||||
import com.google.android.gms.location.FusedLocationProviderApi;
|
|
||||||
import com.google.android.gms.maps.model.LatLng;
|
|
||||||
import com.google.maps.android.SphericalUtil;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class NearbyListFragment extends Fragment {
|
public class NearbyListFragment extends Fragment {
|
||||||
|
|
||||||
private AttractionAdapter mAdapter;
|
|
||||||
private LatLng mLatestLocation;
|
private LatLng mLatestLocation;
|
||||||
private int mImageSize;
|
private int mImageSize;
|
||||||
private boolean mItemClicked;
|
private boolean mItemClicked;
|
||||||
|
|
||||||
public NearbyListFragment() {}
|
public NearbyListFragment() {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
|
|
||||||
mLatestLocation = Utils.getLocation(getActivity());
|
|
||||||
List<Attraction> attractions = loadAttractionsFromLocation(mLatestLocation);
|
|
||||||
mAdapter = new AttractionAdapter(getActivity(), attractions);
|
|
||||||
|
|
||||||
View view = inflater.inflate(R.layout.fragment_main, container, false);
|
|
||||||
AttractionsRecyclerView recyclerView =
|
View view = inflater.inflate(R.layout.fragment_nearby, container, false);
|
||||||
(AttractionsRecyclerView) view.findViewById(android.R.id.list);
|
|
||||||
recyclerView.setEmptyView(view.findViewById(android.R.id.empty));
|
|
||||||
recyclerView.setHasFixedSize(true);
|
|
||||||
recyclerView.setAdapter(mAdapter);
|
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
mItemClicked = false;
|
|
||||||
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
|
|
||||||
mBroadcastReceiver, UtilityService.getLocationUpdatedIntentFilter());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mBroadcastReceiver);
|
|
||||||
}
|
|
||||||
|
|
||||||
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
Location location =
|
|
||||||
intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
|
|
||||||
if (location != null) {
|
|
||||||
mLatestLocation = new LatLng(location.getLatitude(), location.getLongitude());
|
|
||||||
mAdapter.mAttractionList = loadAttractionsFromLocation(mLatestLocation);
|
|
||||||
mAdapter.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private static List<Attraction> loadAttractionsFromLocation(final LatLng curLatLng) {
|
|
||||||
//String closestCity = TouristAttractions.getClosestCity(curLatLng);
|
|
||||||
//if (closestCity != null) {
|
|
||||||
List<Attraction> attractions = TouristAttractions.get();
|
|
||||||
if (curLatLng != null) {
|
|
||||||
Collections.sort(attractions,
|
|
||||||
new Comparator<Attraction>() {
|
|
||||||
@Override
|
|
||||||
public int compare(Attraction lhs, Attraction rhs) {
|
|
||||||
double lhsDistance = SphericalUtil.computeDistanceBetween(
|
|
||||||
lhs.location, curLatLng);
|
|
||||||
double rhsDistance = SphericalUtil.computeDistanceBetween(
|
|
||||||
rhs.location, curLatLng);
|
|
||||||
return (int) (lhsDistance - rhsDistance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return attractions;
|
|
||||||
//}
|
|
||||||
//return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private class AttractionAdapter extends RecyclerView.Adapter<ViewHolder>
|
|
||||||
implements ItemClickListener {
|
|
||||||
|
|
||||||
public List<Attraction> mAttractionList;
|
|
||||||
private Context mContext;
|
|
||||||
|
|
||||||
public AttractionAdapter(Context context, List<Attraction> attractions) {
|
|
||||||
super();
|
|
||||||
mContext = context;
|
|
||||||
mAttractionList = attractions;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
||||||
LayoutInflater inflater = LayoutInflater.from(mContext);
|
|
||||||
View view = inflater.inflate(R.layout.list_row, parent, false);
|
|
||||||
return new ViewHolder(view, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
|
||||||
Attraction attraction = mAttractionList.get(position);
|
|
||||||
|
|
||||||
holder.mTitleTextView.setText(attraction.name);
|
|
||||||
holder.mDescriptionTextView.setText(attraction.description);
|
|
||||||
Glide.with(mContext)
|
|
||||||
.load(attraction.imageUrl)
|
|
||||||
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
|
|
||||||
.placeholder(R.drawable.empty_photo)
|
|
||||||
.override(mImageSize, mImageSize)
|
|
||||||
.into(holder.mImageView);
|
|
||||||
|
|
||||||
String distance =
|
|
||||||
Utils.formatDistanceBetween(mLatestLocation, attraction.location);
|
|
||||||
if (TextUtils.isEmpty(distance)) {
|
|
||||||
holder.mOverlayTextView.setVisibility(View.GONE);
|
|
||||||
} else {
|
|
||||||
holder.mOverlayTextView.setVisibility(View.VISIBLE);
|
|
||||||
holder.mOverlayTextView.setText(distance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long getItemId(int position) {
|
|
||||||
return position;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getItemCount() {
|
|
||||||
return mAttractionList == null ? 0 : mAttractionList.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onItemClick(View view, int position) {
|
|
||||||
if (!mItemClicked) {
|
|
||||||
mItemClicked = true;
|
|
||||||
View heroView = view.findViewById(android.R.id.icon);
|
|
||||||
DetailActivity.launch(
|
|
||||||
getActivity(), mAdapter.mAttractionList.get(position).name, heroView);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class ViewHolder extends RecyclerView.ViewHolder
|
|
||||||
implements View.OnClickListener {
|
|
||||||
|
|
||||||
TextView mTitleTextView;
|
|
||||||
TextView mDescriptionTextView;
|
|
||||||
TextView mOverlayTextView;
|
|
||||||
ImageView mImageView;
|
|
||||||
ItemClickListener mItemClickListener;
|
|
||||||
|
|
||||||
public ViewHolder(View view, ItemClickListener itemClickListener) {
|
|
||||||
super(view);
|
|
||||||
mTitleTextView = (TextView) view.findViewById(android.R.id.text1);
|
|
||||||
mDescriptionTextView = (TextView) view.findViewById(android.R.id.text2);
|
|
||||||
mOverlayTextView = (TextView) view.findViewById(R.id.overlaytext);
|
|
||||||
mImageView = (ImageView) view.findViewById(android.R.id.icon);
|
|
||||||
mItemClickListener = itemClickListener;
|
|
||||||
view.setOnClickListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
mItemClickListener.onItemClick(v, getAdapterPosition());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ItemClickListener {
|
|
||||||
void onItemClick(View view, int position);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
6
app/src/main/res/layout/fragment_nearby.xml
Normal file
6
app/src/main/res/layout/fragment_nearby.xml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:orientation="vertical" android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue