我目前正在使用 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/