我有一个片段可以做一些与searchview相关的事情,但效果很好,
但我想创建一个活动来执行通常作为片段的任务。
XML没有问题主要问题在于Java代码。片段中的一些方法,我不知道它们在活动中的相反方式。
片段代码:
public class AirlineSearchFragment extends Fragment implements SearchView.OnQueryTextListener{
private RecyclerView recyclerview;
private List<AirlineData> airlinesDataModel;
private AirlinesSearchViewAdapter adapter;
public AirlineSearchFragment()
{
setHasOptionsMenu(true);
}
@Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.airline_search_fragment, container, false);
recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerview.setLayoutManager(layoutManager);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
String[] locales = Locale.getISOCountries();
airlinesDataModel = new ArrayList<>();
airlinesDataModel.addAll(AirlinesCore.airlinesList);
adapter = new AirlinesSearchViewAdapter(airlinesDataModel);
recyclerview.setAdapter(adapter);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.auto_complete, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
adapter.setFilter(airlinesDataModel);
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
}
@Override
public boolean onQueryTextChange(String newText) {
final List<AirlineData> filteredModelList = filter(airlinesDataModel, newText);
adapter.setFilter(filteredModelList);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private List<AirlineData> filter(List<AirlineData> models, String query) {
query = query.toLowerCase();
final List<AirlineData> filteredModelList = new ArrayList<>();
for (AirlineData model : models) {
if(model.arabicName==null)
{
model.arabicName="";
}
if(model.shortName==null)
{
model.shortName="";
}
if(model.name==null)
{
model.name="";
}
final String text = model.name.toLowerCase()+model.shortName.toLowerCase()
+model.arabicName.toLowerCase()+model.code.toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
}
我的活动,从片段中复制了一些方法(我应该添加什么?):
public class AirlinesSearchActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
private RecyclerView recyclerview;
private List<AirlineData> airlinesDataModel;
private AirlinesSearchViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_airlines_search);
}
@Override
public boolean onQueryTextChange(String newText) {
final List<AirlineData> filteredModelList = filter(airlinesDataModel, newText);
adapter.setFilter(filteredModelList);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private List<AirlineData> filter(List<AirlineData> models, String query) {
query = query.toLowerCase();
final List<AirlineData> filteredModelList = new ArrayList<>();
for (AirlineData model : models) {
if(model.arabicName==null)
{
model.arabicName="";
}
if(model.shortName==null)
{
model.shortName="";
}
if(model.name==null)
{
model.name="";
}
final String text = model.name.toLowerCase()+model.shortName.toLowerCase()
+model.arabicName.toLowerCase()+model.code.toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
}
答案 0 :(得分:3)
尝试将您的逻辑移至onCreateView
和onViewCreated
至Activity.onCreate()
。
然后覆盖活动中的onCreateOptionsMenu
并复制碎片