Example usage for android.widget AbsListView TRANSCRIPT_MODE_NORMAL

List of usage examples for android.widget AbsListView TRANSCRIPT_MODE_NORMAL

Introduction

In this page you can find the example usage for android.widget AbsListView TRANSCRIPT_MODE_NORMAL.

Prototype

int TRANSCRIPT_MODE_NORMAL

To view the source code for android.widget AbsListView TRANSCRIPT_MODE_NORMAL.

Click Source Link

Document

The list will automatically scroll to the bottom when a data set change notification is received and only if the last item is already visible on screen.

Usage

From source file:de.tubs.ibr.dtn.daemon.LogListFragment.java

@SuppressLint("NewApi")
@Override//from   w ww.j av a2  s.  c o m
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // We have a menu item to show in action bar.
    setHasOptionsMenu(true);

    getListView().setTranscriptMode(AbsListView.TRANSCRIPT_MODE_NORMAL);
    getListView().setStackFromBottom(true);

    // add scrollbar handle
    getListView().setFastScrollEnabled(true);

    getListView().setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            pauseLog();
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }
    });

    setEmptyText(getActivity().getResources().getString(R.string.list_no_log));

    // create a new list adapter
    mAdapter = new LogListAdapter(getActivity());

    // set listview adapter
    setListAdapter(mAdapter);

    // Start out with a progress indicator.
    setListShown(false);
}

From source file:com.raspi.chatapp.ui.chatting.ChatListFragment.java

/**
 * this function will make sure the ui looks right and reload everything.
 * Yeah, it is inefficient calling it every time a little detail has
 * changed but atm I don't care as this won't be as loaded as the
 * ChatFragment./*from  ww  w.j a  v  a2s . co m*/
 */
private void initUI() {
    // create the chatArrayAdapter
    caa = new ChatArrayAdapter(getContext(), R.layout.chat_list_entry);
    ListView lv = (ListView) getView().findViewById(R.id.main_listview);
    lv.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_NORMAL);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // on click open the corresponding chat
            ChatEntry chatEntry = caa.getItem(position);
            mListener.onChatOpened(chatEntry.buddyId, chatEntry.name);
        }
    });
    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
            // on long click rename the chat --> look ChatFragment
            final EditText newName = new EditText(getActivity());
            newName.setText(caa.getItem(position).name);
            String title = getResources().getString(R.string.change_name_title) + " "
                    + caa.getItem(position).name;
            // the dialog with corresponding title and message and with the
            // pre filled editText
            new AlertDialog.Builder(getContext()).setTitle(title).setMessage(R.string.change_name)
                    .setView(newName).setPositiveButton(R.string.rename, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // rename the chat and reinitialize the ui
                            MessageHistory messageHistory = new MessageHistory(getContext());
                            String buddyId = caa.getItem(position).buddyId;
                            String name = newName.getText().toString();
                            messageHistory.renameChat(buddyId, name);
                            initUI();
                        }
                    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // there is a cancel button too...
                        }
                    }).show();
            return true;
        }
    });
    // set the data
    lv.setAdapter(caa);
    MessageHistory messageHistory = new MessageHistory(getContext());
    // retrieve the chats and add them to the listView
    ChatEntry[] entries = messageHistory.getChats();
    for (ChatEntry entry : entries) {
        if (entry != null)
            caa.add(entry);
    }

    // the swipe refresh layout
    final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) getActivity()
            .findViewById(R.id.swipe_refresh);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // make everything related to refreshing in a background thread
            new Thread(new RefreshRunnable(new Handler(), swipeRefreshLayout)).start();
        }
    });

    // make sure that the no internet text displays the emojicon correctly
    EmojiconTextView textView = (EmojiconTextView) getActivity().findViewById(R.id.no_internet_text);
    textView.setText(
            String.format(getActivity().getResources().getString(R.string.no_internet), "\uD83D\uDE28"));

    // set the title
    actionBar.setTitle("ChatApp");
    // make sure that there is no subtitle
    actionBar.setSubtitle(null);
}

From source file:com.raspi.chatapp.ui.chatting.ChatFragment.java

