Android 动态更新RecyclerView中的特定视图

Android 动态更新RecyclerView中的特定视图,android,android-recyclerview,Android,Android Recyclerview,我目前正在android上使用RecyclerView,假设我的自定义行中有2个TextView,我想动态更改TextView中一个的文本,我该怎么做 我的MainActivity中有以下代码 public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private AdaptersOnline adaptersOnline; private Recy

我目前正在android上使用
RecyclerView
,假设我的自定义行中有2个
TextView
,我想动态更改
TextView
中一个的文本,我该怎么做

我的MainActivity中有以下代码

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private AdaptersOnline adaptersOnline;
    private RecyclerView.LayoutManager mLayoutManager;
    private List<ModelClientInformation> modelOnlineLists = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = getApplicationContext();

        mLayoutManager = new LinearLayoutManager(this);
        adaptersOnline = new AdaptersOnline(this, modelOnlineLists);
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adaptersOnline);
    }

    //call this to new row
    public void initializeClient(String id, String data1, String data2){
        this.modelOnlineLists.add(new ModelClientInformation(id, data1, data2));
        adaptersOnline.notifyDataSetChanged();
    }

    //Call this method to update textview
    public void updateSpecificViewItem(String theID){
         //get position base on the ID           
         adaptersOnline.updateTextView(
         adaptersOnline.getPositionBaseOnItemID(theID));
    }

}
我很抱歉,我不知道该怎么做。 有人能帮我吗

更新:

请看我的变化

1:我想通过调用
updatespecificeviewItem(“theID”)
来更新
main活动中的
TextView
类之一

2:通过调用
GetPositionBaseoniMid(“theID”)
获取基于给定
id
的项目位置

3:为了最终更新特定项,我想调用
updateTextView(int-position)
方法


我现在面临的唯一问题是
编号3
,如何只更新
文本2而不更新整个项目?

您需要覆盖onBindViewHolder(视图持有人,int位置,列表有效负载)

@覆盖
公共无效onBindViewHolder(HelloViewHolder,内部位置,列表有效负载){
if(payloads.isEmpty()){
super.onBindViewHolder(支架、位置、有效载荷);
}否则{
用于(对象有效载荷:有效载荷){
if(字符串的有效负载实例){
holder.textView.setText(payload.toString)
}
}
}
}
要更新您的文本视图,您只需拨打

adapter.notifyItemChanged(位置,“例如字符串”)

这将部分更新视图

希望这有帮助。

在适配器中

String textToUpdate;

@Override
public void onBindViewHolder(TheViewHolder holder, int position) {
    final ModelClientInformation info = onlineList.get(position);

    holder.text1.setText(holder.getText1());// update the textview u want by setting the "textToUpdate" variable
    holder.text1.setText(holder.getText2());
}

public void updateTextView(String text){
    textToUpdate = text;
    notifyDatasetChanged(); 
}

希望有帮助

在RecylerView类中创建一个公共静态,只需在该方法中指定一个文本即可。接下来,在类中,您将获得一个到recycler视图实例的钩子,确保添加一个onclick和ontouch接口来存储所选文本行的位置。使用recycler视图实例调用
notifyItemChanged

以下是文档中的描述

在版本22.1.0中添加了notifyItemChanged

无效通知项已更改(int位置)

通知任何注册观察员位置上的物品已更改。相当于调用notifyItemChanged(position,null)

这是项目更改事件,而不是结构更改事件。这表明位置上的任何数据反映均已过时,应予以更新。位置上的项目保留相同的标识。

解决我的问题

main活动

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private AdaptersOnline adaptersOnline;
    private RecyclerView.LayoutManager mLayoutManager;
    private List<ModelClientInformation> modelOnlineLists = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = getApplicationContext();

        mLayoutManager = new LinearLayoutManager(this);
        adaptersOnline = new AdaptersOnline(this, modelOnlineLists);
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adaptersOnline);
    }

    //call this to new row
    public void initializeClient(String id, String data1, String data2){
        this.modelOnlineLists.add(new ModelClientInformation(id, data1, data2));
        adaptersOnline.notifyDataSetChanged();
    }

    //Call this method to update specific Item
    public void updateSpecificViewItem(String theID, String newText){      

         int position = adaptersOnline.getPositionBaseOnItemID(theID); // get position base on the ID
         ModelClientInformation oldItem = adaptersOnline.getOnlineList().get(position); // From my Adapter I created a new method `getOnlineList()` that returns the list item of specific position.

         ModelClientInformation newItem = new ModelClientInformation(
                    oldItem.ItemID(), // get and add the old Item ID
                    oldItem.getText1(), // Get and add the old Text1
                    newText // add the new text for text2
            );

        adaptersOnline.updateTextView(position, newItem); // call updateTextView() from the adapter and pass the position and the newItem.
    }

}
我不太确定这是否应该这样做,但我能够根据位置更新特定列表项及其视图


