从活动切换到Fragment后,UI无法显示内容

时间:2016-03-10 23:15:50

标签: android android-layout android-fragments

我使用Layer(atlas for UI)SDK开发了一个消息传递功能。 如果实现是w.r.t活动,我可以在UI中看到对话列表,但是在切换到活动中的片段后,UI显示为空。切换到片段是必需的,因为应用程序具有使用ViewPager概念实现的导航选项卡,而消息传递是选项卡之一。 以下是我的文件,如果我在使用片段时丢失任何内容,请告诉我。

片段实现

public class ConversationListFragment extends Fragment {
    private AtlasConversationsRecyclerView mConversationsList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (App.routeLogin(getActivity())) {
            if (!getActivity().isFinishing()) getActivity().finish();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_conversations_list, container, false);
        mConversationsList = (AtlasConversationsRecyclerView) v.findViewById(R.id.conversations_list);
        initializeConversationList();

        return v;
    }

    private void initializeConversationList() {
        // Atlas methods
        mConversationsList.init(getLayerClient(), getParticipantProvider(), getPicasso())
                .setInitialHistoricMessagesToFetch(20)
                .setOnConversationClickListener(new AtlasConversationsAdapter.OnConversationClickListener() {
                    @Override
                    public void onConversationClick(AtlasConversationsAdapter adapter, Conversation conversation) {
                        Intent intent = new Intent(getContext(), MessagesListActivity.class);
                        if (Log.isLoggable(Log.VERBOSE)) {
                            Log.v("Launching MessagesListActivity with existing conversation ID: " + conversation.getId());
                        }
                        intent.putExtra(PushNotificationReceiver.LAYER_CONVERSATION_KEY, conversation.getId());
                        startActivity(intent);
                    }

                    @Override
                    public boolean onConversationLongClick(AtlasConversationsAdapter adapter, Conversation conversation) {
                        return false;
                    }
                })
                .setOnConversationSwipeListener(new SwipeableItem.OnSwipeListener<Conversation>() {
                    @Override
                    public void onSwipe(final Conversation conversation, int direction) {
                        new AlertDialog.Builder(getContext())
                                .setMessage(R.string.alert_message_delete_conversation)
                                .setNegativeButton(R.string.alert_button_cancel, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO: simply update this one conversation
                                        mConversationsList.getAdapter().notifyDataSetChanged();
                                        dialog.dismiss();
                                    }
                                })
                                .setPositiveButton(R.string.alert_button_delete, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        conversation.delete(LayerClient.DeletionMode.ALL_PARTICIPANTS);
                                    }
                                }).show();
                    }
                });

        App.getParticipantProvider().registerParticipantListener((AtlasConversationsAdapter) mConversationsList.getAdapter());

    }

    @Override
    public void onResume() {
        android.util.Log.i("###", "BaseFragment-onResume");
        super.onResume();
        LayerClient client = App.getLayerClient();
        if (client == null) return;
        if (client.isAuthenticated()) {
            client.connect();
        } else {
            client.authenticate();
        }
    }

    protected LayerClient getLayerClient() {
        return App.getLayerClient();
    }

    protected ParticipantProvider getParticipantProvider() {
        return App.getParticipantProvider();
    }

    protected Picasso getPicasso() {
        return App.getPicasso();
    }

活动实施

public class ConversationsListActivity extends BaseActivity {

    private AtlasConversationsRecyclerView mConversationsList;

