1

I have a messaging application. I can get messages and list from mysql database via json. When i get messages (for example last 10 messages and newer is at the bottom) first record is shown at the top, so user have to scroll down to see last message. I want to focus to last message and when user scrolls to top, I want to put there a Load Previous Messages button. I found Load More buttoni but its at the bottom of page. How can I do that? My codes are these:

 // Hashmap for ListView
    categoryList = new ArrayList<HashMap<String, String>>();

    // get listview
     ListView lv = getListView();
     Button btnLoadMore = new Button(this);
     btnLoadMore.setText("Load older messages");

    FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.listfooter,null);
    lv.addFooterView(btnLoadMore);
sipdorus
  • 963
  • 1
  • 12
  • 28

1 Answers1

3

As I said in comments, simply change addFooterView() to addHeaderView() method which give you the Load More at the top of the list. This method use three parameters (at least one: View from reference) which are:

  • View v: the Load More view to add at the top
  • Object data: data associated to the view
  • boolean isSelectable: value to make the view selectable or not

Using these three params instead of only the view may allow you to prevent the color state on it by using android:listSelector attribute. Indeed, sometimes you want to prevent a background state on a header/footer view. That being says, the method might be:

lv.addHeaderView(headerLayout, null, false); // this isn't clickable now

Note: now, you can call the view variable headerLayout instead of footerLayout ;)

As I understand your requirements, HeaderView should have the Load More Button into it, to avoid to create it dynamically, as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load More"
        android:onClick="loadMoreDatas" />
</LinearLayout>

Now, you have the right Button at the top of your list and you can add a method to handle the click event into the Activity (refer to android:onClick attribute) as:

public void loadMoreDatas(View v) {
    // load more messages
}

Finally, to focus to a specific item, in your case the last item in the list (at the bottom), you should use setSelection(int position) which go to the index selected in its parameter. Then, after setting the Adapter, call this on the ListView as:

// set the adapter
setListAdapter(adapter);
// go to selection (last item)
lv.setSelection(adapter.getCount() - 1); 

The getCount() method use (normally) your ArrayList size. Then, you have to prevent an IndexOutBoundsException because your array begins with position 0 and not 1. So, the last position is "All Items less First Position (0)".

However, the perfect method to begin at the bottom of the list is setStackFromBottom():

When stack from bottom is set to true, the list fills its content starting from the bottom of the view.

Then, it might be better to have:

// start from bottom
lv.setStackFromBottom(true); 

This should do the trick and I hope this will be usefull.

Blo
  • 11,903
  • 5
  • 45
  • 99
  • Great Solutions, well how can i say "add this messages to top of listview" after clicking Load Older Messages Button? – sipdorus May 19 '14 at 22:03
  • 2
    With `add` method, you can set an index at first parameter @BilalTürk. So basically, do `categoryList.add(0, newData);` and make a `loop` to add new datas always at the top: [this answer](http://stackoverflow.com/a/5903875/2668136) is exactly what you are looking for (except Runnable). – Blo May 19 '14 at 22:20