Skip to content

Commit

Permalink
v2.5.2发布,增加RecyclerView组件化
Browse files Browse the repository at this point in the history
  • Loading branch information
crazysunj committed Nov 2, 2019
1 parent 2a03256 commit c7e31e2
Show file tree
Hide file tree
Showing 36 changed files with 1,204 additions and 9 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation project(libs.MTRVA_File)
// implementation libs.MTRVA_Anno
implementation project(libs.component_File)
// implementation libs.MTRVA
// annotationProcessor libs.MTRVA_Compiler
// implementation libs.component
implementation libs.appcompat
implementation libs.constraintlayout
implementation libs.itemDecoration
implementation libs.shimmer
implementation libs.recyclerview
implementation libs.rxandroid
implementation libs.rxjava
implementation libs.smartRefreshLayout
testImplementation libs.junit
// implementation libs.diffutil
}
8 changes: 5 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.crazysunj.multityperecyclerviewadapter">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LinearActivity" />
<activity android:name=".component.ComponentActivity" />
<activity android:name=".GridActivity" />
<activity android:name=".RxLinearActivity" />
<activity android:name=".RxErrorAndEmptyLinearActivity" />
Expand All @@ -29,7 +31,7 @@
android:windowSoftInputMode="adjustPan" />
<activity android:name=".switchtype.SwitchTypeActivity" />
<activity android:name=".TestLevelActivity" />
<activity android:name=".Refresh240Activity"></activity>
<activity android:name=".Refresh240Activity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import androidx.appcompat.app.AppCompatActivity;

import com.crazysunj.multityperecyclerviewadapter.component.ComponentActivity;
import com.crazysunj.multityperecyclerviewadapter.switchtype.SwitchTypeActivity;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -60,4 +61,8 @@ public void click17(View view) {
public void click18(View view) {
startActivity(new Intent(this, Refresh240Activity.class));
}

public void click19(View view) {
startActivity(new Intent(this, ComponentActivity.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.crazysunj.multityperecyclerviewadapter.component;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.crazysunj.multitypeadapter.entity.MultiTypeEntity;
import com.crazysunj.multitypeadapter.helper.RecyclerViewAdapterHelper;
import com.crazysunj.recycler.CrazyAdapter;
import com.crazysunj.recycler.CrazyViewHolder;

/**
* @author sunjian
* @date 2019-10-18 20:02
*/
public class BaseAdapter<T extends MultiTypeEntity> extends CrazyAdapter<T> implements ComponentSubject {

private OnEventCallback mEventCallback;
private boolean isVisible;
private LoadMoreHelper mLoadMoreHelper;

public BaseAdapter(@NonNull RecyclerViewAdapterHelper<T> helper) {
super(helper);
}

@Override
protected void init() {
super.init();
// MTRVA会接管资源,如果是标准模式(采用level的)想绕过,只能写一个空注册,否则可能会有异常
// mHelper.registerModule(Integer.MIN_VALUE).register();
mLoadMoreHelper = new LoadMoreHelper();
register(LoadMoreViewHolder.ITEM_TYPE_LOAD_MORE, LoadMoreViewHolder.class);
}

@Override
public int getItemViewType(int position) {
if (position == mData.size()) {
return LoadMoreViewHolder.ITEM_TYPE_LOAD_MORE;
}
return super.getItemViewType(position);
}

@Override
public void onBindViewHolder(@NonNull CrazyViewHolder holder, int position) {
if (position == mData.size()) {
return;
}
super.onBindViewHolder(holder, position);
}

@Override
public int getItemCount() {
return super.getItemCount() + 1;
}

@Override
protected void onViewHolderCreated(@NonNull CrazyViewHolder holder) {
super.onViewHolderCreated(holder);
if (holder instanceof BaseViewHolder) {
((BaseViewHolder) holder).subscribe(this);
}
}

/**
* 在主容器可见的时候调用
*/
public void onVisible() {
isVisible = true;
}

/**
* 在主容器不可见的时候调用
*/
public void onInVisible() {
isVisible = false;
}

@Override
public int loadMoreState() {
return mLoadMoreHelper.loadMoreState();
}

@Override
public boolean isVisible() {
return isVisible;
}

public LoadMoreHelper getLoadMoreHelper() {
return mLoadMoreHelper;
}

/**
* 主容器注册子组件的事件,自己需要单独处理的直接在notifyEvent方法中使用
*
* @param callback 事件回调
*/
public void registerEvent(@NonNull OnEventCallback callback) {
mEventCallback = callback;
}

@Override
public void notifyEvent(@NonNull Object event, @Nullable IEvent iEvent) {
if (mEventCallback != null) {
mEventCallback.onEvent(event, iEvent);
}
if (event instanceof LoadMore && iEvent instanceof LoadMoreEvent) {
mLoadMoreHelper.handleLoadMoreEvent((LoadMore) event, (LoadMoreEvent) iEvent);
}
}

public void setOnLoadMoreRequestListener(LoadMoreHelper.OnLoadMoreRequestListener listener) {
mLoadMoreHelper.setOnLoadMoreRequestListener(listener);
}

public interface OnEventCallback {
void onEvent(@NonNull Object event, @Nullable IEvent iEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.crazysunj.multityperecyclerviewadapter.component;

import android.view.View;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;

import com.crazysunj.multitypeadapter.entity.MultiTypeEntity;
import com.crazysunj.recycler.CrazyViewHolder;

/**
* @author sunjian
* @date 2019-10-18 20:03
*/
public class BaseViewHolder<T extends MultiTypeEntity> extends CrazyViewHolder<T> {

protected ComponentSubject mSubject;

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

@CallSuper
protected void subscribe(@NonNull ComponentSubject subject) {
mSubject = subject;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.crazysunj.multityperecyclerviewadapter.component;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.crazysunj.multitypeadapter.entity.MultiTypeEntity;
import com.crazysunj.multityperecyclerviewadapter.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ComponentActivity extends AppCompatActivity {

private MainAdapter mainAdapter;
private SmartRefreshLayout mRefreshLayout;
private Random random = new Random();
private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_component);
mRefreshLayout = findViewById(R.id.refreshLayout);
mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull final RefreshLayout refreshlayout) {
mRefreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
mainAdapter.refreshDiff(getNewData());
mRefreshLayout.finishRefreshWithNoMoreData();
mainAdapter.getLoadMoreHelper().loadMoreDefault();
}
}, 2000);
}
});

recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mainAdapter = new MainAdapter();
mainAdapter.setOnLoadMoreRequestListener(new LoadMoreHelper.OnLoadMoreRequestListener() {
@Override
public void onLoadMore() {
Log.d("ComponentActivity", "onLoadMore");
// mRefreshLayout.postDelayed(new Runnable() {
// @Override
// public void run() {
// mainAdapter.loadMoreGone();
// }
// }, 1000);
// mRefreshLayout.postDelayed(new Runnable() {
// @Override
// public void run() {
// mainAdapter.loadMoreFailed();
// }
// }, 3000);
mRefreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
if (mainAdapter.getData().size() > 40) {
mainAdapter.getLoadMoreHelper().loadMoreEnd();
} else {
mainAdapter.getLoadMoreHelper().loadMoreCompleted();
}
mainAdapter.loadMore(getNewData());
}
}, 5000);
// mRefreshLayout.postDelayed(new Runnable() {
// @Override
// public void run() {
// mainAdapter.loadMoreEnd();
// }
// }, 7000);
// mRefreshLayout.postDelayed(new Runnable() {
// @Override
// public void run() {
// mainAdapter.loadMoreDefault();
// }
// }, 9000);
}
});
recyclerView.setAdapter(mainAdapter);
mRefreshLayout.finishLoadMoreWithNoMoreData();
mainAdapter.refresh(getNewData());
}

