您的位置:首页 > 其它

SwipeRefreshLayout、ScrollView、RecyclerView嵌套使用

2017-05-04 19:02 274 查看
package com.yiban.caesar.Activity.Main;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.yiban.caesar.Activity.BaseActivity;
import com.yiban.caesar.R;
import com.yiban.caesar.Utils.AppTools;
import com.yiban.caesar.Utils.TextStringUtils;
import com.yiban.caesar.Utils.UtilsTools;
import com.yiban.caesar.View.FullyLinearLayoutManager;
import com.yiban.caesar.View.MyScrollView;
import com.yiban.caesar.adapter.RentHouseListAdapter;
import com.yiban.caesar.bean._FilterHouseItem;
import com.yiban.caesar.bean._Return;
import com.yiban.caesar.constant.SysConfig;
import com.yiban.caesar.volley.VolleyClient;
import com.yiban.caesar.volley.VolleyRequest;

import java.util.ArrayList;
import java.util.TreeMap;

public class RecommendActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, RentHouseListAdapter.OnCustomItemClickListener {

private String TAG = "RecommendActivity";
private Context mContext;

p
4000
rivate ImageView iv_title_bar_left;
private TextView tv_title_bar_title;
private TextView tv_list_more;
private SwipeRefreshLayout mSwipeRefreshLayout;
private RecyclerView mRecycleView;
private MyScrollView scrollView;//含有头的头布局和RecyclerView的

private FullyLinearLayoutManager fullyLinearLayoutManager;
private RentHouseListAdapter mAdapter;
private RequestQueue requestQueue;

private int type = 1; // 1租 2买 . 此页目前固定为租的
private ArrayList<_FilterHouseItem> mDataList;
private boolean isLoading = false; // 是否正在加载
private boolean loadMore = true; // 是否有更多数据需要加载
private int mPage = 1;
private String location;   
@Override
protected void initActivity() {
setContentView(R.layout.activity_recommend_house);

mContext = this;
requestQueue = Volley.newRequestQueue(mContext);
location = getIntent().getStringExtra("location");

initView();
initData();
}

private void initData() {
mPage = 1;
mSwipeRefreshLayout.setRefreshing(true);
API_House();
}

private void API_House() {
new Handler().postDelayed(new Runnable() {
public void run() {
TreeMap<String, String> map = new TreeMap<String, String>();
map.put("xxx", "xxxxxxxxxx");
VolleyClient.postRequestQueue(requestQueue, "AGENT_BUILDING_LIST", SysConfig.APIhost, SysConfig.xxxxx, map, volleyRequest);
}
}, 500);
}

VolleyRequest volleyRequest = new VolleyRequest() {
@Override
protected void onMyResponse(String response) {
UtilsTools.Log_e(TAG, "API_House --- response ->" + response);

mSwipeRefreshLayout.setRefreshing(false);
isLoading = false;

_Return Re = new _Return(response);

if (Re.isSuccess(mContext)) {
showMoreBtn(false);
loadMore = true;
setData(AppTools.getIndexArrayList(Re.getData()));

// 因为换了东西,所以现在如果一屏铺不满就会没法滑动.
// 刚打开时直接加载第1,2页就满了,这个判断解决这个bug
// 之后加载的页数还是1页1页的加载
if (mPage == 1) {
startLoadMore();
}
} else {
loadMore = false;
if (mPage > 1) {
mPage--;
} else {
mPage = 1;
}
showMoreBtn(true);
}
}

@Override
public void onMyErrorResponse(VolleyError volleyError) {
super.onMyErrorResponse(volleyError);
mSwipeRefreshLayout.setRefreshing(false);
isLoading = false;
mPage = mPage > 1 ? mPage-- : 1;
}
};

private void setData(ArrayList<_FilterHouseItem> dataList) {

if (mDataList == null) {
mDataList = new ArrayList<>();
}

if (mPage == 1) {
mDataList = dataList;
} else {
mDataList.addAll(dataList);
}

if (mAdapter == null) {
mAdapter = new RentHouseListAdapter(mContext, mDataList, type);
mAdapter.setOnCustomItemClickListener(this);
mRecycleView.setAdapter(mAdapter);
} else {
mAdapter.setDataList(mDataList);
}
}

private void initView() {
tv_title_bar_title = (TextView) findViewById(R.id.tv_title_bar_title);
tv_title_bar_title.setVisibility(View.VISIBLE);
iv_title_bar_left = (ImageView) findViewById(R.id.iv_title_bar_left);
iv_title_bar_left.setVisibility(View.VISIBLE);
iv_title_bar_left.setImageResource(R.mipmap.ic_back);
iv_title_bar_left.setOnClickListener(this);
tv_title_bar_title.setText(TextStringUtils.numToTitle(location));

tv_list_more = (TextView) findViewById(R.id.tv_list_more);
mRecycleView = (RecyclerView) findViewById(R.id.rv_recommend);

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_recommend);
mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorAccent), getResources().getColor(R.color.red));
mSwipeRefreshLayout.setOnRefreshListener(this);

scrollView = (MyScrollView) findViewById(R.id.sv_recommend);
scrollView.smoothScrollTo(0, 0);

initScrollViewAndRefreshLayout();
}

private void initScrollViewAndRefreshLayout() {
scrollView.setOnScrollToBottomLintener(new MyScrollView.OnScrollToBottomListener() {

@Override
public void onScrollBottomListener(boolean isBottom) {
/**这里遇到一个问题,当数据加载完成后,向上滑动ScrollView,还会提示一遍“没有更多数据了”,所以多加了一个向下滑动的标记isTop;如果是判断向下滑动,并且isBottom是滑动到了最低端才加载数据**/
if (isBottom && scrollView.isTop()) {
UtilsTools.Log_e(TAG, "isBottom && scrollView.isTop()");
//GetToast.showToast(ScrollingActivity.this,isBottom+"");
if (isLoading) {
UtilsTools.Log_e(TAG, "正在加载,等会儿======");
return;
}
UtilsTools.Log_e(TAG, "滑动更多======");
startLoadMore();
} else {
UtilsTools.Log_e(TAG, "onScrollBottomListener --- else");
//GetToast.showToast(ScrollingActivity.this,isBottom+"");
}
}
});

fullyLinearLayoutManager = new FullyLinearLayoutManager(this);
fullyLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
fullyLinearLayoutManager.setSmoothScrollbarEnabled(true);
mRecycleView.setLayoutManager(fullyLinearLayoutManager);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_title_bar_left:
finish();
break;
}
}

@Override
public void onRefresh() {
initData();
}

/**
* 房源列表的点击事件
*
* @param view
* @param position
*/
@Override
public void onItemClick(View view, int position) {
// 在adapter里加了点击事件
}

/**
* 加载更多
*/
private void startLoadMore() {
UtilsTools.Log_e(TAG, "加载更多======");
isLoading = true;
mPage += 1;
API_House();
}

private void showMoreBtn(Boolean show) {
UtilsTools.Log_e(TAG, "showMoreBtn --- " + show);
if (show) {
tv_list_more.setVisibility(View.VISIBLE);
tv_list_more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, RentActivity.class));
}
});
} else {
tv_list_more.setVisibility(View.GONE);
tv_list_more.setOnClickListener(null);
}
}
}


- - - - - - - - - - - - - - - - - - - - -
package com.yiban.caesar.View;

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

/**
* Created by LXL on 2017/05/03.
*/

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);
}
}

- - - - - - - - - - - - - - - - - - - - - -
package com.yiban.caesar.View;

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

/**
* Created by LXL on 2017/4/25.
*/

public class FullyLinearLayoutManager extends LinearLayoutManager {

private static final String TAG = FullyLinearLayoutManager.class.getSimpleName();

public FullyLinearLayoutManager(Context context) {
super(context);
}

public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, 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);

//        UtilsTools.Log_e(TAG, "onMeasure called. \nwidthMode " + widthMode
//                + " \nheightMode " + heightSpec
//                + " \nwidthSize " + widthSize
//                + " \nheightSize " + heightSize
//                + " \ngetItemCount() " + getItemCount());

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

if (getOrientation() == HORIZONTAL) {
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
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) {
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();
} finally {
}
}
}

- - - - - - - - - - - - - - - - - - - - - - -
布局 :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_white"
android:orientation="vertical"
tools:context=".Activity.Main.RecommendActivity">

<include
android:id="@+id/title_bar"
layout="@layout/layout_title_bar"/>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/line_gray"/>

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_recommend"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.yiban.caesar.View.MyScrollView
android:id="@+id/sv_recommend"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_recommend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:paddingLeft="4dp"
android:paddingRight="4dp"/>

<TextView
android:id="@+id/tv_list_more"
style="@style/text_30px_blue"
android:layout_width="match_parent"
android:layout_height="@dimen/px70"
android:layout_marginBottom="@dimen/px54"
android:layout_marginLeft="@dimen/px230"
android:layout_marginRight=
bad5
"@dimen/px230"
android:layout_marginTop="@dimen/px40"
android:background="@mipmap/bg_chengzhangzhi"
android:gravity="center"
android:text="更多盘源"
android:visibility="gone"/>
</LinearLayout>
</com.yiban.caesar.View.MyScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: