Example usage for android.view View.OnKeyListener View.OnKeyListener

List of usage examples for android.view View.OnKeyListener View.OnKeyListener

Introduction

In this page you can find the example usage for android.view View.OnKeyListener View.OnKeyListener.

Prototype

View.OnKeyListener

Source Link

Usage

From source file:org.tanrabad.team.ChatFragment.java

private void initUiComponents(View rootView) {
    mListView = (ListView) rootView.findViewById(R.id.list);
    turnOffListViewDecoration(mListView);
    mListView.setAdapter(mAdapter);/*from w  w w .  ja v  a 2s. com*/

    mBtnChannel = (ImageButton) rootView.findViewById(R.id.btn_channel);
    mBtnSend = (Button) rootView.findViewById(R.id.btn_send);
    mBtnUpload = (ImageButton) rootView.findViewById(R.id.btn_upload);
    mProgressBtnUpload = (ProgressBar) rootView.findViewById(R.id.progress_btn_upload);
    mEtxtMessage = (EditText) rootView.findViewById(R.id.etxt_message);

    mBtnSend.setEnabled(false);
    mBtnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            send();
        }
    });

    mBtnChannel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mHandler != null) {
                mHandler.onChannelListClicked();
            }
        }
    });

    mBtnUpload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_PICK_IMAGE);
        }
    });

    mEtxtMessage.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    send();
                }
                return true; // Do not hide keyboard.
            }
            return false;
        }
    });
    mEtxtMessage.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    mEtxtMessage.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            mBtnSend.setEnabled(s.length() > 0);
        }
    });
    mListView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            SoftKeyboard.hide(getActivity());
            return false;
        }
    });
    mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState == SCROLL_STATE_IDLE) {
                if (view.getFirstVisiblePosition() == 0 && view.getChildCount() > 0
                        && view.getChildAt(0).getTop() == 0) {
                    resultHandler = new MessageListQuery.MessageListQueryResult() {
                        @Override
                        public void onResult(List<MessageModel> messageModels) {
                            for (MessageModel model : messageModels) {
                                mAdapter.addMessageModel(model);
                            }

                            mAdapter.notifyDataSetChanged();
                            mListView.setSelection(messageModels.size());
                        }

                        @Override
                        public void onError(Exception e) {
                        }
                    };
                    SendBird.queryMessageList(SendBird.getChannelUrl()).prev(mAdapter.getMinMessageTimestamp(),
                            30, resultHandler);
                }
            }
        }

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

From source file:com.vuze.android.remote.fragment.FilesFragment.java

public View onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container,
        Bundle savedInstanceState) {/*from w w  w .j  a  va2  s  . c  o m*/

    if (AndroidUtils.DEBUG) {
        Log.d(TAG, "onCreateview " + this);
    }

    View view = inflater.inflate(R.layout.frag_torrent_files, container, false);

    FragmentActivity activity = getActivity();

    progressBar = (ProgressBar) activity.findViewById(R.id.details_progress_bar);

    viewAreaToggleEditMode = view.findViewById(R.id.files_area_toggleditmode);
    tvScrollTitle = (TextView) view.findViewById(R.id.files_scrolltitle);

    btnEditMode = (CompoundButton) view.findViewById(R.id.files_editmode);
    if (btnEditMode != null) {
        btnEditMode.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (adapter == null) {
                    return;
                }
                adapter.setInEditMode(btnEditMode.isChecked());
            }
        });
    }

    final SwipeRefreshLayoutExtra swipeRefresh = (SwipeRefreshLayoutExtra) view
            .findViewById(R.id.swipe_container);
    if (swipeRefresh != null) {
        swipeRefresh.setExtraLayout(R.layout.swipe_layout_extra);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (sessionInfo == null) {
                    return;
                }
                showProgressBar();
                sessionInfo.executeRpc(new RpcExecuter() {
                    @Override
                    public void executeRpc(TransmissionRPC rpc) {
                        rpc.getTorrentFileInfo(TAG, torrentID, null, new TorrentListReceivedListener() {

                            @Override
                            public void rpcTorrentListReceived(String callID, List<?> addedTorrentMaps,
                                    List<?> removedTorrentIDs) {
                                AndroidUtils.runOnUIThread(FilesFragment.this, new Runnable() {
                                    @Override
                                    public void run() {
                                        swipeRefresh.setRefreshing(false);
                                    }
                                });
                            }
                        });
                    }
                });

            }
        });
        swipeRefresh.setOnExtraViewVisibilityChange(this);
    }

    listview = (RecyclerView) view.findViewById(R.id.files_list);
    listview.setLayoutManager(new PreCachingLayoutManager(getContext()));
    listview.setAdapter(adapter);

    listview.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            {
                if (event.getAction() != KeyEvent.ACTION_DOWN) {
                    return false;
                }
                switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_RIGHT: {
                    // expand
                    int i = adapter.getSelectedPosition();
                    FilesAdapterDisplayObject item = adapter.getItem(i);
                    if (item instanceof FilesAdapterDisplayFolder) {
                        if (!((FilesAdapterDisplayFolder) item).expand) {
                            ((FilesAdapterDisplayFolder) item).expand = true;
                            adapter.getFilter().filter("");
                            return true;
                        }
                    }
                    break;
                }

                case KeyEvent.KEYCODE_DPAD_LEFT: {
                    // collapse
                    int i = adapter.getSelectedPosition();
                    FilesAdapterDisplayObject item = adapter.getItem(i);
                    if (item instanceof FilesAdapterDisplayFolder) {
                        if (((FilesAdapterDisplayFolder) item).expand) {
                            ((FilesAdapterDisplayFolder) item).expand = false;
                            adapter.getFilter().filter("");
                            return true;
                        }
                    }
                    break;
                }

                case KeyEvent.KEYCODE_MEDIA_PLAY:
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: {
                    launchOrStreamFile();
                    return true;
                }
                }

                return false;
            }
        }
    });

    listview.setOnScrollListener(new RecyclerView.OnScrollListener() {
        int firstVisibleItem = 0;

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            LinearLayoutManager lm = (LinearLayoutManager) listview.getLayoutManager();
            int firstVisibleItem = lm.findFirstCompletelyVisibleItemPosition();
            if (firstVisibleItem != this.firstVisibleItem) {
                this.firstVisibleItem = firstVisibleItem;
                FilesAdapterDisplayObject itemAtPosition = adapter.getItem(firstVisibleItem);
                //               Log.d(TAG, "itemAt" + firstVisibleItem + " is " + itemAtPosition);
                //               Log.d(TAG, "tvScrollTitle=" + tvScrollTitle);
                //               Log.d(TAG, "viewAreaToggleEditMode=" + viewAreaToggleEditMode);

                if (itemAtPosition == null) {
                    return;
                }
                if (itemAtPosition.parent != null) {
                    if (viewAreaToggleEditMode != null) {
                        viewAreaToggleEditMode.setVisibility(View.GONE);
                    }
                    if (tvScrollTitle != null) {
                        tvScrollTitle.setVisibility(View.VISIBLE);
                        tvScrollTitle.setText(itemAtPosition.parent.folder);
                    }
                } else {
                    if (viewAreaToggleEditMode != null) {
                        viewAreaToggleEditMode.setVisibility(View.VISIBLE);
                    }
                    if (tvScrollTitle != null) {
                        if (viewAreaToggleEditMode != null) {
                            tvScrollTitle.setVisibility(View.INVISIBLE);
                        }
                        tvScrollTitle.setText("");
                    }
                }
            }
        }
    });

    FlexibleRecyclerSelectionListener rs = new FlexibleRecyclerSelectionListener<FilesTreeAdapter, FilesAdapterDisplayObject>() {
        @Override
        public void onItemSelected(FilesTreeAdapter adapter, final int position, boolean isChecked) {
        }

        @Override
        public void onItemClick(FilesTreeAdapter adapter, int position) {
            if (AndroidUtils.usesNavigationControl()) {
                FilesAdapterDisplayObject oItem = adapter.getItem(position);
                if (adapter.isInEditMode()) {
                    adapter.flipWant(oItem);
                    return;
                }
                if (oItem instanceof FilesAdapterDisplayFolder) {
                    FilesAdapterDisplayFolder oFolder = (FilesAdapterDisplayFolder) oItem;
                    oFolder.expand = !oFolder.expand;
                    adapter.getFilter().filter("");
                } else {
                    showFileContextMenu();
                }
            }
        }

        @Override
        public boolean onItemLongClick(FilesTreeAdapter adapter, int position) {
            if (AndroidUtils.usesNavigationControl()) {
                if (showFileContextMenu()) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public void onItemCheckedChanged(FilesTreeAdapter adapter, FilesAdapterDisplayObject item,
                boolean isChecked) {

            if (adapter.getCheckedItemCount() == 0) {
                finishActionMode();
            } else {
                // Update the subtitle with file name
                showContextualActions();
            }

            AndroidUtils.invalidateOptionsMenuHC(getActivity(), mActionMode);
        }
    };

    adapter = new FilesTreeAdapter(this.getActivity(), rs);
    adapter.setSessionInfo(sessionInfo);
    adapter.setMultiCheckModeAllowed(false);
    adapter.setCheckOnSelectedAfterMS(100);
    listview.setAdapter(adapter);

    return view;
}