RecyclerView에서 특정 항목을 업데이트 / 새로 고침하는 방법
에서 특정 항목을 새로 고치려고합니다 RecyclerView
.
스토리 : 사용자가 항목을 클릭 할 때마다 AlertDialog
. 사용자는 확인 버튼을 클릭하여 텍스트를 입력 할 수 있습니다. 나는이 항목에서이 텍스트를 표시하려면 보이지 않는 보여 ImageView
- XML 및 어댑터를 선언 ViewHolder
-
AlertDialog
항목을 업데이트하기 위해 Positive Button 에서이 기능을 사용했습니다 .
private void updateListItem(int position) {
View view = layoutManager.findViewByPosition(position);
ImageView medicineSelected = (ImageView) view.findViewById(R.id.medicine_selected);
medicineSelected.setVisibility(View.VISIBLE);
TextView orderQuantity = (TextView) view.findViewById(R.id.order_quantity);
orderQuantity.setVisibility(View.VISIBLE);
orderQuantity.setText(quantity + " packet added!");
medicinesArrayAdapter.notifyItemChanged(position);
}
그러나이 코드는 전달 된 위치에서 itemView를 변경할뿐만 아니라 다른 itemView (s)도 변경합니다!
특정 itemView를 클릭하여 올바르게 변경하려면 어떻게해야합니까?
notifyItemChanged(int position)
RecyclerView.Adapter 클래스 의 메서드를 사용할 수 있습니다 . 문서에서 :
등록 된 관찰자에게 위치의 항목이 변경되었음을 알립니다. notifyItemChanged (position, null); 호출과 동일합니다.
이것은 구조 변경 이벤트가 아니라 항목 변경 이벤트입니다. 위치에있는 데이터의 반영이 오래되어 업데이트해야 함을 나타냅니다. 위치의 항목은 동일한 ID를 유지합니다.
당신이 이미 그 위치를 가지고 있기 때문에 그것은 당신을 위해 일할 것입니다.
단일 항목 업데이트
- 데이터 항목 업데이트
- 어댑터에 변경 사항을 알리십시오.
notifyItemChanged(updateIndex)
예
"I like sheep"이라고 표시되도록 "Sheep"항목을 변경합니다.
String newValue = "I like sheep.";
int updateIndex = 3;
data.set(updateIndex, newValue);
adapter.notifyItemChanged(updateIndex);
더 많은 예제와 함께 나의 완전한 대답은 여기에 있습니다 .
나는 이것을 처리하는 방법에 대한 아이디어가 있다고 생각합니다. 업데이트는 정확한 위치에서 삭제하고 바꾸는 것과 같습니다. 따라서 먼저 아래 코드를 사용하여 해당 위치에서 항목을 제거합니다.
public void removeItem(int position){
mData.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mData.size());
}
그런 다음 아래와 같이 특정 위치에 항목을 추가합니다.
public void addItem(int position, Landscape landscape){
mData.add(position, landscape);
notifyItemInserted(position);
notifyItemRangeChanged(position, mData.size());
}
지금 구현하려고합니다. 완료되면 피드백을 드리겠습니다!
I got to solve this issue by catching the position of the item that needed to be modified and then in the adapter call
public void refreshBlockOverlay(int position) {
notifyItemChanged(position);
}
, this will call onBindViewHolder(ViewHolder holder, int position) for this specific item at this specific position.
Add the changed text to your model data list
mdata.get(position).setSuborderStatusId("5");
mdata.get(position).setSuborderStatus("cancelled");
notifyItemChanged(position);
A way that has worked for me personally, is using the recyclerview's adapter methods to deal with changes in it's items.
It would go in a way similar to this, create a method in your custom recycler's view somewhat like this:
public void modifyItem(final int position, final Model model) {
mainModel.set(position, model);
notifyItemChanged(position);
}
Below solution worked for me:
On a RecyclerView item, user will click a button but another view like TextView will update without directly notifying adapter:
I found a good solution for this without using notifyDataSetChanged() method, this method reloads all data of recyclerView so if you have image or video inside item then they will reload and user experience will not good:
Here is an example of click on a ImageView like icon and only update a single TextView (Possible to update more view in same way of same item) to show like count update after adding +1:
// View holder class inside adapter class
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageViewLike;
public MyViewHolder(View itemView) {
super(itemView);
imageViewLike = itemView.findViewById(R.id.imageViewLike);
imageViewLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = getAdapterPosition(); // Get clicked item position
TextView tv = v.getRootView().findViewById(R.id.textViewLikeCount); // Find textView of recyclerView item
resultList.get(pos).setLike(resultList.get(pos).getLike() + 1); // Need to change data list to show updated data again after scrolling
tv.setText(String.valueOf(resultList.get(pos).getLike())); // Set data to TextView from updated list
}
});
}
In your adapter class, in onBindViewHolder method, set ViewHolder to setIsRecyclable(false) as in below code.
@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder p1, int p2)
{
// TODO: Implement this method
p1.setIsRecyclable(false);
// Then your other codes
}
you just have to add following code in alert dialog box on save click
recyclerData.add(position, updateText.getText().toString());
recyclerAdapter.notifyItemChanged(position);
That's also my last problem. Here my solution I use data Model and adapter for my RecyclerView
/*Firstly, register your new data to your model*/
DataModel detail = new DataModel(id, name, sat, image);
/*after that, use set to replace old value with the new one*/
int index = 4;
mData.set(index, detail);
/*finally, refresh your adapter*/
if(adapter!=null)
adapter.notifyItemChanged(index);
if you are creating one object and adding it to the list that you use in your adapter , when you change one element of your list in the adapter all of your other items change too in other words its all about references and your list doesn't hold separate copies of that single object.
In your RecyclerView adapter, you should have an ArrayList and also one method addItemsToList(items)
to add list items to the ArrayList. Then you can add list items by call adapter.addItemsToList(items)
dynamically. After all your list items added to the ArrayList then you can call adapter.notifyDataSetChanged()
to display your list.
You can use the notifyDataSetChanged
in the adapter for the RecyclerView
참고 URL : https://stackoverflow.com/questions/32457406/how-to-update-refresh-specific-item-in-recyclerview
'IT story' 카테고리의 다른 글
HTML5와 일치하는 두 개의 양식 필드를 요구할 수 있습니까? (0) | 2020.09.05 |
---|---|
nodejs 로그 파일은 어디에 있습니까? (0) | 2020.09.05 |
PHP의 count () 함수는 배열에 대해 O (1) 또는 O (n)입니까? (0) | 2020.09.05 |
지도에 요소를 삽입하는 권장 방법 (0) | 2020.09.05 |
크롬 브라우저에서 로케일을 변경하는 방법 (0) | 2020.09.05 |