Hello.
I want to say in advance that the EventBus library itself has not yet been used, but a question arose about the expediency of using it.
What is:
Activity with ViewPager embedded in TabLayout with 2 snippets. On each fragment is on RecyclerView . Information for them is taken from ArrayList'ов , which are updated by Google Firebase ValueEventListener'у .
What it looks like now:
At the beginning, a SectionsPagerAdapter declared, which initializes 2 fragments. All of them are in the same class. Inside each fragment in the onCreateView method, RecyclerView and its adapter are initialized. Data is taken from private static ArrayList'ов . The adapters themselves are also declared private static . Inside ValueEventListener'а these ArrayList'ы and adapters are updated accordingly. private static searchView and FragmentManager private static searchView the same heap, because They are needed to work inside the fragments.
As for me, it all looks and works terribly. In this regard, I want to know the opinion on this matter:
- Is it worth reworking using EventBus (and is it possible)
- Is it worth it to alter, using any standard methods (and what)
- Keep it as it is with memory leaks
Thank you in advance for your response.
Cut class code:
public class UserActionsActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{ private static Context context; private ViewPager viewPager; private SectionsPagerAdapter mSectionsPagerAdapter; private static RecyclerView unverifiedUsersView; private static RecyclerView usersView; private static DatabaseReference databaseReference; private ValueEventListener valueEventListener; private static ArrayList<User> unverifiedUsersList = new ArrayList<User>(); private static ArrayList<User> usersList = new ArrayList<User>(); private static UnverifiedUserRecyclerAdapter adapter; private static UserRecyclerAdapter adapter1; private static FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user_actions); context = UserActionsActivity.this; initializeComponents(); setEvents(); } private void initializeComponents(){ fragmentManager = getSupportFragmentManager(); databaseReference = FirebaseDatabase.getInstance().getReference(); mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); viewPager = (ViewPager) findViewById(R.id.viewPager); viewPager.setOffscreenPageLimit(2); viewPager.setAdapter(mSectionsPagerAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); } private void setEvents() { valueEventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (!search) { unverifiedUsersList = Globals.Downloads.getSpecificVerifiedUserList(dataSnapshot, DatabaseVariables.Users.DATABASE_UNVERIFIED_USER_TABLE); adapter = new UnverifiedUserRecyclerAdapter(getApplicationContext(), unverifiedUsersList); unverifiedUsersView.setAdapter(adapter); adapter.notifyDataSetChanged(); usersList = Globals.Downloads.getVerifiedUserList(dataSnapshot); adapter1 = new UserRecyclerAdapter(getApplicationContext(), usersList); usersView.setAdapter(adapter1); adapter1.notifyDataSetChanged(); } } @Override public void onCancelled(DatabaseError databaseError) { } }; } public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { if (position == 0) return FirstFragment.newInstance(); else return SecondFragment.newInstance(); } @Override public int getCount() { return 2; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "Не авторизованные"; case 1: return "Все"; } return null; } } public static class FirstFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_recycler, container, false); unverifiedUsersView = (RecyclerView) v.findViewById(R.id.recycler); LinearLayoutManager mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);; adapter = new UnverifiedUserRecyclerAdapter(context, unverifiedUsersList); unverifiedUsersView.setLayoutManager(mLayoutManager); unverifiedUsersView.setHasFixedSize(false); unverifiedUsersView.setAdapter(adapter); adapter.notifyDataSetChanged(); return v; } public static FirstFragment newInstance() { FirstFragment f = new FirstFragment(); return f; } } public static class SecondFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_recycler, container, false); usersView = (RecyclerView) v.findViewById(R.id.recycler); LinearLayoutManager mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);; adapter1 = new UserRecyclerAdapter(context, usersList); usersView.setLayoutManager(mLayoutManager); usersView.setHasFixedSize(false); usersView.setAdapter(adapter1); adapter1.notifyDataSetChanged(); return v; } public static SecondFragment newInstance() { SecondFragment f = new SecondFragment(); return f; } } }