如何重用片段的代码?

How can I reuse my fragment's code?

我正在使用 ViewPager 编写 Android 应用程序。我的 ViewPager 包含两个片段。在每个片段中都有两个 receclerViews 以显示数据库中项目的不同 LiveData 列表(我正在使用 Room)

BaseFragment的代码

public class BaseFragment extends Fragment {

private RecyclerView recyclerView;
private NewsAdapter adapter;
public ViewModel mViewModel;

public BaseFragment() {
    // Required empty public constructor
}


@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mViewModel = ViewModelProviders.of(this).get(ViewModel.class);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View mView = inflater.inflate(R.layout.fragment_feeds, container, false);
    recyclerView = mView.findViewById(R.id.feeds_recycler_view);
    return mView;
}

@Override
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setUpRecyclerView();
    mViewModel.getAllNewsEntities()
            .observe(this, allNewsEntities -> {
                NewsEntityUtilCallback productDiffUtilCallback =
                        new NewsEntityUtilCallback(adapter.getNewsItems(), allNewsEntities);
                DiffUtil.DiffResult newsEntitiesDiffResult = DiffUtil.calculateDiff(productDiffUtilCallback);
                adapter.setNewsItems(allNewsEntities);
                newsEntitiesDiffResult.dispatchUpdatesTo(adapter);
            });
}

protected void setUpRecyclerView() {
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(layoutManager);
    ItemClickListener onClickListener = (v, position) -> {
        String url = adapter.getNewsItems().get(position).getLink();
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        if (browserIntent.resolveActivity(getContext().getPackageManager()) != null) {
            startActivity(browserIntent);
        }
    };
    adapter = new NewsAdapter(getContext(), onClickListener);
    recyclerView.setAdapter(adapter);
}
}

仔细查看这段代码:

mViewModel.getAllNewsEntities()
            .observe(this, allNewsEntities -> {
                NewsEntityUtilCallback productDiffUtilCallback =
                        new NewsEntityUtilCallback(adapter.getNewsItems(), allNewsEntities);
                DiffUtil.DiffResult newsEntitiesDiffResult = DiffUtil.calculateDiff(productDiffUtilCallback);
                adapter.setNewsItems(allNewsEntities);
                newsEntitiesDiffResult.dispatchUpdatesTo(adapter);
            });
}

在这里,我将观察每个片段中的不同 LiveData 查询(在第二个片段中是 mViewModel.getAllBookmarkedNewsEntities())。其他我的代码将是相同的(相同的生命周期方法,相同的 RecyclerView ). 所以可以给我关于重构我的代码的最佳设计原则的建议。我不想仅仅因为一行就将我的代码复制到另一个 Fragment class

只需扩展 BaseFragment 并覆盖 onActivityCreated 方法。像这样:

public class ReuseBaseFragment extends BaseFragment {
    @Override
   public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
      //Your new code here
   }
}