package com.mypackagename.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.mypackagename.Activity.BaseActivity;
import com.mypackagename.R;
import com.mypackagename.Utils.AppTools;
import com.mypackagename.Utils.TextStringUtils;
import com.mypackagename.Utils.UtilsTools;
import com.mypackagename.View.FullyLinearLayoutManager;
import com.mypackagename.View.MyScrollView;
import com.mypackagename.adapter.RentHouseListAdapter;
import com.mypackagename.bean._FilterHouseItem;
import com.mypackagename.bean._Return;
import com.mypackagename.constant.SysConfig;
import com.mypackagename.volley.VolleyClient;
import com.mypackagename.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;
private 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;
private ArrayList<_FilterHouseItem> mDataList;
private boolean isLoading = false; // 是否正在加载
private boolean loadMore = true; // 是否有更多数据需要加载
private int mPage = 1;
// private LinearLayoutManager layoutManager = new LinearLayoutManager(this);
// private FullyLinearLayoutManager layoutManager = new FullyLinearLayoutManager(this);
@Override
protected void initActivity() {
setContentView(R.layout.activity_recommend_house);
mContext = this;
requestQueue = Volley.newRequestQueue(mContext);
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 map = new TreeMap();
map.put("page", mPage + "");
VolleyClient.postRequestQueue(requestQueue, "xxxx", SysConfig.xxx, SysConfig.xxx, 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--;
UtilsTools.Log_e(TAG, "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); // 1租2买
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_list_more = (TextView) findViewById(R.id.tv_list_more);
mRecycleView = (RecyclerView) findViewById(R.id.rv_recommend);
// mRecycleView.addOnScrollListener(new setScrollListener());
// mRecycleView.setNestedScrollingEnabled(false);
// mRecycleView.setLayoutManager(layoutManager); // 新改
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;
}
// int lastVisibleItemPosition = fullyLinearLayoutManager.findLastVisibleItemPosition();
// if (lastVisibleItemPosition + 1 == mDataList.size()) {
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();
}
@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);
// location==6时不显示"更多"按钮.
if (show && !location.equals("6")) {
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.mypackagename.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.mypackagename.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 {
}
}
}
- - - - - - - - - - - - - - - - - - - - - - -
布局 :