如何更改的LinearLayout BG每个上RecyclerView

问题描述 投票:0回答:1

我有RecyclerView,我想如果值lenght更大的改变LinearLayout(list_root)的背景图片比6.我试过的东西,但它不干活需要改变BG为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);
        }
    }
}
android android-studio android-recyclerview android-linearlayout
1个回答
2
投票

这是不正确的引用外部阵列/值,因为语义Viewholder模式应该知道只有它自己的结构/人次。您应该使用从内部适配器的属性信息。

@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);
        }
© www.soinside.com 2019 - 2024. All rights reserved.