单击回收器内的Button时,另一个按钮会响应

时间:2018-06-14 18:40:22

标签: android firebase android-recyclerview

我尝试使用firedatabase和recycler视图编写缺席寄存器,方法是如果单击缺席按钮,它将被隐藏并仅显示当前按钮和存在按钮。 问题是,当我点击项目时,另外一个项目也会被点击,但第二个项目没有点击动作(它不会向第一个发送信息给firebase。)

我的回收者视图持有人

       public KViewHolder(View itemView) {
                super(itemView);
                mView=itemView;
            test = (TextView)mView.findViewById(R.id.test);
            MonthYear = new SimpleDateFormat("MM-yyyy");
            fullDate = new SimpleDateFormat("dd-MM-yyyy");
            date = new Date();
            BUprestent= (Button)mView.findViewById(R.id.BPresent);
            BUabsents = (Button)mView.findViewById(R.id.BAbsent);

            BUprestent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //    Calendar calendar = Calendar.getInstance();
                    FirebaseDatabase.getInstance().getReference("presents").child("kachaf").child(MonthYear.format(date)).child(fullDate.format(date)).child(Id).removeValue();

                    mAbsntDatabase = FirebaseDatabase.getInstance().getReference("presents").child("kachaf").child(MonthYear.format(date)).child(fullDate.format(date)).child(Id);
                    mAbsntDatabase.child("uid").setValue(Id);
                    mAbsntDatabase.child("name").setValue(Name);
                    mAbsntDatabase.child("dayOfBirth").setValue(DayOfBirth);
                    mAbsntDatabase.child("wihda").setValue("kachaf");
                    mAbsntDatabase.child("excused").setValue("false");

                    mAbsntDatabase.child("excuse").setValue("/");
                    mAbsntDatabase.child("month").setValue(MonthYear.format(date));
                    mAbsntDatabase.child("date").setValue(fullDate.format(date));

                    BUprestent.setVisibility(View.GONE);
                    BUabsents.setVisibility(View.VISIBLE);
                    //int pp2 = Postion+1;
                     recyclerView.scrollToPosition(Postion +1);

                }

            });
            BUabsents.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    FirebaseDatabase.getInstance().getReference("absents").child("kachaf").child(MonthYear.format(date)).child(fullDate.format(date)).child(Id).removeValue();

                    mAbsntDatabase = FirebaseDatabase.getInstance().getReference("absents").child("kachaf").child(MonthYear.format(date)).child(fullDate.format(date)).child(Id);
                    mAbsntDatabase.child("uid").setValue(Id);
                    mAbsntDatabase.child("name").setValue(Name);
                    mAbsntDatabase.child("dayOfBirth").setValue(DayOfBirth);
                    mAbsntDatabase.child("wihda").setValue("kachaf");
                    mAbsntDatabase.child("excused").setValue("false");
                    mAbsntDatabase.child("excuse").setValue("/");
                    mAbsntDatabase.child("month").setValue(MonthYear.format(date));
                    mAbsntDatabase.child("date").setValue(fullDate.format(date));

                    BUabsents.setVisibility(View.GONE);
                    BUprestent.setVisibility(View.VISIBLE);
                    //int pp2 = Postion+1;
                    recyclerView.scrollToPosition(Postion +1);
                }
            });
}

    private void nextPostion(int postion) {
        Postion = postion;

    }
    public void setName(String name){
        TextView tvName = (TextView) mView.findViewById(R.id.chible_b_name);
        tvName.setText(name);
        Name = name;
    }
    public void setId(String id){
        Id = id;
    }
    public void setDOB(String DOB){
        TextView tvDOB = (TextView) mView.findViewById(R.id.chible_b_DOB);
        tvDOB.setText(DOB);
        DayOfBirth = DOB;
    }
    public void setReidence(String residence){
        TextView tvDOB = (TextView) mView.findViewById(R.id.chibble_b_residence);
        tvDOB.setText(residence);
    }
    public void setImage(Context ctx, String image){
        CircleImageView img = (CircleImageView) mView.findViewById(R.id.chible_b_image);
        Picasso.with(ctx).load(image).noFade().into(img);
    }
}

项目XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginLeft="7dp"
    android:layout_marginRight="7dp"
    android:background="@drawable/contitmlayoutshape"
    android:clickable="false"

    >


<LinearLayout
    android:id="@+id/itm_brws_achbel_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp"

    android:background="@drawable/itmlayoutshape"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        >
    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/chible_b_image"
        android:layout_gravity="center_vertical"        />

    </LinearLayout>

    <LinearLayout
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:gravity="center"
            android:padding="5dp"
        android:textColor="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="chible name"
        android:id="@+id/chible_b_name"

        />

        <TextView
            android:gravity="center"
            android:visibility="gone"
            android:padding="5dp"
            android:textColor="@color/white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="chible date of birth"
            android:id="@+id/chible_b_DOB"

            />
        <TextView
            android:gravity="center"
            android:padding="5dp"
            android:textColor="@color/white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"

            android:text=" chible residence "
            android:id="@+id/chibble_b_residence"

            />
        <TextView
            android:gravity="center"
            android:padding="5dp"
            android:textColor="@color/white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"

            android:text=" chible residence "
            android:id="@+id/test"

            />

    </LinearLayout>

</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >


            <Button
                android:visibility="visible"
                android:layout_weight="1"
                android:background="#ef8585"
                android:layout_width="match_parent"
                android:layout_height="38dp"
                android:drawableLeft="@drawable/ic_clear_black_24dp"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:textSize="12dp"
                android:text="absent"
                android:textStyle="bold"
                android:id="@+id/BAbsent"



                />

            <Button
                android:layout_weight="1"
                android:visibility="visible"

                android:paddingLeft="5dp"
                android:background="#aaf4a7"
                android:layout_width="match_parent"
                android:layout_height="38dp"
                android:drawableRight="@drawable/ic_done_black_24dp"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"
                android:textSize="12dp"
                android:id="@+id/BPresent"
                android:text="present"
                android:textStyle="bold"

                />


    </LinearLayout>

</LinearLayout>

Firebase recyceler适配器

 final FirebaseRecyclerAdapter<model_kechaf,KViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<model_kechaf, KViewHolder>(
                model_kechaf.class,
                R.layout.itm,
                KViewHolder.class,mDatabase

        ) {
            @Override
            protected void populateViewHolder(final KViewHolder viewHolder, model_kechaf model, final int position) {

                viewHolder.setName(model.getName());
                viewHolder.setDOB(model.getDayOfBirth().toString());
                viewHolder.setReidence(model.getResidence());
                viewHolder.setImage(getApplicationContext(),model.getPhoto());
                viewHolder.nextPostion(position);
                viewHolder.setId(model.getId());
 }

        };

here is a issue GIF

1 个答案:

答案 0 :(得分:0)

正如我的理解..你必须为每个onBindView()的缺席/现在按钮调用setVisibility

public void onBindView(){
 // here you have to check if the item[position]. is marked as absent ? show present 
  button else show absent button 

// in another words you have to **reset** the view state for each onBindView calling .. based on user interactions.
  u have to cache for each cell what the action does the user take with it.
}