我在不使用XML的情况下以编程方式设计RecycleView,因此我可以通过复制Java文件在所有项目中重用此代码。
我需要将SearchView添加到RecycleView并以编程方式设置所有搜索查询过滤器,而不使用任何第三方API。
SearchView searchVyuVar = new SearchView(this);
searchVyuVar.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String query)
{
recylcleLysLysMgrVar.filter(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
recylcleLysLysMgrVar.filter(newText);
return true;
}
});
RecyclerView recylcleLysLyoVav = new RecyclerView(this);
recylcleLysLyoVav.addView(searchVyuVar);
recylcleLysLyoVav.setAdapter(recylcleLysLysMgrVar);
recylcleLysLyoVav.setLayoutManager(new LinearLayoutManager(this));
答案 0 :(得分:0)
经过一整天的奋斗,我得到了这个以编程方式设计的搜索栏
借助以下链接
http://abhiandroid.com/ui/searchview
这是为了帮助所有我不想像我一样挣扎的人。
LinearLayout NamSupRloVav;
SearchView NamSrhVyuVav;
RecyclerView NamLysLyoVav;
NamLysAdrCls NamLysAdrVar;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
NamSupRloVav = new LinearLayout(this);
NamSupRloVav.setOrientation(LinearLayout.VERTICAL);
NamSrhVyuVav = new SearchView(this);
NamSrhVyuVav.setQueryHint("Search . . .");
NamSrhVyuVav.setIconified(false);
NamSrhVyuVav.onActionViewExpanded();
NamSrhVyuVav.setPadding(20,10,20,10);
NamSupRloVav.addView(NamSrhVyuVav);
NamLysLyoVav = new RecyclerView(this);
NamLysLyoVav.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
NamLysLyoVav.setVerticalScrollBarEnabled(true);
NamLysAdrVar = new NamLysAdrCls(GetAllNamAryLysFnc());
NamLysLyoVav.setAdapter(NamLysAdrVar);
NamLysLyoVav.setLayoutManager(new LinearLayoutManager(this));
NamSrhVyuVav.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String SrhSrgPsgVal)
{
NamLysAdrVar.SrhFltFnc(SrhSrgPsgVal);
return true;
}
@Override
public boolean onQueryTextSubmit(String SrhSrgPsgVal)
{
NamLysAdrVar.SrhFltFnc(SrhSrgPsgVal);
return true;
}
});
NamSupRloVav.addView(NamLysLyoVav);
setContentView(NamSupRloVav);
}
public class NamLysAdrCls extends RecyclerView.Adapter<VyuHlrCls>
{
ArrayList<NamCls> FchNamAryLysVar = new ArrayList<NamCls>();
ArrayList<NamCls> SrhNamAryLysVar = new ArrayList<NamCls>();
public NamLysAdrCls(ArrayList<NamCls> NamLysPsgVar)
{
FchNamAryLysVar.addAll(NamLysPsgVar);
SrhNamAryLysVar.addAll(NamLysPsgVar);
}
@Override
public VyuHlrCls onCreateViewHolder(ViewGroup HubVyuGrp, int viewType)
{
// |=> CodTdo Design Itm Idx Lyo
VyuHlrCls VyuHlrVar = new VyuHlrCls(NamLysItmLloVav);
return VyuHlrVar;
}
@Override
public void onBindViewHolder(VyuHlrCls VyuHlrPsgVar, int ItmIdxPsgVar)
{
}
@Override
public int getItemCount()
{
return SrhNamAryLysVar.size();
}
public void DelItmFnc(int ItmPosPsgVar)
{
SrhNamAryLysVar.remove(ItmPosPsgVar);
notifyItemRemoved(ItmPosPsgVar);
}
public void SrhFltFnc(String SrhSrgPsgVal)
{
SrhSrgPsgVal = SrhSrgPsgVal.toLowerCase();
SrhNamAryLysVar.clear();
if (SrhSrgPsgVal.length() == 0) SrhNamAryLysVar.addAll(FchNamAryLysVar);
else
{
for (NamCls NamIdxVar : FchNamAryLysVar)
{
if(NamIdxVar.toLowerCase().contains(SrhSrgPsgVal))
{
SrhNamAryLysVar.add(NamIdxVar);
}
}
}
notifyDataSetChanged();
}
}