Java tutorial
/** * Copyright 2016 JustWayward Team * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.justwayward.reader.base; import android.content.Context; import android.support.v4.content.ContextCompat; import android.support.v7.widget.LinearLayoutManager; import android.view.View; import com.justwayward.reader.R; import com.justwayward.reader.utils.NetworkUtils; import com.justwayward.reader.view.recyclerview.EasyRecyclerView; import com.justwayward.reader.view.recyclerview.adapter.OnLoadMoreListener; import com.justwayward.reader.view.recyclerview.adapter.RecyclerArrayAdapter; import com.justwayward.reader.view.recyclerview.swipe.OnRefreshListener; import java.lang.reflect.Constructor; import butterknife.Bind; /** * @author yuyh. * @date 16/9/3. */ public abstract class BaseRVActivity<T> extends BaseActivity implements OnLoadMoreListener, OnRefreshListener, RecyclerArrayAdapter.OnItemClickListener { @Bind(R.id.recyclerview) protected EasyRecyclerView mRecyclerView; protected RecyclerArrayAdapter<T> mAdapter; protected int start = 0; protected int limit = 20; protected void initAdapter(boolean refreshable, boolean loadmoreable) { if (mAdapter != null) { mAdapter.setOnItemClickListener(this); mAdapter.setError(R.layout.common_error_view).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAdapter.resumeMore(); } }); if (loadmoreable) { mAdapter.setMore(R.layout.common_more_view, this); mAdapter.setNoMore(R.layout.common_nomore_view); } if (refreshable && mRecyclerView != null) { mRecyclerView.setRefreshListener(this); } } if (mRecyclerView != null) { mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setItemDecoration(ContextCompat.getColor(this, R.color.common_divider_narrow), 1, 0, 0); mRecyclerView.setAdapterWithProgress(mAdapter); } } protected void initAdapter(Class<? extends RecyclerArrayAdapter<T>> clazz, boolean refreshable, boolean loadmoreable) { mAdapter = (RecyclerArrayAdapter) createInstance(clazz); initAdapter(refreshable, loadmoreable); } public Object createInstance(Class<?> cls) { Object obj; try { Constructor c1 = cls.getDeclaredConstructor(Context.class); c1.setAccessible(true); obj = c1.newInstance(mContext); } catch (Exception e) { obj = null; } return obj; } @Override public void onLoadMore() { if (!NetworkUtils.isConnected(getApplicationContext())) { mAdapter.pauseMore(); return; } } @Override public void onRefresh() { start = 0; if (!NetworkUtils.isConnected(getApplicationContext())) { mAdapter.pauseMore(); return; } } protected void loaddingError() { mAdapter.clear(); mAdapter.pauseMore(); mRecyclerView.setRefreshing(false); } }