Android ListView with fast scroll and alphabetical section index

后端 未结 3 1487
感情败类
感情败类 2020-12-05 19:49

How to add testview when touching a letter on right alphabet panel as shown in images? Could you please help me? Below is my code.

In details, I am looking for an e

3条回答
  •  情歌与酒
    2020-12-05 20:41

    I have displayed the list inside a fragment, if you want to display in main activity then you can pass this, instead of getActivity()

    //method to display the side indexed scroll list of alphabets 
        public void displayAlphabetsList() {
            final List listOfAlphabet = new ArrayList<>();
            for (int i = 0; i < 26; i++) {
                char alphabet = (char) (ASCII_VALUE_OF_A + i);
                listOfAlphabet.add(String.valueOf(alphabet));
            }
    
            ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listOfAlphabet);
           alphabets_List_View.setAdapter(adapter);
    
            alphabets_List_View.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    ///scroll the recycler view to that position where matching letter was found
                    int positionToScroll = 0;
                    for (int i = 0; i < mContacts.size(); i++) {
                        if (mContacts.get(i).getFirstName().startsWith(listOfAlphabet.get(position)))
                            break;
                        else
                            positionToScroll++;
                    }
                    recyclerView.scrollToPosition(positionToScroll);
                }
            });
        }
    

提交回复
热议问题