在我MainActivity.java
我有一个登录按钮。我还有4个片段,每个片段都包含一个回收站视图(imageview + textview)。
图像视图被隐藏,但是当我按下按钮时它变得可见。我怎样才能做到这一点? notifyDataSetChanged()
在适配器中不起作用,我无法在片段页面中的适配器上调用它,因为它不在onCreateView
方法之内。
这是我的代码:
MainActivity:包含按钮
片段页面:
public class Classici_Tab extends Fragment {
LinkedList<Book> catalog = new LinkedList<>();
RecyclerView mRecyclerView;
BookListAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.classici_tab, container, false);
fillCatalog("catalog.csv"); //fill the LinkedList
mRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.classici_rv);
mAdapter = new BookListAdapter(getContext(), catalog);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return fragmentView;
}
}
适配器:
public class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.BookViewHolder> {
// View Holder
class BookViewHolder extends RecyclerView.ViewHolder
{
public final ImageView cover;
public final TextView title;
public final TextView author;
public final ImageView availability;
final BookListAdapter mAdapter;
public BookViewHolder(View itemView, BookListAdapter adapter) {
super(itemView);
cover = (ImageView) itemView.findViewById(R.id.cover);
title = (TextView) itemView.findViewById(R.id.title);
author = (TextView) itemView.findViewById(R.id.author);
availability = (ImageView) itemView.findViewById(R.id.availability);
MainActivity m = new MainActivity();
if(m.loggedin) availability.setVisibility(View.VISIBLE);
else availability.setVisibility(View.INVISIBLE);
this.mAdapter = adapter;
}
}
private LinkedList<Book> mBookList = new LinkedList<>();
private LayoutInflater mInflater;
boolean isVisible = false;
public BookListAdapter() {}
public BookListAdapter(Context context, LinkedList<Book> bookList) {
mInflater = LayoutInflater.from(context);
this.mBookList = bookList;
}
//IT DOESN'T WORK
public void setPicturesVisibility(boolean isVisible){
this.isVisible = true;
notifyDataSetChanged();
}
@Override
public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mItemView = mInflater.inflate(R.layout.tab_list_item, parent, false);
return new BookViewHolder(mItemView, this);
}
@Override
public void onBindViewHolder(BookViewHolder holder, int position) {
holder.cover.setImageResource(mBookList.get(position).getCover());
holder.title.setText(mBookList.get(position).getTitle());
holder.author.setText(mBookList.get(position).getAuthor());
holder.availability.setImageResource(mBookList.get(position).getAvailability());
if(isVisible){
holder.availability.setVisibility(View.VISIBLE);
}else{
holder.availability.setVisibility(View.INVISIBLE);
}
}
@Override
public int getItemCount() {
return mBookList.size();
}
}
答案 0 :(得分:0)
无需重新创建适配器。您可以在RecyclerViewAdapter中添加布尔值arePicturesVisible = false;
。
并在适配器中创建一个方法:
public void setPicturesVisibility(boolean areVisible){
arePicturesVisible = areVisible;
notifyDataSetChanged();
}
然后添加:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
.....
if(arePicturesVisible){
holder.image.setVisibility(View.VISIBLE);
}else{
holder.image.setVisibility(View.INVISIBLE);
}
.....
}
然后在您的按钮onClick()
方法中,从RecyclerView适配器调用setPicturesVisibility()
。
答案 1 :(得分:0)
不确定为什么需要重新创建适配器。在不重新创建适配器的情况下,您可以向按钮添加侦听器,并在onclick事件处理程序中,使图像在recyclerview适配器的视图构造器中可见。
public ViewHolder(View view) {
super(view);
textview = view.findViewById(R.id.textview);
imgView = view.findViewById(R.id.storeImg);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imgView.setVisibility(View.VISIBLE);
}
});
}
找到recyclerview示例