399 lines
15 KiB
Java
399 lines
15 KiB
Java
package com.uiui.aios.fragment;
|
|
|
|
import android.content.ComponentName;
|
|
import android.content.ContentResolver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import android.util.Log;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.ImageView;
|
|
import android.widget.ProgressBar;
|
|
import android.widget.TextView;
|
|
|
|
import com.bumptech.glide.Glide;
|
|
import com.trello.rxlifecycle4.RxLifecycle;
|
|
import com.trello.rxlifecycle4.android.FragmentEvent;
|
|
import com.uiui.aios.R;
|
|
import com.uiui.aios.base.BaseFragment;
|
|
import com.uiui.aios.bean.ActivityBean;
|
|
import com.uiui.aios.bean.ArticleInfo;
|
|
import com.uiui.aios.bean.BaseResponse;
|
|
import com.uiui.aios.bean.DemandBean;
|
|
import com.uiui.aios.bean.GoodsInfo;
|
|
import com.uiui.aios.network.NetInterfaceManager;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.ButterKnife;
|
|
import io.reactivex.rxjava3.annotations.NonNull;
|
|
import io.reactivex.rxjava3.core.Observer;
|
|
import io.reactivex.rxjava3.disposables.Disposable;
|
|
|
|
/**
|
|
* A simple {@link Fragment} subclass.
|
|
* Use the {@link SecondFragment#newInstance} factory method to
|
|
* create an instance of this fragment.
|
|
*/
|
|
public class SecondFragment extends BaseFragment {
|
|
private static final String TAG = SecondFragment.class.getSimpleName();
|
|
|
|
@BindView(R.id.iv_img)
|
|
ImageView iv_img;
|
|
@BindView(R.id.tv_goods_name)
|
|
TextView tv_goods_name;
|
|
@BindView(R.id.tv_goods_desc)
|
|
TextView tv_goods_desc;
|
|
@BindView(R.id.tv_buying_price)
|
|
TextView tv_buying_price;
|
|
@BindView(R.id.progressBar)
|
|
ProgressBar progressBar;
|
|
@BindView(R.id.tv_snapup)
|
|
TextView tv_snapup;
|
|
|
|
@BindView(R.id.iv_aimg)
|
|
ImageView iv_aimg;
|
|
@BindView(R.id.tv_title)
|
|
TextView tv_title;
|
|
@BindView(R.id.tv_content)
|
|
TextView tv_content;
|
|
|
|
@BindView(R.id.iv_avatar_a)
|
|
ImageView iv_avatar_a;
|
|
@BindView(R.id.tv_title_a)
|
|
TextView tv_title_a;
|
|
@BindView(R.id.tc_joined_num_a)
|
|
TextView tc_joined_num_a;
|
|
@BindView(R.id.tv_time_a)
|
|
TextView tv_time_a;
|
|
@BindView(R.id.tv_address_a)
|
|
TextView tv_address_a;
|
|
|
|
@BindView(R.id.iv_avatar_d)
|
|
ImageView iv_avatar_d;
|
|
@BindView(R.id.tv_title_d)
|
|
TextView tv_title_d;
|
|
@BindView(R.id.tc_joined_num_d)
|
|
TextView tc_joined_num_d;
|
|
@BindView(R.id.tv_time_d)
|
|
TextView tv_time_d;
|
|
@BindView(R.id.tv_address_d)
|
|
TextView tv_address_d;
|
|
|
|
@BindView(R.id.iv1)
|
|
ImageView iv1;
|
|
@BindView(R.id.iv2)
|
|
ImageView iv2;
|
|
@BindView(R.id.iv3)
|
|
ImageView iv3;
|
|
@BindView(R.id.iv4)
|
|
ImageView iv4;
|
|
|
|
@BindView(R.id.cl_activity)
|
|
ConstraintLayout cl_activity;
|
|
|
|
@BindView(R.id.cl_demand)
|
|
ConstraintLayout cl_demand;
|
|
@BindView(R.id.cl1)
|
|
ConstraintLayout cl1;
|
|
@BindView(R.id.cl2)
|
|
ConstraintLayout cl2;
|
|
@BindView(R.id.cl3)
|
|
ConstraintLayout cl3;
|
|
@BindView(R.id.cl4)
|
|
ConstraintLayout cl4;
|
|
|
|
|
|
private View rootView;
|
|
private Context mContext;
|
|
private ContentResolver mCRv;
|
|
|
|
// TODO: Rename parameter arguments, choose names that match
|
|
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
|
private static final String ARG_PARAM1 = "param1";
|
|
private static final String ARG_PARAM2 = "param2";
|
|
|
|
// TODO: Rename and change types of parameters
|
|
private String mParam1;
|
|
private String mParam2;
|
|
|
|
public SecondFragment() {
|
|
// Required empty public constructor
|
|
}
|
|
|
|
/**
|
|
* Use this factory method to create a new instance of
|
|
* this fragment using the provided parameters.
|
|
*
|
|
* @param param1 Parameter 1.
|
|
* @param param2 Parameter 2.
|
|
* @return A new instance of fragment SecondFragment.
|
|
*/
|
|
// TODO: Rename and change types and number of parameters
|
|
public static SecondFragment newInstance(String param1, String param2) {
|
|
SecondFragment fragment = new SecondFragment();
|
|
Bundle args = new Bundle();
|
|
args.putString(ARG_PARAM1, param1);
|
|
args.putString(ARG_PARAM2, param2);
|
|
fragment.setArguments(args);
|
|
return fragment;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
if (getArguments() != null) {
|
|
mParam1 = getArguments().getString(ARG_PARAM1);
|
|
mParam2 = getArguments().getString(ARG_PARAM2);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
Bundle savedInstanceState) {
|
|
// Inflate the layout for this fragment
|
|
rootView = inflater.inflate(R.layout.fragment_second, container, false);
|
|
mContext = rootView.getContext();
|
|
mCRv = mContext.getContentResolver();
|
|
ButterKnife.bind(this, rootView);
|
|
initView();
|
|
initData();
|
|
return rootView;
|
|
}
|
|
|
|
private void initData() {
|
|
getGoods();
|
|
getArticle();
|
|
getActivityList();
|
|
getDemandList();
|
|
}
|
|
|
|
private void getDemandList() {
|
|
NetInterfaceManager.getInstance().getDemandListObservable()
|
|
.compose(RxLifecycle.bindUntilEvent(lifecycleSubject, FragmentEvent.DESTROY))
|
|
.subscribe(new Observer<BaseResponse<List<DemandBean>>>() {
|
|
@Override
|
|
public void onSubscribe(@NonNull Disposable d) {
|
|
Log.e("getDemandList", "onSubscribe: ");
|
|
}
|
|
|
|
@Override
|
|
public void onNext(@NonNull BaseResponse<List<DemandBean>> listBaseResponse) {
|
|
Log.e("getDemandList", "onNext: " + listBaseResponse);
|
|
if (listBaseResponse.code == 200) {
|
|
cl4.setVisibility(View.VISIBLE);
|
|
iv4.setVisibility(View.GONE);
|
|
List<DemandBean> demandBeans = listBaseResponse.data;
|
|
if (demandBeans != null && demandBeans.size() != 0) {
|
|
DemandBean demandBean = demandBeans.get(0);
|
|
setDemand(demandBean);
|
|
}
|
|
} else {
|
|
cl4.setVisibility(View.GONE);
|
|
iv4.setVisibility(View.VISIBLE);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onError(@NonNull Throwable e) {
|
|
Log.e("getDemandList", "onError: " + e.getMessage());
|
|
}
|
|
|
|
@Override
|
|
public void onComplete() {
|
|
Log.e("getDemandList", "onComplete: ");
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
private void setDemand(DemandBean demandBean) {
|
|
Glide.with(iv_avatar_d).load(demandBean.getAvatar()).error(getResources().getDrawable(R.drawable.default_head)).into(iv_avatar_d);
|
|
tv_title_d.setText(demandBean.getTitle());
|
|
// tv_price.setText(String.valueOf(demandBean.getPrice()));
|
|
tc_joined_num_d.setText(demandBean.getLike_count() + "人参加");
|
|
tv_time_d.setText(getTime(demandBean.getDemand_time()));
|
|
tv_address_d.setText(demandBean.getAddress());
|
|
}
|
|
|
|
private void getActivityList() {
|
|
NetInterfaceManager.getInstance().getActivityListObservable()
|
|
.compose(RxLifecycle.bindUntilEvent(lifecycleSubject, FragmentEvent.DESTROY))
|
|
.subscribe(new Observer<BaseResponse<List<ActivityBean>>>() {
|
|
@Override
|
|
public void onSubscribe(@NonNull Disposable d) {
|
|
Log.e("getActivityList", "onSubscribe: ");
|
|
}
|
|
|
|
@Override
|
|
public void onNext(@NonNull BaseResponse<List<ActivityBean>> listBaseResponse) {
|
|
Log.e("getActivityList", "onNext: " + listBaseResponse);
|
|
if (listBaseResponse.code == 200) {
|
|
cl3.setVisibility(View.VISIBLE);
|
|
iv3.setVisibility(View.GONE);
|
|
List<ActivityBean> activityBeans = listBaseResponse.data;
|
|
if (activityBeans != null && activityBeans.size() != 0) {
|
|
ActivityBean activityBean = activityBeans.get(0);
|
|
setActivity(activityBean);
|
|
}
|
|
} else {
|
|
cl3.setVisibility(View.GONE);
|
|
iv3.setVisibility(View.VISIBLE);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onError(@NonNull Throwable e) {
|
|
Log.e("getActivityList", "onError: " + e.getMessage());
|
|
}
|
|
|
|
@Override
|
|
public void onComplete() {
|
|
Log.e("getActivityList", "onComplete: ");
|
|
}
|
|
});
|
|
}
|
|
|
|
private void setActivity(ActivityBean activity) {
|
|
Glide.with(iv_avatar_a).load(activity.getAvatar()).error(getResources().getDrawable(R.drawable.default_head)).into(iv_avatar_a);
|
|
tv_title_a.setText(activity.getTitle());
|
|
tc_joined_num_a.setText(activity.getJoin_num() + "人参加");
|
|
tv_time_a.setText(getTime(activity.getActivity_time()));
|
|
tv_address_a.setText(activity.getAddress());
|
|
}
|
|
|
|
private String getTime(long second) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
|
|
Date date = new Date(second * 1000);
|
|
return sdf.format(date);
|
|
}
|
|
|
|
private void initView() {
|
|
cl_activity.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
openApp(0);
|
|
}
|
|
});
|
|
cl_demand.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
openApp(1);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void openApp(int position) {
|
|
try {
|
|
Intent intent = new Intent(Intent.ACTION_MAIN);
|
|
/*知道要跳转应用的包命与目标Activity*/
|
|
ComponentName componentName = new ComponentName("com.uiui.city", "com.uiui.city.activity.MainActivity");
|
|
intent.putExtra("position", position);
|
|
intent.setComponent(componentName);
|
|
intent.putExtra("", "");//这里Intent传值
|
|
startActivity(intent);
|
|
} catch (Exception e) {
|
|
Log.e(TAG, "openApp: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
private void getGoods() {
|
|
NetInterfaceManager.getInstance()
|
|
.getGoodsListObservable()
|
|
.compose(RxLifecycle.bindUntilEvent(lifecycleSubject, FragmentEvent.DESTROY))
|
|
.subscribe(new Observer<BaseResponse<List<GoodsInfo>>>() {
|
|
@Override
|
|
public void onSubscribe(@NonNull Disposable d) {
|
|
Log.e("getGoods", "onSubscribe: ");
|
|
}
|
|
|
|
@Override
|
|
public void onNext(@NonNull BaseResponse<List<GoodsInfo>> listBaseResponse) {
|
|
Log.e("getGoods", "onNext: " + listBaseResponse);
|
|
if (listBaseResponse.code == 200) {
|
|
cl1.setVisibility(View.VISIBLE);
|
|
iv1.setVisibility(View.GONE);
|
|
List<GoodsInfo> goodsInfoList = listBaseResponse.data;
|
|
if (goodsInfoList != null && goodsInfoList.size() != 0) {
|
|
GoodsInfo goodsInfo = goodsInfoList.get(0);
|
|
setGoodsInfo(goodsInfo);
|
|
}
|
|
} else {
|
|
cl1.setVisibility(View.GONE);
|
|
iv1.setVisibility(View.VISIBLE);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onError(@NonNull Throwable e) {
|
|
Log.e("getGoods", "onError: " + e.getMessage());
|
|
}
|
|
|
|
@Override
|
|
public void onComplete() {
|
|
Log.e("getGoods", "onComplete: ");
|
|
}
|
|
});
|
|
}
|
|
|
|
private void setGoodsInfo(GoodsInfo goodsInfo) {
|
|
Glide.with(iv_img).load(goodsInfo.getImg()).into(iv_img);
|
|
tv_goods_name.setText(goodsInfo.getGoods_name());
|
|
tv_goods_desc.setText(goodsInfo.getGoods_desc());
|
|
tv_buying_price.setText(String.valueOf(goodsInfo.getBuying_price()));
|
|
}
|
|
|
|
private void getArticle() {
|
|
NetInterfaceManager.getInstance().getArticleListObservable()
|
|
.compose(RxLifecycle.bindUntilEvent(lifecycleSubject, FragmentEvent.DESTROY))
|
|
.subscribe(new Observer<BaseResponse<List<ArticleInfo>>>() {
|
|
@Override
|
|
public void onSubscribe(@NonNull Disposable d) {
|
|
Log.e("getArticle", "onSubscribe: ");
|
|
}
|
|
|
|
@Override
|
|
public void onNext(@NonNull BaseResponse<List<ArticleInfo>> listBaseResponse) {
|
|
Log.e("getArticle", "onNext: " + listBaseResponse);
|
|
if (listBaseResponse.code == 200) {
|
|
cl2.setVisibility(View.VISIBLE);
|
|
iv2.setVisibility(View.GONE);
|
|
List<ArticleInfo> articleInfos = listBaseResponse.data;
|
|
if (articleInfos != null && articleInfos.size() != 0) {
|
|
ArticleInfo articleInfo = articleInfos.get(0);
|
|
setArticleInfo(articleInfo);
|
|
}
|
|
} else {
|
|
cl2.setVisibility(View.GONE);
|
|
iv2.setVisibility(View.VISIBLE);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onError(@NonNull Throwable e) {
|
|
Log.e("getArticle", "onError: " + e.getMessage());
|
|
}
|
|
|
|
@Override
|
|
public void onComplete() {
|
|
Log.e("getArticle", "onComplete: ");
|
|
}
|
|
});
|
|
}
|
|
|
|
private void setArticleInfo(ArticleInfo articleInfo) {
|
|
Glide.with(iv_aimg).load(articleInfo.getImg()).into(iv_aimg);
|
|
tv_title.setText(articleInfo.getTitle());
|
|
tv_content.setText(articleInfo.getContent());
|
|
}
|
|
}
|