Actually, i created a navigation drawer that has three items and on clicking on every item it goes to its fragment that displays certain type of products , so i have an Activity with three fragments for each type of them.
If i would like to add another product type i will have to create its fragment. So ,My question is , are there any methods that can only make one fragment and every time an item is clicked only the data inside the fragment to be changed/replaced and not to replace the whole fragment itself with another ?
Edited my main activity:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
RecyclerView recyclerView;
String navTitles[];
private NavigationView navigationView;
TypedArray navIcons;
RecyclerViewAdapter recyclerViewAdapter;
ActionBarDrawerToggle drawerToggle;
Fragment[] fFragments = new Fragment[3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Let's first set up toolbar
setupToolbar();
//Initialize Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerMainActivity);
//Setup Titles and Icons of Navigation Drawer
navTitles = getResources().getStringArray(R.array.navDrawerItems);
navIcons = getResources().obtainTypedArray(R.array.navDrawerIcons);
recyclerViewAdapter = new RecyclerViewAdapter(navTitles, navIcons, this);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.setClickedListener(new RecyclerViewAdapter.ClickListerner() {
@Override
public void onItemlistener(int index) {
updateUIWithIndex(index);
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Finally setup ActionBarDrawerToggle
setupDrawerToggle();
//Add the Very First Fragment to the Container
updateUIWithIndex(1);
}
// on click update fragment
private void updateUIWithIndex(int index) {
drawerLayout.closeDrawers();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment fFragment = null;
if (fFragments[index - 1] == null) {
switch (index) {
case 1:
fFragment = new FirstFragment();
break;
case 2:
fFragment = new SecondFragment();
break;
case 3:
fFragment = new ThirdFragment();
break;
}
fFragments[index - 1] = fFragment;
} else {
fFragment = fFragments[index - 1];
}
fragmentTransaction.replace(R.id.containerView, fFragment);
fragmentTransaction.commit();
}
void setupToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
void setupDrawerToggle() {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
//This is necessary to change the icon of the Drawer Toggle upon state change.
drawerToggle.syncState();
}
}
My Fragment :
public class FirstFragment extends Fragment implements ClickListner {
private final String LOG_TAG = FirstFragment.class.getSimpleName();
private DisplayAdapter recyclerViewAdapter;
private RecyclerView recyclView;
private ArrayList<Products> pProduct = null;
private List<Products> prods = null;
ProductDbHelper pDB;
ProgressDialog mJsonDialog;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.all_products, container, false);
pDB = new ProductDbHelper(getActivity());
mJsonDialog = new ProgressDialog(getActivity());
mJsonDialog.setIndeterminate(true);
if (pDB.isDataAvailable() == 0) {
mJsonDialog.setMessage("Parsing JSON feed...");
mJsonDialog.show();
getFeed();
} else {
new FetchDatabaseTask().execute();
}
return myView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclView = (RecyclerView) view.findViewById(R.id.RecycleList);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclView.setLayoutManager(layoutManager);
recyclerViewAdapter = new DisplayAdapter(getActivity(), new ArrayList<Products>());
recyclView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.setClickListener(this);
}
@Override
public void itemClicked(View view, Parcelable product) {
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra("P", product);
startActivity(intent);
}
public void getFeed() {
RestInterface interfaces = Client.getClient().create(RestInterface.class);
Call<List<Products>> call = interfaces.getProductsReport();
call.enqueue(new Callback<List<Products>>() {
@Override
public void onResponse(Call<List<Products>> call, Response<List<Products>> response) {
prods = response.body();
for (int i = 0; i < prods.size(); i++) {
pDB.addShop(prods.get(i));
}
new FetchDatabaseTask().execute();
if (mJsonDialog.isShowing())
mJsonDialog.dismiss();
}
@Override
public void onFailure(Call<List<Products>> call, Throwable t) {
Log.e(LOG_TAG, "FFFF" + t.toString());
}
});
}
public class FetchDatabaseTask extends AsyncTask<Void, Void, List<Products>> {
protected void onPreExecute() {
mJsonDialog.setMessage("Reading from internal storage...");
mJsonDialog.show();
}
@Override
protected List<Products> doInBackground(Void... voids) {
// get all the shop's products
List<Products> lProduct = pDB.getAllShops();
// in the second fragment , sort the products' price in ascending order
List<Products> lProduct = pDB.sortShopsAscend();
// in the third fragment sort the products descendingly
List<Products> lProduct = pDB.sortShopsDescend();
return lProduct;
}
protected void onPostExecute(List<Products> shops) {
super.onPostExecute(shops);
if (shops != null) {
if (recyclerViewAdapter != null) {
recyclerViewAdapter.setData(shops);
} else {
pProduct = new ArrayList<>();
pProduct.addAll(shops);
}
}
if (mJsonDialog.isShowing())
mJsonDialog.dismiss();
}
}
}