CoordinatorLayout与AppBarLayout实现顶部悬停效果

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wangxiaohuhu1314/article/details/79298847

效果图


1.0添加依赖

compile 'com.android.support:design:26.1.0'
1.布局代码
app:layout_scrollFlags="scroll|snap" 实现滑到一般自动滑动到顶端或者自动滑动到底部的功能

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--头部局-->
    <RelativeLayout
        android:id="@+id/topRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="@color/lightGray2_color">
        <RelativeLayout
            android:id="@+id/backRelativeLayout"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="3dp">
            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/btn_back"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                android:background="?android:attr/selectableItemBackgroundBorderless"
                android:clickable="false"
                />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/addfollowRelativeLayout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            >
            <TextView
                android:id="@+id/addfollowTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="互相关注"
                android:textColor="@color/cyanine_color"
                android:layout_centerVertical="true"
                android:layout_marginRight="15dp"
                android:layout_marginLeft="10dp"
                android:textSize="16sp" />
        </RelativeLayout>

    </RelativeLayout>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:fitsSystemWindows="true">
            <!--
            AppBarLayout里放入的都可以跟着向上滑动滑出布局的的,
            添加app:layout_scrollFlags="scroll"属性可以滑出布局
            TabLayout没有添加,所以停留在顶部
            -->
            <RelativeLayout
                app:layout_scrollFlags="scroll|snap"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/lightGray2_color">


                <!--头像-->
                <RelativeLayout
                    android:id="@+id/photoRelativeLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp">

                    <ImageView
                        android:id="@+id/photoImageView"
                        android:layout_width="64dp"
                        android:layout_height="64dp"
                        android:layout_centerInParent="true"
                        android:src="@drawable/user_small_photo_bg" />

                    <com.cccollector.news.view.RoundImageView
                        android:id="@+id/userhotoCircleImageView"
                        android:layout_width="62dp"
                        android:layout_height="62dp"
                        android:layout_centerInParent="true"
                        android:scaleType="centerCrop" />
                </RelativeLayout>

                <TextView
                    android:id="@+id/userNameTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="20dp"
                    android:layout_marginLeft="20dp"
                    android:layout_below="@id/photoRelativeLayout"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:ellipsize="end"
                    android:gravity="center"
                    android:maxLines="1"
                    android:text="账户名称"
                    android:textColor="@color/black_color"
                    android:textSize="18sp" />
                <LinearLayout
                    android:id="@+id/followAndfollowed"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_below="@id/userNameTextView"
                    >
                    <!--关注 -->
                    <RelativeLayout
                        android:id="@+id/followRelativeLayout"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="16dp"
                        android:layout_marginRight="18dp"
                        >

                        <TextView
                            android:id="@+id/followNumberTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="0"
                            android:textColor="@color/followTextView"
                            android:textSize="14sp"
                            android:layout_alignParentTop="true"
                            android:layout_centerHorizontal="true" />

                        <TextView
                            android:id="@+id/followTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/followNumberTextView"
                            android:layout_centerHorizontal="true"
                            android:text="关注"
                            android:layout_marginTop="2dp"
                            android:textColor="@color/black_color"
                            android:textSize="16sp" />
                    </RelativeLayout>
                    <!--粉丝 -->
                    <RelativeLayout
                        android:id="@+id/followedsRelativeLayout"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="16dp"
                        android:layout_marginLeft="18dp"
                        >

                        <TextView
                            android:id="@+id/followedsNumberTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerHorizontal="true"
                            android:text="0"
                            android:textColor="@color/followerTextView"
                            android:textSize="14sp" />

                        <TextView
                            android:id="@+id/followedsTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/followedsNumberTextView"
                            android:layout_centerHorizontal="true"
                            android:text="粉丝"
                            android:layout_marginTop="2dp"
                            android:textColor="@color/black_color"
                            android:textSize="16sp" />
                    </RelativeLayout>
                </LinearLayout>
                <!--简介-->
                <TextView
                    android:id="@+id/introTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/followAndfollowed"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginTop="14dp"
                    android:layout_centerHorizontal="true"
                    android:text=""
                    android:gravity="center_horizontal"
                    android:textColor="@color/black_color"
                    android:textSize="14sp" />
            </RelativeLayout>



            <RelativeLayout
                android:id="@+id/OptionRelativeLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white_color"
                >

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/lightGray2_color"
                    />

                <TextView
                    android:id="@+id/TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="动态"
                    android:textSize="14sp"
                    android:textColor="@color/grayish_color"
                    android:layout_marginTop="6dp"
                    android:layout_marginLeft="15dp"
                    />
                <LinearLayout
                    android:id="@+id/dynamicRelativeLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:layout_below="@id/TextView"
                    android:layout_centerHorizontal="true"
                    >
                    <LinearLayout
                        android:id="@+id/commentLinearLayout"
                        android:layout_width="38dp"
                        android:layout_height="25dp"
                        android:orientation="vertical"

                        >
                        <TextView
                            android:id="@+id/commentTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="评论"
                            android:textSize="16sp"
                            android:layout_gravity="center_horizontal"
                            android:textColor="@color/caesious_color"
                            />
                        <View
                            android:id="@+id/commentView"
                            android:layout_width="38dp"
                            android:layout_height="2dp"
                            android:layout_marginTop="1dp"
                            android:background="@color/cyanine_color"
                            android:layout_alignParentBottom="true"
                            android:layout_gravity="center_horizontal"

                            />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/replieLinearLayout"
                        android:layout_width="38dp"
                        android:layout_height="25dp"
                        android:orientation="vertical"
                        android:layout_marginLeft="23dp"
                        android:layout_marginRight="23dp"
                        >
                        <TextView
                            android:id="@+id/replieTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="回复"
                            android:textSize="16sp"
                            android:layout_gravity="center_horizontal"
                            android:textColor="@color/caesious_color"
                            />
                        <View
                            android:id="@+id/replieView"
                            android:layout_width="38dp"
                            android:layout_height="2dp"
                            android:layout_marginTop="1dp"
                            android:background="@color/cyanine_color"
                            android:layout_alignParentBottom="true"
                            android:layout_gravity="center_horizontal"
                            android:visibility="gone"

                            />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/likeLinearLayout"
                        android:layout_width="38dp"
                        android:layout_height="25dp"
                        android:orientation="vertical"
                        >
                        <TextView
                            android:id="@+id/likeTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="赞"
                            android:textSize="16sp"
                            android:layout_gravity="center_horizontal"
                            android:textColor="@color/caesious_color"
                            />
                        <View
                            android:id="@+id/likeView"
                            android:layout_width="38dp"
                            android:layout_height="2dp"
                            android:layout_marginTop="1dp"
                            android:background="@color/cyanine_color"
                            android:layout_alignParentBottom="true"
                            android:layout_gravity="center_horizontal"
                            android:visibility="gone"

                            />
                    </LinearLayout>

                </LinearLayout>
                <View
                    style="@style/mview"
                    android:id="@+id/view"
                    android:layout_below="@id/dynamicRelativeLayout"
                    android:layout_marginTop="2dp"/>

                <TextView
                    android:id="@+id/hintTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="此用户设置了隐藏个人动态信息"
                    android:textSize="16sp"
                    android:textColor="@color/grayish_color"
                    android:layout_marginTop="15dp"
                    android:layout_centerHorizontal="true"
                    android:layout_below="@id/view"
                    android:visibility="gone"
                    />
            </RelativeLayout>
          />

        </android.support.design.widget.AppBarLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:background="@color/white_color"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </android.support.v4.view.ViewPager>
        </RelativeLayout>

    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>
