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; } }
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 (withextends)? - Jarvis_J