適配器之值傳遞(fragment適配器獲得listview控件對象)

      @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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章