private List<MultiTypeEntity> getNewData() {
List<MultiTypeEntity> data = new ArrayList<>();
for (int i = 0; i < 30; i++) {
data.add(getItem());
}
return data;
}

private MultiTypeEntity getItem() {
int type = random.nextInt(3);
if (type == 1) {
return new TypeTwo();
}
if (type == 2) {
return new TypeThree();
}
return new TypeOne();
}

@Override
protected void onResume() {
super.onResume();
mainAdapter.onVisible();
}

@Override
protected void onPause() {
super.onPause();
mainAdapter.onInVisible();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.crazysunj.multityperecyclerviewadapter.component;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
* 主容器主题,子组件和主容器之间交互桥梁
*
* @author sunjian
* @date 2019-10-18 20:07
*/
public interface ComponentSubject {

/**
* @return 主容器上拉加载状态
*/
int loadMoreState();

/**
* @return 当前主容器是否可见
*/
boolean isVisible();

/**
* 事件交互,通过callback可达到循环依赖
*
* @param event 事件对象
* @param callback 事件回调
*/
void notifyEvent(@NonNull Object event, @Nullable IEvent callback);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.crazysunj.multityperecyclerviewadapter.component;

/**
* @author sunjian
* @date 2019-10-18 20:37
*/
public interface IEvent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.crazysunj.multityperecyclerviewadapter.component;

/**
* @author sunjian
* @date 2019-10-21 20:08
*/
class LoadMore {
static int STATE_INIT = 0x100;
static int STATE_LOADING = 0x101;
int state;
}
Loading

0 comments on commit c7e31e2

Please sign in to comment.