package com.dyhoa.school.ui.community;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.dyh.frame.BaseFragmentActivity;
import com.dyh.frame.util.JsonUtils;
import com.dyh.library.base.PageHelperNew;
import com.dyh.library.base.net.DefaultStringRequestNew;
import com.dyh.library.base.net.ResponseListenerNew;
import com.dyh.library.base.net.VolleyUtil;
import com.dyh.library.util.UserHelper;
import com.dyh.library.widget.NoDataView;
import com.dyhoa.school.R;
import com.dyhoa.school.global.HostStudentNew;
import com.dyhoa.school.ui.community.adapter.MyCommunityFansAdapter;
import com.dyhoa.school.ui.community.bean.CommunityBean;
import com.dyhoa.school.ui.community.bean.CommunityFansListBean;
import com.dyhoa.school.ui.login.LoginAcitvity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.util.HashMap;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* @author lijianguo
* @desc 我的粉丝
* @date 2019/3/19
*/
public class MyCommunityFansActivity extends BaseFragmentActivity {
@BindView(R.id.list_records)
ListView listRecords;
@BindView(R.id.refreshLayout)
SmartRefreshLayout refreshLayout;
@BindView(R.id.relat_not)
NoDataView relatNot;
@BindView(R.id.iv_back)
ImageView ivBack;
@BindView(R.id.tv_title)
TextView tvTitle;
private PageHelperNew pageHelper;
private MyCommunityFansAdapter adapter;
//用户id
private String articleMid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_community_fans);
initView();
reqDatalist();
}
private void initView() {
ButterKnife.bind(this);
articleMid = getIntent().getStringExtra("articleMid");
if (UserHelper.getUUUID().equals(articleMid + "")) {
tvTitle.setText("我的粉丝");
} else {
tvTitle.setText("TA的粉丝");
}
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
pageHelper = PageHelperNew.getNewInstance();
pageHelper.setPageSize(10);
refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout layout) {
refreshLayout.setNoMoreData(false);
pageHelper.pullDown();
reqDatalist();
refreshLayout.finishRefresh();//恢复没有更多数据的原始状态
}
});
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout layout) {
if (pageHelper.pullUp(refreshLayout)) {
reqDatalist();
}
}
});
adapter = new MyCommunityFansAdapter(mContext, null);
listRecords.setAdapter(adapter);
adapter.setOnOperClickListener(new MyCommunityFansAdapter.OnOperClickListener() {
@Override
public void onFansClick(CommunityFansListBean.DataEntity.PageEntity dataItem, int position) {
if (UserHelper.isLogined()) {
reqFocus(dataItem);
} else {
startActivity(LoginAcitvity.class);
}
}
});
}
/**
* 社区会员关注
*/
private void reqFocus(final CommunityFansListBean.DataEntity.PageEntity item) {
HashMap<String, String> map = new HashMap<>();
map.put("mid", UserHelper.getUUUID());
map.put("attentionMid", item.getMid() + "");
map.put("type", item.getIsAttenTion() + "");
String path = pageHelper.getUrlNull(HostStudentNew.COMMUNITY_FOLLW, map);
DefaultStringRequestNew req = new DefaultStringRequestNew(mContext, path, new ResponseListenerNew(mContext) {
@Override
public void onSuccess(String response) {
CommunityBean bean = JsonUtils.fromJson(response, CommunityBean.class);
if (bean != null) {
if (bean.getResult() == 0) {
if (!TextUtils.isEmpty(bean.getData()))
showToast(bean.getData());
if (item.getIsAttenTion() == 1) {
item.setIsAttenTion(2);
adapter.notifyDataSetChanged();
} else {
item.setIsAttenTion(1);
adapter.notifyDataSetChanged();
}
} else {
showToast(bean.getMessage());
}
}
}
});
VolleyUtil.getInstance().addToRequestQueue(req);
}
/**
* 粉丝列表
*/
private void reqDatalist() {
HashMap<String, String> map = new HashMap<>();
map.put("attentionMid", articleMid);
map.put("pageIndex", pageHelper.getCurPage() + "");
map.put("pageSize", pageHelper.getPageSize() + "");
String path = pageHelper.getUrlNull(HostStudentNew.COMMUNITY_FANS_LIST, map);
DefaultStringRequestNew req = new DefaultStringRequestNew(mContext, path, new ResponseListenerNew(mContext) {
@Override
public void onSuccess(String response) {
CommunityFansListBean detailsBean = JsonUtils.fromJson(response, CommunityFansListBean.class);
if (detailsBean != null) {
if (detailsBean.getResult() == 0) {
CommunityFansListBean.DataEntity data = detailsBean.getData();
if (data != null) {
pageHelper.successSmart(refreshLayout, relatNot, data.getPage(), data.getPageCount(), adapter);
}
} else {
showToast(detailsBean.getMessage());
}
}
if (refreshLayout != null)
refreshLayout.finishLoadMore();
}
});
VolleyUtil.getInstance().addToRequestQueue(req);
}
}
-------------------------------------------------------------------以下适配器-------------------------------------------------------------
package com.dyhoa.school.ui.community.adapter; import android.content.Context; import android.view.View; import android.widget.TextView; import com.dyh.frame.simple.AbstractAdapter; import com.dyh.library.util.UserHelper; import com.dyhoa.school.R; import com.dyhoa.school.ui.community.bean.CommunityFansListBean; import com.dyhoa.school.utils.GlideUtils; import com.itheima.roundedimageview.RoundedImageView; import java.util.List; /** * @author lijianguo * @desc 我的粉丝适配器 * @date 2019/3/19 */ public class MyCommunityFansAdapter extends AbstractAdapter<CommunityFansListBean.DataEntity.PageEntity> { public MyCommunityFansAdapter(Context mContext, List<CommunityFansListBean.DataEntity.PageEntity> dataList) { super(mContext, dataList); } @Override public int getListViewItemId() { return R.layout.item_community_fans; } @Override public long getItemId(int position) { return position; } public void convert(final int position, View convertView, final CommunityFansListBean.DataEntity.PageEntity dataItem) { //关注 TextView tvFollow = convertView.findViewById(R.id.tv_follow); TextView tvNserName = convertView.findViewById(R.id.tv_user_name);//用户昵称 RoundedImageView ivUserIcon = convertView.findViewById(R.id.iv_user_icon);//用户头像 //isAttenTion 1 未关注 2 已关注 if (dataItem.getIsAttenTion() == 1) { tvFollow.setText("+关注"); } else if (dataItem.getIsAttenTion() == 2) { tvFollow.setText("已关注"); } if (UserHelper.getUUUID().equals(dataItem.getAttentionMid()+"")) { tvFollow.setVisibility(View.VISIBLE); } else { tvFollow.setVisibility(View.GONE); } tvNserName.setText(dataItem.getNickname() + "");//用户昵称 // ImageLoader.getInstance().displayImage(dataItem.getIcon(), ivUserIcon, DisplayOption.getHeadOptionNew()); GlideUtils.loadHeadImageViewDiskCache(mContext, dataItem.getIcon(), ivUserIcon); tvFollow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onOperClickListener.onFansClick(dataItem, position); } }); } private OnOperClickListener onOperClickListener; public void setOnOperClickListener(OnOperClickListener onOperClickListener) { this.onOperClickListener = onOperClickListener; } public interface OnOperClickListener { void onFansClick(CommunityFansListBean.DataEntity.PageEntity dataItem, int position); } }