简体   繁体   中英

ListView for select Tag like Facebook

I need a listview when after type "@key" on a Edittext, Just like facebook. The list will dynamically change on key. I try it by PopupMenu but can't make in proper place & also can't make dynamic list. Please give me some suggestion or a way to solve it.

在此处输入图片说明 在此处输入图片说明

Thanks

You can done it with AutoCompleteTextView

Please check following example

//layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.palaswadi.forgotme.SplashScreen">

    <AutoCompleteTextView
        android:id="@+id/auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

//Activity Class

public class SplashScreen extends AppCompatActivity {

    boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);




        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);
        final AutoCompleteTextView textView = (AutoCompleteTextView)
                findViewById(R.id.auto);
        textView.addTextChangedListener(new TextWatcher() {

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

                if (s.equals("@")) {
                    flag = true;
                }
                if (flag)
                    textView.setAdapter(adapter);

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }

    private static final String[] COUNTRIES = new String[]{
            "Belgium", "France", "Italy", "Germany", "Spain"
    };

}

它是AutoCompleteTextView ,基本上是PopupWindow内的ListVIew

Are you asking for auto complete textview. if it so try as following in xml file

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="36dp"
android:layout_marginTop="17dp"
android:ems="10"
android:text=""/>

In my opinion, you can use PopupWindow to implement this.

The scenario will be:

  1. User type text -> filter @ character -> get input data

  2. Calculate position of PopupWindow to display and its gravity (display on top or bottom) and size of PopupWindow

  3. Create ListView to display options inside PopupWindow

  4. Display the popup window

Thats all. You can do it manually or simply use AutoCompleteTextView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM