我已将SearchView添加到我的应用程序中,唯一的问题是它没有显示结果,我可能知道原因,因为它没有与生成的新项目相关联(新项目) ),或者其他我不知道的事情。
以下是文件:
适配器:
public class RecyclingViewAdapter extends RecyclerView.Adapter<RecyclingViewAdapter.MyRecycleViewHolder>{
//Booleans for Expand & Favorite Buttons Visibility
boolean expand_visibility = true;
//Booleans favorite Buttons
boolean fav_button = true;
//Settings ItemsData List & Recycling Info
List<itemsdata> data = Collections.emptyList();
private final LayoutInflater inflater;
public RecyclingViewAdapter adapter;
public RecyclingViewAdapter(Context context,List<itemsdata> data)
{
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public MyRecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row, parent,false);
MyRecycleViewHolder MyHolder = new MyRecycleViewHolder(view);
return MyHolder;
}
@Override
public void onBindViewHolder(final MyRecycleViewHolder holder, int position) {
//Referencing Data
final itemsdata currentobject = data.get(position);
//Referencing Items
holder.ProbTitle.setText(currentobject.title);
holder.ProbDescr.setText(currentobject.Descr);
holder.CategoryPic.setImageResource(currentobject.CatPic);
holder.ExpandButton.setImageResource(currentobject.Exapnd);
holder.ExpandNoButton.setImageResource(currentobject.expand_no);
//Swipe Layout Codes
holder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
// Drag From Right
holder.swipeLayout.addDrag(SwipeLayout.DragEdge.Right, holder.swipeLayout.findViewById(R.id.bottom_wrapper));
// Handling different events when swiping
holder.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onClose(SwipeLayout layout) {
//when the SurfaceView totally cover the BottomView.
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
//you are swiping.
}
@Override
public void onStartOpen(SwipeLayout layout) {
}
@Override
public void onOpen(SwipeLayout layout) {
//when the BottomView totally show.
}
@Override
public void onStartClose(SwipeLayout layout) {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
//when user's hand released.
}
});
holder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
/*
//What Happen When You Press "Star" Text .
holder.Card_Star.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (fav_button) {
Toast.makeText(view.getContext(), "Starred!", Toast.LENGTH_LONG).show();
holder.Card_Star.setVisibility(View.GONE);
holder.Card_UnStar.setVisibility(View.VISIBLE);
fav_button = false;
}
}
});
//What Happen When You Re-Press "Star" Text .
holder.Card_UnStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (fav_button == false) {
holder.Card_UnStar.setVisibility(View.GONE);
holder.Card_Star.setVisibility(View.VISIBLE);
Toast.makeText(view.getContext(), "Removed", Toast.LENGTH_LONG).show();
fav_button = true;
}
}
});
*/
//What Happen When You Press "Share" Text .
holder.Card_Share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String shareQ = view.getContext().getString(R.string.ShareQ);
String shareA = view.getContext().getString(R.string.ShareA);
String shareinfo = view.getContext().getString(R.string.ShareInfo);
String sharetitle = currentobject.title;
String sharedescription = currentobject.Descr;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT,
shareQ + " " + sharetitle
+ "\n"
+ "\n"
+ shareA + " " + sharedescription
+ "\n"
+ "\n"
+ shareinfo
);
view.getContext().startActivity(Intent.createChooser(share, "Share The Answer"));
}
});
//What Happen When You Click Expand Button .
holder.ExpandButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (expand_visibility) {
//Set Expand Button To Gone
holder.ExpandButton.setVisibility(View.GONE);
//Set Description To Visible
holder.ProbDescr.setVisibility(View.VISIBLE);
//Set UnExpand To Button Visible
holder.ExpandNoButton.setVisibility(View.VISIBLE);
expand_visibility = false;
}
}
});
//What Happen When You Click UnExpand Button
holder.ExpandNoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (expand_visibility == false) {
//Set Description To Gone
holder.ProbDescr.setVisibility(View.GONE);
//Set UnExpand Button To Gone
holder.ExpandNoButton.setVisibility(View.GONE);
//Set Expand Button To Visible
holder.ExpandButton.setVisibility(View.VISIBLE);
expand_visibility = true;
}
}
});
}
@Override
public int getItemCount() {
return data.size();
}
public static class MyRecycleViewHolder extends RecyclerView.ViewHolder
{
SwipeLayout swipeLayout;
//Defining Items .
TextView ProbTitle;
ImageButton ExpandButton;
TextView ProbDescr;
ImageButton ExpandNoButton;
ImageView CategoryPic;
/*
TextView Card_Star;
TextView Card_UnStar;
*/
TextView Card_Share;
//Referencing Resources
public MyRecycleViewHolder(View itemView) {
super(itemView);
ProbTitle = (TextView) itemView.findViewById(R.id.prob_title);
CategoryPic = (ImageView) itemView.findViewById(R.id.cat_pic);
ProbDescr = (TextView) itemView.findViewById(R.id.prob_descr);
ExpandButton = (ImageButton) itemView.findViewById(R.id.expand_button);
ExpandNoButton = (ImageButton) itemView.findViewById(R.id.expand_no_button);
/*
Card_Star = (TextView) itemView.findViewById(R.id.card_star);
Card_UnStar = (TextView) itemView.findViewById(R.id.card_unstar);
*/
Card_Share = (TextView) itemView.findViewById(R.id.card_share);
swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
}
}
public void setFilter(List<itemsdata> searchingdata) {
data = new ArrayList<>();
data.addAll(searchingdata);
notifyDataSetChanged();
}
}
Tab_1(我使用SlidingTabLayout,所以这是标签1的片段)
public class tab_1 extends Fragment implements SearchView.OnQueryTextListener {
private List<itemsdata> msearchingdata;
private RecyclerView mRecyclerView;
public RecyclingViewAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.tab_1, container, false);
mRecyclerView = (RecyclerView)layout.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new RecyclingViewAdapter(getActivity(),getData());
mRecyclerView.setAdapter(adapter);
return layout;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, 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(msearchingdata);
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<itemsdata> filteredModelList = filter(msearchingdata, newText);
adapter.setFilter(filteredModelList);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private List<itemsdata> filter(List<itemsdata> current2, String query) {
query = query.toLowerCase();
final List<itemsdata> filteredModelList = new ArrayList<>();
for (itemsdata model : current2) {
final String text = model.getText().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
public List<itemsdata> getData()
{
//Titles Strings
String sys_title1 = getString(R.string.system_item_title_1);
String sys_title2 = getString(R.string.system_item_title_2);
String sys_title3 = getString(R.string.system_item_title_3);
//Description Strings
String sys_descr1 = getString(R.string.system_item_desc_1);
String sys_descr2 = getString(R.string.system_item_desc_2);
String sys_descr3 = getString(R.string.system_item_desc_3);
//Adding New Cards
List<itemsdata> data = new ArrayList<>();
//Categories Icons New Items ** Make It The Same
int[] icons = {
R.drawable.facebook_icon ,
R.drawable.twitter_icon ,
R.drawable.twitter_icon
};
//Expand Button New Items
int[] expandbutton = {
R.drawable.expanded ,
R.drawable.expanded ,
R.drawable.expanded
};
//UnExpand Button New Items
int[] unexpandbutton = {
R.drawable.ca_expand ,
R.drawable.ca_expand ,
R.drawable.ca_expand
};
//Titles New Items
String[] titles = {
sys_title1 ,
sys_title2 ,
sys_title3
};
//Description New Items
String[] Description = {
sys_descr1 ,
sys_descr2 ,
sys_descr3
};
for(int i = 0;i<titles.length && i < icons.length && i < Description.length ; i++)
{
itemsdata current = new itemsdata();
current.CatPic = icons[i];
current.title = titles[i];
current.Descr = Description[i];
data.add(current);
}
return data;
}
}
我的itemsdata:
public class itemsdata {
int CatPic;
String title;
String Descr;
int Exapnd;
int expand_no;
public String getText() {
return title;
}
}
正如您所看到的,它没有与新项目相关联,我也不知道我将如何做到这一点。 提前致谢!