PublishTrendActivity.java 9.59 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7
package com.yidianling.dynamic.publishTrend;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
YKai committed
8 9 10
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
konghaorui committed
11 12 13 14 15 16 17
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
konghaorui committed
18
import android.widget.LinearLayout;
konghaorui committed
19 20 21 22 23 24 25 26 27 28
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.ydl.ydlcommon.base.BaseActivity;
import com.ydl.ydlcommon.base.config.YDLConstants;
import com.ydl.ydlcommon.utils.SharedPreferencesEditor;
konghaorui committed
29
import com.ydl.ydlcommon.utils.StatusBarUtils;
konghaorui committed
30
import com.ydl.ydlcommon.view.NoScrollViewPager;
31
import com.yidianling.dynamic.bean.DynamicConstants;
konghaorui committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import com.yidianling.dynamic.R;
import com.yidianling.dynamic.bean.TrendTabEntity;
import com.yidianling.dynamic.publishTrend.fragment.PublishQuestionAndTrendFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * 发布动态
 */
@Route(path = "/trend/publish")
public class PublishTrendActivity extends BaseActivity {
    private static final String URL = "Test_url";
    private static final String COVER = "Test_cover";
    private static final String TITLE = "Test_title";
    private static final String ISFROMFM = "isfromfm";
    private ImageView mBackIv;
    private TextView mToastView;
    private NoScrollViewPager mViewPager;
    private CommonTabLayout mTableLayout;
    //    private RelativeLayout mNoticeLayout;
    private ArrayList<CustomTabEntity> titleList = new ArrayList<>();
    private List<Fragment> fragmentList = new ArrayList<>();
    private PopupWindow noticePopupWindow;
    private Handler handler;
konghaorui committed
58
    private LinearLayout mPublishTrendTitle;
konghaorui committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    public static Intent newIntent(Activity activity, String url, String cover, String title) {
        Intent intent = new Intent(activity, PublishTrendActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(URL, url);
        bundle.putString(COVER, cover);
        bundle.putString(TITLE, title);
        intent.putExtra(YDLConstants.BUNDLE, bundle);
        return intent;
    }


    public static Intent newIntentForArticle(Activity activity, String url, String cover, String title) {
        Intent intent = new Intent(activity, PublishTrendActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(URL, url);
        bundle.putString(COVER, cover);
        bundle.putString(TITLE, title);
        bundle.putString(DynamicConstants.PUBLISH_TYPE, "topic_publish");
        bundle.putString("topic_id", "44");
        bundle.putString("topic_title", "今日打卡");
        intent.putExtra(YDLConstants.BUNDLE, bundle);
        return intent;
    }


    public static Intent newIntentForFm(Activity activity, String url, String cover, String title, int isFromFm) {
        Intent intent = new Intent(activity, PublishTrendActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(URL, url);
        bundle.putString(COVER, cover);
        bundle.putString(TITLE, title);
        bundle.putInt(ISFROMFM, isFromFm);
        bundle.putString(DynamicConstants.PUBLISH_TYPE, "topic_publish");
        bundle.putString("topic_id", "44");
        bundle.putString("topic_title", "今日打卡");
        intent.putExtra(YDLConstants.BUNDLE, bundle);
        return intent;
    }

    @Override
    protected int layoutResId() {
konghaorui committed
101
        return R.layout.dynamic_activity_trend;
konghaorui committed
102 103 104 105 106 107 108
    }

    @Override
    protected void initDataAndEvent() {
        SharedPreferencesEditor.putString("trend_state", "");
        handler = new Handler();
        mBackIv = findViewById(R.id.img_back);
konghaorui committed
109
        mPublishTrendTitle= findViewById(R.id.publish_trend_title_rel);
konghaorui committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        mBackIv.setOnClickListener(v -> {

            finish();
        });
        //发布按钮
        findViewById(R.id.publish_trend_sure_tv).setOnClickListener(v -> {
            if (fragmentList.get(mViewPager.getCurrentItem()) != null) {
                ((PublishQuestionAndTrendFragment) fragmentList.get(mViewPager.getCurrentItem())).publish();
            }
        });
        mViewPager = findViewById(R.id.view_pager);
        mViewPager.setScanScroll(true);
        mViewPager.setOffscreenPageLimit(2);
        mTableLayout = findViewById(R.id.tab_layout);
        mToastView = findViewById(R.id.tv_coustom_toast_view);
        initData();
konghaorui committed
126
        StatusBarUtils.Companion.setCustomStatusView(this,mPublishTrendTitle);
konghaorui committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    }

    private void initData() {
        titleList.add(new TrendTabEntity("动态", 0, 0));
        titleList.add(new TrendTabEntity("提问", 0, 0));
        mTableLayout.setTabData(titleList);

        fragmentList.add(PublishQuestionAndTrendFragment.getInstance(PublishQuestionAndTrendFragment.TYPE_TREND));
        fragmentList.add(PublishQuestionAndTrendFragment.getInstance(PublishQuestionAndTrendFragment.TYPE_QUESTION));

        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        mViewPager.setAdapter(adapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (mTableLayout.getCurrentTab() != position) {
                    mTableLayout.setCurrentTab(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mTableLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (mViewPager.getCurrentItem() != position) {
                    mViewPager.setCurrentItem(position);
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        handler.postDelayed(() -> showNoticeView(), 300);
    }

    /**
     * 展示提问页面的提示窗
     */
    private void showNoticeView() {
        if (mViewPager.getCurrentItem() == 1) {
            //当前已经是提问页面了
            return;
        }

        TextView textView = mTableLayout.getTitleView(1);
        textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int textViewWidth = textView.getMeasuredWidth();
konghaorui committed
195
        View view = LayoutInflater.from(this).inflate(R.layout.dynamic_popup_notice, null);
konghaorui committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int wid = view.getMeasuredWidth();
        noticePopupWindow = new PopupWindow(view);
        noticePopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        noticePopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置点击空白处消失
        noticePopupWindow.setTouchable(true);
        noticePopupWindow.setOutsideTouchable(true);
        noticePopupWindow.setBackgroundDrawable(new BitmapDrawable());
        noticePopupWindow.showAsDropDown(textView, textViewWidth / 2 - wid / 2, -15);
        handler.postDelayed(() -> {
            if (noticePopupWindow != null) {
                noticePopupWindow.dismiss();
            }
        }, 1000);
    }

    /**
     * 显示页面自定的提示
     *
     * @param msg
     */
    public void showCoustomToast(String msg) {
        if (mToastView == null) {
            return;
        }
        mToastView.setText(msg);
        if (mToastView.getVisibility() != View.VISIBLE) {
            TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, -(mToastView.getMeasuredHeight() / 4), 0);
            translateAnimation.setDuration(1000);
            mToastView.setVisibility(View.VISIBLE);
            mToastView.startAnimation(translateAnimation);
            mToastView.postDelayed(() -> {
                if (mToastView == null || mToastView.getVisibility() == View.GONE) {
                    return;
                }
                Animation animation = new AlphaAnimation(1f, 0f);
                animation.setDuration(1000);
                mToastView.setAnimation(animation);
                mToastView.setVisibility(View.GONE);
            }, 1000);
        }
    }

    @Override
    public void onBackPressed() {
        if (fragmentList.get(mViewPager.getCurrentItem()) != null) {
            if (((PublishQuestionAndTrendFragment) fragmentList.get(mViewPager.getCurrentItem())).getEmojiLayoutIsShow()) {
                ((PublishQuestionAndTrendFragment) fragmentList.get(mViewPager.getCurrentItem())).hideEmojiLayout();
            } else {
                super.onBackPressed();
            }
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除任务
        if (null != handler) {
            handler.removeCallbacksAndMessages(null);
        }
    }
}