Skip to content

Instantly share code, notes, and snippets.

@gavilanch
Created August 6, 2017 15:11
Show Gist options
  • Save gavilanch/14de87904d6b4120f05d92b4c60f7963 to your computer and use it in GitHub Desktop.
Save gavilanch/14de87904d6b4120f05d92b4c60f7963 to your computer and use it in GitHub Desktop.
// Nota: Debes remover el MainLauncher del MainActivity.cs
// En NewsListRow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/newsTitle" />
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/newsImage" />
</LinearLayout>
// En NewsList.axml
<?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"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/newsListView" />
</LinearLayout>
// En NewsListActivity
[Activity(Label = "NotiXamarin", MainLauncher = true)]
public class NewsListActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.NewsList);
var newsListView = FindViewById<ListView>(Resource.Id.newsListView);
var newsService = new NewsService();
var news = newsService.GetNews();
newsListView.Adapter = new NewsListAdapter(this, news);
}
}
// En NewsListAdapter.cs
class NewsListAdapter : BaseAdapter<News>
{
private Activity _context;
private List<News> _news;
public NewsListAdapter(Activity context, List<News> news)
{
_context = context;
_news = news;
}
public override News this[int position] => _news[position];
public override int Count => _news.Count;
public override long GetItemId(int position)
{
return this[position].Id;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = this[position];
if (convertView == null)
{
convertView = _context.LayoutInflater.Inflate(Resource.Layout.NewsListRow, null);
}
convertView.FindViewById<TextView>(Resource.Id.newsTitle).Text = item.Title;
var newsImage = convertView.FindViewById<ImageView>(Resource.Id.newsImage);
var imageURL = string.Concat(ValuesService.ImagesBaseURL, item.ImageName);
Picasso.With(_context)
.Load(imageURL)
.Placeholder(_context.GetDrawable(Resource.Drawable.Icon))
.Into(newsImage);
return convertView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment