333 lines
11 KiB
Java
333 lines
11 KiB
Java
package com.yunbao.live.dialog;
|
|
|
|
import android.os.Bundle;
|
|
import android.text.Editable;
|
|
import android.text.TextWatcher;
|
|
import android.util.Log;
|
|
import android.view.Gravity;
|
|
import android.view.View;
|
|
import android.view.Window;
|
|
import android.view.WindowManager;
|
|
import android.widget.EditText;
|
|
import android.widget.ImageView;
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.Nullable;
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.google.android.material.tabs.TabLayout;
|
|
import com.yunbao.common.adapter.RefreshAdapter;
|
|
import com.yunbao.common.bean.RandomPkUserBean;
|
|
import com.yunbao.common.custom.CommonRefreshView;
|
|
import com.yunbao.common.dialog.AbsDialogFragment;
|
|
import com.yunbao.common.http.HttpCallback;
|
|
import com.yunbao.common.http.live.LiveNetManager;
|
|
import com.yunbao.common.interfaces.OnItemClickListener;
|
|
import com.yunbao.common.utils.DpUtil;
|
|
import com.yunbao.common.utils.StringUtil;
|
|
import com.yunbao.common.utils.ToastUtil;
|
|
import com.yunbao.common.utils.WordsTypeUtil;
|
|
import com.yunbao.live.R;
|
|
import com.yunbao.live.adapter.RandomPkRecyclerAdapter;
|
|
import com.yunbao.live.bean.LivePkBean;
|
|
import com.yunbao.live.http.LiveHttpUtil;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 自由PK
|
|
*/
|
|
public class FreePkDialogFragment extends AbsDialogFragment implements View.OnClickListener {
|
|
private static final int TAB_STATUS_SEARCH = 1;
|
|
private static final int TAB_STATUS_FOLLOW = 2;
|
|
|
|
private static final String TAG = "自由PK";
|
|
private int mLiveUid;
|
|
private TabLayout tabLayout;
|
|
private View reset;
|
|
private CommonRefreshView mRecyclerView;
|
|
private View mSearchLayout;
|
|
private EditText mSearch;
|
|
private TextView freePkNum;
|
|
private ImageView mClear;
|
|
private RandomPkRecyclerAdapter adapter;
|
|
private DataHelper helper;
|
|
private int tabStatus;
|
|
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.dialog_live_free_pk_function;
|
|
}
|
|
|
|
@Override
|
|
protected int getDialogStyle() {
|
|
return R.style.dialog2;
|
|
}
|
|
|
|
@Override
|
|
protected boolean canCancel() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
protected void setWindowAttributes(Window window) {
|
|
WindowManager.LayoutParams params = window.getAttributes();
|
|
window.setWindowAnimations(R.style.bottomToTopAnim);
|
|
params.width = WindowManager.LayoutParams.MATCH_PARENT;
|
|
params.height = DpUtil.dp2px(400);
|
|
params.gravity = Gravity.BOTTOM;
|
|
window.setAttributes(params);
|
|
}
|
|
|
|
public void setLiveUid(int mLiveUid) {
|
|
this.mLiveUid = mLiveUid;
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
}
|
|
|
|
@Override
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
super.onActivityCreated(savedInstanceState);
|
|
initView();
|
|
initTab();
|
|
initData();
|
|
}
|
|
|
|
private void initView() {
|
|
tabLayout = (TabLayout) findViewById(R.id.menu_tab);
|
|
reset = findViewById(R.id.menu_reset);
|
|
mRecyclerView = (CommonRefreshView) findViewById(R.id.random_container_view);
|
|
mSearchLayout = findViewById(R.id.random_pk_search_layout);
|
|
mSearch = (EditText) findViewById(R.id.search_edit);
|
|
mClear = (ImageView) findViewById(R.id.search_clear);
|
|
freePkNum = (TextView) findViewById(R.id.free_pk_num);
|
|
mClear.setOnClickListener(this);
|
|
reset.setOnClickListener(this);
|
|
mSearch.addTextChangedListener(new TextWatcher() {
|
|
@Override
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
helper.search(s.toString());
|
|
}
|
|
|
|
@Override
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
}
|
|
});
|
|
|
|
initRecycler();
|
|
}
|
|
|
|
private void initRecycler() {
|
|
helper = new DataHelper();
|
|
helper.setStatus(DataHelper.LIST_MODEL_LIST);
|
|
adapter = new RandomPkRecyclerAdapter(mContext);
|
|
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
|
|
mRecyclerView.setDataHelper(helper);
|
|
mRecyclerView.initData();
|
|
mRecyclerView.setRefreshEnable(false);
|
|
mRecyclerView.setEmptyLayoutId(R.layout.view_live_search_list_empty);
|
|
adapter.setOnItemClickListener(new OnItemClickListener<RandomPkUserBean>() {
|
|
@Override
|
|
public void onItemClick(RandomPkUserBean bean, int position) {
|
|
if (position == -1) {
|
|
dismiss();
|
|
return;
|
|
}
|
|
mRecyclerView.initData();
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
private void initData() {
|
|
LiveNetManager.get(mContext).getFreePkNumber(new com.yunbao.common.http.base.HttpCallback<Integer>() {
|
|
@Override
|
|
public void onSuccess(Integer data) {
|
|
freePkNum.setText("" + data);
|
|
}
|
|
|
|
@Override
|
|
public void onError(String error) {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
private void initTab() {
|
|
TabLayout.Tab searchTag = tabLayout.newTab();
|
|
TabLayout.Tab listTag = tabLayout.newTab();
|
|
searchTag.setTag(TAB_STATUS_SEARCH);
|
|
searchTag.setText("主播搜索");
|
|
listTag.setTag(TAB_STATUS_FOLLOW);
|
|
listTag.setText("關注列表");
|
|
tabLayout.addTab(searchTag);
|
|
tabLayout.addTab(listTag);
|
|
tabStatus = TAB_STATUS_SEARCH;
|
|
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
|
|
@Override
|
|
public void onTabSelected(TabLayout.Tab tab) {
|
|
if (tab.getTag() != null) {
|
|
tabStatus = (int) tab.getTag();
|
|
mRecyclerView.setVisibility(View.GONE);
|
|
mSearchLayout.setVisibility(View.GONE);
|
|
switch (tabStatus) {
|
|
case TAB_STATUS_SEARCH:
|
|
helper.setStatus(DataHelper.LIST_MODEL_LIST);
|
|
mSearchLayout.setVisibility(View.VISIBLE);
|
|
mRecyclerView.setVisibility(View.VISIBLE);
|
|
mRecyclerView.initData();
|
|
mRecyclerView.setRefreshEnable(false);
|
|
mRecyclerView.showEmpty();
|
|
break;
|
|
case TAB_STATUS_FOLLOW:
|
|
mSearch.setText("");
|
|
helper.setStatus(DataHelper.LIST_MODEL_FOLLOW);
|
|
mRecyclerView.setVisibility(View.VISIBLE);
|
|
mRecyclerView.initData();
|
|
mRecyclerView.setRefreshEnable(true);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onTabUnselected(TabLayout.Tab tab) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
int id = v.getId();
|
|
if (id == R.id.menu_reset) {
|
|
if (helper.status == DataHelper.LIST_MODEL_LIST) {
|
|
mSearch.setText("");
|
|
mRecyclerView.initData();
|
|
mRecyclerView.showEmpty();
|
|
} else {
|
|
mRecyclerView.initData();
|
|
}
|
|
} else if (id == R.id.search_clear) {
|
|
mSearch.setText("");
|
|
}
|
|
}
|
|
|
|
public class DataHelper implements CommonRefreshView.DataHelper<RandomPkUserBean> {
|
|
private final static int LIST_MODEL_LIST = 0;
|
|
private final static int LIST_MODEL_FOLLOW = 1;
|
|
private int status;
|
|
private String searchKey;
|
|
|
|
public void setStatus(int status) {
|
|
this.status = status;
|
|
}
|
|
|
|
public void search(String key) {
|
|
if (StringUtil.isEmpty(key)) {
|
|
adapter.getList().clear();
|
|
adapter.notifyDataSetChanged();
|
|
mRecyclerView.showEmpty();
|
|
searchKey = null;
|
|
return;
|
|
}
|
|
searchKey = key;
|
|
LiveNetManager.get(mContext)
|
|
.randomPkSearchUser(key, WordsTypeUtil.changeTraditional(key), new com.yunbao.common.http.base.HttpCallback<List<RandomPkUserBean>>() {
|
|
@Override
|
|
public void onSuccess(List<RandomPkUserBean> data) {
|
|
if (!data.isEmpty()) {
|
|
mRecyclerView.hideEmpty();
|
|
}
|
|
List<RandomPkUserBean> list = new ArrayList<>();
|
|
list.add(null);
|
|
list.addAll(data);
|
|
adapter.setList(list);
|
|
adapter.notifyDataSetChanged();
|
|
}
|
|
|
|
@Override
|
|
public void onError(String error) {
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public RefreshAdapter<RandomPkUserBean> getAdapter() {
|
|
return adapter;
|
|
}
|
|
|
|
@Override
|
|
public void loadData(int p, HttpCallback callback) {
|
|
if (status == LIST_MODEL_LIST) {
|
|
if (searchKey == null) {
|
|
callback.onSuccess(0, "", new String[]{});
|
|
mRecyclerView.showEmpty();
|
|
return;
|
|
}
|
|
search(searchKey);
|
|
} else {
|
|
LiveHttpUtil.getHomeFollow(p, callback);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<RandomPkUserBean> processData(String[] info) {
|
|
Log.i(TAG, "processData: 获取数据成功");
|
|
List<RandomPkUserBean> list = new ArrayList<>();
|
|
if (info == null || info.length == 0) {
|
|
return list;
|
|
}
|
|
List<LivePkBean> beans = JSON.parseArray(Arrays.toString(info), LivePkBean.class);
|
|
list.add(null);
|
|
for (LivePkBean bean : beans) {
|
|
RandomPkUserBean userBean = new RandomPkUserBean();
|
|
userBean.setId(bean.getUid());
|
|
userBean.setUserNiceName(bean.getUserNiceName());
|
|
userBean.setAvatar(bean.getAvatar());
|
|
userBean.setPk(bean.getPkUid().equals("0") ? 0 : 1);
|
|
userBean.setAttention("1");
|
|
list.add(userBean);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
@Override
|
|
public void onRefreshSuccess(List<RandomPkUserBean> list, int listCount) {
|
|
}
|
|
|
|
@Override
|
|
public void onRefreshFailure() {
|
|
mRecyclerView.showEmpty();
|
|
}
|
|
|
|
@Override
|
|
public void onLoadMoreSuccess(List<RandomPkUserBean> loadItemList, int loadItemCount) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onLoadMoreFailure() {
|
|
}
|
|
}
|
|
}
|