簡體   English   中英

Android RecyclerView 不會出現在 EditText 下方

[英]Android RecyclerView doesnot appear below EditText

我有一個布局,它有一個 EditText,當用戶輸入時,它應該在它下面顯示建議。我以編程方式創建了一個 RecyclerView,它應該在 EditText 下方顯示建議項。但是當用戶在 EditText 中輸入時它不顯示 RecyclerView . 它應該看起來像下面包含的屏幕截圖:

在此處輸入圖片說明 Java 代碼: 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) {

        }
    });

自定義列表適配器.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>

首先,您必須為您的recyclerView設置布局管理器。 檢查這個:

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

然后將RecyclerView添加到您的RelativeLayout

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

在您的onTextChanged()方法中,您創建RecyclerView但不通過addView()將其附加到布局。 您應該考慮將RecyclerView直接添加到 XML 布局中,而不是以編程方式添加。 然后在方法onTextChanged()更改其可見性並通知其適配器。


編輯

下面是一個布局示例。

<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"/>

我不知道你為什么要浪費時間創建自己的適配器並且不需要所有這些 Just Use

AutoCompleteTextView android 視圖

 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);

如何使用它 - http://www.journaldev.com/9574/android-autocompletetextview-example-tutorial

Android 代碼 - https://developer.android.com/reference/android/widget/AutoCompleteTextView.html最佳示例 - https://www.tutorialspoint.com/android/android_autocompletetextview_control.htm

聰明點哥

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM