@@ -1,15 +1,21 @@
package com.sl.house_property.discovery ;
import android.content.Context ;
import android.content.Intent ;
import android.graphics.Bitmap ;
import android.graphics.Rect ;
import android.graphics.drawable.BitmapDrawable ;
import android.graphics.drawable.Drawable ;
import android.os.Bundle ;
import android.support.annotation.NonNull ;
import android.support.annotation.Nullable ;
import android.support.v7.widget.LinearLayoutManager ;
import android.text.TextUtils ;
import android.util.Log ;
import android.view.LayoutInflater ;
import android.view.MotionEvent ;
import android.view.View ;
import android.view.ViewTreeObserver ;
import android.widget.Toast ;
import com.bumptech.glide.Glide ;
@@ -17,19 +23,23 @@ import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson ;
import com.google.gson.internal.LinkedTreeMap ;
import com.google.gson.reflect.TypeToken ;
import com.gyf.immersionbar.ImmersionBar ;
import com.lxj.xpopup.XPopup ;
import com.lzy.ninegrid.ImageInfo ;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter ;
import com.scwang.smartrefresh.layout.api.RefreshLayout ;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener ;
import com.sl.house_property.BaseFragment ;
import com.sl.house_property.MainTabActivity ;
import com.sl.house_property.R ;
import com.sl.house_property.databinding.FragmentHistoryRecordBinding ;
import com.sl.house_property.databinding.ItemCommentBinding ;
import com.sl.house_property.databinding.ItemDiscoveryBinding ;
import com.sl.house_property.order.SettleActivity ;
import com.squareup.picasso.Picasso ;
import com.squareup.picasso.Target ;
import org.json.JSONException ;
import org.json.JSONObject ;
import java.lang.reflect.Type ;
@@ -39,6 +49,7 @@ import java.util.Map;
import adapter.BaseRecycleViewAdapter ;
import entity.DiscoveryListEntity ;
import entity.GoodsDetailEntity ;
import entity.RegisterUser ;
import http.ApiConfig ;
import my_loader.Loader ;
@@ -49,10 +60,13 @@ import rx.Subscription;
import rx.functions.Action1 ;
import tools.Config ;
import tools.PicassoRoundTransform ;
import utils.CommonUtils ;
import utils.DateUtils ;
import utils.KeyboardUtil ;
import utils.Md5 ;
import utils.UtilHelpers ;
public class HistoryRecordFragment extends BaseFragment < FragmentHistoryRecordBinding > {
public class HistoryRecordFragment extends BaseFragment < FragmentHistoryRecordBinding > implements MainTabActivity . MyOnTouchListener {
private Context context ;
private int type = 2 ;
private int page = 1 ;
@@ -79,6 +93,19 @@ public class HistoryRecordFragment extends BaseFragment<FragmentHistoryRecordBin
super . onCreate ( savedInstanceState ) ;
type = getArguments ( ) . getInt ( " param1 " ) ;
userId = getArguments ( ) . getString ( " param2 " ) ;
}
@Override
protected void onCreateVew ( LayoutInflater inflater , Bundle savedInstanceState ) {
super . onCreateVew ( inflater , savedInstanceState ) ;
if ( getActivity ( ) instanceof MainTabActivity ) {
final MainTabActivity activity = ( MainTabActivity ) getActivity ( ) ;
activity . registerMyOnTouchListener ( this ) ;
} else if ( getActivity ( ) instanceof HistoryRecordActivity ) {
( ( HistoryRecordActivity ) getActivity ( ) ) . registerMyOnTouchListener ( this ) ;
}
}
@Override
@@ -87,6 +114,7 @@ public class HistoryRecordFragment extends BaseFragment<FragmentHistoryRecordBin
context = getContext ( ) ;
loader = new Loader ( ) ;
mDataBinding . dw . setOnRefreshLoadMoreListener ( new OnRefreshLoadMoreListener ( ) {
@Override
public void onLoadMore ( @NonNull RefreshLayout refreshLayout ) {
@@ -103,9 +131,92 @@ public class HistoryRecordFragment extends BaseFragment<FragmentHistoryRecordBin
mDataBinding . dw . autoRefresh ( ) ;
}
@Override
public boolean onTouch ( MotionEvent ev ) {
switch ( ev . getAction ( ) ) {
case MotionEvent . ACTION_DOWN :
//点击空白处隐藏键盘,隐藏输入框相关
if ( getActivity ( ) . getCurrentFocus ( ) ! = null & & getActivity ( ) . getCurrentFocus ( ) . getId ( ) = = R . id . tv_send_comment ) {
return false ;
}
boolean b = UtilHelpers . hideKeyboard ( ev , getActivity ( ) . getCurrentFocus ( ) , getActivity ( ) ) ;
if ( b ) {
CommonUtils . hideSoftInput ( mDataBinding . etComment . getContext ( ) , mDataBinding . etComment ) ;
if ( getActivity ( ) instanceof MainTabActivity ) {
MainTabActivity activity = ( MainTabActivity ) getActivity ( ) ;
activity . visibilyBottom ( View . VISIBLE ) ;
}
mDataBinding . llComment . setVisibility ( View . GONE ) ;
}
break ;
}
return false ;
}
private int currentKeyboardH ;
private int editTextBodyHeight ;
private void setViewTreeObserver ( ) {
final ViewTreeObserver swipeRefreshLayoutVTO = mDataBinding . rl . getViewTreeObserver ( ) ;
swipeRefreshLayoutVTO . addOnGlobalLayoutListener ( new ViewTreeObserver . OnGlobalLayoutListener ( ) {
@Override
public void onGlobalLayout ( ) {
Rect r = new Rect ( ) ;
mDataBinding . rl . getWindowVisibleDisplayFrame ( r ) ;
int statusBarH = ImmersionBar . getStatusBarHeight ( HistoryRecordFragment . this ) ; //状态栏高度
int screenH = mDataBinding . rl . getRootView ( ) . getHeight ( ) ;
if ( r . top ! = statusBarH ) {
//r.top代表的是状态栏高度, 在沉浸式状态栏时r.top= 0, 通过getStatusBarHeight获取状态栏高度
r . top = statusBarH ;
}
int keyboardH = screenH - ( r . bottom - r . top ) ;
if ( keyboardH = = currentKeyboardH ) { //有变化时才处理,否则会陷入死循环
return ;
}
currentKeyboardH = keyboardH ;
editTextBodyHeight = mDataBinding . llComment . getHeight ( ) ;
if ( keyboardH < 150 ) { //说明是隐藏键盘的情况
updateEditTextBodyVisible ( View . GONE ) ;
return ;
}
}
} ) ;
}
public void updateEditTextBodyVisible ( int visibility ) {
mDataBinding . llComment . setVisibility ( visibility ) ;
if ( View . VISIBLE = = visibility ) {
mDataBinding . llComment . requestFocus ( ) ;
//弹出键盘
CommonUtils . showSoftInput ( mDataBinding . etComment . getContext ( ) , mDataBinding . etComment ) ;
} else if ( View . GONE = = visibility ) {
//隐藏键盘
MainTabActivity activity = ( MainTabActivity ) getActivity ( ) ;
activity . visibilyBottom ( View . VISIBLE ) ;
CommonUtils . hideSoftInput ( mDataBinding . etComment . getContext ( ) , mDataBinding . etComment ) ;
}
}
private void intRecycleView ( ) {
mDataBinding . tvSendComment . setOnClickListener ( new View . OnClickListener ( ) {
@Override
public void onClick ( View v ) {
setViewTreeObserver ( ) ;
}
} ) ;
mDataBinding . sv . setOnTouchListener ( new View . OnTouchListener ( ) {
@Override
public boolean onTouch ( View v , MotionEvent event ) {
if ( mDataBinding . llComment . getVisibility ( ) = = View . VISIBLE ) {
updateEditTextBodyVisible ( View . GONE ) ;
return true ;
}
return false ;
}
} ) ;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager ( context ) ;
mDataBinding . recyView . setNestedScrollingEnabled ( false ) ; //禁止rcyc嵌套滑动
@@ -125,7 +236,23 @@ public class HistoryRecordFragment extends BaseFragment<FragmentHistoryRecordBin
homeGridAdapterItemBinding . tvNikeName . setText ( discoveryListEntity . getNickname ( ) ) ;
homeGridAdapterItemBinding . tvContent . setText ( discoveryListEntity . getGoods_detail ( ) ) ;
mDataBinding . tvSendComment . setOnClickListener ( new View . OnClickListener ( ) {
@Override
public void onClick ( View v ) {
if ( getActivity ( ) instanceof MainTabActivity ) {
MainTabActivity activity = ( MainTabActivity ) getActivity ( ) ;
CommonUtils . hideSoftInput ( getContext ( ) , mDataBinding . etComment ) ;
mDataBinding . llComment . setVisibility ( View . GONE ) ;
activity . visibilyBottom ( View . VISIBLE ) ;
comment ( discoveryListEntity . getGoods_id ( ) , position , " " , mDataBinding . etComment . getText ( ) . toString ( ) ) ;
} else if ( getActivity ( ) instanceof HistoryRecordActivity ) {
CommonUtils . hideSoftInput ( getContext ( ) , mDataBinding . etComment ) ;
mDataBinding . llComment . setVisibility ( View . GONE ) ;
comment ( discoveryListEntity . getGoods_id ( ) , position , " " , mDataBinding . etComment . getText ( ) . toString ( ) ) ;
}
}
} ) ;
if ( discoveryListEntity . getLike ( ) . isEmpty ( ) ) {
homeGridAdapterItemBinding . tvLike . setVisibility ( View . GONE ) ;
} else {
@@ -222,9 +349,12 @@ public class HistoryRecordFragment extends BaseFragment<FragmentHistoryRecordBin
@Override
public void onClick ( final View v ) {
final CommentDialog commentDialog = new CommentDialog ( getContext ( ) ) ;
commentDialog . setShowBug ( discoveryListEntity . getType ( ) = = 2 ) ;
commentDialog . setPraise ( discoveryListEntity . getIs_like ( ) = = 1 ) ;
// commentDialog.setShowBug(discoveryListEntity.getType() == 2);
commentDialog . setPraise ( discoveryListEntity . getIs_like ( ) = = 1 ) ;
commentDialog . setType ( ) ;
final int mBottomY = getCoordinateY ( homeGridAdapterItemBinding . ivComment ) + homeGridAdapterItemBinding . ivComment . getHeight ( ) ;
commentDialog . setOnCommentOperateListener ( new CommentDialog . OnCommentOperateListener ( ) {
@Override
public void click ( int type ) {
@@ -233,6 +363,34 @@ public class HistoryRecordFragment extends BaseFragment<FragmentHistoryRecordBin
//点赞/取消
commentDialog . dismiss ( ) ;
like ( discoveryListEntity . getGoods_id ( ) , position ) ;
} else if ( type = = 3 ) {
mDataBinding . llComment . setVisibility ( View . VISIBLE ) ;
mDataBinding . etComment . requestFocus ( ) ;
mDataBinding . etComment . setHint ( " 评论 " ) ;
MainTabActivity activity = ( MainTabActivity ) getActivity ( ) ;
KeyboardUtil . showSoftInput ( getContext ( ) ) ;
activity . visibilyBottom ( View . GONE ) ;
commentDialog . dismiss ( ) ;
mDataBinding . etComment . setText ( " " ) ;
homeGridAdapterItemBinding . ivComment . postDelayed ( new Runnable ( ) {
@Override
public void run ( ) {
int y = getCoordinateY ( mDataBinding . llComment ) - 20 ;
//评论时滑动到对应item底部和输入框顶部对齐
mDataBinding . sv . smoothScrollBy ( 0 , mBottomY - y ) ;
}
} , 300 ) ;
} else if ( type = = 4 ) {
// addCart(discoveryListEntity.getGoods_id(),discoveryListEntity.)
getGoodsDetail ( discoveryListEntity . getGoods_id ( ) , discoveryListEntity , type ) ;
commentDialog . dismiss ( ) ;
} else if ( type = = 5 ) {
getGoodsDetail ( discoveryListEntity . getGoods_id ( ) , discoveryListEntity , type ) ;
commentDialog . dismiss ( ) ;
}
}
} ) ;
@@ -249,6 +407,180 @@ public class HistoryRecordFragment extends BaseFragment<FragmentHistoryRecordBin
baseRecycleViewAdapter . setData ( discoveryListEntities ) ;
}
private void comment ( String goodsId , final int position , String reply_id , final String content ) {
final RegisterUser user =
Config . getInstance ( getContext ( ) ) . getUser ( ) ;
Map < String , String > map = new HashMap < > ( ) ;
if ( user ! = null ) {
map . put ( " userid " , user . getUserid ( ) ) ;
} else {
map . put ( " userid " , 0 + " " ) ;
}
map . put ( " app " , " Goods " ) ;
map . put ( " class " , " DisGoods " ) ;
map . put ( " goods_id " , goodsId ) ;
if ( ! TextUtils . isEmpty ( reply_id ) ) {
map . put ( " reply_id " , reply_id ) ;
}
map . put ( " contents " , content ) ;
map . put ( " sign " , Md5 . md5 ( " Goods " + " DisGoods " + Md5 . secret ) ) ;
progressDialog . setMessage ( " 提交中 " ) ;
progressDialog . setCancelable ( false ) ;
// progressDialog.show();
Loader mGankLoader = new Loader ( ) ;
Subscription subscription = mGankLoader . getMovie ( ApiConfig . BASE_URL , map ) . subscribe ( new Action1 < Resultcode > ( ) {
@Override
public void call ( Resultcode resultcode ) {
progressDialog . dismiss ( ) ;
if ( ! ( resultcode . status = = 0 ) ) {
Toast . makeText ( getContext ( ) , resultcode . msg , Toast . LENGTH_SHORT ) . show ( ) ;
}
if ( resultcode . status = = 0 ) {
BaseRecycleViewAdapter adapter = ( BaseRecycleViewAdapter ) mDataBinding . recyView . getAdapter ( ) ;
DiscoveryListEntity . Dis_content dis_content = new DiscoveryListEntity . Dis_content ( ) ;
LinkedTreeMap adta = ( LinkedTreeMap ) resultcode . data ;
dis_content . setContents ( adta . get ( " contents " ) . toString ( ) ) ;
// dis_content.set(adta.get("goods_id").toString());
adapter . upDateOne ( position , discoveryListEntities . get ( position ) ) ;
}
}
} , new Action1 < Throwable > ( ) {
@Override
public void call ( Throwable throwable ) {
progressDialog . dismiss ( ) ;
Toast . makeText ( getContext ( ) , " 提交失败 " , Toast . LENGTH_SHORT ) . show ( ) ;
throwable . printStackTrace ( ) ;
}
} ) ;
}
private void getGoodsDetail ( final String goodsId , final DiscoveryListEntity entity , final int goodtype ) {
final RegisterUser user =
Config . getInstance ( getContext ( ) ) . getUser ( ) ;
Map < String , String > map = new HashMap < > ( ) ;
if ( user ! = null ) {
map . put ( " userid " , user . getUserid ( ) ) ;
} else {
map . put ( " userid " , 0 + " " ) ;
}
map . put ( " app " , " Goods " ) ;
map . put ( " class " , " GoodsInfo " ) ;
map . put ( " goods_id " , goodsId ) ;
map . put ( " sign " , Md5 . md5 ( " Goods " + " GoodsInfo " + Md5 . secret ) ) ;
progressDialog . setMessage ( " 获取信息中 " ) ;
progressDialog . setCancelable ( false ) ;
Loader mGankLoader = new Loader ( ) ;
Subscription subscription = mGankLoader . getMovie ( ApiConfig . BASE_URL , map )
. subscribe ( new Action1 < Resultcode > ( ) {
@Override
public void call ( Resultcode resultcode ) {
progressDialog . dismiss ( ) ;
if ( resultcode . status = = 0 ) {
Gson gs = new Gson ( ) ;
LinkedTreeMap adta = ( LinkedTreeMap ) resultcode . data ;
String s = gs . toJson ( adta ) ;
try {
JSONObject jsonObject = new JSONObject ( s ) ;
org . json . JSONArray array = jsonObject . getJSONArray ( " info " ) ;
final Type type = new TypeToken < ArrayList < GoodsDetailEntity > > ( ) {
} . getType ( ) ;
ArrayList < GoodsDetailEntity > o = gs . fromJson ( array . toString ( ) , type ) ;
GoodsInfoDialog goodsInfoDialog = new GoodsInfoDialog ( getContext ( ) , entity , o ) ;
goodsInfoDialog . setOnConfirmListener ( new GoodsInfoDialog . OnGoodsSelectPropertyListener ( ) {
@Override
public void onSelect ( String code , int num ) {
if ( goodtype = = 4 ) {
Intent intent = new Intent ( getContext ( ) , SettleActivity . class ) ;
intent . putExtra ( " goodsType " , 1 ) ;
intent . putExtra ( " goods_id " , goodsId ) ;
intent . putExtra ( " product_code " , code ) ;
intent . putExtra ( " sale_num " , num + " " ) ;
startActivity ( intent ) ;
} else if ( goodtype = = 5 ) {
addCart ( goodsId , code , num + " " ) ;
}
}
} ) ;
new XPopup . Builder ( getContext ( ) ) . asCustom ( goodsInfoDialog ) . show ( ) ;
} catch ( JSONException e ) {
e . printStackTrace ( ) ;
}
} else {
// .setToast(resultcode.msg);
Toast . makeText ( getContext ( ) , resultcode . msg , Toast . LENGTH_SHORT ) . show ( ) ;
}
}
} , new Action1 < Throwable > ( ) {
@Override
public void call ( Throwable throwable ) {
progressDialog . dismiss ( ) ;
Toast . makeText ( getContext ( ) , " 获取信息失败 " , Toast . LENGTH_SHORT ) . show ( ) ;
throwable . printStackTrace ( ) ;
}
} ) ;
}
private void addCart ( String goods_id , String product_code , String goodsNum ) {
RegisterUser user =
Config . getInstance ( getContext ( ) ) . getUser ( ) ;
Map < String , String > map = new HashMap < > ( ) ;
if ( user ! = null ) {
map . put ( " userid " , user . getUserid ( ) ) ;
} else {
map . put ( " userid " , 0 + " " ) ;
}
map . put ( " app " , " Goods " ) ;
map . put ( " class " , " AddCart " ) ;
map . put ( " goods_id " , goods_id ) ;
map . put ( " product_code " , product_code ) ;
map . put ( " num " , goodsNum ) ;
map . put ( " sign " , Md5 . md5 ( " Goods " + " AddCart " + Md5 . secret ) ) ;
progressDialog . setMessage ( " 提交中 " ) ;
progressDialog . setCancelable ( false ) ;
Loader mGankLoader = new Loader ( ) ;
Subscription subscribe = mGankLoader . getMovie ( ApiConfig . BASE_URL , map ) . subscribe ( new Action1 < Resultcode > ( ) {
@Override
public void call ( Resultcode resultcode ) {
progressDialog . dismiss ( ) ;
if ( resultcode . status = = 0 ) {
Toast . makeText ( getContext ( ) , " 成功加入购物车 " , Toast . LENGTH_SHORT ) . show ( ) ;
// baseActivity.setToast(0, "成功加入购物车");
} else {
Toast . makeText ( getContext ( ) , resultcode . msg , Toast . LENGTH_SHORT ) . show ( ) ;
}
}
} , new Action1 < Throwable > ( ) {
@Override
public void call ( Throwable throwable ) {
progressDialog . dismiss ( ) ;
Toast . makeText ( getContext ( ) , " 提交失败 " , Toast . LENGTH_SHORT ) . show ( ) ;
throwable . printStackTrace ( ) ;
}
} ) ;
}
private void like ( String goodsId , final int position ) {
final RegisterUser user =
Config . getInstance ( getContext ( ) ) . getUser ( ) ;
@@ -266,7 +598,7 @@ public class HistoryRecordFragment extends BaseFragment<FragmentHistoryRecordBin
progressDialog . setCancelable ( false ) ;
// progressDialog.show();
Loader mGankLoader = new Loader ( ) ;
Loader mGankLoader = new Loader ( ) ;
Subscription subscription = mGankLoader . getMovie ( ApiConfig . BASE_URL , map ) . subscribe ( new Action1 < Resultcode > ( ) {
@@ -275,7 +607,7 @@ public class HistoryRecordFragment extends BaseFragment<FragmentHistoryRecordBin
progressDialog . dismiss ( ) ;
if ( ! ( resultcode . status = = 0 ) ) {
Toast . makeText ( getContext ( ) , resultcode . msg , Toast . LENGTH_SHORT ) . show ( ) ;
Toast . makeText ( getContext ( ) , resultcode . msg , Toast . LENGTH_SHORT ) . show ( ) ;
}
if ( resultcode . status = = 0 ) {
BaseRecycleViewAdapter adapter = ( BaseRecycleViewAdapter ) mDataBinding . recyView . getAdapter ( ) ;
@@ -307,14 +639,14 @@ public class HistoryRecordFragment extends BaseFragment<FragmentHistoryRecordBin
@Override
public void call ( Throwable throwable ) {
progressDialog . dismiss ( ) ;
Toast . makeText ( getContext ( ) , " 提交失败 " , Toast . LENGTH_SHORT ) . show ( ) ;
Toast . makeText ( getContext ( ) , " 提交失败 " , Toast . LENGTH_SHORT ) . show ( ) ;
throwable . printStackTrace ( ) ;
}
} ) ;
}
/**
* 获取控件左上顶点Y坐标
*