我只有一个回收站内的活动 - 这就是我的一项活动:
在我的recyclerview中,有两种类型的视图。一个是个人资料视图,其中显示了我的姓名,年龄和性别,另一个是信息视图,其中提供了我订阅的课程的信息。我决定将两个视图都放在recyclerview中,以便当用户滚动此Recyclerview时两个视图都会向上滚动。
为了创造这个,我做了以下几点:
public class FeedRecyclerViewAdapter
extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<Info> info;
@Override
public int getItemViewType(int position) {
switch (position) {
case 0:
return PROFILE;
case 1:
return INFO;
}
}
public ProfileFeedActivityRecyclerViewAdapter(ArrayList<Info> info) {
this.info = info;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case PROFILE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_profile_info, parent, false);
return new ProfileInfoViewHolder(view);
case INFO:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_information, parent, false);
return new InformationViewHolder(view);
}
}
class ProfileInfoViewHolder extends RecyclerView.ViewHolder {
ImageView profilePic;
TextView name;
TextView age;
TextView gender;
public ProfileInfoViewHolder(View v) {
super(v);
//get a handler on all these textviews / imageViews and set the text or image based on whatever is stored in sharedPrefs.
}
}
class InformationViewHolder extends RecyclerView.ViewHolder {
TextView heading;
TextView subheading;
public InformationViewHolder(View v) {
super(v);
//get a handler on these views so that I can bind them in the onBindViewHolder method
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ProfileInfoViewHolder) {
//do nothing as everything was pulled from sharedPrefs. There is no object to back this view.
}
if (holder instanceof InformationViewHolder) {
((InformationViewHolder)holder).heading.setText(info.get(position).getHeading());
//binding the view to the arrayList that was passed into the adapter.
}
}
}
现在,假设我可以更改我的隐私设置,以在配置文件itemView中显示/隐藏我的年龄。我很容易将其更改为我的sharedPreference文件中的布尔值,以反映年龄false
,但我想更新我的recyclerview,以便在我更改设置时立即隐藏我的年龄,所以我所做的是notifyDataSetChanged()
in适配器,以便recyclerview自行更新。
由于配置文件itemView没有任何对象支持,因此所有数据都是从sharedPreferences中提取的,并且它不在ArrayList info
内,因此我的ArrayList info
数据集从未更改notifyDataSetChanged()
{{1}} 1}}没有效果。
如何在更改设置时让配置文件itemView自行更新?
答案 0 :(得分:0)
您可能希望使用Preference.OnPreferenceChangeListener来通知您是否更改了某个密钥并通知您的ui。
您将获得Preference对象和新值。