小编JVN*_*JVN的帖子

如何以编程方式更改RecyclerView项目属性,尤其是recyclerview项目内部的自定义视图

我正在做一些作业,并且想要执行以下操作:列表中的对象具有一个值Tag。我想recyclerview根据Tag值在项目布局中显示/隐藏自定义视图。想法是使用此方法在recyclerview项目上画一条线,以根据Tag值将该项目标记为已连接,而不管其位置。

setVisibility(View.GONE)在以前的项目中使用过,并且只能在简单的视图上使用,但是无论我在哪里放置代码以及如何放置代码,在此示例中都无法使用。

这些是recyclerview项目视图中的自定义视图,它们会膨胀(lineTextViewTopHalflineTextViewBottomHalf

    <LinearLayout

        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.example.taskmodel.view.LineTextView
            android:id="@+id/lineTextViewTopHalf"
            android:layout_width="2dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

        <com.example.taskmodel.view.LineTextView
            android:id="@+id/lineTextViewBottomHalf"
            android:layout_width="2dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

lineTextViewTop/BottomHalf如预期平


public class LineTextView extends AppCompatTextView {

    private Paint paint = new Paint();
    //constructors

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.parseColor("#000000"));
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawLine(0, 0, 0, getHeight(), paint);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我试图在适配器类中显示/隐藏的,但不是(//其中代码什么都不做)


@Override
    public void onBindViewHolder(@NonNull final ElementHolder holder, final int position) {

        holder.tv1.setText(elementModels.get(position).getB());
        holder.tv2.setText(String.valueOf(elementModels.get(position).getA()));
        holder.tv3.setText(String.valueOf(elementModels.get(position).getC())); …
Run Code Online (Sandbox Code Playgroud)

java android android-layout android-recyclerview

5
推荐指数
1
解决办法
64
查看次数