gpt4 book ai didi

Android RecyclerView 不出现在 EditText 下面

转载 作者:行者123 更新时间:2023-11-30 00:47:30 27 4
gpt4 key购买 nike

我有一个布局,它有一个 EditText,当用户输入时应该在它下面显示建议。我以编程方式创建了一个 RecyclerView,它应该在 EditText 下面显示建议项目。但是当用户输入时它不显示 RecyclerView在编辑文本中。它应该看起来像下面的屏幕截图:

enter image description hereJava 代码: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>

最佳答案

首先,您必须为您的recyclerView 设置布局管理器。检查这个:

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

然后将 RecyclerView 添加到您的 RelativeLayout

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

关于Android RecyclerView 不出现在 EditText 下面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41545557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com