@Override
public void handleMessage(android.os.Message message) {
switch (message.what) {
case HANDLER_MESSAGE_REFRESH_LIST:
refreshList();
break;
case HANDLER_MESSAGE_SELECT_LAST:
if (activity instanceof ChatContainerActivity) {
ListView listView_fragment = chatLookerFragment.getRecyclerView();
if (messages.length > 0) {
listView_fragment.setSelection(messages.length - 1);
}
ListView listView = ((ChatContainerActivity) activity).getListView();
if (messages.length > 0) {
listView.setSelection(messages.length - 1);
}
}
break;
case HANDLER_MESSAGE_SEEK_TO:
int position = message.arg1;
if (activity instanceof ChatContainerActivity) {
ListView listView = ((ChatContainerActivity) activity).getListView();
listView.setSelection(position);
}
break;
default:
break;
}
}
chatLookerFragment.getRecyclerView();
這個是重點;
我之前通過單例模式獲得實例,不能獲得到listview對象,報空針
public static ChatLookerFragment newInstance() {
ChatLookerFragment fragment = new ChatLookerFragment();
return fragment;
}
public ChatLookerFragment() {
}
我的解決辦法是在adapter的適配器中傳入fragment的實例,通過該實例獲得listview對象
public MyChatMsgAdapter(EMConversation conversation, ChatContainerActivity mcontext, String username, ChatLookerFragment chatLookerFragment) {
// this.conversation = conversation;
this.conversation = EMChatManager.getInstance().getConversation(username);
this.mcontext = mcontext;
activity = mcontext;
this.chatLookerFragment = chatLookerFragment;
}