Question

I have list of conversations, and messenger UI.. what I want is: When an item of the list of conversations clicked, the messenger UI appear with that specific conversation messages.. my attempt: Until now I haven't code this part, I just look for an idea,is it good Idea to save each message immediately to DB, and when I navigate between the conversations I drop the appropriate messages?

Was it helpful?

Solution

Recently I've been working in something similar. For achieving this I've used a TabHost, with a FrameLayout associated to each one that might be converted to a TextView, which is what I suppose you want.

This approach has a handicap: You can access just to the currently opened tab's view (in this case, a TextView). If you don't plan to add text to an inactive tab, you don't need to do anything further as TabHost handles each tab's previous messages. But for instance, if you want to add messages to an inactive tab, you have to store them first in a queue and once the tab is activated, process them with an OnTabChangedListener event.

That's my definition for the TabHost structure:

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

<LinearLayout
            android:id="@+id/TabContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="99"
            android:orientation="vertical">
  <TabHost
            android:id="@+android:id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    <LinearLayout
            android:id="@+id/TabLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
      <!-- This makes your TabHost horizontally scrollable if it reaches a certain number of tabs -->
      <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none">
        <TabWidget
            android:id="@+android:id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></TabWidget>
      </HorizontalScrollView>
      <!-- You may also want to make this FrameLayour vertically scrollable -->
      <FrameLayout
        android:id="@+android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"></FrameLayout>
    </LinearLayout>
  </TabHost>
</LinearLayout>

If you need to process events on a tabchange, you should have something like this:

final TabHost th = (TabHost) (this.findViewById(android.R.id.tabhost));
  th.setOnTabChangedListener(new OnTabChangeListener() {
  @Override
  public void onTabChanged(final String tabId) {
    // Here you do the stuff you need. tabId is the name (also called Indicator) of the activated tab
  }
});

You should also be aware of controlling the buffer of each tab. That is, control that each tab doesn't have more than X messages, as if it grows without control your app may start being irresponsive.

These links might help you as they did to me when I was programming this part:

https://gist.github.com/jerolimov/618086

http://androidituts.com/android-tab-layout-example/

http://learnandroideasily.blogspot.com.es/2013/07/android-tabwidget-example.html

OTHER TIPS

I guess saving convs in DB isn't a good idea. You should serialize the data or should you use files to save'em.? Or rather, you can use delimiters to have a random access.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top