我有一个具有EditText的布局,当用户输入时应显示它下面的建议。我以编程方式创建了一个RecyclerView,它应该显示EditText下面的建议项。但是当用户输入时它不会显示RecyclerView在EditText中。它应该看起来像下面的屏幕截图:
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>
答案 0 :(得分:1)
首先,您必须为recyclerView
设置布局管理器。检查一下:
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()))
然后将RecyclerView
添加到您的RelativeLayout
RelativeLayout myRelative = (RelativeLayout )findViewById(R.id.lyt);
myRelative.addView(recyclerView);
答案 1 :(得分:0)
在onTextChanged()
方法中,您可以创建RecyclerView
,但不会通过addView()
将其附加到布局中。您应该考虑直接在XML布局中添加RecyclerView
而不是以编程方式添加。然后在方法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"/>
答案 2 :(得分:0)
我不知道你为什么要浪费时间创建自己的adpter并且不需要所有这些只是使用
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
聪明兄弟