android - 动态更新 RecyclerView 中的特定 View

标签 android android-recyclerview

我目前正在使用 RecyclerView 开发 Android,假设我的自定义行中有 2 个 TextView,我想动态更改 TextView 中的一个的文本code>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));
    }

}

在我的适配器类中

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

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

}

我不知道该怎么做。 。 。 有人可以帮助我吗?

更新:

请查看我的更改,

1:我想通过调用 updateSpecificViewItem("theID") 来更新 MainActivity 类中的 TextView 之一。

2:通过调用getPositionBaseOnItemID("theID"),根据给定的id获取项目的位置。

3:为了最终更新特定项目,我想调用updateTextView(intposition)方法。

我现在面临的唯一问题是number 3,我怎样才能只更新text2而不是整个项目?

最佳答案

您需要重写onBindViewHolder(TheViewHolderholder,int位置,List有效负载)

@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 , "例如字符串")

这将为您提供 View 的部分更新。

希望这有帮助。

关于android - 动态更新 RecyclerView 中的特定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47730739/

相关文章:

java - 房间数据库 : RecyclerView does not update after delete data in detail activity

java - RecyclerView 不显示任何内容

Android 覆盖整个 map

javascript - 如何使用适用于 android 和 iphone 的 javascript 检测长触摸压力?

android - DrawableCompat.setTint(drawable, color) vs Drawable.setColorFilter(color, mode)

c# - 如何在我的统一创建的 android 应用程序中显示导航栏

Android DeviceAdminReceiver : onNetworkLogsAvailable intent never received

android - 使用 Android LiveData 更新 RecyclerView

android - 加载recyclerView时出现Glide错误 "You must pass in a non null View"

android - Recyclerview item点击波纹效果