隐藏多个空文本视图

时间:2019-07-10 13:58:23

标签: java android

我正在使用RecyclerView,其中包含多个CardView的{​​{1}}。每张卡需要显示不同数量的TextView,因此有些卡将为空,如果为空,则仅在卡上显示空白。

这全部在TextView的适配器中执行。

例如:

RecyclerView

我目前使用if else语句确定它是哪组文本,然后它分配文本视图,设置文本或隐藏视图。

我试图创建一个简单的循环以循环浏览所有textview,如果它们为空则将其隐藏。但是,我尝试的方法无法正常工作,因为它使用字符串作为Card 1: (id 1) 3 TextViews showing Card 2: (id 2) 10 TextViews showing Card 3: (id 3) 7 TextViews showing

适配器类:

TextView

我要执行的操作的示例代码:

        .Adapter<SettingsAdapter
        .DataObjectHolder> {

    private ArrayList<DataObject> mDataset;
    Context context;

    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);
           //... assigning all the textviews
        }

    }

    public SettingsAdapter(ArrayList<DataObject> myDataset, Context context) {
        mDataset = myDataset;
        this.context = context;
    }

    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.settings_card_view, parent, false);
        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {
        int id= mDataset.get(position).getmIndex();
        if (id == 1) {
            holder.setting1.setText(mDataset.get(position).getmText1());
            holder.setting2.setVisibility(View.GONE);
            holder.setting3.setVisibility(View.GONE);
            holder.setting4.setVisibility(View.GONE);
            holder.setting5.setVisibility(View.GONE);
            holder.setting6.setVisibility(View.GONE);
            holder.setting7.setVisibility(View.GONE);
            holder.setting8.setVisibility(View.GONE);
            holder.setting9.setVisibility(View.GONE);
            holder.setting10.setVisibility(View.GONE);
        } 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());
            holder.setting8.setVisibility(View.GONE);
            holder.setting9.setVisibility(View.GONE);
            holder.setting10.setVisibility(View.GONE);
        } 
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}

2 个答案:

答案 0 :(得分:0)

我个人将根据您的textViews创建一个数组,并遍历所有它们。如果他们的文字长度== 0,则隐藏文字视图,否则显示。

如果public void onBindViewHolder()

,我也会一开始就隐藏并清除所有TextView。
@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);
        }
    }
}

答案 1 :(得分:0)

我认为您可以在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]);
        }
    }
}