2.顶部布局的颜色渐变 
public class PersonalHomepageActivity extends BaseActivity {

    private TextView commentTextView;
    private TextView replieTextView;
    private TextView likeTextView;
    private View commentView;
    private View replieView;
    private View likeView;
    private int userAppIdIntent;
    private ViewPager viewPager;
    private TextView addfollowTextView;
    private RoundImageView userhotoCircleImageView;
    private TextView userNameTextView;
    private RelativeLayout followRelativeLayout;
    private TextView followNumberTextView;
    private RelativeLayout followedsRelativeLayout;
    private TextView followedsNumberTextView;
    private TextView introTextView;
    private RelativeLayout addfollowRelativeLayout;
    private com.cccollector.news.model.UserApp userAppUnlogged;
    private User user;
    private TextView hintTextView;
    private int mScrollY = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_personalhomepage_activity);
        //注册EventBus
        EventBus.getDefault().register(this);
        RelativeLayout backRelativeLayout = (RelativeLayout) findViewById(R.id.backRelativeLayout);//返回按钮
        //添加关注
        addfollowRelativeLayout = (RelativeLayout) findViewById(R.id.addfollowRelativeLayout);
        addfollowTextView = (TextView) findViewById(R.id.addfollowTextView);
        //用户头像
        userhotoCircleImageView = (RoundImageView) findViewById(R.id.userhotoCircleImageView);
        //用户名称
        userNameTextView = (TextView) findViewById(R.id.userNameTextView);
        followRelativeLayout = (RelativeLayout) findViewById(R.id.followRelativeLayout);
        //关注数
        followNumberTextView = (TextView) findViewById(R.id.followNumberTextView);
        followedsRelativeLayout = (RelativeLayout) findViewById(R.id.followedsRelativeLayout);
        //粉丝数
        followedsNumberTextView = (TextView) findViewById(R.id.followedsNumberTextView);
        //个人简介
        introTextView = (TextView) findViewById(R.id.introTextView);
        hintTextView = (TextView) findViewById(R.id.hintTextView);


        viewPager= (ViewPager) findViewById(R.id.viewpager);
        LinearLayout commentLinearLayout = (LinearLayout) findViewById(R.id.commentLinearLayout);//评论
        LinearLayout replieLinearLayout = (LinearLayout) findViewById(R.id.replieLinearLayout);//回复
        LinearLayout likeLinearLayout = (LinearLayout) findViewById(R.id.likeLinearLayout);//赞

        commentTextView = (TextView) findViewById(R.id.commentTextView);
        replieTextView = (TextView) findViewById(R.id.replieTextView);
        likeTextView = (TextView) findViewById(R.id.likeTextView);

        commentView = (View) findViewById(R.id.commentView);
        replieView = (View) findViewById(R.id.replieView);
        likeView = (View) findViewById(R.id.likeView);

        //退出本页面
        backRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.transparent_out, R.anim.activity_out_right);
            }
        });

        //接收哪个界面进入到此页面
        Intent intent = getIntent();
        userAppIdIntent = intent.getIntExtra("PersonalHomepageUserAppId", -1);

        if(UserAppApiClient.getInstance().isLogged()){
            //从用户平台获取已登录用户应用UserApp
            UserApp userAppLoged = UserAppApiClient.getInstance().getLogin().getUserApp();
            int userAppIdLoged = userAppLoged.getUserAppId();

            //从新闻平台获取已登录用户应用UserApp
            com.cccollector.news.model.UserApp userApp = com.cccollector.news.api.UserAppApiClient.getInstance().getUserApp();

            //防止出现同步用户数据失败
            if ( userApp == null ) return;

            if ( userAppIdLoged == userAppIdIntent){
                logedUserData();
            }else {
                //别人的页面
                addfollowRelativeLayout.setVisibility(View.VISIBLE);
                //获取已登录用户的关注
                Map<Integer, Follow> follows = userApp.getFollows();
                Follow follow = follows.get(userAppIdIntent);
                if ( follow != null ){
                    Boolean mutual = follow.getMutual();
                    addfollowTextView.setText( mutual == true ? "互相关注" : "已关注");
                    addfollowTextView.setTextColor(ContextCompat.getColor(getApplication(), R.color.caesious_color));
                    addfollowRelativeLayout.setEnabled(false);
                }else {
                    addfollowTextView.setText("+ 关注");
                    addfollowTextView.setTextColor(ContextCompat.getColor(getApplication(), R.color.cyanine_color));
                    addfollowRelativeLayout.setEnabled(true);
                }
                unLogedUserData(userAppIdIntent);
            }
        }else {
            //未登录进入别人的页面
            addfollowRelativeLayout.setVisibility(View.VISIBLE);
            addfollowTextView.setText("+ 关注");
            addfollowTextView.setTextColor(ContextCompat.getColor(getApplication(), R.color.cyanine_color));
            addfollowTextView.setClickable(false);
            unLogedUserData(userAppIdIntent);
        }


        Bundle bundle = new Bundle();
        bundle.putInt("PersonalHomepageUserAppId", userAppIdIntent);

        List<Fragment> fragments = new ArrayList<>();
        PersonalHomepageCommentFragment personalHomepageCommentFragment = new PersonalHomepageCommentFragment();
        personalHomepageCommentFragment.setArguments(bundle);

        PersonalHomepageReplieFragment personalHomepageReplieFragment = new PersonalHomepageReplieFragment();
        personalHomepageReplieFragment.setArguments(bundle);

        PersonalHomepageLikeFragment personalHomepageLikeFragment = new PersonalHomepageLikeFragment();
        personalHomepageLikeFragment.setArguments(bundle);

        fragments.add(personalHomepageCommentFragment);
        fragments.add(personalHomepageReplieFragment);
        fragments.add(personalHomepageLikeFragment);

        PersonalHomepageAdapter personalHomepageAdapter = new PersonalHomepageAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(personalHomepageAdapter);


        AppBarLayout appBarLayout = findViewById(R.id.appBarLayout);
        final RelativeLayout topRelativeLayout = findViewById(R.id.topRelativeLayout);
        //顶部布局颜色的改变,当滑动到顶部的时候,颜色为白色,当滑动到底部的时候,颜色为灰色
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                //appBarLayout.getTotalScrollRange()
                if (verticalOffset == 0){
                    topRelativeLayout.setBackgroundColor(getResources().getColor(R.color.lightGray2_color));
                }else if (Math.abs(verticalOffset) > 800){
                    topRelativeLayout.setBackgroundColor(getResources().getColor(R.color.white_color));
                }
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updateTabs(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //评论的点击事件
        commentLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateTabs(PersonalHomepageActivity3.option.评论.getPosition());
                viewPager.setCurrentItem(PersonalHomepageActivity3.option.评论.getPosition(), false);
            }
        });

        //回复的点击事件
        replieLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateTabs(PersonalHomepageActivity3.option.回复.getPosition());
                viewPager.setCurrentItem(PersonalHomepageActivity3.option.回复.getPosition(), false);
            }
        });

        //赞的点击事件
        likeLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateTabs(PersonalHomepageActivity3.option.赞.getPosition());
                viewPager.setCurrentItem(PersonalHomepageActivity3.option.赞.getPosition(), false);
            }
        });

        //关注的点击事件
        followRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent followIntent = new Intent(PersonalHomepageActivity.this, UserAttentionFansActivity.class);
                followIntent.putExtra("PersonalHomepageUserAppId", userAppIdIntent);
                followIntent.putExtra("sign", "关注");
                startActivity(followIntent);
            }
        });

        //粉丝的点击事件
        followedsRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent followeds = new Intent(PersonalHomepageActivity.this, UserAttentionFansActivity.class);
                followeds.putExtra("PersonalHomepageUserAppId", userAppIdIntent);
                followeds.putExtra("sign", "粉丝");
                startActivity(followeds);
            }
        });

        //添加
        addfollowRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserAppApiClient.getInstance().isLogged()) {

                    //从新闻平台获取已登录用户应用UserApp
                    com.cccollector.news.api.UserAppApiClient.getInstance().addLoggedUserAppFollow(userAppIdIntent, new com.cccollector.news.api.UserAppApiClient.CompletionHandler() {
                        @Override
                        public void onComplete(Object object) {
                            SystemUtil.runOnMainThread(new SystemUtil.MainThreadHandler() {
                                @Override
                                public void onMainThread() {
                                    //从新闻平台获取已登录用户应用UserApp
                                    com.cccollector.news.model.UserApp userApp = com.cccollector.news.api.UserAppApiClient.getInstance().getUserApp();
                                    //获取已登录用户的关注
                                    Map<Integer, Follow> follows = userApp.getFollows();
                                    final Follow follow = follows.get(userAppIdIntent);
                                    if ( follow != null ){
                                        Boolean mutual = follow.getMutual();
                                        addfollowTextView.setText( mutual == true ? "相互关注" : "已关注");
                                        addfollowTextView.setTextColor(ContextCompat.getColor(getApplication(), R.color.caesious_color));
                                        addfollowRelativeLayout.setEnabled(false);
                                    }
                                    Config.showShortToast("添加成功");
                                }
                            });
                        }

                        @Override
                        public void onError(Throwable e, Integer errorCode, String errorMessage) {
                            SystemUtil.runOnMainThread(new SystemUtil.MainThreadHandler() {
                                @Override
                                public void onMainThread() {
                                    addfollowTextView.setText( "+ 关注");
                                    addfollowTextView.setTextColor(ContextCompat.getColor(getApplication(), R.color.cyanine_color));
                                    addfollowRelativeLayout.setEnabled(true);
                                    Config.showShortToast("添加失败");
                                }
                            });
                        }
                    });
                }else {
                    //如果没有登录进入登录界面
                    Intent intent = new Intent(SystemUtil.getApplication(), UserLoginOrRegisterActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.activity_bottom_in, R.anim.transparent_out);
                }
            }
        });
    }

    private void updateTabs(int position) {
        if (position == option.评论.getPosition()){
            commentView.setVisibility(View.VISIBLE);
            replieView.setVisibility(View.GONE);
            likeView.setVisibility(View.GONE);
        }

        if (position == option.回复.getPosition()){
            commentView.setVisibility(View.GONE);
            replieView.setVisibility(View.VISIBLE);
            likeView.setVisibility(View.GONE);
        }

        if (position == option.赞.getPosition()){
            commentView.setVisibility(View.GONE);
            replieView.setVisibility(View.GONE);
            likeView.setVisibility(View.VISIBLE);
        }

        updateTab(position, option.评论.getPosition(), commentTextView);
        updateTab(position, option.回复.getPosition(), replieTextView);
        updateTab(position, option.赞.getPosition(), likeTextView);

    }

    //修改文字颜色
    private void updateTab(int position, int tabPosition, TextView tab) {
        if (position == tabPosition) {
            tab.setTextColor(ContextCompat.getColor(getApplication(), R.color.cyanine_color));
        } else {
            tab.setTextColor(getResources().getColor(R.color.caesious_color));
        }
    }

    public static   enum  option {
        评论(0),
        回复(1),
        赞(2);

        private int _position;
        option(int position) {
            this._position = position;
        }

        public int getPosition() {
            return _position;
        }
    }

    //未登录用户设置数据
    private void unLogedUserData(final int userAppIdIntent) {

        //从用户平台获取未登录用户数据
        UserAppApiClient.getInstance().updateUserApp(userAppIdIntent, new UserAppApiClient.CompletionHandler() {

            @Override
            public void onComplete(Object object) {
                //根据用户Id从用户平台获取未登录用户数据
                SystemUtil.runOnMainThread(new SystemUtil.MainThreadHandler() {
                    @Override
                    public void onMainThread() {
                        final UserApp noLogedUserApp = UserAppApiClient.getInstance().userApp(userAppIdIntent);
                        user = noLogedUserApp.getUser();
                        //设置用户名称
                        userNameTextView.setText(user.getNickName());
                        // 设置用户头像
                        File avatarFile = user.avatarFile(User.AvatarEnum.方图);
                        if (avatarFile.exists()){
                            Bitmap bitma = SystemUtil.getBitmap(avatarFile);
                            userhotoCircleImageView.setImageBitmap(bitma);
                        }else {
                            //下载用户头像
                            UserApiClient.getInstance().downloadUserAvatar(user);
                        }
                    }
                });

            }
            @Override
            public void onError(Throwable e, Integer errorCode, String errorMessage) {

            }
        });

        com.cccollector.news.api.UserAppApiClient.getInstance().getUserAppByUserAppId(userAppIdIntent, new com.cccollector.news.api.UserAppApiClient.CompletionHandler() {
            @Override
            public void onComplete(Object object) {
                SystemUtil.runOnMainThread(new SystemUtil.MainThreadHandler() {
                    @Override
                    public void onMainThread() {
                        //根据用户Id从新闻平台获取未登录用户数据
                        userAppUnlogged = com.cccollector.news.api.UserAppApiClient.getInstance().getUserAppByUnloggedUserData(userAppIdIntent);
                        //关注数
                        followNumberTextView.setText(userAppUnlogged.getFollowCount()+"");
                        //粉丝数
                        followedsNumberTextView.setText(userAppUnlogged.getFollowerCount()+"");
                        //个人简介
                        String intro = userAppUnlogged.getIntro();
                        if(intro != null){
                            String s = SystemUtil.unicodeConvertString(intro);
                            introTextView.setText(s);
                        }else {
                            introTextView.setText("好像忘记写签名了");
                        }
                    }
                });
            }

            @Override
            public void onError(Throwable e, Integer errorCode, String errorMessage) {
                String s = errorMessage.toString();
            }
        });
    }

    //已登录用户设置数据
    private void logedUserData() {

        //自己的页面
        addfollowRelativeLayout.setVisibility(View.GONE);

        //从用户平台获取已登录用户应用UserApp
        UserApp userAppLoged = UserAppApiClient.getInstance().getLogin().getUserApp();
        //从新闻平台获取已登录用户应用UserApp
        com.cccollector.news.model.UserApp userApp = com.cccollector.news.api.UserAppApiClient.getInstance().getUserApp();

        //防止出现同步用户数据失败
        if ( userApp == null ) return;

        // 设置用户头像
        File avatarFile = userAppLoged.getUser().avatarFile(User.AvatarEnum.方图);
        if (avatarFile.exists()) {
            Bitmap bitma = SystemUtil.getBitmap(avatarFile);
            userhotoCircleImageView.setImageBitmap(bitma);
        }

        //设置用户名称
        userNameTextView.setText(userAppLoged.getUser().getNickName());
        //关注数
        followNumberTextView.setText(userApp.getFollowCount()+"");
        //粉丝数
        followedsNumberTextView.setText(userApp.getFollowerCount()+"");
        //个人简介
        String intro = userApp.getIntro();
        if(intro != null){
            String s = SystemUtil.unicodeConvertString(intro);
            introTextView.setText(s);
        }else {
            introTextView.setText("好像忘记写签名了");
        }

    }

    /**
     * 响应用户完成登录事件
     */
    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onUserDidLoginEvent(final UserApiClient.UserDidLoginEvent event) {
        //登录完成刷新已登录用户数据
        logedUserData();
    }

    /**
     * 获取用户头像成功
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onLoggedUserDidLogoutEvent(UserApiClient.UserAvatarDidDownloadEvent event) {
        User muser = event.getUser();
        if (user != null){
            int userId = user.getUserId();
            if (userId == muser.getUserId()){
                File file = user.avatarFile(User.AvatarEnum.方图);
                if (file.exists()){
                    // 设置用户头像
                    Bitmap bitma = SystemUtil.getBitmap(file);
                    userhotoCircleImageView.setImageBitmap(bitma);
                }
            }
        }
    }

    /**
     * 退出动画
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.transparent_out, R.anim.activity_out_right);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解注册
        EventBus.getDefault().unregister(this);
    }


}




展开阅读全文

没有更多推荐了,返回首页