feat:add slider control events

This commit is contained in:
pengfei.zhou 2019-12-06 15:46:23 +08:00
parent c53e223a38
commit c389174ec5

View File

@ -15,8 +15,10 @@
*/ */
package pub.doric.shader.slider; package pub.doric.shader.slider;
import android.text.TextUtils;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper; import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -25,7 +27,9 @@ import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.extension.bridge.DoricPromise;
import pub.doric.shader.SuperNode; import pub.doric.shader.SuperNode;
import pub.doric.shader.ViewNode; import pub.doric.shader.ViewNode;
@ -37,6 +41,7 @@ import pub.doric.shader.ViewNode;
@DoricPlugin(name = "Slider") @DoricPlugin(name = "Slider")
public class SliderNode extends SuperNode<RecyclerView> { public class SliderNode extends SuperNode<RecyclerView> {
private final SlideAdapter slideAdapter; private final SlideAdapter slideAdapter;
private String onPageSlidedFuncId;
public SliderNode(DoricContext doricContext) { public SliderNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
@ -47,12 +52,28 @@ public class SliderNode extends SuperNode<RecyclerView> {
protected RecyclerView build() { protected RecyclerView build() {
RecyclerView recyclerView = new RecyclerView(getContext()); RecyclerView recyclerView = new RecyclerView(getContext());
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager); recyclerView.setLayoutManager(layoutManager);
PagerSnapHelper mPagerSnapHelper = new PagerSnapHelper(); final PagerSnapHelper snapHelper = new PagerSnapHelper();
mPagerSnapHelper.attachToRecyclerView(recyclerView); snapHelper.attachToRecyclerView(recyclerView);
recyclerView.setAdapter(this.slideAdapter); recyclerView.setAdapter(this.slideAdapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
View view = snapHelper.findSnapView(layoutManager);
if (view != null && !TextUtils.isEmpty(onPageSlidedFuncId)) {
callJSResponse(onPageSlidedFuncId, layoutManager.getPosition(view));
}
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
return recyclerView; return recyclerView;
} }
@ -109,7 +130,7 @@ public class SliderNode extends SuperNode<RecyclerView> {
case "itemCount": case "itemCount":
this.slideAdapter.itemCount = prop.asNumber().toInt(); this.slideAdapter.itemCount = prop.asNumber().toInt();
break; break;
case "renderItem": case "renderPage":
// If reset renderItem,should reset native cache. // If reset renderItem,should reset native cache.
this.slideAdapter.itemValues.clear(); this.slideAdapter.itemValues.clear();
clearSubModel(); clearSubModel();
@ -117,9 +138,30 @@ public class SliderNode extends SuperNode<RecyclerView> {
case "batchCount": case "batchCount":
this.slideAdapter.batchCount = prop.asNumber().toInt(); this.slideAdapter.batchCount = prop.asNumber().toInt();
break; break;
case "onPageSlided":
this.onPageSlidedFuncId = prop.asString().toString();
break;
default: default:
super.blend(view, name, prop); super.blend(view, name, prop);
break; break;
} }
} }
@DoricMethod
public void slidePage(JSObject params, DoricPromise promise) {
int page = params.getProperty("page").asNumber().toInt();
boolean smooth = params.getProperty("smooth").asBoolean().value();
if (smooth) {
mView.smoothScrollToPosition(page);
} else {
mView.scrollToPosition(page);
}
promise.resolve();
}
@DoricMethod
public int getSlidedPage() {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mView.getLayoutManager();
return linearLayoutManager != null ? linearLayoutManager.findFirstVisibleItemPosition() : 0;
}
} }