简体   繁体   中英

Android RecyclerView doesnot appear below EditText

I have a layout which has an EditText which when typed by the user should display suggestions below it.I created a RecyclerView programatically which should show the suggestion items below the EditText.But it doesn't display the RecyclerView when the user typed in the EditText. It should look like the screenshot included below:

在此处输入图片说明 Java Code: MainActivity.java:

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
          if(charSequence.toString().equals("Taxi")){
         RelativeLayout.LayoutParams layoutParams1=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
              layoutParams1.setMargins(20,0,20,0);
              RecyclerView recyclerView=new RecyclerView(getContext());
              recyclerView.setAdapter(new CustomListAdapter());
              layoutParams1.addRule(RelativeLayout.BELOW,R.id.srch_qry);
              recyclerView.setLayoutParams(layoutParams1);

          }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

CustomListAdapter.java:

 public class CustomListAdapter extends RecyclerView.Adapter<CustomListAdapter.Holder> {
        public String[] taxi_list={"Audi","Tavera","Chevrolet"};
        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.cstm_lst_lyt,parent,false);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
        holder.taxi_text.setText(taxi_list[position]);
        }

        @Override
        public int getItemCount() {
            return taxi_list.length;
        }

        public class Holder extends RecyclerView.ViewHolder{
             TextView taxi_text;
            public Holder(View itemView) {

                super(itemView);
                taxi_text=(TextView) itemView.findViewById(R.id.taxi_txt);
            }

        }
    }

cstm_lst_lyt.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#10000000"
    android:id="@+id/lyt">
<TextView
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:id="@+id/taxi_txt"
    android:textSize="20sp"
    android:text="Audi"
    android:paddingLeft="10dp"
    android:gravity="center|start"/>
</RelativeLayout>

First you have to set layout manager for your recyclerView . check this :

recyclerView.setLayoutManager(new LinearLayoutManager(getContext()))

then add RecyclerView to your RelativeLayout

RelativeLayout myRelative = (RelativeLayout )findViewById(R.id.lyt);
myRelative.addView(recyclerView);

In your onTextChanged() method you create the RecyclerView but does not attach it to the layout via addView() . You should consider to add your RecyclerView directly in your XML layout instead of programmatically. And then change its visibility + notifying its adapter in the method onTextChanged() .


EDIT

Here is a example of layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/edit_text"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/text_suggestions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit_text"/>

I dont know why you are wasting time on creating own adpter and set no need for all of this Just Use

AutoCompleteTextView android view

 String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi", "Mango", "Pear"};
    //Creating the instance of ArrayAdapter containing list of fruit names
            ArrayAdapter<String> adapter = new ArrayAdapter<String>
                    (this, android.R.layout.select_dialog_item, fruits);
            //Getting the instance of AutoCompleteTextView
            AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
            actv.setThreshold(1);//will start working from first character
            actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
            actv.setTextColor(Color.RED);

How to use it - http://www.journaldev.com/9574/android-autocompletetextview-example-tutorial

Android code - https://developer.android.com/reference/android/widget/AutoCompleteTextView.html Best Example - https://www.tutorialspoint.com/android/android_autocompletetextview_control.htm

Be smart bro

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