Я создаю приложение, в котором мне нужно показывать разные данные на разных элементах. Нажмите на навигационную панель. Я хочу изменить запрос , когда я нажимаю на меню навигационной панели и показываю данные в одном и том же фрагменте.
Этот код работает нормально, я хочу знать, как изменить запрос в хранилище, чтобы показывать разные данные в одном фрагменте
I пытаюсь передать данные из mainActivity, но приложение не работает, я пытаюсь просто передать запрос, создав объект класса LatestFragment в основной активности.
MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.main_toolbar);
//adding the toolbar
setSupportActionBar(toolbar);
ViewPager viewPager = findViewById(R.id.view_pager);
TabLayout tabLayout = findViewById(R.id.tab_layout);
LatestFragment latestFragment = new LatestFragment();
TrendingFragment trendingFragment = new TrendingFragment();
DailyQuizFragment dailyQuizFragment = new DailyQuizFragment();
YojanaFragment yojanaFragment = new YojanaFragment();
//tablayout setup with viewPager
tabLayout.setupWithViewPager(viewPager);
//setting viewPager Adapter
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),0);
//adding fragment to viewPager Adapter start
viewPagerAdapter.addFragment(latestFragment,"latest");
viewPagerAdapter.addFragment(trendingFragment,"trending");
viewPagerAdapter.addFragment(dailyQuizFragment,"daily quiz");
viewPagerAdapter.addFragment(yojanaFragment,"yojana");
//adding fragment to viewPager Adapter end
//adding viewPager Adapter
viewPager.setAdapter(viewPagerAdapter);
//adding icon to tab layout start
tabLayout.getTabAt(0).setIcon(R.drawable.ic_new_icon_black);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_trending_icon_black);
tabLayout.getTabAt(2).setIcon(R.drawable.ic_snooze_icon_black);
tabLayout.getTabAt(3).setIcon(R.drawable.ic_book_icon_black);
//adding icon to tab layout start
//navigation drawer start
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.openNavDrawer,
R.string.closeNavDrawer
);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
//navigation drawer end
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
update();
}
});
}
//navigation drawer method start
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return false;
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}//navigation drawer method end
public void update(){
TabLayout tabLayout = findViewById(R.id.tab_layout);
if (tabLayout.getTabCount()>2) {
tabLayout.removeTabAt(3);
tabLayout.removeTabAt(2);
}
}
}
Последний фрагмент
public class LatestFragment extends Fragment {
View view;
private RecyclerView recyclerView;
private FirebaseFirestore firebaseFirestore;
private FirestoreAdapter firestorePagingAdapter;
public LatestFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_latest, container, false);
recyclerView = view.findViewById(R.id.latest_recView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
fetch();
return view;
}
public void fetch() {
firebaseFirestore = FirebaseFirestore.getInstance();
//Query
Query query = firebaseFirestore.collection("History");
//Pagination
PagedList.Config config = new PagedList.Config.Builder()
.setInitialLoadSizeHint(10)
.setPageSize(3)
.build();
//RecyclerOption
FirestorePagingOptions<AdapterData> options = new FirestorePagingOptions.Builder<AdapterData>()
.setLifecycleOwner(getActivity())
.setQuery(query, config, AdapterData.class)
.build();
firestorePagingAdapter = new FirestoreAdapter(options);
recyclerView.setAdapter(firestorePagingAdapter);
}
}
FirestoreAdapter
public class FirestoreAdapter extends FirestorePagingAdapter<AdapterData, FirestoreAdapter.ProductsViewHolder> {
public FirestoreAdapter(@NonNull FirestorePagingOptions<AdapterData> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull ProductsViewHolder holder, int position, @NonNull final AdapterData model) {
Log.d("data",model.getTitle());
holder.recView_title.setText(model.getTitle());
holder.recView_content.setText(model.getContent());
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), FullSrcreenActivity.class);
intent.putExtra("title",model.getTitle());
intent.putExtra("content",model.getContent());
v.getContext().startActivity(intent);
}
});
}
@NonNull
@Override
public ProductsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view =inflater.inflate(R.layout.recycler_view_layout, parent,false);
return new ProductsViewHolder(view);
}
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
super.onLoadingStateChanged(state);
switch (state) {
case LOADING_MORE:
Log.d("PAGING_LOG","Loading Next Page");
break;
case LOADING_INITIAL:
Log.d("PAGING_LOG","Loading Initial Data");
break;
case FINISHED:
Log.d("PAGING_LOG","All Data Loaded");
break;
case ERROR:
Log.d("PAGING_LOG","ERROR Loading Data");
break;
case LOADED:
Log.d("PAGING_LOG","Total Item Loaded : " + getItemCount());
break;
}
}
//View Holder
public class ProductsViewHolder extends RecyclerView.ViewHolder {
TextView recView_title, recView_content;
View view;
public ProductsViewHolder(@NonNull View itemView) {
super(itemView);
recView_title = itemView.findViewById(R.id.title);
recView_content = itemView.findViewById(R.id.content);
view = itemView;
}
}
}
AdapterData
public class AdapterData {
private String title;
private String content;
public AdapterData() {}
public AdapterData(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}