How Add and delete the Contact Bubbles properly in multiautocompletetextview with space tokenizer like gmail to field in android

后端 未结 1 1344
灰色年华
灰色年华 2020-12-29 16:14

i am struggling alot with adding the Bubbles to to field like in gmail or facebook messanger. please look into this picture below..

1条回答
  •  醉梦人生
    2020-12-29 16:32

    I open-sourced our solution TokenAutoComplete on github. Mine has been tested back to 2.2. I designed my code to allow pretty simple implementations and customizations. I'm not sure if this quite answers your question, but it might be a better starting point than the chips source code.

    Here's an example implementation using my library:

    Subclass TokenCompleteTextView

    public class ContactsCompletionView extends TokenCompleteTextView {
        public ContactsCompletionView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected View getViewForObject(Object object) {
            Person p = (Person)object;
    
            LayoutInflater l = (LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            LinearLayout view = (LinearLayout)l.inflate(R.layout.contact_token, (ViewGroup)ContactsCompletionView.this.getParent(), false);
            ((TextView)view.findViewById(R.id.name)).setText(p.getEmail());
    
            return view;
        }
    
        @Override
        protected Object defaultObject(String completionText) {
            //Stupid simple example of guessing if we have an email or not
            int index = completionText.indexOf('@');
            if (index == -1) {
                return new Person(completionText, completionText.replace(" ", "") + "@example.com");
            } else {
                return new Person(completionText.substring(0, index), completionText);
            }
        }
    }
    

    Layout code for contact_token (you can use any kind of layout here or could throw an ImageView in if you want images in the token)

    
    
        
    
    
    

    Token backgound drawable

    
        
        
    
    

    Person object code

    public class Person implements Serializable {
        private String name;
        private String email;
    
        public Person(String n, String e) { name = n; email = e; }
    
        public String getName() { return name; }
        public String getEmail() { return email; }
    
        @Override
        public String toString() { return name; }
    }
    

    Sample activity

    public class TokenActivity extends Activity {
        ContactsCompletionView completionView;
        Person[] people;
        ArrayAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            people = new Person[]{
                    new Person("Marshall Weir", "marshall@example.com"),
                    new Person("Margaret Smith", "margaret@example.com"),
                    new Person("Max Jordan", "max@example.com"),
                    new Person("Meg Peterson", "meg@example.com"),
                    new Person("Amanda Johnson", "amanda@example.com"),
                    new Person("Terry Anderson", "terry@example.com")
            };
    
            adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, people);
    
            completionView = (ContactsCompletionView)findViewById(R.id.searchView);
            completionView.setAdapter(adapter);
            completionView.setPrefix("To: ");
        }
    }
    

    Layout code

    
    
        
    
    
    

    0 讨论(0)
提交回复
热议问题