Here is a complete project: Google Drive.
Follow code:
Frag_Home_1_Tab.cs:
public class Frag_Home_1_Tab : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public static Frag_Home_1_Tab NewInstance()
{
return new Frag_Home_1_Tab { Arguments = new Bundle() };
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View inflate = inflater.Inflate(Resource.Layout.frag_tab1, container, false);
ListView mListView = inflate.FindViewById<ListView>(Resource.Id.listView);
ViewPager viewPager = inflate.FindViewById<ViewPager>(Resource.Id.viewPager);
mListView.ItemClick += (sender, e) =>
{
mListView.SetItemChecked(e.Position, true);
// ** Here, how to navigate to tab 2 and refresh gridview **
};
ArrayAdapter<string> adapter = new ArrayAdapter<string>(Activity, Android.Resource.Layout.SimpleListItemActivated1, List());
mListView.Adapter = adapter;
// Set the ChoiceMode
mListView.ChoiceMode = ChoiceMode.Single;
// Select default
mListView.SetItemChecked(0, true);
return inflate;
}
private List<string> List()
{
List<string> list = new List<string>
{
"List 1",
"List 2",
"List 3",
"List 4",
"List 5",
"List 6",
"List 7",
"List 8",
"List 9"
};
return list;
}
}
frag_tab1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView" />
</LinearLayout>
If you select from the "List5" list in tab 1, load 5 rows in the gridview of tab 2 and navigate to tab 2.
If you select from the "List9" list in tab 1, load 9 rows in the gridview of tab 2 and navigate to tab 2.
If you select from the "List2" list in tab 1, load 2 rows in the gridview of tab 2 and navigate to tab 2.
And so it goes ...
What I'm trying to say is: navigate to tab 2 when you click on tab button 1. Simple like that.
Any solution ?
Frag_Home_1:
public class Frag_Home_1 : Fragment
{
TabLayout tabLayout;
private static class SingletonHolder
{
public static Frag_Home_1 INSTANCE = new Frag_Home_1();
}
public static Frag_Home_1 getInstance()
{
return SingletonHolder.INSTANCE;
}
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View inflate = inflater.Inflate(Resource.Layout.frag_home_1, container, false);
ViewPager viewPager = inflate.FindViewById<ViewPager>(Resource.Id.viewPager);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(ChildFragmentManager);
viewPager.Adapter = adapter;
//TabLayout
tabLayout = inflate.FindViewById<TabLayout>(Resource.Id.sliding_tabs);
tabLayout.SetupWithViewPager(viewPager);
return inflate;
}
public void toTab2()
{
TabLayout.Tab tab = tabLayout.GetTabAt(1);
tab.Select();
}
public class MyFragmentPagerAdapter : FragmentPagerAdapter
{
public string[] titles = new string[] { "Tab1", "Tab2" };
List<Fragment> fgls = new List<Fragment>();
public MyFragmentPagerAdapter(FragmentManager fm) : base(fm)
{
fgls.Add(Frag_Home_1_Tab.NewInstance());
fgls.Add(Frag_Home_2_Tab.getInstance());
}
public override Fragment GetItem(int position)
{
return fgls[position];
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return new String(titles[position]);
}
public override int Count
{
get { return titles.Length; }
}
}
}
Frag_Home_1_Tab's OnCreateView method:
mListView.ItemClick += (sender, e) =>
{
mListView.SetItemChecked(e.Position, true);
Frag_Home_1.getInstance().toTab2();
Frag_Home_2_Tab.getInstance().updateGrid(e.Position);
// ** Here, how to navigate to tab 2 and refresh gridview **
};
Frag_Home_2_Tab:
public class Frag_Home_2_Tab : Fragment,adapterUpdate
{
ArrayAdapter<string> adapter;
List<string> numbers = new List<string>();
GridView grid;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
private static class SingletonHolder
{
public static Frag_Home_2_Tab INSTANCE = new Frag_Home_2_Tab();
}
public static Frag_Home_2_Tab getInstance()
{
return SingletonHolder.INSTANCE;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
initData(9);
View view = inflater.Inflate(Resource.Layout.frag_tab2, container, false);
grid = (GridView)view.FindViewById(Resource.Id.gridview);
grid.ChoiceMode = ChoiceMode.Single;
grid.ItemClick += (sender, e) =>
{
grid.SetItemChecked(e.Position, true);
};
adapter = new ArrayAdapter<string>(Activity, Resource.Layout.item_tab_2, numbers);
grid.Adapter = adapter;
return view;
}
private void initData(int count)
{
numbers.Clear();
for (int i = 0; i < count; i++)
{
numbers.Add((i + 1).ToString());
}
}
public void updateGrid(int count)
{
initData(count+1);
adapter = new ArrayAdapter<string>(Activity, Resource.Layout.item_tab_2, numbers);
grid.Adapter = adapter;
}
}
public interface adapterUpdate
{
void updateGrid(int count);
}
In Frag_Home_1 and Frag_Home_2_Tab, I use single instance to avoid NullPointException.
Related
As you can see from the title I'm a little confused on how to resolve my problem. I have an activity that use Tabs to switch between two fragments that contain one list eache one. The list have the same adapter, and in every list there's a button that refresh the two lists.
Here's my MainActivity.class:
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
public static TreeSet<Task> tasksTODO = new TreeSet<>();
public static TreeSet<Task> tasksDONE = new TreeSet<>();
private TabLayout tabs;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.container);
tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(mViewPager);
tabs.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
addTasks();
}
#Override
protected void onResume() {
super.onResume();
addTasks();
}
private void setupViewPager(ViewPager vp) {
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mSectionsPagerAdapter.addFragment(new ShowTodoTasks(), "todo");
mSectionsPagerAdapter.addFragment(new ShowDoneTasks(), "done");
vp.setAdapter(mSectionsPagerAdapter);
}
public void addTasks() {
tasksDONE.add(new Task("Number 1", "pepin", "", "Description 1", 1));
tasksDONE.add(new Task("Number 2", "pepin", "", "Description 2", 1));
tasksDONE.add(new Task("Number 3", "pepin", "", "Description 3", 1));
tasksTODO.add(new Task("Number 4", "pepin", "", "Description 4", 1));
setupViewPager(mViewPager);
}
}
Here's my TaskAdapter:
public class TaskAdapter extends BaseAdapter {
protected Context context;
protected ArrayList<Task> items;
private ImageView edit;
private CheckBox checked;
Task dir;
private TextView title, comment, description, date;
public TaskAdapter(Context context, ArrayList<Task> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int position, View view, ViewGroup viewGroup) {
LayoutInflater inf = (LayoutInflater) viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.task_item_template, null);
edit = (ImageView) view.findViewById(R.id.item_edit);
checked = (CheckBox) view.findViewById(R.id.item_done);
final View finalView = view;
dir = items.get(position);
int priority = dir.getPriority();
if (priority == 1) {
view.setBackgroundResource(R.drawable.major_prior_task_border);
}
if (priority == 2) {
view.setBackgroundResource(R.drawable.medium_prior_task_border);
}
if (priority == 3) {
view.setBackgroundResource(R.drawable.minor_prior_task_border);
}
title = view.findViewById(R.id.item_title);
title.setText(dir.getTitle());
comment = view.findViewById(R.id.tv_comment);
comment.setText(dir.getComment());
description = view.findViewById(R.id.item_description);
description.setText(dir.getDescription());
date = view.findViewById(R.id.item_date);
date.setText(dir.getCreationDate());
return view;
}
}
And here are my Fragments:
public class ShowDoneTasks extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_show_done_tasks, container, false);
setup(MainActivity.tasksDONE,v);
return v;
}
#Override
public void onStart() {
super.onStart();
}
private void setup(TreeSet<Task> tasks, View v) {
ListView lv = v.findViewById(R.id.lv_tasks_done);
ArrayList<Task> tareasDone = new ArrayList<>(tasks);
TaskAdapter adapter = new TaskAdapter(v.getContext(), tareasDone);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
And the other one:
public class ShowTodoTasks extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_show_todo_tasks, container, false);
setup(MainActivity.tasksTODO,v);
return v;
}
private void setup(TreeSet<Task> tasks, View v) {
ListView lv = v.findViewById(R.id.lv_tasks_todo);
ArrayList<Task> tareasTodo = new ArrayList<>(tasks);
TaskAdapter adapter = new TaskAdapter(v.getContext(), tareasTodo);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
}
}
What I want is this: when you click on the checkBox that's in TaskAdapter i want to refresh the list. I tried using adapter.notifyDataSetChanged() in the fragments but it dosn't seem to work.
Any ideas?
I have tablayout in my app,and i want to set recyclerview in tablayout fragment.if i set static String array it is working fine.but i dont know how can i access custom arraylist in fragment to set data in my recyclerview.following is my code can any one help me with that
public class CategoriesActivity extends AppCompatActivity{
private Header myview;
private ArrayList<SubcategoryModel> subct;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categoris_activity);
ArrayList<CategoryModel> filelist = (ArrayList<CategoryModel>)getIntent().getSerializableExtra("categorylist");
System.out.println("Category list size"+filelist.size());
myview = (Header) findViewById(R.id.categorisactivity_headerView);
myview.setActivity(this);
TabLayout tabLayout = (TabLayout) findViewById(R.id.cat_tab_layout);
for(int i = 0; i < filelist.size(); i++){
subct=filelist.get(i).getItems();
for(int j=0;j<subct.size();j++)
{
}
System.out.println("SubCategory list size"+subct.size());
}
for(int i = 0; i < filelist.size(); i++){
tabLayout.addTab(tabLayout.newTab().setText(filelist.get(i).getCategory_typename()));
ArrayList<SubcategoryModel> subct=filelist.get(i).getItems();
for(int j=0;j<subct.size();j++)
{
}
}
Bundle bundleObject = new Bundle();
bundleObject.putSerializable("key", filelist);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.categories_pager);
CategoriesAdapter mPagerAdapter = new CategoriesAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(mPagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public class CategoriesAdapter extends FragmentStatePagerAdapter {
ArrayList<CategoryModel> catlist;
int numoftabs;
public CategoriesAdapter(FragmentManager fm, int numoftabs) {
super(fm);
this.numoftabs = numoftabs;
}
#Override
public Fragment getItem(int position) {
Log.v("adapter", "getitem" + String.valueOf(position));
return FirstFragment.create(position);
}
#Override
public int getCount() {
return numoftabs;
}
}
}
Fragment
public class FirstFragment extends Fragment {
// Store instance variables
public static final String ARG_PAGE = "page";
private int mPageNumber;
private Context mContext;
private int Cimage;
private ArrayList<SubcategoryModel> subcatlist;
private RecyclerView rcylervw;
private ArrayList<CategoryModel> filelist;
public static FirstFragment create(int pageNumber){
FirstFragment fragment = new FirstFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
// image uri get uri of image that saved in directory of app
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.test, container, false);
// Get the Bundle Object
Bundle bundleObject = getActivity().getIntent().getExtras();
// Get ArrayList Bundle
ArrayList<CategoryModel> classObject = (ArrayList<CategoryModel>) bundleObject.getSerializable("key");
System.out.println("Frag Category list size"+classObject.size());
rcylervw=(RecyclerView)rootView.findViewById(R.id.subcategory_recycler_view);
rcylervw.setHasFixedSize(true);
MyAdapter adapter = new MyAdapter(new String[]{"test one", "test two", "test three", "test four", "test five" , "test six" , "test seven"});
rcylervw.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rcylervw.setLayoutManager(llm);
return rootView;
}
getting NPE here
// Get the Bundle Object
Bundle bundleObject = getActivity().getIntent().getExtras();
// Get ArrayList Bundle
ArrayList<CategoryModel> classObject = (ArrayList<CategoryModel>) bundleObject.getSerializable("key");
Make CategoryModel implements serializable. Put data in bundle using
Bundle args = new Bundle();
args.putSerializable("key", data);
fragment.setArguments(args);
Then, while getting data use:
if (getArguments() != null) {
classObject = (ArrayList<CategoryModel>) getArguments().getSerializable("key");
}
Make CategoryModel implement Parcelable then use bundle.putParcelableArrayList(key,list) and bundle.getParcelableArrayList(key)
I am using the navigation drawer with sliding tab in my app and I want to change the item list of the navigation drawer according to the tab. Like, In tab 1 navigation drawer with item 1 item 2 and in tab 2 navigation drawer with item 3 and item 4.
Here is the code of my navigation drawer fragment class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);;
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerAdapter = new RecyclerAdapter(getActivity(), getData());
recyclerView.setAdapter(recyclerAdapter);
//recyclerAdapter.setClickListener(this);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
/*recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(),
recyclerView, new ClickListener() {
#Override
public void onClick(View view, int position) {
startActivity(new Intent(getActivity(), SubActivity.class));
}
#Override
public void onLongClick(View view, int position) {
startActivity(new Intent(getActivity(), SubActivity.class));
}
} ));*/
return view;
}
public List<Information> getData(){
List<Information> data = new ArrayList<>();
int[] icons = {R.drawable.abc_ic_ab_back_mtrl_am_alpha, R.drawable.abc_ic_ab_back_mtrl_am_alpha,
R.drawable.abc_ic_ab_back_mtrl_am_alpha};
String[] title = {"Title 1", "Title 2", "Title 3"};
for (int i=0; i<icons.length; i++) {
Information information = new Information();
information.setResIdImage(icons[i]);
information.setText(title[i]);
data.add(information);
}
return data;
}
This is code of tab class
public class Tab2 extends NavigationDrawerFragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab2, container, false);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText("The position is 2");
return view;
}
}
So, I want to change the getData() method items of navigationDrawerFragment class from tab2 class.
Thank You.
You can add this
private NavigationDrawerAdapter adapter;
private static String[] titles = null;
private static String[] icons = null;
...
public void setItems(String[] titles,String[] icons) {
// drawer labels
this.titles = titles;
this.icons = icons;
this.adapter.data = getData();
}
public static List<NavDrawerItem> getData() {
List<NavDrawerItem> data = new ArrayList<>();
// preparing navigation drawer items
for (int i = 0; i < titles.length; i++) {
NavDrawerItem navItem = new NavDrawerItem();
navItem.setTitle(titles[i]);
navItem.setIcon(icons[i]);
data.add(navItem);
}
return data;
}
to FragmentDrawer class
I have a ViewPager with two Fragments with RecyclerViews that show Locations and Items. Each location contains different items. Now I want to update the recyclerView inside ItemsFragment when a location is selected in LocationsFragment. I set onClick method to location element to update the activeLocation field but I don't know how to refresh the data in items' recyclerView.
I would need to supply a new List object I get from getItems() into the adapter, but the adapter is null whenever ItemsFragment loses focus or make it so the fragment is recreated every time the activeLocation changes.
The only time it refreshes now is when the ItemsFragment's onCreateView is called. The list updates, because a new ItemAdapter is created and getItems() is called inside constructor. But this only happens when I scroll to 4th tab and back, so that the itemsFragment (2nd) is cleared from memory and reloaded.
Activity:
public class MainActivity extends AppCompatActivity implements MaterialTabListener {
MaterialTabHost tabHost;
ViewPager pager;
ViewPagerAdapter adapter;
private final int[] icons = {R.drawable.icon_locations, R.drawable.icon_items};
public static Location activeLocation;
#Override
protected void onCreate(Bundle savedInstanceState) {
Realm.setDefaultConfiguration(new RealmConfiguration.Builder(this).build());
Realm realm = Realm.getDefaultInstance();
activeLocation = realm.where(Location.class).findFirst();
pager = (ViewPager) findViewById(R.id.view_pager);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabHost = (MaterialTabHost) this.findViewById(R.id.materialTabHost);
for (int i = 0; i < adapter.getCount(); i++) {
tabHost.addTab(
tabHost.newTab()
.setIcon(ResourcesCompat.getDrawable(getResources(), icons[i], null)).setTabListener(this)
);
}
}
ViewPager:
class ViewPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = {"Locations", "Items"};
private final int[] ICONS = {R.drawable.icon_locations, R.drawable.icon_items};
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case FragmentType.LOCATIONS: {
fragment = LocationsFragment.newInstance();
break;
}
case FragmentType.ITEMS: {
fragment = ItemsFragment.newInstance();
break;
}
}
return fragment;
}
}
LocationsFragment:
public class LocationsFragment extends Fragment {
private RecyclerView recyclerView;
private LocationAdapter adapter;
public static LocationsFragment newInstance() {
return new LocationsFragment();
}
public LocationsFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_locations, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.locations_recycler_view);
adapter = new LocationAdapter(getActivity(), getLocations());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public List<Location> getLocations() {
List<Location> locations;
Log.d("null act", getActivity() == null? "null" : "not null");
Realm realm = Realm.getDefaultInstance();
locations = realm.where(Location.class).findAll();
return locations;
}
}
LocationAdapter:
public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.LocationHolder> {
List<Location> list = Collections.emptyList();
Context context;
private LayoutInflater inflater;
public LocationAdapter(Context context, List<Location> list) {
this.list = list;
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public LocationHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.card_location, parent, false);
return new LocationHolder(view);
}
#Override
public void onBindViewHolder(final LocationHolder holder, int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("clicked", "Location " + location.getId() + " " + location.getName() + " pressed");
if (context instanceof MainActivity) {
MainActivity mainActivity = (MainActivity) context;
MainActivity.activeLocation = location;
mainActivity.setFragment(1); //this changes the title on actionBar and tab highlights
}
}
});
}
ItemsFragment and ItemAdapter are the same as Locations.
I'm trying to implement an activity with tabs in action bar and a view pager. I have the same fragment for every tab.
I need to update fragments after onCreateView is called, passing objects.
Pager adapter :
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter() {
super(getSupportFragmentManager());
}
#Override
public int getCount() {
return cat_pages.getList_categories().size();
}
#Override
public Fragment getItem(int position) {
Log.i("position fragment", "i = " + position);
final Handler uiHandler = new Handler();
int index = position;
final Category cat = cat_pages.getList_categories().get(index);
Log.i("position fragment", "name = " + cat.getName());
mCategoriesFragment = new ListPostFragment();
return mCategoriesFragment;
}
}
Fragment :
public class ListPostFragment extends Fragment {
// Layouts
private LinearLayout layout_loading;
public static GridView list_view_articles;
private TextView txt_nothing, txt_title;
// The list of headlines that we are displaying
private List<Post> mPostsList = new ArrayList<Post>();
private List<Post> list_posts = new ArrayList<Post>();
// The list adapter for the list we are displaying
private ArticlesAdapter mListAdapter;
private Category mCurrentCat;
Category clicked_cat;
String activity;
String title;
public ListPostFragment() {
super();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListAdapter = new ArticlesAdapter(getActivity(), mPostsList);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_grid_posts, container, false);
layout_loading = (LinearLayout) rootView.findViewById(R.id.layout_loading);
list_view_articles = (GridView) rootView.findViewById(R.id.gridViewArticles);
txt_nothing = (TextView) rootView.findViewById(R.id.txt_no_result);
txt_title = (TextView) rootView.findViewById(R.id.txt_title);
Log.i("frag", "frag create view");
return rootView;
}
#Override
public void onStart() {
super.onStart();
list_view_articles.setAdapter(mListAdapter);
}
public void loadPosts(Category clicked_cat, List<Post> list_posts, String title) {
Log.i("frag", "frag load");
layout_loading.setVisibility(View.VISIBLE);
list_view_articles.setVisibility(View.GONE);
txt_title.setVisibility(View.GONE);
mPostsList.clear();
layout_loading.setVisibility(View.GONE);
if(list_posts != null){
mPostsList.addAll(list_posts);
if(mPostsList.size() > 0){
list_view_articles.setVisibility(View.VISIBLE);
txt_nothing.setVisibility(View.GONE);
}else{
list_view_articles.setVisibility(View.GONE);
txt_nothing.setVisibility(View.VISIBLE);
}
mListAdapter.notifyDataSetChanged();
}else{ // Nothing to display
list_view_articles.setVisibility(View.GONE);
txt_nothing.setVisibility(View.VISIBLE);
}
}
}
==> What I need to to call loadPosts(...) in PagerAdapter getItem(), once onCreateView has been called.
Any help is very appreciated.
You should pass your Category as an Extra to the fragment like so:
Bundle bundle = new Bundle();
bundle.putSerializable("CATEGORY", cat);
mCategoriesFragment.setArguments(bundle);
and then in onCreate() in the fragment you can read it using:
Bundle bundle = this.getArguments();
Category cat = (Category) bundle.getSerializable("CATEGORY");
// now you can call loadPosts()