[英]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.我有一个布局,它有一个 EditText,当用户输入时,它应该在它下面显示建议。我以编程方式创建了一个 RecyclerView,它应该在 EditText 下方显示建议项。但是当用户在 EditText 中输入时它不显示 RecyclerView . It should look like the screenshot included below:
它应该看起来像下面包含的屏幕截图:
Java Code: MainActivity.java:
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) {
}
});
CustomListAdapter.java:自定义列表适配器.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: 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
.首先,您必须为您的
recyclerView
设置布局管理器。 check this :检查这个:
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()))
then add RecyclerView
to your RelativeLayout
然后将
RecyclerView
添加到您的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()
.在您的
onTextChanged()
方法中,您创建RecyclerView
但不通过addView()
将其附加到布局。 You should consider to add your RecyclerView
directly in your XML layout instead of programmatically.您应该考虑将
RecyclerView
直接添加到 XML 布局中,而不是以编程方式添加。 And then change its visibility + notifying its adapter in the method onTextChanged()
.然后在方法
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我不知道你为什么要浪费时间创建自己的适配器并且不需要所有这些 Just Use
AutoCompleteTextView android view 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);
How to use it - http://www.journaldev.com/9574/android-autocompletetextview-example-tutorial如何使用它 - 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 Android 代码 - https://developer.android.com/reference/android/widget/AutoCompleteTextView.html最佳示例 - https://www.tutorialspoint.com/android/android_autocompletetextview_control.htm
Be smart bro聪明点哥
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.