Recycler view not updating with dynamic tab layout in fragments - android

I have Recycler view in list income fragment and I want to use the same list income fragment for all my dynamically created tags by updating the data of recycler view according to the tab selected. I have the below code and works fine for the first time and only in first tab it works. When I swipe to 2nd, 3rd and 4th or come back to 1st, it shows empty recycler data. I am struggling with this past 2 days. Help is very much appreciated.
IncomeListTabsFragment.java
public class IncomeListTabsFragment extends Fragment {
private TabLayout branchesTabLayout;
private ViewPager incomeListViewPager;
private BranchAPIService branchAPIService;
private List<Branch> branchesList;
private View view;
private ViewPagerAdapter viewPagerAdapter;
public IncomeListTabsFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_income_list_tabs, container, false);
incomeListViewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewPager(incomeListViewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
getBranchesFromAPI();
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPagerAdapter = new ViewPagerAdapter(getFragmentManager());
for (Branch branch: branchesList) {
IncomeListFragment incomeListFragment = IncomeListFragment.newInstance(branch.getId());
viewPagerAdapter.addFrag(incomeListFragment, branch.getName());
}
branchesTabLayout = (TabLayout) view.findViewById(R.id.branchTabLayout);
branchesTabLayout.setupWithViewPager(viewPager);
viewPager.setAdapter(viewPagerAdapter);
}
private List<Branch> getBranchesFromAPI() {
// .......
return branchesList;
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
IncomeListFragment.java
public class IncomeListFragment extends Fragment {
View view;
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
TabLayout branchesTabLayout;
ViewPager incomeListViewPager;
public static final String ARG_BRANCH_ID = "branch_id";
private BranchAPIService branchAPIService;
private List<Branch> branchesList;
private int branchId;
public IncomeListFragment() {
// Required empty public constructor
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
((IncomeAdapter) recyclerView.getAdapter()).onSaveInstanceState(outState);
}
public static IncomeListFragment newInstance(int branch_id) {
IncomeListFragment incomeListFragment = new IncomeListFragment();
Bundle args = new Bundle();
args.putInt(ARG_BRANCH_ID, branch_id);
incomeListFragment.setArguments(args);
return incomeListFragment;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_income_list, container, false);
branchId = getArguments().getInt(ARG_BRANCH_ID);
List<IncomeParent> incomeParentTest = ((GlobalVariableHelper) getActivity().getApplicationContext()).getIncomeParents();
recyclerView = (RecyclerView) view.findViewById(R.id.income_list_recycler_view);
layoutManager = new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(layoutManager);
IncomeAdapter incomeAdapter = new IncomeAdapter(getActivity(), incomeData());
incomeAdapter.setParentClickableViewAnimationDefaultDuration();
incomeAdapter.setParentAndIconExpandOnClick(true);
recyclerView.setAdapter(incomeAdapter);
return view;
}
private List<ParentObject> incomeData() {
//get data for recycler view
return incomeParentObject;
}
}

Related

Viewpager empty when opened second time

I'm creating an apps to create a Sales Order. It has 1 fragment to show list of Sales Orders (ListFragment), 1 fragment to show Sales Order's form (FormFragment) and inside, there is a viewpager which has 2 fragments: HeaderFragment and ItemsFragment. When I accessed it the first time, it works fine, header and items are showing the input fields. But when I accessed it the second time, header and items are empty.
Here is how I open FormFragment from ListFragment:
public class ListFragment extends Fragment {
ListView listViewSO;
Bundle bundle;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
((MainActivity)getActivity()).getSupportActionBar().setTitle("SO LIST");
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_list_so, container, false);
bundle = getArguments();
listViewSO = root.findViewById(R.id.list_view_so);
listViewSO.setOnItemClickListener(SOListItemClick);
return root;
}
private AdapterView.OnItemClickListener SOListItemClick = new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
SalesOrder salesOrder = (ListFragment.this).adapter.getItem(i);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
bundle.putParcelable("salesorder", salesOrder);
FormFragment FormFragment = new FormFragment();
FormFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.fragment_container, FormFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
};
}
This is my FormFragment
public class FormFragment extends Fragment {
Bundle bundle;
SalesOrder salesorder;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
((MainActivity)getActivity()).getSupportActionBar().setTitle("EDIT SO");
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_so_form, container, false);
((MainActivity)getActivity()).drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
((MainActivity)getActivity()).toggle.setDrawerIndicatorEnabled(false);
((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
bundle = getArguments();
salesorder = bundle.getParcelable("salesorder");
viewPager = (ViewPager) root.findViewById(R.id.view_pager);
setupViewPager(viewPager);
tabLayout = (TabLayout) root.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return root;
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getActivity().getSupportFragmentManager(), BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
adapter.addFragment(new HeaderFragment(), "Header");
adapter.addFragment(new ItemFragment(), "Items");
adapter.addBundle(bundle);
viewPager.setAdapter(adapter);
}
}
This is my adapter:
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionsPageAdapter(#NonNull FragmentManager fm, int behavior) {
super(fm, behavior);
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public void addBundle(Bundle bundle) {
for(int i = 0; i < mFragmentList.size(); i++) {
mFragmentList.get(i).setArguments(bundle);
}
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#NonNull
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
}
And lastly, this is my HeaderFragment
public class VisitationSOHeaderFragment extends Fragment {
Bundle bundle;
SalesOrder salesorder;
TextInputLayout textInputSoCode, textInputSoDate;
ProgressBar loading;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_visitation_so_header, container, false);
bundle = getArguments();
salesorder = bundle.getParcelable("salesorder");
loading = root.findViewById(R.id.loading);
textInputSoCode = root.findViewById(R.id.text_input_so_code);
textInputSoDate = root.findViewById(R.id.text_input_so_date);
textInputSoCode.getEditText().setText(salesorder.getSoCode());
textInputSoDate.getEditText().setText(salesorder.getSoDate());
return root;
}
}
How is it happen? Is there something wrong with the viewpager settings? Any help would be much appreciated.
Do not use getActivity().getSupportFragmentManager() to get fragment manager for FragmentPagerAdapter. Just use getChildFragmentManager method instead.

