简体   繁体   English

如何在回收者视图的一个元素上添加浮动textview?

[英]How to add floating textview on one element of a recycler view?

I have a RecyclerView as follows 我有一个RecyclerView如下

<android.support.v7.widget.RecyclerView
    android:id="@+id/drawerList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/nav_header_container"
    android:layout_marginTop="15dp" />

I have a string array like this 我有一个这样的字符串数组

 <string-array name="my_array">
    <item>@string/item_1</item>
    <!--<item>@string/item_2</item>-->
    <item>@string/item_3</item>
    <item>@string/item_4</item>
    <item>@string/item_5</item>
    <item>@string/item_6</item>

This string array is used to display data in RecyclerView. 此字符串数组用于在RecyclerView中显示数据。 I want to display a textview alongside with item_4, Is it possible? 我想与item_4一起显示textview, 可以吗? How to make it? 怎么做?

From Activity Pass this list to RecyclerView adapter 从“活动”中将此列表传递到RecyclerView适配器

try {
      String[] array = getApplicationContext().getResources().getStringArray(R.array.my_array);
      List<String> list = new ArrayList<String>();
      list = Arrays.asList(array);

      //pass this list to RecyclerView adapter

    }catch (Exception e){e.printStackTrace();}

在您的onBindViewHolder方法中,只需检查要显示的元素是否不同,然后对其进行处理即可。

Some solutions: 一些解决方案:

1.- You can concat a string to the 4th item when you check the position in the onBindViewHolder. 1.-当您在onBindViewHolder中检查位置时,可以将字符串连接到第四项。

2.- You can create a layout and inflate it on onCreateViewHolder which has two textViews one for the items and one for the text that will go next to it. 2.-您可以在onCreateViewHolder上创建一个布局并将其充气,该布局具有两个textViews,一个用于项目,另一个用于将在其旁边的文本。 When you detect that you need to show the second textview change the content and the visibility of the textview. 当您检测到需要显示第二个textview时,请更改textview的内容和可见性。

<LinearLayout android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >

<TextView
    android:id="@+id/items"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textViewNextToItemOfChoice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" />
</LinearLayout> 

Then on code you just change the visibility of textview "textViewNextToItemOfChoice" and the content of it. 然后在代码上,您只需更改textview“ textViewNextToItemOfChoice”的可见性及其内容。

I think you should make an object (You own model) instead of using just an array of string to populate the list. 我认为您应该制作一个对象(您自己的模型),而不是仅使用字符串数组来填充列表。 In that way you can create an object with 2 Strings and in the adapter when you check if it has both string you show them. 这样,您可以创建带有2个字符串的对象,并在适配器中检查对象是否同时具有两个字符串时显示它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM