Add custom adapter class and item_place layout file

This commit is contained in:
misaochan 2016-08-24 18:55:56 +12:00
parent 45a1f546e3
commit e7032658a2
2 changed files with 100 additions and 4 deletions

View file

@ -1,19 +1,27 @@
package fr.free.nrw.commons.nearby;
import android.content.Context;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.List;
import fr.free.nrw.commons.R;
@ -51,7 +59,7 @@ public class NearbyListFragment extends ListFragment {
//TODO: Original is an AttractionAdapter. Not a CursorAdapter.
// Create an empty adapter we will use to display the loaded data.
// We pass null for the cursor, then update it in onLoadFinished()
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, fromColumns, toViews, 0);
mAdapter = new NearbyAdapter();
setListAdapter(mAdapter);
// Prepare the loader. Either re-connect with an existing one,
@ -64,9 +72,83 @@ public class NearbyListFragment extends ListFragment {
private class NearbyAdapter extends ArrayAdapter {
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something when a list item is clicked
public List<Place> mAttractionList;
private Context mContext;
public NearbyAdapter(Context context, List<Place> attractions) {
super(context, R.layout.item_place);
mContext = context;
mAttractionList = attractions;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHome);
// Populate the data into the template view using the data object
tvName.setText(user.name);
tvHome.setText(user.hometown);
// Return the completed view to render on screen
return convertView;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.list_row, parent, false);
return new RecyclerView.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);
}
}
}
}

View file

@ -0,0 +1,14 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="@+id/tvHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HomeTown" />
</LinearLayout>