android :SwipyRefreshLayout嵌套Scr

2017-05-05  本文已影响0人  黄晓果

布局:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgcolor"
    android:orientation="vertical">

    <include layout="@layout/top_oper" />

    <com.xianghuocircle.widget.pulltorefresh.SwipyRefreshLayout

        xmlns:xtao="http://schemas.android.com/apk/res-auto"
        android:id="@+id/refresh_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgcolor"
        xtao:direction="both">

        <com.xianghuocircle.widget.MyScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/iv_photo"
                    android:layout_width="match_parent"
                    android:layout_height="180dp"
                    android:scaleType="centerCrop"
                    android:src="@mipmap/icon_chaozhi_guanggao" />

                <TextView
                    android:id="@+id/tv_baotuan_gifts"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="@dimen/padding_15"
                    android:text="@string/baotuan_gifts"
                    android:textColor="@color/black70"
                    android:textSize="@dimen/small_size"
                    android:visibility="gone" />

                <TextView
                    android:id="@+id/tv_baotuan_gifts_dec"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="@dimen/padding_5"
                    android:layout_marginTop="@dimen/padding_5"
                    android:text="@string/baotuan_gifts_dec"
                    android:textSize="@dimen/smallest_size"
                    android:visibility="gone" />

                <RelativeLayout
                    android:id="@+id/rl_brand_info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="@dimen/activity_horizontal_margin">

                    <ImageView
                        android:id="@+id/iv_brand_photo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/ic_launcher" />

                    <TextView
                        android:id="@+id/tv_brand_info"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignLeft="@id/iv_brand_photo"
                        android:layout_alignRight="@id/iv_brand_photo"
                        android:layout_below="@id/iv_brand_photo"
                        android:layout_marginTop="@dimen/padding_5"
                        android:gravity="center"
                        android:text="@string/app_name"
                        android:textColor="@color/black60"
                        android:textSize="@dimen/smallest_size" />

                    <TextView
                        android:id="@+id/tv_brand_info_dec"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@id/iv_brand_photo"
                        android:layout_toRightOf="@id/iv_brand_photo"
                        android:maxLength="3"
                        android:paddingLeft="@dimen/padding_20"
                        android:paddingTop="@dimen/padding_5"
                        android:text="@string/app_name"
                        android:textColor="@color/black70"
                        android:textSize="@dimen/smaller_size" />
                </RelativeLayout>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="@dimen/padding_5"
                    android:paddingRight="@dimen/padding_5" />
            </LinearLayout>
        </com.xianghuocircle.widget.MyScrollView>
    </com.xianghuocircle.widget.pulltorefresh.SwipyRefreshLayout>
</LinearLayout>

自定义ScrollView

package com.xianghuocircle.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

/**
 * 自定义ScrollView
 */

public class MyScrollView extends ScrollView {

    private int downX;
    private int downY;
    private int mTouchSlop;

    public boolean isTop() {
        return isTop;
    }

    public void setTop(boolean top) {
        isTop = top;
    }

    private boolean isTop = false;//是不是滑动到了最低端 ;使用这个方法,解决了上拉加载的问题
    private OnScrollToBottomListener onScrollToBottom;

    public MyScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,
                                  boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        if(scrollY != 0 && null != onScrollToBottom &&isTop()){
            onScrollToBottom.onScrollBottomListener(clampedY);
        }
    }

    public void setOnScrollToBottomLintener(OnScrollToBottomListener listener){
        onScrollToBottom = listener;
    }

    public interface OnScrollToBottomListener{
        public void onScrollBottomListener(boolean isBottom);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                setTop(false);
                downX = (int) e.getRawX();
                downY = (int) e.getRawY();
                Log.i("-----::----downY-----::",downY+"");
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) e.getRawY();
                Log.i("-----::----moveY-----::",moveY+"");
                /****判断是向下滑动,才设置为true****/
                if(downY-moveY>0){
                    setTop(true);
                }else{
                    setTop(false);
                }
                if (Math.abs(moveY - downY) > mTouchSlop) {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(e);
    }
}

FullyGridLayoutManager

package com.xianghuocircle.manager;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

