Hello! I have such a code fragment that is used in more than 10 fragments, tell me how it can be standardized, so that it can be called up in one or two lines. I understand it is necessary to implement a callback interface, but I do not quite understand how to implement it.

public class MyFragment extends Fragment { private RecyclerView recyclerView; private LinearLayoutManager layoutManager; private BookListAdapter bookListAdapter; private List<Book> bookList = new ArrayList<>(); public MyFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.fragment, container, false); final Context context = getActivity(); tests(); Log.e("Hrre ","qwertyuio " + bookList.get(2).getAuthor()); bookListAdapter = new BookListAdapter(context, bookList); recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); layoutManager = new LinearLayoutManager(getActivity()); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(bookListAdapter); recyclerView.setLayoutManager(layoutManager); // bookListAdapter.notifyDataSetChanged(); return view; } 

}

  • not quite clear question. Do you want to know how to insert it 10 times into markup or programmatically? If a fragment is registered and properly inserted, there is no need to additionally refer to it. - Jarvis_J
  • and some functions in the fragment will change, and some remain basic? - Jarvis_J
  • @Jarvis_J, I wanted to exclude copying of the code in all fragments, and simplify future support. I wanted to do something similar: class Fragment_1 extends MyFragment {public void myMethods () {bookList = getBookList (); myFragment (bookList); }} - Valeriy
  • According to the rules of good code, it is generally better to keep the booklist separately and create a fragment using newInstance , passing the list position to the argument. And if necessary, get a booklist from where it is stored. Convenient MCV pattern. If you need implementation with inheritance, write what exactly does not work in your version (with extends )? - Jarvis_J
  • one
    Here is a good big book on this (and not only) occasion: infasoft.com/library/books/… . Well, regarding your question, now I will write the answer to how I understood the question - Jarvis_J

2 answers 2

Make a BaseFragment (for example) which is in turn inherited from Fragmen. In this BaseFragment you implement all the logic that repeats, then every new fragment in which there is the same logic you inherit from BaseFragment and that's it. If you need to call any BaseFragment methods directly from the heir, set the visibility property for the required protected methods. These are basic things about Java OOP.

  • Apparently here in the question is the classic problem XY. The fact is that the person in question needs completely the same classes, in which only the data (different sets of books) that need to be displayed differ . This is not decided by inheritance, but by transferring data to the same class. A person does not really understand exactly how his task is being solved; it is worth waiting for him to reappear and clarify exactly what his problem is, and not his [wrong] idea of ​​its solution. - pavlofff
  • )) Well then we are waiting for the explanations of the author - Eugene Zaychenko
  • BaseFragment, this is what I need. Only I can not understand how to implement it and how to call it. - Valeriy
  • This is what should happen: there is one activity that has several fragments: genres, search, news, etc., there is a second activity: which also has genres, news, etc., but with a different logic of work. But all of them will visually have the same design, it will be a RecyclerView with CardView, which will be programmed from several markups (something like dynamic markup in web programming). Accordingly, why should I write in each fragment the implementation of the fragment that is given above, although it will be identical for all. - Valeriy
  • @Valeriy if it is 10 identical fragments, why should we inherit it from something? Use the same fragment, it is so difficult to understand it. Nobody makes a separate fragment to see genres on the screen, another - new items, etc., if everything is the same, but sorted or filtered in different ways. - pavlofff

Keep your list separate from everything. To simplify the MVC pattern:

 public class BookLab { private static BookLab bookLab; private ArrayList<Book> books; private ArrayList<Book> books2; private ArrayList<Book> books3; ... public static BookLab get(Context ctx) { if (bookLab== null) bookLab= new BookLab (ctx); return bookLab; } private BookLab (Context ctx) { books = new ArrayList<>(); String [] books_names = ctx.getResources().getStringArray(R.array.ваш array из ресурсов); for (int i=0;i<book_names.lenght;i++){ Book book = new Book(); book.setName(book_names[i]); books.add(book); } повторить для остальных books } ArrayList<Book> getBooks (int number){ switch(number){ case 1: return books; case 2: return books1; case 3: return books2; } } 

Book class:

 public class Book { private String name, description; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } 

In the fragment:

 public static MyFragment newInstance(int number) { Bundle args = new Bundle(); args.putInt("number", number); MyFragment fragment = new MyFragment (); fragment.setArguments(args); return fragment; } onCreateView(...){ number_of_list = getArguments().getInt("number",0) myList = BookLab.get(getActivity()).getBooks(number_of_list); } int getNumberOfList(){ return number_of_list; } 

Get the list from another location: BookLab.get(getActivity()).getBooks(MyFragment.getNumberOfList());

  • Thank you, but this is a bit different, although it will be useful for me. I wanted to simplify the implementation of the fragments directly, so that in each fragment not to create 30-50 lines of the same code, which in the future will be problematic to change and supplement, because there will be more than 10 classes (fragments). And so you need to change the animation, changed the parent and everything is fine, you need to redo the shadows, please, etc. After all, all the fragments are in the same application and should behave plus / minus equally, so as not to cause a stunned user - Valeriy
  • A) what's the problem with extends ? - Jarvis_J
  • I understand that you are creating a class containing all the arrays, and from there you are already getting them. Is this done to simplify data access? - Valeriy
  • the problem is that I cannot understand how to call the parent class in the child class and pass it the output values. Do I need to call onCreate () or onCreateView () and what do they need to pass? And how do I pass my array? - Valeriy
  • if you work with the heir to the parent you do not need to contact. If you want to rewrite some parent variable or function, do @Override myFunction (or myVar , defined in the parent, = the desired value. - Jarvis_J