Skip to content

Instantly share code, notes, and snippets.

@gavilanch
Created September 16, 2017 18:39
Show Gist options
  • Save gavilanch/8e1af1c8bed4723f48609863f501ebd0 to your computer and use it in GitHub Desktop.
Save gavilanch/8e1af1c8bed4723f48609863f501ebd0 to your computer and use it in GitHub Desktop.
gist fragmentos
// NewsListActivity
public class NewsListActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.NewsList);
var transaction = FragmentManager.BeginTransaction();
transaction.Add(Resource.Id.newsListFragmentContainer, new AllNewsListFragment());
transaction.Commit();
}
}
// AllNewsListFragment
public class AllNewsListFragment: Fragment
{
private NewsService _newsService;
private List<News> _news;
private ListView _newsListView;
public AllNewsListFragment()
{
_newsService = new NewsService();
_news = new List<News>();
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
if (!_news.Any())
{
_news = _newsService.GetNews();
}
SetupViews();
SetupEvents();
_newsListView.Adapter = new NewsListAdapter(Activity, _news);
}
protected void SetupViews()
{
_newsListView = View.FindViewById<ListView>(Resource.Id.newsListView);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.NewsListFragment, container, false);
}
protected void SetupEvents()
{
_newsListView.ItemClick += NewsListView_ItemClick;
}
private void NewsListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var intent = new Intent(Activity, typeof(MainActivity));
var id = (int)e.Id;
intent.PutExtra(MainActivity.KEY_ID, id);
StartActivity(intent);
}
}
// 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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/newsListFragmentContainer" />
</LinearLayout>
// newsListFragment.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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment