动态更新RecyclerView中的特定视图

时间:2017-12-09 16:40:52

标签: android android-recyclerview recycler-adapter

我目前正在使用RecyclerView处理Android,让我说我的自定义行中有2 TextView,我想动态更改{中的一个文本{1}},我该怎么做?

我的MainActivity中有以下代码

TextView

在我的适配器类

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));
    }

}

和Pojo

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();
    }


    /**Possibly way to update one of the TextView here**/
    public void updateTextView(int position){
        //what should I do to update the TextView
    }

    /*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
}
}

我没有任何想法。 。 。 有人可以帮帮我吗?

更新

请查看我的更改,

1:我想通过调用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; } } 来更新TextView课程中MainActivity之一。

2:通过调用updateSpecificViewItem("theID")获取项目基础在给定id上的位置。

3:要最终更新特定项目,我想调用getPositionBaseOnItemID("theID")方法。

我现在面临的唯一问题是updateTextView(int position),如何才更新number 3而不是整个项目?

4 个答案:

答案 0 :(得分:0)

你需要覆盖onBindViewHolder(TheViewHolder holder,int position,List payload)

@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) 
                 }
              }
          }

    }

要更新textView,您只需致电

adapter.notifyItemChanged(position , "an string for example")

这样可以部分更新您的观点。

希望这有帮助。

答案 1 :(得分:0)

在你的适配器中,

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(); 
}

希望它有所帮助!

答案 2 :(得分:0)

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

以下是文档中的说明。

  

在版本22.1.0中添加了notifyItemChanged

void notifyItemChanged(int position)

通知任何注册观察员该位置的项目已更改。相当于调用notifyItemChanged(position,null);。

这是项目更改事件,而不是结构更改事件。它表示位置数据的任何反映都已过时,应予以更新。位置上的项目保持相同的身份。

答案 3 :(得分:0)

解决我的问题

<强> 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 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
    }
}

<强> POJO的

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;
    }

}

我不确定这是否应该如何完成但是我能够更新特定的列表项并且它的位置基于位置。

感谢所有给我notifyItemChanged()的想法的人!希望这也有助于对方。