如何在 RecyclerView 上为每个更改 LinearLayout bg

How to change LinearLayout bg for each on RecyclerView

我有 RecyclerView,我想更改 LinearLayout(list_root) 的背景图像,如果值长度大于 6.I 尝试了一些但没有 work.I需要为 each.How 更改背景,我应该怎么做?

list_item XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp">

    <LinearLayout
        android:id="@+id/list_root"
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:orientation="vertical"
        android:background="@drawable/macbg"
        android:padding="16dp">



        <LinearLayout
            android:id="@+id/bombalinerarsiv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="-12dp"
            android:orientation="horizontal">



            <TextView
                android:id="@+id/list_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:background="@android:color/transparent"
                android:text="@string/yukleniyor"
                android:textSize="19dp" />

        </LinearLayout>





        <LinearLayout
            android:id="@+id/bamba2arsiv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="-12dp"
            android:orientation="horizontal">



            <TextView
                android:id="@+id/list_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:background="@android:color/transparent"
                android:text="@string/yukleniyor"
                android:textSize="22dp"
                android:textStyle="bold"/>

        </LinearLayout>




    </LinearLayout>




</android.support.v7.widget.CardView>

主要xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".delta">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

主要代码

public class delta extends AppCompatActivity {
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private FirebaseRecyclerAdapter adapter;

    String barzoka;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delta);



        recyclerView = findViewById(R.id.list);



        linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        fetch();
    }

    private void fetch() {
        Query query = FirebaseDatabase.getInstance()
                .getReference()
                .child("posts");

        FirebaseRecyclerOptions<Model> options =
                new FirebaseRecyclerOptions.Builder<Model>()
                        .setQuery(query, new SnapshotParser<Model>() {
                            @NonNull
                            @Override
                            public Model parseSnapshot(@NonNull DataSnapshot snapshot) {
                                return new Model(snapshot.child("id").getValue().toString(),
                                    barzoka = snapshot.child("title").getValue().toString(),
                                        snapshot.child("desc").getValue().toString());
                            }
                        })
                        .build();

        adapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item, parent, false);

                return new ViewHolder(view);
            }


            @Override
            protected void onBindViewHolder(ViewHolder holder, final int position, Model model) {
                holder.setTxtTitle(model.getmTitle());
                holder.setTxtDesc(model.getmDesc());

                holder.root.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(delta.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
                    }
                });
            }

        };
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout root;
        public TextView txtTitle;
        public TextView txtDesc;

        public ViewHolder(View itemView) {
            super(itemView);



            root = itemView.findViewById(R.id.list_root);

            if (barzoka.length()>6) {
                root.setBackgroundResource(R.drawable.macbg2);
            }




            txtTitle = itemView.findViewById(R.id.list_title);
            txtDesc = itemView.findViewById(R.id.list_desc);
        }

        public void setTxtTitle(String string) {
            txtTitle.setText(string);
        }


        public void setTxtDesc(String string) {
            txtDesc.setText(string);
        }
    }
}

引用外部 array/value 是不正确的,因为从语义上讲,Viewholder 模式应该只知道它自己的 structure/views。您应该使用来自内部适配器属性的信息。

@Override 
        protected void onBindViewHolder(ViewHolder holder, final int position, Model model) {
            holder.setTxtTitle(model.getmTitle());
            holder.setTxtDesc(model.getmDesc());

            holder.root.setOnClickListener(new View.OnClickListener() {
                @Override 
                public void onClick(View view) {
                    Toast.makeText(delta.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
                } 
            }); 
            if (model.getmTitle().length() > 6) {
                    holder.root.setBackgroundResource(R.drawable.macbg2);
             } else holder.root.setBackgroundResource(R.drawable.macbg);
        }