这里先搭建viewpage+BottomNavigationView+Fragment的底部导航栏
activity_tabhost.xml
fragment_friends.xml
fragment_message.xml
fragment_mine.xml
ViewPagerFragmentAdapter
package heath.com.chat.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewPagerFragmentAdapter extends FragmentPagerAdapter {
private Fragment[] fragments;
public ViewPagerFragmentAdapter(FragmentManager fm, Fragment[] fragments) {
super(fm);
this.fragments=fragments;
}
@Override
public Fragment getItem(int position) {
return fragments[position];
}
@Override
public int getCount() {
return fragments.length;
}
}
TabHostActivity
package heath.com.chat;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import heath.com.chat.adapter.ViewPagerFragmentAdapter;
import heath.com.chat.friend.AddFriendsActivity;
import heath.com.chat.friend.FriendsFragment;
import heath.com.chat.message.MessageFragment;
import heath.com.chat.mine.MineFragment;
public class TabHostActivity extends BaseActivity implements View.OnClickListener {
private ImageView mIvAdd;
private BottomNavigationView navigationView;
private ViewPager viewPager;
private Fragment[] fragments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabhost);
//初始化控件
initView();
initListener();
//将各个fragment加入到fragments中
addFragment();
//将viewPager和BottomNavigationView的选择事件连接起来
link();
//设置viewPager的适配器
ViewPagerFragmentAdapter adapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(adapter);
//设置初始的页面项
navigationView.setSelectedItemId(R.id.navigation_message);
}
/**
* 初始化控件
*/
private void initView() {
mIvAdd = this.findViewById(R.id.iv_add);
navigationView = this.findViewById(R.id.bnv_viewpager);
viewPager = this.findViewById(R.id.vp_viewpager);
//将控件默认的图标隐藏,以显示自定义的按钮图标
navigationView.setItemIconTintList(null);
}
private void initListener() {
mIvAdd.setOnClickListener(this);
}
/**
* 填充fragments
*/
private void addFragment() {
fragments = new Fragment[]{
new MessageFragment(),
new FriendsFragment(),
new MineFragment()
};
}
/**
* 将viewPager和BottomNavigationView的事件连接起来
* 分为两步
* 1.给viewPager添加监听事件,即viewPager的页面变化时,navigation的选中项跟随变化
* 2.给navigation添加监听事件,即当navigation的选中项变化时,viewPager的页面跟随变化
*/
private void link() {
//给viewPager设置监听事件,使viewPager页面变化时BottomNavigationView的item跟随其变化
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//以下三个方法是需要重写的父类方法
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
////当页面滑动,即当前页面滑动时调用
}
@Override
public void onPageSelected(int position) {
//当viewpager选定(滑动)后调用
switch (position) {
case 0:
navigationView.setSelectedItemId(R.id.navigation_message);
break;
case 1:
navigationView.setSelectedItemId(R.id.navigation_friends);
break;
case 2:
navigationView.setSelectedItemId(R.id.navigation_mine);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
//当滚动状态发生变化时,比如从滚动变到不滚动时调用
}
});
//配置viewPager的页面随navigation的选中项变化
navigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//每次点击后都将所有图标重置到默认不选中图片
resetToDefaultIcon();
switch (item.getItemId()) {
case R.id.navigation_message:
viewPager.setCurrentItem(0);
//设置按钮的图标
item.setIcon(R.drawable.select_message);
return true;
case R.id.navigation_friends:
viewPager.setCurrentItem(1);
//设置按钮的图标
item.setIcon(R.drawable.select_friends);
return true;
case R.id.navigation_mine:
viewPager.setCurrentItem(2);
//设置按钮的图标
item.setIcon(R.drawable.select_mine);
return true;
}
return false;
}
});
}
/**
* 重置按钮的图片为未选中的图标
*/
private void resetToDefaultIcon() {
navigationView.getMenu().findItem(R.id.navigation_message).setIcon(R.drawable.unselect_message);
navigationView.getMenu().findItem(R.id.navigation_friends).setIcon(R.drawable.unselect_friends);
navigationView.getMenu().findItem(R.id.navigation_mine).setIcon(R.drawable.unselect_mine);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_add:
startActivity(new Intent(TabHostActivity.this, AddFriendsActivity.class));
break;
default:
break;
}
}
}
FriendsFragment
package heath.com.chat.friend;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.friend.FriendService;
import com.netease.nimlib.sdk.uinfo.UserService;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
import com.netease.nimlib.sdk.uinfo.model.UserInfo;
import java.util.HashMap;
import java.util.List;
import heath.com.chat.R;
import heath.com.chat.adapter.FriendAdapter;
public class FriendsFragment extends Fragment {
private RecyclerView mRvFriends;
private FriendAdapter friendAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_friends, container,
false);
initView(view);
init();
return view;
}
private void initView(View view) {
mRvFriends = view.findViewById(R.id.rv_friends);
}
private void init(){
List friends = NIMClient.getService(FriendService.class).getFriendAccounts();
NIMClient.getService(UserService.class).fetchUserInfo(friends)
.setCallback(new RequestCallback>() {
@Override
public void onSuccess(List param) {
if (param.size()>0){
friendAdapter = new FriendAdapter(getActivity(), param);
mRvFriends.setAdapter(friendAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
mRvFriends.setLayoutManager(linearLayoutManager);
mRvFriends.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
friendAdapter.setOnItemClickListener(new FriendAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
NimUserInfo nimUserInfo = param.get(position);
Intent intent = new Intent(
getActivity(),
FriendInfoActivity.class);
intent.putExtra("nimUserInfo", nimUserInfo);
startActivityForResult(intent, 0);
}
});
}
}
@Override
public void onFailed(int code) {
}
@Override
public void onException(Throwable exception) {
}
});
}
}
MessageFragment
package heath.com.chat.message;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import heath.com.chat.R;
public class MessageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_message, container,
false);
return view;
}
}
MineFragment
package heath.com.chat.mine;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import heath.com.chat.R;
public class MineFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mine, container,
false);
return view;
}
}
在res文件夹下创建一个menu的文件夹,里面放navigation.xml
在drawable创建color_navigation_selector.xml
这是底部导航栏样式
接下来,我们开始写好友搜索
activity_add_friends.xml
AddFriendsActivity
package heath.com.chat.friend;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.uinfo.UserService;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import heath.com.chat.BaseActivity;
import heath.com.chat.R;
import heath.com.chat.utils.LoadingUtils;
public class AddFriendsActivity extends BaseActivity implements View.OnClickListener {
private String TAG = "friend";
private SearchView mSvSearchFriends;
private LinearLayout mLlReturn;
private TextView mTvResult;
private LoadingUtils loadingUtils;
final ArrayList
好友信息展示
activity_friend_info.xml
FriendInfoActivity
package heath.com.chat.friend;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.friend.FriendService;
import com.netease.nimlib.sdk.friend.constant.VerifyType;
import com.netease.nimlib.sdk.friend.model.AddFriendData;
import com.netease.nimlib.sdk.uinfo.constant.GenderEnum;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import heath.com.chat.BaseActivity;
import heath.com.chat.LoginActivity;
import heath.com.chat.OKhttp.IHttpClient;
import heath.com.chat.OKhttp.IRequest;
import heath.com.chat.OKhttp.IResponse;
import heath.com.chat.OKhttp.impl.OkHttpClientImpl;
import heath.com.chat.OKhttp.impl.RequestImpl;
import heath.com.chat.R;
import heath.com.chat.utils.Common;
import heath.com.chat.utils.LoadingUtils;
import heath.com.chat.utils.ThreadUtils;
public class FriendInfoActivity extends BaseActivity implements View.OnClickListener {
private LinearLayout mLlReturn;
private ImageView mIvHeadPhoto;
private TextView mTvNickname;
private ImageView mIvSex;
private TextView mTvAddress;
private TextView mTvSign;
private Button mBtnAddFriend;
private Button mBtnSendMessage;
private NimUserInfo nimUserInfo;
protected LoadingUtils loadingUtils;
private BaseAnimatorSet mBasIn;
private BaseAnimatorSet mBasOut;
private LoginInfo loginInfo;
private String TAG = "friend";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friend_info);
initView();
initListener();
init();
}
private void initView() {
mLlReturn = this.findViewById(R.id.ll_return);
mIvHeadPhoto = this.findViewById(R.id.iv_head_photo);
mTvNickname = this.findViewById(R.id.tv_nickname);
mIvSex = this.findViewById(R.id.iv_sex);
mTvAddress = this.findViewById(R.id.tv_address);
mTvSign = this.findViewById(R.id.tv_sign);
mBtnAddFriend = this.findViewById(R.id.btn_add_friend);
mBtnSendMessage = this.findViewById(R.id.btn_send_message);
}
private void initListener() {
mBtnAddFriend.setOnClickListener(this);
mBtnSendMessage.setOnClickListener(this);
mLlReturn.setOnClickListener(this);
}
private void init() {
Intent intent = getIntent();
nimUserInfo = (NimUserInfo) intent.getSerializableExtra("nimUserInfo");
loginInfo = (LoginInfo) aCache.getAsObject("loginInfo");
loadingUtils = new LoadingUtils(FriendInfoActivity.this, "添加好友...");
mBasIn = new BounceTopEnter();
mBasOut = new SlideBottomExit();
loadingUtils.creat();
if (nimUserInfo.getAccount().equals(loginInfo.getAccount())){
mBtnAddFriend.setVisibility(View.GONE);
mBtnSendMessage.setVisibility(View.GONE);
}else{
boolean isMyFriend = NIMClient.getService(FriendService.class).isMyFriend(nimUserInfo.getAccount());
Log.i(TAG, "isMyFriend: "+isMyFriend);
if (isMyFriend) {
mBtnAddFriend.setVisibility(View.GONE);
mBtnSendMessage.setVisibility(View.VISIBLE);
}else {
mBtnAddFriend.setVisibility(View.VISIBLE);
mBtnSendMessage.setVisibility(View.GONE);
}
}
if (nimUserInfo.getName() != null) {
mTvNickname.setText(nimUserInfo.getName());
}
if (nimUserInfo.getSignature() != null) {
mTvSign.setText(nimUserInfo.getSignature());
}
GenderEnum genderEnum = nimUserInfo.getGenderEnum();
switch (genderEnum) {
case MALE:
mIvSex.setImageResource(R.drawable.boy);
break;
case FEMALE:
mIvSex.setImageResource(R.drawable.girl);
break;
case UNKNOWN:
mIvSex.setImageResource(R.drawable.unknown);
break;
default:
mIvSex.setImageResource(R.drawable.unknown);
break;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.btn_add_friend:
loadingUtils.show();
addFriend();
break;
case R.id.btn_send_message:
break;
case R.id.ll_return:
finish();
break;
}
}
private void addFriend() {
final VerifyType verifyType = VerifyType.DIRECT_ADD; // DIRECT_ADD 直接加对方为好友;VERIFY_REQUEST 发起好友验证请求
String msg = "好友请求附言";
NIMClient.getService(FriendService.class).addFriend(new AddFriendData(nimUserInfo.getAccount(), verifyType, msg))
.setCallback(new RequestCallback() {
@Override
public void onSuccess(Void param) {
loadingUtils.dismiss();
NormalDialogOneBtn("添加好友成功", 200);
}
@Override
public void onFailed(int code) {
loadingUtils.dismiss();
NormalDialogOneBtn("添加好友失敗", 414);
}
@Override
public void onException(Throwable exception) {
loadingUtils.dismiss();
NormalDialogOneBtn("添加好友失敗", 500);
}
});
}
private void NormalDialogOneBtn(String msg, int code) {
final NormalDialog dialog = new NormalDialog(this);
dialog.content(msg)//
.btnNum(1)
.btnText("确定")//
.showAnim(mBasIn)//
.dismissAnim(mBasOut)//
.show();
dialog.setOnBtnClickL(new OnBtnClickL() {
@Override
public void onBtnClick() {
dialog.dismiss();
if (code == 200) {
finish();
}
}
});
}
}
使用aadapter来好友列表显示
item_friend.xml
FriendAdapter.java
package heath.com.chat.adapter;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
import java.util.Collections;
import java.util.List;
import heath.com.chat.R;
public class FriendAdapter extends RecyclerView.Adapter {
private List listdata;
private Activity context;
private LayoutInflater mInflater;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
private OnItemClickListener mOnItemClickListener;
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
public FriendAdapter(Activity context, List listdata) {
this.context = context;
this.listdata = listdata;
mInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public FriendHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View convertView = mInflater.inflate(R.layout.item_friend, viewGroup, false);
FriendHolder viewHolder = new FriendHolder(convertView);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final FriendHolder holder, int position) {
// if (listdata.get(position).getUserInfo().getIcon() != null && !listdata.get(position).getUserInfo().getIcon().equals("null")) {
// ImageUitl imageUitl = new ImageUitl(BaseActivity.cache);
// imageUitl.asyncloadImage(holder.mIvHeadPhoto, Common.HTTP_ADDRESS + Common.USER_FOLDER_PATH + "/" + listdata.get(position).getUserInfo().getIcon());
// }
if (listdata.get(position).getName() != null) {
holder.mTvNickname.setText(listdata.get(position).getName());
}
if (mOnItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int layoutPosition = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView, layoutPosition);
}
});
}
}
@Override
public int getItemCount() {
return listdata.size();
}
public void setData(int position, List list) {
Collections.reverse(list);
for (NimUserInfo friendBean : list) {
listdata.add(position, friendBean);
notifyItemInserted(position);
}
}
}
class FriendHolder extends RecyclerView.ViewHolder {
ImageView mIvHeadPhoto;
TextView mTvNickname;
FriendHolder(@NonNull View itemView) {
super(itemView);
mIvHeadPhoto = itemView.findViewById(R.id.iv_head_photo);
mTvNickname = itemView.findViewById(R.id.tv_nickname);
}
}
项目下载地址:https://download.csdn.net/download/qq_32090185/11122479
下一章介绍发送消息(文本)