继承自定义片段的最佳做法是什么?

时间:2015-12-19 20:48:04

标签: android inheritance android-fragments

我正在为我的机器人考试制作一个Reddit应用程序,我有一个关于遗产的问题。

我有一个拥有RecyclerView的Fragment。该recyclerview包含redditposts列表。我的应用程序包含多个subreddits(搞笑,游戏,新闻等)。每个subreddit都有自己的片段。我有一些方法,每个片段必须有。 (showProgressBar,hideProgressBar,populateResult等...)我认为如果我只是创建一个Fragment类,其中所有subreddit片段都可以继承,那将是很简单的。我可以将所有方法放在该片段类中,因为每个subreddit片段的方法都相同。但是我的讲师说这是对继承的不好用。那么有没有人围绕这个问题进行最佳实践?

这是我正在谈论的片段: 包com.example.thomas.redditapp;

public class FunnyFragment extends Fragment {

    private OnListFragmentInteractionListener mListener;

    @Bind(R.id.funny_recyclerview)
    RecyclerView mRecyclerView;

    @Bind(R.id.progressBarFetch)
    ProgressBar progress;

    private RedditHelper helper;

    private RedditPostRecyclerViewAdapter mAdapter;

    List<RedditPost> redditPosts;

    public FunnyFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        helper = null;
        helper = new RedditHelper(SubRedditEnum.funny, this);
        redditPosts = new ArrayList<>();
        startLoad();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_funny_list, container, false);
        ButterKnife.bind(this, view);
        showProgressBar();

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        mAdapter = new RedditPostRecyclerViewAdapter(redditPosts, mListener, mRecyclerView);

        mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                redditPosts.add(null);
                helper.loadListFromUrl();
            }
        });

        mRecyclerView.setAdapter(mAdapter);



        return view;
    }

    protected void startLoad() {
        if (helper != null) {
            helper.loadListFromDb();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        if (isTaskRunning()) {
            showProgressBar();
        } else {
            hideProgressBar();
        }
        super.onActivityCreated(savedInstanceState);

    }

    public void hideProgressBar() {
        progress.setVisibility(View.GONE);

    }

    public void showProgressBar() {
        progress.setVisibility(View.VISIBLE);
        progress.setIndeterminate(true);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnListFragmentInteractionListener) {
            mListener = (OnListFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public void populateResult(List<RedditPost> result) {
        if(!redditPosts.isEmpty()){
            redditPosts.remove(redditPosts.size() - 1);
        }
        redditPosts.addAll(result);
        mAdapter.setLoaded();
        mAdapter.notifyDataSetChanged();
    }

    protected boolean isTaskRunning() {
        if (helper == null) {
            return false;
        } else if (helper.getStatus() == 0) {
            return false;
        } else {
            return true;
        }
    }
}

我在helper类中调用了hideProgressBar(),showProgressBar()和populateResult()。

0 个答案:

没有答案