在适配器

时间:2016-06-13 03:13:06

标签: android android-layout checkbox android-recyclerview

我无法找到为什么我的复选框在从自定义适配器调用时无法选中。

上下文:我有一个RecyclerView,Recycler视图中的每个项目都包含一个图像,文本和一个复选框。列表中的每个项目对应一个Friend对象,可以选择其中一些朋友对象。

public class FriendSelectionAdapter extends RecyclerView.Adapter<FriendSelectionAdapter.FriendViewHolder> {

    private final LayoutInflater inflater;
    private List<Friend> friends;
    private Context context;
    private final OnItemClickListener listener;
    private String TAG = "FriendSelectionAD";

    public interface OnItemClickListener {
        void onItemClick( Friend friend );
    }

    public FriendSelectionAdapter(Context context, List<Friend> friends, OnItemClickListener listener ) {
        this.friends = friends;
        this.context = context;
        this.listener = listener;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public FriendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View itemView = inflater.inflate(R.layout.list_item_friend,parent,false);
        return new FriendViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(FriendViewHolder holder, int position) {
        final Friend friend = friends.get(position);
        holder.bind(friend, listener );
        holder.checkbox.setChecked(true);
    }

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

    public void setFriends( List<Friend> friends ) {
        this.friends = new ArrayList<>(friends);
    }

    public Friend removeItem(int position) {
        final Friend friend= friends.remove(position);
        notifyItemRemoved(position);
        return friend;
    }

    public void addItem(int position, Friend friend) {
        friends.add(position, friend);
        notifyItemInserted(position);
    }

    public void moveItem(int fromPosition, int toPosition) {
        final Friend friend = friends.remove(fromPosition);
        friends.add(toPosition, friend);
        notifyItemMoved(fromPosition, toPosition);
    }

    public void animateTo(List<Friend> friends) {
        applyAndAnimateRemovals(friends);
        applyAndAnimateAdditions(friends);
        applyAndAnimateMovedItems(friends);
    }

    private void applyAndAnimateRemovals(List<Friend> newFriends) {
        for (int i = friends.size() - 1; i >= 0; i--) {
            final Friend friend = friends.get(i);
            if (!newFriends.contains(friend)) {
                removeItem(i);
            }
        }
    }

    private void applyAndAnimateAdditions(List<Friend> newFriends) {
        for (int i = 0, count = newFriends.size(); i < count; i++) {
            final Friend friend = newFriends.get(i);
            if (!friends.contains(friend)) {
                addItem(i, friend);
            }
        }
    }

    private void applyAndAnimateMovedItems( List<Friend> newFriends ) {
        for (int toPosition = newFriends.size() - 1; toPosition >= 0; toPosition--) {
            final Friend friend = newFriends.get(toPosition);
            final int fromPosition = friends.indexOf(friend);
            if (fromPosition >= 0 && fromPosition != toPosition) {
                moveItem(fromPosition, toPosition);
            }
        }
    }

    public class FriendViewHolder extends RecyclerView.ViewHolder {
        private TextView nameView;
        private CircleImageView profilePicture;
        private CheckBox checkbox;

        public FriendViewHolder( View view ) {
            super(view);
            this.nameView = (TextView)view.findViewById(R.id.item_friend_name);
            this.profilePicture = (CircleImageView) view.findViewById(R.id.item_friend_profile_picture);
            this.checkbox = (CheckBox) view.findViewById(R.id.item_friend_checkbox);
        }


        public void bind( final Friend friend, final OnItemClickListener listener ) {
            nameView.setText(friend.getDisplayName());
            String profileUrl = "https://graph.facebook.com/" + friend.getId() + "/picture?type=large";
            Picasso.with(FriendSelectionAdapter.this.context).load(profileUrl).into(profilePicture);
            checkbox.setChecked(true);
            checkbox.setSelected(true);
            /*
            initialCheck( friend );

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

            checkbox.setOncheckListener(new CheckBox.OnCheckListener() {
                @Override
                public void onCheck(CheckBox checkbox, boolean b) {
                    itemClicked( friend );
                }
            });
            */

            Log.e(TAG,"Binding friend - name: " + friend.getDisplayName() + " selected: " + friend.isSelected() );
        }
        private void itemClicked( Friend friend ) {
            checkbox.setChecked( !friend.isSelected() );
            friend.setSelected( !friend.isSelected() );
        }
        private void initialCheck( Friend friend ) {
            checkbox.setChecked( friend.isSelected() );
        }

    }
}

每个项目都是这样的:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:clickable="true"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:background="?attr/selectableItemBackground">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/item_friend_profile_picture"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:contentDescription="Facebook profile picture"/>

    <TextView
        android:id="@+id/item_friend_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:layout_gravity="center_vertical"
        android:paddingStart="72dp"
        android:paddingEnd="0dp"
        android:textSize="13sp" />

    <com.gc.materialdesign.views.CheckBox
        android:id="@+id/item_friend_checkbox"
        android:background="@color/colorAccent"
        android:layout_gravity="end|center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="2dp" />

</FrameLayout>

正在调用绑定方法,虽然令人困惑的是为什么复选框的UI没有更新。

谢谢!

1 个答案:

答案 0 :(得分:1)

问题出在您的适配器

public class FriendViewHolder extends RecyclerView.Adapter<FriendViewHolder .MyViewHolder> {

private ArrayList<Fr> arrayList = new ArrayList<>();

public class MyViewHolder extends RecyclerView.ViewHolder {
   public TextView nameView;
    public CircleImageView profilePicture;
    public CheckBox checkbox;

    public MyViewHolder(View view) {
        super(view);
        nameView = (TextView)view.findViewById(R.id.item_friend_name);
        profilePicture = (CircleImageView) view.findViewById(R.id.item_friend_profile_picture);
        checkbox = (CheckBox) view.findViewById(R.id.item_friend_checkbox);
    }
}


public FriendViewHolder (ArrayList<ModelReviewOrder> modelReviewOrders) {
    this.arrayList = modelReviewOrders;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.xml_of_each_row_of_list, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    ModelReviewOrder modelReviewOrder = arrayList.get(position);
    holder.nameView.setText(friend.getDisplayName());
        String profileUrl = "https://graph.facebook.com/" + friend.getId() + "/picture?type=large";
        Picasso.with(FriendSelectionAdapter.this.context).load(profileUrl).into(holder.profilePicture);
        holder.checkbox.setChecked(true);
}

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

}