线性布局背景颜色不会以编程方式更改

时间:2018-06-03 04:47:37

标签: android android-recyclerview android-linearlayout background-color

我正在为锁定和解锁时间创建一个活动日志,我需要根据锁定或解锁对我的背景颜色进行排序。但是,无论我放置什么,我似乎无法改变线性布局的背景颜色。非常感谢任何帮助,谢谢!

这是我的主要活动

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;

private List<ListItem> listItems;

DatabaseReference database;
@Override
protected void onCreate(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
    View view = inflater.inflate(R.layout.list_item, null);
    final LinearLayout layout= (LinearLayout) view.findViewById(R.id.damsi);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    // SharedPreferences preferences=getSharedPreferences(LOCK_PREFS,MODE_PRIVATE);
    database = FirebaseDatabase.getInstance().getReference("Activity Log/device321");

    listItems = new ArrayList<>();

    database.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot usersnapshot : dataSnapshot.getChildren()) {
                Map<String, String> map = (Map<String, String>) usersnapshot.getValue();
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    if (entry.getKey().equals("LockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
                    if (entry.getKey().equals("UnlockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
                    listItems.add(new ListItem(entry.getKey(), entry.getValue()));
                }


                //   ListItem listItem = usersnapshot.getValue(ListItem.class);
            }
            recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
            adapter = new MyAdapter(listItems, MainActivity.this);
            recyclerView.setAdapter(adapter);
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
  }
}

这是我的XML文件

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

<android.support.v7.widget.CardView

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin">

    <LinearLayout
        android:id="@+id/damsi"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#baa40f"
        android:orientation="vertical"
        android:padding="16dp"
        android:visibility="visible">

        <TextView
            android:id="@+id/textViewHead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:text="Heading"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/textViewDesc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:text="Description" />
    </LinearLayout>

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

</LinearLayout>

MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private List<ListItem> listItems;
private Context context;

public MyAdapter(List<ListItem> listItems, Context context) {
    this.listItems = listItems;
    this.context = context;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    ListItem listItem = listItems.get(position);


    holder.textViewHead.setText(listItem.getHead());
    holder.textViewDesc.setText(listItem.getDesc());

}

@Override
public int getItemCount() {
    return listItems.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    public LinearLayout layout;
    public TextView textViewHead;
    public TextView textViewDesc;


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

        textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
        textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);

    }
}


}

1 个答案:

答案 0 :(得分:2)

从MainActivity中删除此代码

 LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        View view = inflater.inflate(R.layout.list_item, null);
        final LinearLayout layout= (LinearLayout) view.findViewById(R.id.damsi);

从OnDataChangedMethod

中删除这些行
if (entry.getKey().equals("LockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
                    if (entry.getKey().equals("UnlockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }

在ViewHolder类中,在viewholder构造函数中添加这些行。

layout= (LinearLayout) view.findViewById(R.id.damsi);

在onBindViewHolder方法中添加这些行。

String key = listItem.getHead();
if(key.equals("LockTime"))
{
    System.out.println(entry.getKey());
    layout.setBackgroundColor(Color.parseColor("#FFB71616"));
}
if(key..equals("UnlockTime")){
    System.out.println(entry.getKey());
    layout.setBackgroundColor(Color.parseColor("#FF001600"));
    //I changed the color as both were having same hexcodes :P
}
holder.textViewHead.setText(listItem.getHead());
holder.textViewDesc.setText(listItem.getDesc());