我不希望更新回收者视图列表中的第一项。我每x秒检索所有元素的一些数据,但我想保持第一项始终相同。
有关如何执行操作的任何线索?
编辑
*fragment*
viewModel.onGetFees.observe(this, Observer<FeesResponse> {
if (it != null) {
adapter.setFees(it)
}
})
*adapter*
fun setFees(feesResponse: FeesResponse) {
this.feesList = AppUtils.toFeesList(feesResponse)
this.notifyDataSetChanged()
}
override fun onBindViewHolder(holder: FeesViewHolder, position: Int) {
this.context = holder.itemView.context
holder.tvTitle.text = feesList[position].title
holder.tvSubtitle.text =
context.resources.getString(AppUtils.getName(feesList[position].title))
holder.etFee.setText(feesList[position].rating.toString())
if (position == 0) {
holder.etFee.addTextChangedListener(onTextChangedListener)
}
Glide.with(holder.ivFlag.context).load(AppUtils.getFlag(feesList[position].countryCode))
.apply(
RequestOptions.circleCropTransform()
).into(holder.ivFlag)
}
答案 0 :(得分:1)
这取决于您如何更新recyclerview ?。 这是你可能理解的我的例子。
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if(position==0) //this is for only 1st item
{
if(holder.itemView.tag.equal(0)==false) // initially itemview tag will be null . this heppen only for 1st time
{
holder.itemView.tag=position //then assign the value to tag
//SET YOUR CONTENT HERE
}
}
}
答案 1 :(得分:1)
根据您的要求使用viewType尝试以下代码行
创建CustomPojo.class
public class CustomPojo{
public String title;
public String name;
public boolean isNeedUpdate;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isNeedUpdate() {
return isNeedUpdate;
}
public void setNeedUpdate(boolean needUpdate) {
isNeedUpdate = needUpdate;
}
}
在“活动”或“片段”中的recyclerview处写下以下代码
ArrayList listItem = new ArrayList();
public void setAdapter(){
CustomPojo customPojoNoUpdate = new CustomPojo();
customPojoNoUpdate.setTitle("xyz");
customPojoNoUpdate.setName("xyz");
customPojoNoUpdate.setNeedUpdate(false);
CustomPojo customPojoUpdate = new CustomPojo();
customPojoUpdate.setTitle("xyz");
customPojoUpdate.setName("xyz");
customPojoUpdate.setNeedUpdate(true);
listItem.add(customPojoNoUpdate);
listItem.add(customPojoUpdate);
MyAdapter adapter = new MyAdapter(listItem);
recyclerview.setAdapter(adapter);
}
编写以下适配器代码
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int TYPE_NO_UPDATE = 0;
public static final int TYPE_VIEW_UPDATE = 1;
ArrayList<CustomPojo> list;
public MyAdapter(ArrayList<CustomPojo> list) {
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case TYPE_VIEW_UPDATE:
View v1 = inflater.inflate(R.layout.row_update_view, viewGroup, false);
viewHolder = new UpdateView(v1);
break;
case TYPE_NO_UPDATE:
View v4 = inflater.inflate(R.layout.row_no_update, viewGroup, false);
viewHolder = new NoUpdateView(v4);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
if (viewHolder == null) {
return;
}
switch (viewHolder.getItemViewType()) {
case TYPE_VIEW_UPDATE:
UpdateView vh1 = (UpdateView) viewHolder;
vh1.title.setText(list.get(position).getTitle());
vh1.name.setText(list.get(position).getName());
break;
case TYPE_NO_UPDATE:
NoUpdateView vh4 = (NoUpdateView) viewHolder;
vh4.title.setText(list.get(position).getTitle());
break;
}
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public int getItemViewType(int position) {
if (list.size() >= 1 && !list.get(position).isNeedUpdate()) {
return TYPE_VIEW_UPDATE;
} else {
return TYPE_NO_UPDATE;
}
return -1;
}
class UpdateView extends RecyclerView.ViewHolder {
TextView title,name;
public UpdateView(View view) {
super(view);
title = view.findViewById(R.id.title);
name = view.findViewById(R.id.name);
}
}
class NoUpdateView extends RecyclerView.ViewHolder {
TextView title;
public NoUpdateView(final View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
}
}}
希望对您有帮助