|
@@ -0,0 +1,394 @@
|
|
|
+package org.telegram.cricdit.ui;
|
|
|
+
|
|
|
+
|
|
|
+import static org.telegram.cricdit.utils.TimeUtils.getStringToDate;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.CountDownTimer;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.RelativeLayout;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
+
|
|
|
+import com.lxj.xpopup.XPopup;
|
|
|
+import com.scwang.smart.refresh.layout.SmartRefreshLayout;
|
|
|
+import com.scwang.smart.refresh.layout.api.RefreshLayout;
|
|
|
+import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
|
|
|
+import com.youth.banner.Banner;
|
|
|
+import com.youth.banner.indicator.BaseIndicator;
|
|
|
+import com.youth.banner.listener.OnPageChangeListener;
|
|
|
+
|
|
|
+import org.greenrobot.eventbus.Subscribe;
|
|
|
+import org.greenrobot.eventbus.ThreadMode;
|
|
|
+import org.telegram.cricdit.adapter.NewsAdapter;
|
|
|
+import org.telegram.cricdit.adapter.NewsBannerAdapter;
|
|
|
+import org.telegram.cricdit.adapter.RecommendGroupAdapter;
|
|
|
+import org.telegram.cricdit.base.RecommendBannerBean;
|
|
|
+import org.telegram.cricdit.base.RecommendGroupBean;
|
|
|
+import org.telegram.cricdit.bean.BaseViewFragment;
|
|
|
+import org.telegram.cricdit.bean.MoreGroupBean;
|
|
|
+import org.telegram.cricdit.bean.NewsBean;
|
|
|
+import org.telegram.cricdit.bean.NewsItemEvent;
|
|
|
+import org.telegram.cricdit.bean.NewsMsgEvent;
|
|
|
+import org.telegram.cricdit.bean.ShieldIdListBean;
|
|
|
+import org.telegram.cricdit.mvp.NewsPresenter;
|
|
|
+import org.telegram.cricdit.mvp.NewsView;
|
|
|
+import org.telegram.cricdit.utils.ACache;
|
|
|
+import org.telegram.cricdit.utils.EventBusHelper;
|
|
|
+import org.telegram.cricdit.utils.GsonHel;
|
|
|
+import org.telegram.cricdit.utils.SPUtils;
|
|
|
+import org.telegram.cricdit.utils.TimeUtils;
|
|
|
+import org.telegram.cricdit.view.BannerIndicatorView;
|
|
|
+import org.telegram.cricdit.view.popup.ChooeseBottomPopup;
|
|
|
+import org.telegram.messenger.R;
|
|
|
+import org.telegram.onecric.ui.activity.CricketDetailActivity;
|
|
|
+import org.telegram.onecric.ui.activity.NewMatchActivity;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class HeadlinesFragment implements BaseViewFragment, NewsView {
|
|
|
+ public View view;
|
|
|
+ private Context mContext;
|
|
|
+ private NewNewsActivity mFragment;
|
|
|
+ private RecyclerView mRecyclerView;
|
|
|
+ private SmartRefreshLayout smartRefreshlayout;
|
|
|
+ private TextView tv_live_num;
|
|
|
+// private TextView tv_back_top;
|
|
|
+ private Banner banner_headlines;
|
|
|
+ private BannerIndicatorView indicatorView;
|
|
|
+ private RecyclerView recycler_group;
|
|
|
+ private RelativeLayout rl_live;
|
|
|
+ private RelativeLayout rl_group;
|
|
|
+ private String newsType = "news";
|
|
|
+
|
|
|
+ private int page = 1, pagesize = 10, offset = 0, topNum = 0;
|
|
|
+ private NewsAdapter adapter;
|
|
|
+ private int jumpPosition = -1;
|
|
|
+ private NewsPresenter presenter;
|
|
|
+ private int distance;
|
|
|
+ private boolean topVisible = false;
|
|
|
+ private NewsBannerAdapter bannerAdapter;
|
|
|
+ private RecommendGroupAdapter groupAdapter;
|
|
|
+
|
|
|
+ public HeadlinesFragment(NewNewsActivity fragment) {
|
|
|
+ mContext = fragment.getContext();
|
|
|
+ mFragment = fragment;
|
|
|
+ presenter = new NewsPresenter();
|
|
|
+ presenter.attachView(this);
|
|
|
+ view = View.inflate(mContext, R.layout.fragment_headlines, null);
|
|
|
+ initView(view);
|
|
|
+ showCacheList(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initData() {
|
|
|
+ page = 1;
|
|
|
+ presenter.getData(newsType, page, pagesize);
|
|
|
+ presenter.getRecommendBanner();
|
|
|
+ presenter.getRecommendGroup();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initView(View view) {
|
|
|
+ EventBusHelper.register(this);
|
|
|
+ mRecyclerView = view.findViewById(R.id.mRecyclerView);
|
|
|
+ smartRefreshlayout = view.findViewById(R.id.smart_refreshlayout);
|
|
|
+
|
|
|
+ tv_live_num = view.findViewById(R.id.tv_live_num);
|
|
|
+// tv_back_top = view.findViewById(R.id.tv_back_top);
|
|
|
+ banner_headlines = view.findViewById(R.id.banner_headlines);
|
|
|
+ indicatorView = view.findViewById(R.id.indicator);
|
|
|
+ recycler_group = view.findViewById(R.id.recycler_group);
|
|
|
+ rl_live = view.findViewById(R.id.rl_live);
|
|
|
+ rl_group = view.findViewById(R.id.rl_group);
|
|
|
+
|
|
|
+ banner_headlines.setIndicator(new BaseIndicator(mContext), false);
|
|
|
+ banner_headlines.addOnPageChangeListener(new OnPageChangeListener() {
|
|
|
+ @Override
|
|
|
+ public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
|
|
+ if (bannerAdapter.getItemCount() > 0) {
|
|
|
+ indicatorView.changeSelected(position);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPageSelected(int position) {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPageScrollStateChanged(int state) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ bannerAdapter = new NewsBannerAdapter(mContext,new ArrayList<>());
|
|
|
+ banner_headlines.setAdapter(bannerAdapter);
|
|
|
+
|
|
|
+ recycler_group.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
|
|
|
+ groupAdapter = new RecommendGroupAdapter(R.layout.item_recommend_group,mFragment,new ArrayList<>());
|
|
|
+ recycler_group.setAdapter(groupAdapter);
|
|
|
+
|
|
|
+ LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
|
|
|
+ linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
|
|
|
+ mRecyclerView.setLayoutManager(linearLayoutManager);
|
|
|
+ adapter = new NewsAdapter(new ArrayList<>(), mFragment, presenter, newsType);
|
|
|
+
|
|
|
+ adapter.setmOnItemClickListener((v, position, bean) -> {
|
|
|
+ jumpPosition = position;
|
|
|
+ NewsDetailCommentFragment.newsBean = bean;
|
|
|
+ mFragment.presentFragment(new NewsDetailCommentFragment(new Bundle()));
|
|
|
+ });
|
|
|
+
|
|
|
+ adapter.setOnReportClickListener((id, username) -> new XPopup.Builder(mFragment.getContext())
|
|
|
+ .moveUpToKeyboard(false) //如果不加这个,评论弹窗会移动到软键盘上面
|
|
|
+ .enableDrag(true)
|
|
|
+ .isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
|
|
|
+ .asCustom(new ChooeseBottomPopup(mFragment.getContext(), id, username, userID -> {
|
|
|
+ String shieldIds = SPUtils.getInstance().get("shield_ids", "");
|
|
|
+ if (TextUtils.isEmpty(shieldIds)) {
|
|
|
+ ShieldIdListBean shieldIdListBean = new ShieldIdListBean();
|
|
|
+ shieldIdListBean.shieldIdsList = new ArrayList<>();
|
|
|
+ shieldIdListBean.shieldIdsList.add(userID);
|
|
|
+ SPUtils.getInstance().save("shield_ids", GsonHel.toJson(shieldIdListBean));
|
|
|
+ shieldData(userID);
|
|
|
+ } else {
|
|
|
+ ShieldIdListBean shieldIdListBean = GsonHel.fromJson(shieldIds, ShieldIdListBean.class);
|
|
|
+ if (shieldIdListBean != null && shieldIdListBean.shieldIdsList != null && !shieldIdListBean.shieldIdsList.contains(userID)) {
|
|
|
+ shieldIdListBean.shieldIdsList.add(userID);
|
|
|
+ SPUtils.getInstance().save("shield_ids", GsonHel.toJson(shieldIdListBean));
|
|
|
+ shieldData(userID);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }))
|
|
|
+ .show());
|
|
|
+
|
|
|
+ mRecyclerView.setAdapter(adapter);
|
|
|
+
|
|
|
+ smartRefreshlayout.setOnRefreshListener(refreshLayout -> {
|
|
|
+ page = 1;
|
|
|
+ offset = 0;
|
|
|
+ presenter.getData(newsType, page, pagesize);
|
|
|
+ presenter.getRecommendBanner();
|
|
|
+ presenter.getRecommendGroup();
|
|
|
+ });
|
|
|
+
|
|
|
+ smartRefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
|
|
|
+ @Override
|
|
|
+ public void onLoadMore(RefreshLayout refreshLayout) {
|
|
|
+ if (adapter.getItemCount() == page * pagesize + topNum) {
|
|
|
+ page++;
|
|
|
+ offset = page * pagesize + topNum;
|
|
|
+ presenter.getData(newsType, page, pagesize);
|
|
|
+ } else {
|
|
|
+ smartRefreshlayout.finishLoadMore();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+/* mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
+ @Override
|
|
|
+ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
|
+ super.onScrolled(recyclerView, dx, dy);
|
|
|
+ if (distance < -ViewConfiguration.getTouchSlop() && !topVisible) {
|
|
|
+ int currentPosition = ((RecyclerView.LayoutParams) recyclerView.getChildAt(0).getLayoutParams()).getViewAdapterPosition();
|
|
|
+ if(currentPosition < 2){
|
|
|
+ showTodayBtnAnim(1);
|
|
|
+ topVisible = false;
|
|
|
+ }else{
|
|
|
+ showTodayBtnAnim(0);
|
|
|
+ topVisible = true;
|
|
|
+ }
|
|
|
+ distance = 0;
|
|
|
+
|
|
|
+ } else if (distance > ViewConfiguration.getTouchSlop() && topVisible) {
|
|
|
+ showTodayBtnAnim(1);
|
|
|
+ distance = 0;
|
|
|
+ topVisible = false;
|
|
|
+ }
|
|
|
+ if ((dy > 0 && topVisible) || (dy < 0 && !topVisible))
|
|
|
+ distance += dy;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ tv_back_top.setOnClickListener(v -> {
|
|
|
+ mRecyclerView.smoothScrollToPosition(0);
|
|
|
+ distance = 0;
|
|
|
+ topVisible = true;
|
|
|
+ });
|
|
|
+ showTodayBtnAnim(1);*/
|
|
|
+
|
|
|
+
|
|
|
+ view.findViewById(R.id.tv_all_live).setOnClickListener(v -> {
|
|
|
+ //更多直播
|
|
|
+ mFragment.presentFragment(new NewMatchActivity(), true, 0);
|
|
|
+ });
|
|
|
+
|
|
|
+ view.findViewById(R.id.tv_all_group).setOnClickListener(v -> {
|
|
|
+ //更多群组
|
|
|
+ mFragment.selectTab(3);
|
|
|
+ });
|
|
|
+
|
|
|
+ initData();
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<NewsBean.NewsFeedBean> shieldData(String userID) {
|
|
|
+ List<NewsBean.NewsFeedBean> data = adapter.getData();
|
|
|
+ Iterator<NewsBean.NewsFeedBean> iterator = data.iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ NewsBean.NewsFeedBean next = iterator.next();
|
|
|
+ if ((next.getUserId() + "").equals(userID)) {
|
|
|
+ iterator.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ adapter.notifyDataSetChanged();
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public View getView() {
|
|
|
+ return view;
|
|
|
+ }
|
|
|
+
|
|
|
+/* private void showTodayBtnAnim(int type){
|
|
|
+ switch (type){
|
|
|
+ case 0:
|
|
|
+ if(tv_back_top.isSelected()){
|
|
|
+ transAnimX(tv_back_top,tv_back_top.getMeasuredHeight() + UIUtil.dip2px(mContext,12),0);
|
|
|
+ tv_back_top.setSelected(false);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ if(!tv_back_top.isSelected()){
|
|
|
+ transAnimX(tv_back_top,0,tv_back_top.getMeasuredHeight() + UIUtil.dip2px(mContext,12));
|
|
|
+ tv_back_top.setSelected(true);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void showList(NewsBean mList) {
|
|
|
+ smartRefreshlayout.setVisibility(View.VISIBLE);
|
|
|
+ smartRefreshlayout.finishRefresh();
|
|
|
+ smartRefreshlayout.finishLoadMore();
|
|
|
+
|
|
|
+ List<NewsBean.NewsFeedBean> list = mList.getList();
|
|
|
+
|
|
|
+ String shieldIds = SPUtils.getInstance().get("shield_ids", "");
|
|
|
+ if (!TextUtils.isEmpty(shieldIds)) {
|
|
|
+ ShieldIdListBean shieldIdListBean = GsonHel.fromJson(shieldIds,ShieldIdListBean.class);
|
|
|
+ if (shieldIdListBean.shieldIdsList.size()>0) {
|
|
|
+ for (String userID : shieldIdListBean.shieldIdsList) {
|
|
|
+ Iterator<NewsBean.NewsFeedBean> iterator =list.iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ NewsBean.NewsFeedBean next = iterator.next();
|
|
|
+ if ((next.getUserId()).equals(userID)) {
|
|
|
+ iterator.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (offset == 0) {
|
|
|
+ adapter.setData(list);
|
|
|
+ } else {
|
|
|
+ adapter.addData(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void showError(Throwable e) {
|
|
|
+ smartRefreshlayout.finishRefresh();
|
|
|
+ smartRefreshlayout.finishLoadMore();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setCacheList(NewsBean bean, int topSize) {
|
|
|
+ ACache mACache = ACache.get(mContext);
|
|
|
+ mACache.put("cache_news_news_list", bean, ACache.TIME_DAY);
|
|
|
+ if (newsType == "news" && topSize != -1) {
|
|
|
+ this.topNum = topSize;
|
|
|
+ }
|
|
|
+ if (bean != null) {
|
|
|
+ showList(bean);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void showCacheList(Throwable e) {
|
|
|
+ ACache mACache = ACache.get(mContext);
|
|
|
+ NewsBean data = (NewsBean) mACache.getAsObject("cache_news_news_list");
|
|
|
+ if (data != null && adapter.getItemCount() == 0) {
|
|
|
+ showList(data);
|
|
|
+ } else {
|
|
|
+ showError(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void showRecommendBanner(List<RecommendBannerBean> bean) {
|
|
|
+ if(bean != null && bean.size()>0){
|
|
|
+ rl_live.setVisibility(View.VISIBLE);
|
|
|
+ tv_live_num.setText(mContext.getString(R.string.watch_live)+"("+bean.size()+")");
|
|
|
+
|
|
|
+ banner_headlines.setDatas(bean);
|
|
|
+ banner_headlines.setOnBannerListener((data, position) -> {
|
|
|
+ if(data instanceof RecommendBannerBean){
|
|
|
+ RecommendBannerBean b = (RecommendBannerBean) data;
|
|
|
+ if(b.getStatus() == 1){
|
|
|
+ CricketDetailActivity.forward(mContext, b.getId(),2);
|
|
|
+ }else{
|
|
|
+ CricketDetailActivity.forward(mContext, b.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ indicatorView.setCount(bean.size());
|
|
|
+ }else{
|
|
|
+ rl_live.setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void showRecommendGroup(List<MoreGroupBean.MoreGroupItem> bean) {
|
|
|
+ if(bean != null && bean.size()>0){
|
|
|
+ rl_group.setVisibility(View.VISIBLE);
|
|
|
+ groupAdapter.setNewData(bean);
|
|
|
+ }else{
|
|
|
+ rl_group.setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更改登录状态后要刷新列表数据
|
|
|
+ */
|
|
|
+ @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
+ public void refreshFocusOnData(NewsMsgEvent event) {
|
|
|
+/* if(TextUtils.isEmpty(event.newsType) && NewsActivity.tabPosition<2){
|
|
|
+ adapter.notifyDataSetChanged();
|
|
|
+ return;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ if (newsType.equals(event.newsType)) {
|
|
|
+ page = 1;
|
|
|
+ offset = 0;
|
|
|
+ presenter.getData(event.newsType, page, pagesize);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新本地的单条数据
|
|
|
+ */
|
|
|
+ @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
+ public void refreshItem(NewsItemEvent event) {
|
|
|
+ if (newsType.equals(event.newsType) && jumpPosition != -1) {
|
|
|
+ adapter.updateItemData(jumpPosition);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|