基于RecyclerView封装优化

上篇文章讲到在RecyclerView赋值的过程中, ((SampleViewHolderholder).mSampleListItemLabel.setText(mDataList.get(position));有强转。
如果有一个viewholder还可以,如果是多布局,那么这里就会有多个viewholder,会写多个判断。先解决强转问题。如果我在SampleViewHolder里面赋值,是不是就不用强转了,但是ViewHolder中没有绑定方法,所以写一个BaseViewHolder

public abstract class BaseViewHolder extends RecyclerView.ViewHolder {

    public BaseViewHolder(View itemView) {
        super(itemView);
    }

    public abstract void onBind(int position);
}
public abstract class BaseListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    protected SwipeRefreshLayout mSwipeRefreshLayout;
    protected RecyclerView mRecyclerView;
    protected BaseListAdapter adapter;
    protected ArrayList mDataList = new ArrayList<>();


    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_sample_list, R.string.sample_list_title);
    }


    @Override
    protected void setUpView() {
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.mSwipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void setUpData() {
        mRecyclerView.setLayoutManager(getLayoutManager());
        adapter = new BaseListAdapter();
        mRecyclerView.setAdapter(adapter);

    }

    protected void setRefreshing() {
        mSwipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(true);
                onRefresh();
            }
        });
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getApplicationContext());
    }


    class BaseListAdapter extends RecyclerView.Adapter {

        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            return getViewHolder(parent, viewType);
        }

        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
          holder.onBind(position);
        }

        @Override
        public int getItemCount() {
            return mDataList != null ? mDataList.size() : 0;
        }

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }
    }

    protected abstract BaseViewHolder getViewHolder(ViewGroup parent, int viewType);

}
public class SampleListActivity extends BaseListActivity implements SwipeRefreshLayout.OnRefreshListener {


    @Override
    protected void setUpView() {
        super.setUpView();
    }

    @Override
    protected void setUpData() {
        super.setUpData();
        setRefreshing();
    }

    @Override
    public void onRefresh() {
        mDataList.clear();
        for (int i = 0; i < 50; i++) {
            mDataList.add("sample list item aa " + i);
        }
        adapter.notifyDataSetChanged();
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    protected BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_sample_list_item, parent, false);
        return new SampleViewHolder(view);
    }


    class SampleViewHolder extends BaseViewHolder {

        TextView mSampleListItemLabel;

        public SampleViewHolder(View itemView) {
            super(itemView);
            mSampleListItemLabel = (TextView) itemView.findViewById(R.id.mSampleListItemLabel);
        }

        @Override
        public void onBind(int position) {
            mSampleListItemLabel.setText(mDataList.get(position));
        }
    }
}

你可能感兴趣的:(基于RecyclerView封装优化)