public class FullyGridLayoutManager extends GridLayoutManager {
    public FullyGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }

    public FullyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
        super(context, spanCount, orientation, reverseLayout);
    }

    private int[] mMeasuredDimension = new int[2];

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
        final int widthMode = View.MeasureSpec.getMode(widthSpec);
        final int heightMode = View.MeasureSpec.getMode(heightSpec);
        final int widthSize = View.MeasureSpec.getSize(widthSpec);
        final int heightSize = View.MeasureSpec.getSize(heightSpec);

        int width = 0;
        int height = 0;
        int count = getItemCount();
        int span = getSpanCount();
        for (int i = 0; i < count; i++) {
            measureScrapChild(recycler, i,
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    mMeasuredDimension);

            if (getOrientation() == HORIZONTAL) {
                if (i % span == 0) {
                    width = width + mMeasuredDimension[0];
                }
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                if (i % span == 0) {
                    height = height + mMeasuredDimension[1];
                }
                if (i == 0) {
                    width = mMeasuredDimension[0];
                }
            }
        }

        switch (widthMode) {
            case View.MeasureSpec.EXACTLY:
                width = widthSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        switch (heightMode) {
            case View.MeasureSpec.EXACTLY:
                height = heightSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        setMeasuredDimension(width, height);
    }

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec, int[] measuredDimension) {
        if (position < getItemCount()) {
            try {
                View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
                if (view != null) {
                    RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
                    int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                            getPaddingLeft() + getPaddingRight(), p.width);
                    int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                            getPaddingTop() + getPaddingBottom(), p.height);
                    view.measure(childWidthSpec, childHeightSpec);
                    measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
                    measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
                    recycler.recycleView(view);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

页面

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xianghuocircle.R;
import com.xianghuocircle.adapter.SpellGroupZoneNewAdapter;
import com.xianghuocircle.adapter.fresh.GiffZoneAdapter;
import com.xianghuocircle.manager.FullyGridLayoutManager;
import com.xianghuocircle.ui.activity.BaseActivity;
import com.xianghuocircle.utils.UIUtils;
import com.xianghuocircle.widget.MyScrollView;
import com.xianghuocircle.widget.pulltorefresh.SwipyRefreshLayout;

import butterknife.Bind;

/**
 * 送礼专区
 */
public class GiffZoneActivity extends BaseActivity implements GiffZoneAdapter.OnItemClickListener {

    /**
     * 商品图
     */
    @Bind(R.id.iv_photo)
    ImageView ivPhoto;
    /**
     * 包团送礼
     */
    @Bind(R.id.tv_baotuan_gifts)
    TextView tvBaotuanGifts;
    @Bind(R.id.tv_baotuan_gifts_dec)
    TextView tvBaotuanGiftsDec;
    /**
     * 商品信息
     */
    @Bind(R.id.iv_brand_photo)
    ImageView ivBrandPhoto;
    @Bind(R.id.tv_brand_info)
    TextView tvBrandInfo;
    @Bind(R.id.tv_brand_info_dec)
    TextView tvBrandInfoDec;
    @Bind(R.id.rl_brand_info)
    RelativeLayout rlBrandInfo;
    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.refresh_view)
    SwipyRefreshLayout refreshView;
    @Bind(R.id.scrollview)
    MyScrollView scrollview;

    private FullyGridLayoutManager manager;
    private GiffZoneAdapter mAdapter;

    @Override
    protected int initContentView() {
        return R.layout.activity_giff_zone;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        manager = new FullyGridLayoutManager(mContext, 2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        manager.setSmoothScrollbarEnabled(true);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new GiffZoneAdapter(mContext);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setFocusable(false);
    }

    @Override
    protected void initView() {
        super.initView();
        onShowTopBack(true);
        setTitleText(getString(R.string.send_gifts));
        onMessage(false);
        onShowRightShare(true);
        tvBaotuanGifts.setVisibility(View.VISIBLE);
        tvBaotuanGiftsDec.setVisibility(View.VISIBLE);
        rlBrandInfo.setVisibility(View.GONE);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mAdapter.setOnItemClickListener(this);
    }


    @Override
    public void OnItemClick(int position) {
        UIUtils.showToast("position==>"+position);
//        startActivity(new Intent(mContext, ShopDetailNewActivity.class).putExtra("bottomType", 2));
    }
}

解决.........................

上一篇下一篇

猜你喜欢

热点阅读