ViewPager not showing content onResume

I'm using FragmentStatePagerAdapter for TabLayout with a ViewPager going to show a RecyclerView and when I click on a item of my list and i click back to go to previous page It's showing me a blank page with TabLayout and no RecyclerView.
Here is my Adapter code:
public class MyViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public MyViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
And here is my fragment handling ViewPager and TabLayout:
public class InternetFragment extends Fragment {
private View rootView;
private TabLayout tabLayout;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_internet_package, container, false);
initUI();
return rootView;
}
private void initUI() {
ViewPager viewPager = rootView.findViewById(R.id.viewpager);
tabLayout = rootView.findViewById(R.id.tabs);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TextView txtTabTitle = (TextView) LayoutInflater.from(GC.getInstance().getAppContext()).inflate(R.layout.custom_tablayout_cell, null);
tabLayout.getTabAt(i).setCustomView(txtTabTitle);
txtTabTitle.setTypeface(GC.font);
}
}
private void setupViewPager(ViewPager viewPager) {
String IMSI = GC.getInstance().getSimNumber();
MyViewPagerAdapter pageAdapter = new MyViewPagerAdapter(getActivity().getSupportFragmentManager());
if (IMSI.startsWith(ClientConstant.IMSI_RIGHTEL) || IMSI.startsWith(ClientConstant.IMSI_TALIA)) {
tabLayout.setVisibility(View.GONE);
pageAdapter.addFragment(new InternetOnlinePackageFragment(), "");
} else {
pageAdapter.addFragment(new InternetUSSDPackageFragment(), getString(R.string.package_ussd));
pageAdapter.addFragment(new InternetOnlinePackageFragment(), getString(R.string.package_online));
}
viewPager.setAdapter(pageAdapter);
if (!IMSI.startsWith(ClientConstant.IMSI_RIGHTEL) || !IMSI.startsWith(ClientConstant.IMSI_TALIA))
viewPager.setCurrentItem(1);
}
}

Android tabs show wrong fragments

