Suppose that I have an application that stores information about the objects of my city. I have a MainActivity , which contains a HomeFragment , which in turn contains a RecyclerView . This is a list of object categories (Shops, shopping centers, etc.). When I click on each item RecyclerView I get on a new Activity , which contains a list of CardView (these are already items of a specific category). And, accordingly, when I click on each CardView element CardView I get on a new Activity , which contains a description of a specific object. I have already begun to make it so that when I click on each item RecyclerView , for example, I create a new Activity and switch to it. And so on. The result is a lot of Activity . But after all, you can do, for example, a HomeFragment with a list of categories, one Activity , which contains elements of a specific category and one Activity , which contains a description of a specific object and only display the necessary data in them. The question is how to do it.

HomeFragment Code:

 public class HomeFragment extends Fragment { private final String placeNames[] = { "Категория 1", "Категория 2", "Категория 3", "Категория 4", "Категория 5", }; private final String imageUrls[] = { "https://cat1.jpg", "httsp://cat2.jpg", "https://cat3.jpg", "https://cat4.jpg", "https://cat5.jpg", }; public HomeFragment() {} @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home, container, false); RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); recyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(layoutManager); // при нажатии на каждый элемент создаю новую Активити recyclerView.addOnItemTouchListener( new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { switch (position) { case 0: getActivity().startActivity(new Intent(getActivity(), ShopsActivity.class)); break; case 1: getActivity().startActivity(new Intent(getActivity(), ShopCentersActivity.class)); break; case 2: getActivity().startActivity(new Intent(getActivity(), FunActivity.class)); break; case 3: getActivity().startActivity(new Intent(getActivity(), NightLifeActivity.class)); break; case 4: getActivity().startActivity(new Intent(getActivity(), BeautyAndHealthActivity.class)); break; case 5: getActivity().startActivity(new Intent(getActivity(), SportActivity.class)); break; default: break; } } }) ); ArrayList placeItems = prepareData(); DataAdapter adapter = new DataAdapter(placeItems, getActivity()); recyclerView.setAdapter(adapter); return view; } private ArrayList prepareData() { ArrayList placeItemList = new ArrayList<>(); for (int i = 0; i < placeNames.length; i++) { PlaceItem itemPlace = new PlaceItem(); itemPlace.setPlaceName(placeNames[i]); itemPlace.setImageUrl(imageUrls[i]); placeItemList.add(itemPlace); } return placeItemList; } } 

Code specific category ShopsActivity :

 public class ShopsActivity extends AppCompatActivity { private ShopsAdapter adapter; private List<ConcreteObject> objectList; @SuppressLint("PrivateResource") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shops); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); if (toolbar != null) { toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onBackPressed(); } }); } RecyclerView recyclerView = (RecyclerView) findViewById(R.id.shops_recycler_view); objectList = new ArrayList<>(); adapter = new ShopsAdapter(this, objectList); RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2); if (recyclerView != null) { recyclerView.setLayoutManager(layoutManager); recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true)); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(adapter); // тут я снова создаю новые Активити и перехожу на них recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() { @Override public void onClick(View view, int position) { switch (position) { case 0: startActivity(new Intent(ShopsActivity.this, OneShop.class)); break; case 1: startActivity(new Intent(ShopsActivity.this, TwoShop.class)); break; case 2: startActivity(new Intent(ShopsActivity.this, ThreeShop.class)); break; case 3: startActivity(new Intent(ShopsActivity.this, FourShop.class)); break; case 4: startActivity(new Intent(ShopsActivity.this, FiveShop.class)); break; case 5: startActivity(new Intent(ShopsActivity.this, SixShop.class)); break; case 6: startActivity(new Intent(ShopsActivity.this, SevenShop.class)); break; case 7: startActivity(new Intent(ShopsActivity.this, Eight.class)); break; default: break; } } @Override public void onLongClick(View view, int position) { } })); } prepareObjects(); Glide.with(this) .load(R.drawable.cover_backdrop) .into((ImageView) findViewById(R.id.backdrop)); } @Override protected void prepareObjects() { objectList.add(new ConcreteObject("Oneshop", 3.9, R.drawable.one)); objectList.add(new ConcreteObject("TwoShop", 3.5, R.drawable.two)); objectList.add(new ConcreteObject("ThreeShop", 3.5, R.drawable.three)); objectList.add(new ConcreteObject("FourShop", 3.5, R.drawable.four)); ... adapter.notifyDataSetChanged(); } } 

The code for a particular ConcreteOneActivity object:

 public class ConcreteOneActivity extends StartConfig { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_concrete_one); setToolbar(R.id.toolbar_concrete_one_toolbar); setCollapsingToolbar("ConcreteOne", R.id.concrete_one_collapsing_toolbar); CustomPagerAdapter adapter = new CustomPagerAdapter(this) { @Override public Object instantiateItem(ViewGroup collection, int position) { LayoutInflater inflater = LayoutInflater.from(context); View view = null; switch (position) { case 0: view = inflater.inflate(R.layout.concrete_one_information, collection, false); break; case 1: view = inflater.inflate(R.layout.concrete_one_location, collection, false); break; case 2: view = inflater.inflate(R.layout.map_fragment, collection, false); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); break; } collection.addView(view); return view; } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; LatLng concreteOne = new LatLng(1, 1); mMap.addMarker(new MarkerOptions().position(concreteOne).title("ConcreteOne"); mMap.moveCamera(CameraUpdateFactory.newLatLng(concreteOne)); } }; setViewPager(adapter); } } 
  • one
    If your activations of each level have the same markup and logic, and differ only in the displayed content, of course, it is too wasteful and laborious to make “named” activations for each item. Group your content into a structure in which you can refer to a specific type of content by ordinal number (for example, ArrayList collection) / Next, when you click on a list, get a position and transfer it to the next activation. At the received position number, get the desired content and fill them with widgets. Also the next level. - pavlofff
  • Thank you, I took your advice! - NikiZ

0