效果图
1.0添加依赖
compile 'com.android.support:design:26.1.0'
/>
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 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 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 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);
}
}