Hello, I'm trying to implement an Activity with tabs. Some tabs contain a fragment that shows simple list, others a tree view like list.
The fragments with simple list work fine, while I'm having troubles with the one which shows tree view like list: the tree are build properly, but I get them in the wrong tabs. In the first tab I always see the content of the second one, and swiping back or expanding/collapsing the tree produces a new unwanted substitution of the whole tree.
I'm struggling to understand what I'm doing wrong. I hope you can suggest me what to do. Thanks in advance!
Here's my code:
Pager Adapter:
public class PagerAdapter extends FragmentStatePagerAdapter {
private final List<ListFragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager manager)
{
super(manager);
}
#Override
public ListFragment getItem(int index) {
return mFragmentList.get(index);
}
#Override
public int getCount()
{
return mFragmentList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public void addFragment(int index, boolean tree) {
Bundle b=new Bundle();
String category= Items.getCategoryByIndex(index);
b.putString(Items.CATEGORY,category);
ListFragment fragment;
if(!tree) {
fragment = new ContentFragment();
} else {
fragment = new TreeFragment();
}
fragment.setArguments(b);
mFragmentList.add(fragment);
mFragmentTitleList.add(Items.getCategoryByIndex(index));
}
}
This is the TreeFragment class:
public class TreeFragment extends TreeViewFragment {
TreeListAdapter mAdapter;
public TreeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle b = getArguments();
String category = (String) b.get(Items.CATEGORY);
GlobalVariables.dataList = Items.getTreeNodeByCategory(category);
GlobalVariables.nodes = TreeViewLists.LoadInitialNodes(GlobalVariables.dataList);
TreeViewLists.LoadDisplayList();
mAdapter = new TreeListAdapter(inflater.getContext());
setListAdapter(mAdapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
and in the activity:
public class MyActivity extends AppCompatActivity {
private ViewPager mViewPager;
private PagerAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mAdapter = new PagerAdapter(getSupportFragmentManager());
// (...) Content creation (...)
mAdapter.addFragment(0, true);
mAdapter.addFragment(1, true);
mAdapter.addFragment(2, true);
mAdapter.addFragment(3, true);
mAdapter.addFragment(4, false);
mAdapter.addFragment(5, false);
mViewPager.setAdapter(mAdapter);
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(mViewPager);
}

Android studio Fragment ViewPager for sliding drawable

I cant create fragment with vierpager. I'll use this on my sliding menu to replace activity. I don't wnat to use extends FragmentActivity because it will not work when I use in
getFragmentManager().beginTransaction().replace(R.id.container, home).commit();
public class Home extends Fragment{
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home, container, false);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new Recent(), "RECENT");
adapter.addFragment(new Shake(), "SHAKE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<android.support.v4.app.Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
My error is the getChildFragmentManager()

Sliding Tabs to load XML Data using RecyclerView Adapter is not displaying content properly

I have been working on an app to take the blog feed and parse and display it in the app using the Recycler and Card View. I got to the point of adding sliding tabs to display articles in each of the categories in the site in separate tabs. I parse the feed using the category URL and am able to display it using the same Recycler view adapter.
Here comes the problem, as I swipe through the tabs quickly, the content doesn't load in some cases. In some cases, I get articles from one category displayed in another category's tab.
Below is the code for my Tabs fragment:
public class TabsFragment extends Fragment {
ViewPager mViewPager;
ViewPagerAdapter mAdapter;
SlidingTabLayout mSlidingTabLayout;
CharSequence Titles[] = {"Home", "Events", "Category", "Bookmarks", "News"};
int NoOfTabs = 5;
private ProgressBar mProgressBar;
private RecyclerView mRecyclerView;
private MyRecyclerViewAdapter myRecyclerViewAdapter;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_tabs, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
mAdapter = new ViewPagerAdapter(Titles, NoOfTabs);
mProgressBar = (ProgressBar) view.findViewById(R.id.loading_spinner);
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.tabs);
mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.secondaryBackgroundColor);
}
#Override
public int getDividerColor(int position) {
return getResources().getColor(R.color.secondaryBackgroundColor);
}
});
mSlidingTabLayout.setViewPager(mViewPager);
}
public class ViewPagerAdapter extends PagerAdapter {
CharSequence mTitles[];
int mNoOfTabs;
public ViewPagerAdapter(CharSequence[] titles, int noOfTabs) {
mTitles = titles;
mNoOfTabs = noOfTabs;
}
public CharSequence getPageTitle(int position) {
return mTitles[position];
}
#Override
public int getCount() {
return mNoOfTabs;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item_articles,
container, false);
container.addView(view);
ProcessArticles processArticles;
processArticles = new ProcessArticles(null, mTitles[position]);
processArticles.execute();
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
myRecyclerViewAdapter = new MyRecyclerViewAdapter(new ArrayList<Article>(),
getActivity().getApplicationContext());
mRecyclerView.setAdapter(myRecyclerViewAdapter);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return object == view;
}
}
public class ProcessArticles extends GetXmlData {
public ProcessArticles(String searchCriteria, String category) {
super(searchCriteria, category);
}
#Override
public void execute() {
super.execute();
ProcessData processData = new ProcessData();
processData.execute();
}
public class ProcessData extends DownloadXmlData {
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ParseArticles parseArticles = new ParseArticles(getData());
boolean operationStatus = parseArticles.process();
if (operationStatus) {
ArrayList<Article> allArticles = parseArticles.getArticles();
myRecyclerViewAdapter.loadNewData(allArticles);
} else {
Toast.makeText(getActivity().getApplicationContext(), "Error establishing correction.", Toast.LENGTH_SHORT).show();
Log.d("MainActivity", "Error establishing correction.");
}
}
}
}
}
The code for the activity displaying the tabs is:
public class TabsActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_tabs);
activateToolbarWithHomeEnabled();
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
TabsFragment fragment = new TabsFragment();
transaction.replace(R.id.tab_content_frame, fragment);
transaction.commit();
}
}
}
How do I separate the content according to category in the tabs? I have tried to reduce the amount of code, so, I have used the same Recycler adapter. Is there any solution to this problem? Or should I change the method of implementation? I am looking to implement something like the Play Newsstand app.

Categories

Resources