2 回答
TA贡献1943条经验 获得超7个赞
我认为你可以在你的 DataObject 类中做一个在列表或数组中返回所有消息的方法。
// 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};
}
然后你可以创建一个 TextView 列表来循环它,比如:
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方法中你可以循环你的列表:
@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贡献1853条经验 获得超6个赞
就我个人而言,我会从您的 textViews 创建一个数组,并循环遍历所有这些数组。如果他们的文本长度 == 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);
}
}
}
添加回答
举报