2 回答

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
我認(rèn)為你可以在你的 DataObject 類(lèi)中做一個(gè)在列表或數(shù)組中返回所有消息的方法。
// Somewhere in your DataObject class, it's important to add all the messages, even if they are empty
public String[] getAllMessages(){
return new String[]{messages1, messages2, messages3};
}
然后你可以創(chuàng)建一個(gè) TextView 列表來(lái)循環(huán)它,比如:
private ArrayList<DataObject> mDataset;
Context context;
//Here your list
private List<TextView> tvList = new ArrayList();
public static class DataObjectHolder extends RecyclerView.ViewHolder {
public DataObjectHolder(View itemView) {
super(itemView);
setting1 = (TextView) itemView.findViewById(R.id.textView);
setting2 = (TextView) itemView.findViewById(R.id.textView2);
// Then you add all your TextViews to your list
tvList.add(setting1);
tvList.add(setting2);
}
}
然后在你的onBindViewHolder方法中你可以循環(huán)你的列表:
@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
String[] messages = mDataset.get(position).getAllMessages(); // The method that we just code
for(int i = 0; i < tvList.size(); i++){
if(messages[i].isEmpty()){
tvList.get(i).setVisibility(View.GONE);
}else{
tvList.get(i).setVisibility(View.VISIBLE);
tvList.get(i).setText(messages[i]);
}
}
}

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊
就我個(gè)人而言,我會(huì)從您的 textViews 創(chuàng)建一個(gè)數(shù)組,并循環(huán)遍歷所有這些數(shù)組。如果他們的文本長(zhǎng)度 == 0 隱藏文本視圖,否則顯示。
如果public void onBindViewHolder()
@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
for(TextView textView : textViews) {
textView.setText("");
textView.setVisibility(View.GONE);
}
int id= mDataset.get(position).getmIndex();
if (id == 1) {
holder.setting1.setText(mDataset.get(position).getmText1());
} else if (id == 2) {
holder.setting1.setText(mDataset.get(position).getmText1());
holder.setting2.setText(mDataset.get(position).getmText2());
holder.setting3.setText(mDataset.get(position).getmText3());
holder.setting4.setText(mDataset.get(position).getmText4());
holder.setting5.setText(mDataset.get(position).getmText5());
holder.setting6.setText(mDataset.get(position).getmText6());
holder.setting7.setText(mDataset.get(position).getmText7());
holder.setting8.setText(mDataset.get(position).getmText8());
holder.setting9.setText(mDataset.get(position).getmText9());
holder.setting10.setText(mDataset.get(position).getmText10());
} else if (id == 3) {
holder.setting1.setText(mDataset.get(position).getmText1());
holder.setting2.setText(mDataset.get(position).getmText2());
holder.setting3.setText(mDataset.get(position).getmText3());
holder.setting4.setText(mDataset.get(position).getmText4());
holder.setting5.setText(mDataset.get(position).getmText5());
holder.setting6.setText(mDataset.get(position).getmText6());
holder.setting7.setText(mDataset.get(position).getmText7());
}
for(TextView textView : textViews) {
if (textView.getText().length > 0) {
textView.setVisibility(View.VISIBLE);
}
}
}
添加回答
舉報(bào)