-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,204 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
app/src/main/java/com/crazysunj/multityperecyclerviewadapter/component/BaseAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/crazysunj/multityperecyclerviewadapter/component/BaseViewHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
...src/main/java/com/crazysunj/multityperecyclerviewadapter/component/ComponentActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/crazysunj/multityperecyclerviewadapter/component/ComponentSubject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/crazysunj/multityperecyclerviewadapter/component/IEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/crazysunj/multityperecyclerviewadapter/component/LoadMore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.