感谢所有让我想到
notifyItemChanged()
的人!希望这也能帮助其他人。

您能解释更多吗..?您在哪里调用updateTextView方法?您需要调用哪个操作来更改文本?很抱歉,响应太晚,我想从mainActivity类调用方法updateTextView。如果要更改按钮上的文本,请单击,然后使用更新的文本更新pojo类并通知适配器。如果我的
数据列表中有
10项
,该怎么办?我只想
更改特定位置中的一个项目
?是的,可以,只需使用onTouch或Onclick界面获得特定位置即可。然后在该位置调用该方法,确保通知适配器更改,如上所述。
@Override
    public void onBindViewHolder(HelloViewHolder holder, int position, List<Object> payload) {

        if (payloads.isEmpty()) {
             super.onBindViewHolder(holder, position , payloads);
        }else{

            for (Object payload : payloads) {
               if (payload instanceof String) {
                  holder.textView.setText(payload.toString) 
                 }
              }
          }

    }
String textToUpdate;

@Override
public void onBindViewHolder(TheViewHolder holder, int position) {
    final ModelClientInformation info = onlineList.get(position);

    holder.text1.setText(holder.getText1());// update the textview u want by setting the "textToUpdate" variable
    holder.text1.setText(holder.getText2());
}

public void updateTextView(String text){
    textToUpdate = text;
    notifyDatasetChanged(); 
}
public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private AdaptersOnline adaptersOnline;
    private RecyclerView.LayoutManager mLayoutManager;
    private List<ModelClientInformation> modelOnlineLists = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = getApplicationContext();

        mLayoutManager = new LinearLayoutManager(this);
        adaptersOnline = new AdaptersOnline(this, modelOnlineLists);
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adaptersOnline);
    }

    //call this to new row
    public void initializeClient(String id, String data1, String data2){
        this.modelOnlineLists.add(new ModelClientInformation(id, data1, data2));
        adaptersOnline.notifyDataSetChanged();
    }

    //Call this method to update specific Item
    public void updateSpecificViewItem(String theID, String newText){      

         int position = adaptersOnline.getPositionBaseOnItemID(theID); // get position base on the ID
         ModelClientInformation oldItem = adaptersOnline.getOnlineList().get(position); // From my Adapter I created a new method `getOnlineList()` that returns the list item of specific position.

         ModelClientInformation newItem = new ModelClientInformation(
                    oldItem.ItemID(), // get and add the old Item ID
                    oldItem.getText1(), // Get and add the old Text1
                    newText // add the new text for text2
            );

        adaptersOnline.updateTextView(position, newItem); // call updateTextView() from the adapter and pass the position and the newItem.
    }

}
public class AdaptersOnline extends RecyclerView.Adapter<AdaptersOnline.TheViewHolder> {
    Context mContext;
    public List<ModelClientInformation> onlineList;

    public AdaptersOnline(Context mContext, List<ModelClientInformation> modelOnlineList){
        this.mContext = mContext;
        this.onlineList = modelOnlineList;
    }

    public class TheViewHolder extends RecyclerView.ViewHolder {

        TextView text1, text2;

        public TheViewHolder(View view) {
            super(view);

            text1 = (TextView)view.findViewById(R.id.text1);
            text2 = (TextView)view.findViewById(R.id.text2);
        }
    }

    @Override
    public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_row, parent, false);
        return new TheViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(TheViewHolder holder, int position) {
        final ModelClientInformation info = onlineList.get(position);

        holder.text1.setText(holder.getText1());
        holder.text1.setText(holder.getText2());
    }


    /**Function to Return the size of List**/
    @Override
    public int getItemCount() {
        return onlineList.size();
    }

    /**Function to Clear the List**/
    public void clear(){
        onlineList.clear();
    }

    /**Function to return the Data List**/
    public List<ModelClientInformation> getOnlineList(){
        return this.onlineList;
    }

    /**Function to update the specific Item**/
    public void updateTextView(int position, ModelClientInformation newItem){
        onlineList.set(position, newItem); //set the item 
        notifyItemChanged(position, newItem); //notify the adapter for changes
    }

    /*Get the position of item inside data list base on the given ID*/
    public int getPositionBaseOnItemID(String theID) {

        int length = onlineList.size();
        for (int i = 0; i < length; i ++) {
            if(onlineList.get(i).getItemID().equals(theID)) {
                return i;
            }
        }
        return -1; //Item not found
    }
}
public ModelClientInformation class{

    private String theID, text1, text2;

    public ModelClientInformation(String theID, String text1, String text2){
        this.theID = theID;
        this.text1 = text1;
        this.text2 = text2;
    }

    public String getItemID(){
        return theID;
    }

    public String getText1(){
        return text1;
    }

    public String getText2(){
        return text2;
    }

}