Activity嵌套ViewPager+Fragment

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhenbao.orange.adnroid.BaseActivity;
import com.zhenbao.orange.entity.Tab;
import com.zhenbao.orange.fragments.PersonalPraiseFragment1;
import com.zhenbao.orange.fragments.PersonalPraiseFragment2;
import com.zhenbao.orange.fragments.PersonalPraiseFragment3;
import com.zhenbao.orange.im.R;
import com.zhenbao.orange.utils.LocalStorage;
import com.zhenbao.orange.view.FragmentTabHost;

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

import butterknife.BindView;
import butterknife.OnClick;

public class PersonalPraiseActivity extends BaseActivity implements ViewPager.OnPageChangeListener{
//    @BindView(R.id.toolbar_bar)
//    RelativeLayout toolbarBar;
//    @BindView(R.id.toolbar_title)
//    TextView title;
//    //    android:id="@+id/toolbar"/>

    private int index = 0;
    @BindView(R.id.personal_praise_viewpage)
    ViewPager viewPager;
    private FragmentTabHost mTabhost;
    private List mTabs = new ArrayList<>();
    private LayoutInflater mInflater;
    private List fragments;
    PersonalPraiseActivity.MyAdapter my;
    @BindView(R.id.personal_praise_left)
    TextView left;
    @BindView(R.id.personal_praise_right)
    TextView right;
    @BindView(R.id.personal_praise_right1)
    TextView right1;

    @BindView(R.id.toolbar_sub_title)
    TextView title_sub;
    @BindView(R.id.toolbar_bar)
    RelativeLayout toolbarBar;//toolbar_back
    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.toolbar_back)
    ImageView toolbar_back;
    @Override
    protected int setContentView() {
        return R.layout.activity_personal_praise;
    }

    @Override
    protected void initView() {
        String wo=getIntent().getStringExtra("wo");
        System.out.println("wwwwwwww:="+wo);
        setStatusBar(toolbarBar);
        title.setText("点赞");
        title_sub.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
        title_sub.setText("点赞规则");
        fragments = new ArrayList();
        fragments.add(new PersonalPraiseFragment1());
        fragments.add(new PersonalPraiseFragment2());
        fragments.add(new PersonalPraiseFragment3());
        if (LocalStorage.get("gender").equals("1")){//如果是男生
            right1.setText("我的女神");
        }else {//否则,如果是女生
            right1.setText("我的铁粉");
//            right1.setVisibility(View.GONE);
        }
        my = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(my);
        // 滑动事件
        viewPager.setOnPageChangeListener(this);
        if (!TextUtils.isEmpty(wo)){
            viewPager.setCurrentItem(1);
        }

    }

    @OnClick({R.id.personal_praise_left, R.id.personal_praise_right,R.id.toolbar_back,R.id.personal_praise_right1,R.id.toolbar_sub_title})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_sub_title:
                startActivity(new Intent(PersonalPraiseActivity.this,PraiseRule.class));
                break;
            case R.id.toolbar_back:
                finishA();
                break;
            case R.id.personal_praise_left:
                index = 0;
                left.setTextColor(getResources().getColor(R.color.orange));
                right.setTextColor(getResources().getColor(R.color.black));
                right1.setTextColor(getResources().getColor(R.color.black));
                break;
            case R.id.personal_praise_right:
                right1.setTextColor(getResources().getColor(R.color.black));
                left.setTextColor(getResources().getColor(R.color.black));
                right.setTextColor(getResources().getColor(R.color.orange));
                index = 1;
                break;
            case R.id.personal_praise_right1:
                right1.setTextColor(getResources().getColor(R.color.orange));
                left.setTextColor(getResources().getColor(R.color.black));
                right.setTextColor(getResources().getColor(R.color.black));
                index = 2;
                break;
        }
        viewPager.setCurrentItem(index);
    }
    private class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int arg0) {

            return fragments.get(arg0);
        }

        @Override
        public int getCount() {

            return fragments.size();
        }

    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {
                Toast.makeText(PersonalPraiseActivity.this, "需要允许写入权限来存储图片", Toast.LENGTH_SHORT).show();
            }
        }
        if(Build.VERSION.SDK_INT>=23){
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,
                    Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,
                    Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,
                    Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this,mPermissionList,123);
        }
    }

    private void requestPermission() {
        if (ContextCompat.checkSelfPermission(PersonalPraiseActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            System.out.println(1);
            ActivityCompat.requestPermissions(PersonalPraiseActivity.this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
                    100);
        } else {
            System.out.println(2);

        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (position==0){
            left.setTextColor(getResources().getColor(R.color.orange));
            right.setTextColor(getResources().getColor(R.color.black));
            right1.setTextColor(getResources().getColor(R.color.black));
        }else if (position==1){
            left.setTextColor(getResources().getColor(R.color.black));
            right.setTextColor(getResources().getColor(R.color.orange));
            right1.setTextColor(getResources().getColor(R.color.black));
        }else{
            left.setTextColor(getResources().getColor(R.color.black));
            right.setTextColor(getResources().getColor(R.color.black));
            right1.setTextColor(getResources().getColor(R.color.orange));
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}

你可能感兴趣的:(android,项目总结)