    public ConversationsListActivity() {
        super(R.layout.activity_conversations_list, R.menu.menu_conversations_list, R.string.title_conversations_list, false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (App.routeLogin(this)) {
            if (!isFinishing()) finish();
            return;
        }

        mConversationsList = (AtlasConversationsRecyclerView) findViewById(R.id.conversations_list);

        // Atlas methods
        mConversationsList.init(getLayerClient(), getParticipantProvider(), getPicasso())
                .setInitialHistoricMessagesToFetch(20)
                .setOnConversationClickListener(new AtlasConversationsAdapter.OnConversationClickListener() {
                    @Override
                    public void onConversationClick(AtlasConversationsAdapter adapter, Conversation conversation) {
                        Intent intent = new Intent(ConversationsListActivity.this, MessagesListActivity.class);
                        if (Log.isLoggable(Log.VERBOSE)) {
                            Log.v("Launching MessagesListActivity with existing conversation ID: " + conversation.getId());
                        }
                        intent.putExtra(PushNotificationReceiver.LAYER_CONVERSATION_KEY, conversation.getId());
                        startActivity(intent);
                    }

                    @Override
                    public boolean onConversationLongClick(AtlasConversationsAdapter adapter, Conversation conversation) {
                        return false;
                    }
                })
                .setOnConversationSwipeListener(new SwipeableItem.OnSwipeListener<Conversation>() {
                    @Override
                    public void onSwipe(final Conversation conversation, int direction) {
                        new AlertDialog.Builder(ConversationsListActivity.this)
                                .setMessage(R.string.alert_message_delete_conversation)
                                .setNegativeButton(R.string.alert_button_cancel, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO: simply update this one conversation
                                        mConversationsList.getAdapter().notifyDataSetChanged();
                                        dialog.dismiss();
                                    }
                                })
                                .setPositiveButton(R.string.alert_button_delete, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        conversation.delete(LayerClient.DeletionMode.ALL_PARTICIPANTS);
                                    }
                                }).show();
                    }
                });

        App.getParticipantProvider().registerParticipantListener((AtlasConversationsAdapter) mConversationsList.getAdapter());

        findViewById(R.id.floating_action_button)
                .setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        startActivity(new Intent(ConversationsListActivity.this, MessagesListActivity.class));
                    }
                });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;

            case R.id.action_sendlogs:
                LayerClient.sendLogs(getLayerClient(), this);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

基本活动

public abstract class BaseActivity extends AppCompatActivity {
    private final int mLayoutResId;
    private final int mMenuResId;
    private final int mMenuTitleResId;
    private final boolean mMenuBackEnabled;

    public BaseActivity(int layoutResId, int menuResId, int menuTitleResId, boolean menuBackEnabled) {
        mLayoutResId = layoutResId;
        mMenuResId = menuResId;
        mMenuTitleResId = menuTitleResId;
        mMenuBackEnabled = menuBackEnabled;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mLayoutResId);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar == null) return;
        if (mMenuBackEnabled) actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(mMenuTitleResId);
    }

    @Override
    public void setTitle(CharSequence title) {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar == null) {
            super.setTitle(title);
        } else {
            actionBar.setTitle(title);
        }
    }

    @Override
    public void setTitle(int titleId) {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar == null) {
            super.setTitle(titleId);
        } else {
            actionBar.setTitle(titleId);
        }
    }

    @Override
    protected void onResume() {
        Log.i("###","BaseActivity-onResume");
        super.onResume();
        LayerClient client = App.getLayerClient();
        if (client == null) return;
        if (client.isAuthenticated()) {
            client.connect();
        } else {
            client.authenticate();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(mMenuResId, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            // Menu "Navigate Up" acts like hardware back button
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected LayerClient getLayerClient() {
        return App.getLayerClient();
    }

    protected ParticipantProvider getParticipantProvider() {
        return App.getParticipantProvider();
    }

    protected Picasso getPicasso() {
        return App.getPicasso();
    }
}

1 个答案:

答案 0 :(得分:1)

经过良好的调试会议后,发现Layer-Atlas SDK有条件可以在恢复时刷新。

public AtlasMessagesRecyclerView refresh() {
    if (mAdapter != null) mAdapter.refresh();
    return this;
}

所以只需从ConversationListFragment onResume调用refresh方法,就解决了这个问题。

@Override
public void onResume() {
    super.onResume();
    LayerClient client = App.getLayerClient();
    if (client == null) return;
    if (client.isAuthenticated()) {
        client.connect();
    } else {
        client.authenticate();
    }
    mConversationsList.refresh();
}