package com.sheep.gamegroup.view.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sheep.gamegroup.view.adapter.MessageAdapter;
import com.sheep.jiuyan.samllsheep.R;


/**
 * created on：2018/8/28 on 11:44
 * created by: YSL
 * 描述:消息Activity，展示消息中心和互动消息两个模块
 */
public class ActivityMessages extends AppCompatActivity {
    private String[] mTitle = new String[]{"消息中心", "回复互动"};
    private TabLayout tabLayout = null;
    private ViewPager viewPager = null;


    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messages);
        viewPager = findViewById(R.id.message_view_pager_grd);
        tabLayout = findViewById(R.id.tab_layout);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                for (int i = 0; i < tabLayout.getTabCount(); i++) {
                    View tabView = tabLayout.getTabAt(i).getCustomView();
                    if (tabView == null) {
                        tabView = getCustomView(i);
                        tabLayout.getTabAt(i).setCustomView(tabView);
                    }
                    TextView tvtable = tabView.findViewById(R.id.tv_message);
                    ImageView ivtable = tabView.findViewById(R.id.iv_message);
                    if (i == tab.getPosition()) {
                        tvtable.setTextColor(Color.parseColor("#ff333333"));
                    } else {
                        tvtable.setTextColor(Color.parseColor("#ff666666"));
                    }
                    tvtable.setText(mTitle[i]);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        viewPager.setAdapter(new MessageAdapter(getSupportFragmentManager(), this));
        tabLayout.setupWithViewPager(viewPager);
        //返回
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityMessages.this.finish();
            }
        });
    }

    /**
     * 、
     * 设置每个tab的view，可根据position的不同，针对性的对每个tab做处理，这里消息互动和消息中心的tab布局一样，所以就统一设置
     *
     * @param position tab的位置
     * @return ，tab的view
     */
    private View getCustomView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_fragment_mseeages, null, false);
        return view;
    }
}

