如何重用我的片段代码?

时间:2018-04-26 06:47:55

标签: android fragment refactoring android-room android-livedata

我正在使用ViewPager编写Android应用程序。我的ViewPager包含两个片段。在每个片段中都有两个receclerViews来显示来自Database的不同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类中

1 个答案:

答案 0 :(得分:1)

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

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