private void initUI() {
    // load wallpaper
    loadWallPaper();//from   www  .  j a v  a 2 s. c o  m
    // enable the emojicon-keyboard
    initEmoji();
    // set the actionBar title
    if (actionBar != null)
        actionBar.setTitle(chatName);
    // create the messageArrayAdapter
    maa = new MessageArrayAdapter(getContext(), R.layout.message_text);

    listView = (ListView) getView().findViewById(R.id.chat_listview);
    textIn = (EmojiconEditText) getView().findViewById(R.id.chat_in);

    //    // Change the TypFace
    //    Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(),
    //            "fonts/Aileron-Bold.otf");
    //    textIn.setTypeface(typeface);

    Button sendBtn = (Button) getView().findViewById(R.id.chat_sendBtn);

    sendBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // clicking the sendButton will send the message, uhh.
            // trim the message. Spaces are a waste of resources!
            String message = textIn.getText().toString().trim();
            // and ofc only send if the message has something in it.
            if (!message.isEmpty()) {
                // add the message to the messageHistory
                long id = messageHistory.addMessage(buddyId,
                        getContext().getSharedPreferences(Constants.PREFERENCES, 0)
                                .getString(Constants.USERNAME, ""),
                        // this will be a textMessage for sure
                        MessageHistory.TYPE_TEXT, message, MessageHistory.STATUS_WAITING, -1);
                // also add the message to the ui
                maa.add(new TextMessage(false, message, new GregorianCalendar().getTimeInMillis(),
                        MessageHistory.STATUS_WAITING, id, -1));
                // and probably the buddy also wants the message, so send it
                sendTextMessage(message, id);
                // revert the editText
                textIn.setText("");
                int i = 0;
                // remove the NewMessage mac if it exists.
                for (MessageArrayContent mac : maa) {
                    if (mac instanceof NewMessage)
                        maa.remove(i);
                    i++;
                }
                // select the last item to scroll down.
                listView.setSelection(maa.getCount() - 1);
            }
        }
    });

    // this fucking bitch cost me a lot of time. Yep normal seems quite good,
    // doesn't it. Then why the actual fuck is "NORMAL" not the default?
    // Android?
    listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_NORMAL);
    // set the corresponding adapter
    listView.setAdapter(maa);
    // also clicking on items will do sometimes something.
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MessageArrayContent mac = maa.getItem(position);
            // if it is an imageMessage show the image
            if (mac instanceof ImageMessage) {
                ImageMessage im = (ImageMessage) mac;
                // yeah pretty straight forward, just view the image with the
                // default application.
                //          Intent viewIntent = new Intent(Intent.ACTION_VIEW);
                //          viewIntent.setDataAndType(Uri.fromFile(new File(im.file)),
                //                  "image/*");
                Intent viewIntent = new Intent(getContext(), ImageViewActivity.class);
                viewIntent.putExtra(Constants.BUDDY_ID, buddyId);
                viewIntent.putExtra(Constants.MESSAGE_ID, im._ID);
                startActivity(viewIntent);

                // don't ask for a pwd as I am just viewing an image
                getContext().getSharedPreferences(Constants.PREFERENCES, 0).edit()
                        .putBoolean(Constants.PWD_REQUEST, false).apply();
            } else if (mac instanceof LoadMoreMessages) {
                // clicking on loadMoreMessage should load more messages shouldn't it?
                // FIXME: fix bugs xD
                // 1. need further investigation, sometimes if there is only one more
                // message to be loaded it is not loaded but only the dateMessage

                // retrieve the messages from db
                MessageArrayContent[] macs = messageHistory.getMessages(buddyId, MESSAGE_LIMIT, messageAmount,
                        true);
                messageAmount += macs.length;
                //save position in order not to scroll
                int index = listView.getFirstVisiblePosition() + 2;
                View v = listView.getChildAt(2);
                int top = (v == null) ? 0 : v.getTop();
                //remove the date
                maa.remove(1);
                // calculating whether we need a new DatMessage works with
                // converting the time in millis in days and the compare the days.
                // Therefore, I need this constant.
                final long c = 24 * 60 * 60 * 1000;
                // get the oldDate which is the date of the first message that is
                // currently loaded, so the oldest currently loaded message
                MessageArrayContent macT = maa.getItem(1);
                long oldDate = (macT instanceof TextMessage) ? ((TextMessage) macT).time
                        : ((ImageMessage) macT).time;
                // this is needed for restoring the position after adding messages
                int count = macs.length;
                // loop through all new messages and add them, increase the count
                // and eventually insert a DateMessage
                for (MessageArrayContent macTemp : macs) {
                    if (macTemp instanceof TextMessage) {
                        TextMessage msg = (TextMessage) macTemp;
                        if (msg.time / c < oldDate / c) {
                            maa.insert(new Date(msg.time), 1);
                            count++;
                        }
                        // reset the old date
                        oldDate = msg.time;
                    } else if (macTemp instanceof ImageMessage) {
                        ImageMessage msg = (ImageMessage) macTemp;
                        if (msg.time / c < oldDate / c) {
                            maa.insert(new Date(msg.time), 1);
                            count++;
                        }
                        oldDate = msg.time;
                    }
                    maa.insert(macTemp, 1);
                }
                // insert the date at the end
                maa.insert(new Date(oldDate), 1);
                // reset the selection (aka scroll height
                listView.setSelectionFromTop(index + count, top);
                // if there are no more messages to be loaded than I currently have
                // loaded remove the loadMoreMessages item
                if (messageAmount >= messageHistory.getMessageAmount(buddyId)) {
                    maa.remove(0);
                    count--;
                }
                // hmh reselect the new scrollHeight?! why not
                listView.setSelectionFromTop(index + count, top);
            }
        }
    });

    // activate the multiSelectionOption
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(multiChoiceModeListener);

    // yeah loading messages, seems to be a good idea
    loadAllMessages();
    // set the status in the actionBar
    String lastOnline = messageHistory.getOnline(buddyId);
    updateStatus(lastOnline);
    //scroll down. Due to "I don't know why" clicking a notification is
    // different from clicking a chat in the ChatListFragment and therefore I
    // need to select the last message.
    listView.setSelection(maa.getCount() - 1);
}