下面列出了怎么用android.support.v4.view.NestedScrollingChild的API类实例代码及写法,或者点击链接到github查看源代码。
private void findScrollableView(View content, RefreshKernel kernel) {
mScrollableView = findScrollableViewInternal(content, true);
try {
if (mScrollableView instanceof CoordinatorLayout) {
kernel.getRefreshLayout().setNestedScrollingEnabled(false);
wrapperCoordinatorLayout(((CoordinatorLayout) mScrollableView), kernel.getRefreshLayout());
}
} catch (Throwable e) {//try 不能删除
}
if (mScrollableView instanceof NestedScrollingParent
&& !(mScrollableView instanceof NestedScrollingChild)) {
mScrollableView = findScrollableViewInternal(mScrollableView, false);
}
if (mScrollableView instanceof ViewPager) {
wrapperViewPager((ViewPager) this.mScrollableView);
}
if (mScrollableView == null) {
mScrollableView = content;
}
}
private View findScrollableViewInternal(View content, boolean selfable) {
View scrollableView = null;
Queue<View> views = new LinkedBlockingQueue<>(Collections.singletonList(content));
while (!views.isEmpty() && scrollableView == null) {
View view = views.poll();
if (view != null) {
if ((selfable || view != content) && (view instanceof AbsListView
|| view instanceof ScrollView
|| view instanceof ScrollingView
|| view instanceof NestedScrollingChild
|| view instanceof NestedScrollingParent
|| view instanceof WebView
|| view instanceof ViewPager)) {
scrollableView = view;
} else if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int j = 0; j < group.getChildCount(); j++) {
views.add(group.getChildAt(j));
}
}
}
}
return scrollableView;
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
if (object instanceof View) {
mScrollableView = ((View) object);
} else if (object instanceof Fragment) {
mScrollableView = ((Fragment) object).getView();
}
if (mScrollableView != null) {
mScrollableView = findScrollableViewInternal(mScrollableView, true);
if (mScrollableView instanceof NestedScrollingParent
&& !(mScrollableView instanceof NestedScrollingChild)) {
mScrollableView = findScrollableViewInternal(mScrollableView, false);
}
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (getChildCount() > 1) {
throw new IllegalStateException("QRefreshLayout can only have one child");
}
mTarget = getChildAt(0);
mTarget.setClickable(true);
boolean targetNestedScrollingEnabled = false;
if (mTarget instanceof NestedScrollingChild) {
targetNestedScrollingEnabled = ((NestedScrollingChild) mTarget).isNestedScrollingEnabled();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
targetNestedScrollingEnabled = mTarget.isNestedScrollingEnabled();
}
setNestedScrollingEnabled(targetNestedScrollingEnabled);
ViewGroup.LayoutParams params = mTarget.getLayoutParams();
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
mTarget.setLayoutParams(params);
if (mHeaderView == null) {
setHeaderView(new HeaderView(getContext()));
}
if (mFooterView == null) {
setFooterView(new FooterView(getContext()));
}
}
private View findScrollingChild(View view) {
if (view instanceof NestedScrollingChild) {
return view;
}
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0, count = group.getChildCount(); i < count; i++) {
View scrollingChild = findScrollingChild(group.getChildAt(i));
if (scrollingChild != null) {
return scrollingChild;
}
}
}
return null;
}
private View findScrollingChild(View view) {
if (view instanceof NestedScrollingChild) {
return view;
}
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0, count = group.getChildCount(); i < count; i++) {
View scrollingChild = findScrollingChild(group.getChildAt(i));
if (scrollingChild != null) {
return scrollingChild;
}
}
}
return null;
}
private View findScrollingChild(View view) {
if (view instanceof NestedScrollingChild) {
return view;
}
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0, count = group.getChildCount(); i < count; i++) {
View scrollingChild = findScrollingChild(group.getChildAt(i));
if (scrollingChild != null) {
return scrollingChild;
}
}
}
return null;
}
private View findScrollingChild(View view) {
if (view instanceof NestedScrollingChild) {
return view;
}
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0, count = group.getChildCount(); i < count; i++) {
View scrollingChild = findScrollingChild(group.getChildAt(i));
if (scrollingChild != null) {
return scrollingChild;
}
}
}
return null;
}
private View findScrollingChild(View view) {
if (view instanceof NestedScrollingChild) {
return view;
}
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0, count = group.getChildCount(); i < count; i++) {
View scrollingChild = findScrollingChild(group.getChildAt(i));
if (scrollingChild != null) {
return scrollingChild;
}
}
}
return null;
}
/**
* 通过id得到相应的view
*/
@Override
protected void onFinishInflate()
{
final int childCount = getChildCount();
if (childCount > 0)
{
mRefreshView = findViewById(com.lib.ptrview.R.id.common_ptrview_refresh_view);
mContentView = findViewById(com.lib.ptrview.R.id.common_ptrview_content);
mLoadMoreView = findViewById(com.lib.ptrview.R.id.common_ptrview_loadmore_view);
mScrollContentView = findViewById(com.lib.ptrview.R.id.common_ptrview_content_scroll);
}
if (mContentView == null)
{
throw new IllegalStateException("CommonPtrLayout: mContentView is null");
}
if (mIsCoo)
{
if (mContentView instanceof CoordinatorLayout)
{
CoordinatorLayout coo = (CoordinatorLayout) mContentView;
mAppBar = (AppBarLayout) coo.getChildAt(0);
setAppBarListener();
} else
{
throw new IllegalStateException("CommonPtrLayout: mContentView is not CoordinatorLayout");
}
if (mScrollContentView == null)
{
throw new IllegalStateException("CommonPtrLayout: mScrollContentView is null");
}
if (!(mScrollContentView instanceof NestedScrollingChild))
{
throw new IllegalStateException("CommonPtrLayout: mScrollContentView is not NestedScrollingChild");
}
}
if (mRefreshEnabled)
{
if (mRefreshView == null)
throw new IllegalStateException("CommonPrtLayout : You must set a refreshView when the refreshEnabled is true.");
if (!(mRefreshView instanceof CommonPtrViewImpl))
throw new IllegalStateException("CommonPrtLayout : The refreshView must instanceof CommonPtrViewImpl.");
mRefreshView.setBackgroundResource(mRefreshBgResId);
getRefreshView().setIsPullDownMode(true);
}
if (mLoadMoreEnabled)
{
if (mLoadMoreView == null)
throw new IllegalStateException("CommonPrtLayout : You must set a loadMoreView when the refreshEnabled is true.");
if (!(mLoadMoreView instanceof CommonPtrViewImpl))
throw new IllegalStateException("CommonPrtLayout : The loadMoreView must instanceof CommonPtrViewImpl.");
mLoadMoreView.setBackgroundResource(mLoadMoreBgResId);
getLoadMoreView().setIsPullDownMode(false);
}
super.onFinishInflate();
for (int i = 0; i < childCount; i++)
{
View v = getChildAt(i);
if (v != mContentView)
bringChildToFront(v);
}
}
/**
* 通过id得到相应的view
*/
@Override
protected void onFinishInflate() {
final int childCount = getChildCount();
if (childCount > 0) {
mHeaderView = findViewById(com.canyinghao.canrefresh.R.id.can_refresh_header);
mContentView = findViewById(com.canyinghao.canrefresh.R.id.can_content_view);
mFooterView = findViewById(com.canyinghao.canrefresh.R.id.can_refresh_footer);
mScrollView = findViewById(com.canyinghao.canrefresh.R.id.can_scroll_view);
}
if (mContentView == null) {
throw new IllegalStateException("mContentView is null");
}
if (mIsCoo) {
if (mContentView instanceof CoordinatorLayout) {
CoordinatorLayout coo = (CoordinatorLayout) mContentView;
mAppBar = (AppBarLayout) coo.getChildAt(0);
setAppBarListener();
} else {
throw new IllegalStateException("mContentView is not CoordinatorLayout");
}
if (mScrollView == null) {
throw new IllegalStateException("mScrollView is null");
}
if (mScrollView instanceof ViewPager) {
mViewPager = (ViewPager) mScrollView;
mIsViewPager = true;
} else if (mScrollView instanceof NestedScrollingChild) {
mIsViewPager = false;
} else {
throw new IllegalStateException("mScrollView is not NestedScrollingChild or ViewPager");
}
}
if (mHeaderView != null && !(mHeaderView instanceof CanRefresh)) {
throw new IllegalStateException("mHeaderView error");
}
if (mFooterView != null && !(mFooterView instanceof CanRefresh)) {
throw new IllegalStateException("mFooterView error");
}
if (mHeaderView != null) {
getHeaderInterface().setIsHeaderOrFooter(true);
}
if (mFooterView != null) {
getFooterInterface().setIsHeaderOrFooter(false);
}
super.onFinishInflate();
setStyle(mHeadStyle, mFootStyle);
}