隐藏多个空文本视图

我正在使用一个RecyclerView, 和一个CardView, 其中包括多个TextViews。每张卡片都需要显示不同数量的TextViews,因此有些卡片会是空的,如果是空的,它只会在卡片上显示一个空白区域。


这一切都在 的适配器中执行RecyclerView。


例如:


Card 1: (id 1)

3 TextViews showing

Card 2: (id 2)

10 TextViews showing

Card 3: (id 3)

7 TextViews showing

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


我试图创建一个简单的循环来循环遍历所有文本视图并在它们为空时隐藏它们。但是,我尝试的方式不起作用,因为它使用字符串作为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;

    }


繁星淼淼
浏览 76回答 2
2回答

杨__羊羊

我认为你可以在你的 DataObject 类中做一个在列表或数组中返回所有消息的方法。// Somewhere in your DataObject class, it's important to add all the messages, even if they are emptypublic String[] getAllMessages(){&nbsp; &nbsp; return new String[]{messages1, messages2, messages3};}然后你可以创建一个 TextView 列表来循环它,比如:private ArrayList<DataObject> mDataset;Context context;//Here your listprivate List<TextView> tvList = new ArrayList();public static class DataObjectHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; public DataObjectHolder(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; setting1 = (TextView) itemView.findViewById(R.id.textView);&nbsp; &nbsp; &nbsp; &nbsp; setting2 = (TextView) itemView.findViewById(R.id.textView2);&nbsp; &nbsp; &nbsp; &nbsp;// Then you add all your TextViews to your list&nbsp; &nbsp; &nbsp; &nbsp;tvList.add(setting1);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;tvList.add(setting2);&nbsp;&nbsp; &nbsp; }}然后在你的onBindViewHolder方法中你可以循环你的列表:@Overridepublic void onBindViewHolder(DataObjectHolder holder, int position) {&nbsp; &nbsp; String[] messages = mDataset.get(position).getAllMessages(); // The method that we just code&nbsp; &nbsp; for(int i = 0; i < tvList.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; if(messages[i].isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvList.get(i).setVisibility(View.GONE);&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tvList.get(i).setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tvList.get(i).setText(messages[i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

墨色风雨

就我个人而言,我会从您的 textViews 创建一个数组,并循环遍历所有这些数组。如果他们的文本长度 == 0 隐藏文本视图,否则显示。如果public void onBindViewHolder()@Overridepublic void onBindViewHolder(DataObjectHolder holder, int position) {&nbsp; &nbsp; for(TextView textView : textViews) {&nbsp; &nbsp; &nbsp; &nbsp; textView.setText("");&nbsp; &nbsp; &nbsp; &nbsp; textView.setVisibility(View.GONE);&nbsp; &nbsp; }&nbsp; &nbsp; int id= mDataset.get(position).getmIndex();&nbsp; &nbsp; if (id == 1) {&nbsp; &nbsp; &nbsp; &nbsp; holder.setting1.setText(mDataset.get(position).getmText1());&nbsp; &nbsp; } else if (id == 2) {&nbsp; &nbsp; &nbsp; &nbsp; holder.setting1.setText(mDataset.get(position).getmText1());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting2.setText(mDataset.get(position).getmText2());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting3.setText(mDataset.get(position).getmText3());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting4.setText(mDataset.get(position).getmText4());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting5.setText(mDataset.get(position).getmText5());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting6.setText(mDataset.get(position).getmText6());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting7.setText(mDataset.get(position).getmText7());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting8.setText(mDataset.get(position).getmText8());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting9.setText(mDataset.get(position).getmText9());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting10.setText(mDataset.get(position).getmText10());&nbsp; &nbsp; } else if (id == 3) {&nbsp; &nbsp; &nbsp; &nbsp; holder.setting1.setText(mDataset.get(position).getmText1());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting2.setText(mDataset.get(position).getmText2());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting3.setText(mDataset.get(position).getmText3());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting4.setText(mDataset.get(position).getmText4());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting5.setText(mDataset.get(position).getmText5());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting6.setText(mDataset.get(position).getmText6());&nbsp; &nbsp; &nbsp; &nbsp; holder.setting7.setText(mDataset.get(position).getmText7());&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; for(TextView textView : textViews) {&nbsp; &nbsp; &nbsp; &nbsp; if (textView.getText().length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;textView.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java