在Android中的RecyclerView中更新ImageView和TextView OnClick的单卡

时间:2017-07-03 10:32:28

标签: java android android-recyclerview android-adapter android-cardview

我有一个应用程序,我想要执行以下操作:

当用户点击星形图像时,它会更新服务器(将帖子添加到我的 favoritePosts ,并将+1添加到 likesCounter ),但它也应该更新imageView(喜欢不喜欢,反之亦然),以及我点击的cardView上带有喜欢数量的textView。

目前,imageView发生了变化,并且它正确地更新了服务器,但有些东西不能按预期方式工作,例如:

当我点击星形图像视图(将帖子添加到收藏夹)时,某些标题会消失并重新出现,而textView几乎从不更新,当它出现时,它有时会减少而不是增加反之亦然。 在我的数据库中,它已正确更新,如正确添加/删除,喜欢计数器正确更新,当我退出并重新输入活动时,它也会正确更新。

我尝试了thisthisthisthisthis等,但这些解决方案都不适用于我。

UserPostsAdapter:

public class UserPostsAdapter extends RecyclerView.Adapter<UserPostsAdapter.PostsViewHolder> {

    private List<UserPosts> postItems;
    private Context context;
    private SQLiteHandler db;
    private static final String TAG = UserPosts.class.getSimpleName();

    class PostsViewHolder extends RecyclerView.ViewHolder {
        TextView userPostsTitle, userPostsDate, userPost, textViewStars, textViewComments;
        ImageView imageViewDisplayComments, imageViewReply, imageViewCommentStar;
        String pCcontactId, postId;

        PostsViewHolder(View itemView) {
            super(itemView);

            userPostsTitle = (TextView) itemView.findViewById(R.id.userPostsTitle);
            userPostsDate = (TextView) itemView.findViewById(R.id.userPostsDate);
            userPost = (TextView) itemView.findViewById(R.id.userPost);

            textViewStars = (TextView) itemView.findViewById(R.id.textViewStars);
            textViewComments = (TextView) itemView.findViewById(R.id.textViewComments);

            imageViewCommentStar = (ImageView) itemView.findViewById(R.id.imageViewCommentStar);
            imageViewDisplayComments = (ImageView) itemView.findViewById(R.id.imageViewDisplayComments);
            imageViewReply = (ImageView) itemView.findViewById(R.id.imageViewReply);
        }
    }

    public UserPostsAdapter(List<UserPosts> postsList, Context context) {
        this.postItems = postsList;
        this.context = context;
    }

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

        return new UserPostsAdapter.PostsViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final UserPostsAdapter.PostsViewHolder holder, int position) {
        final UserPosts post = postItems.get(position);

        db = new SQLiteHandler(context.getApplicationContext());
        HashMap<String, String> user = db.getUserDetails();
        final String currentUserId = user.get("uid");

        if (post.getTitle() != null && !post.getTitle().isEmpty()) {
            holder.userPostsTitle.setText(post.getTitle());
        } else {
            holder.userPostsTitle.setVisibility(View.GONE);
        }
        holder.userPostsDate.setText(post.getCreatedDateTime());
        holder.userPost.setText(post.getPostedText());
        if (Integer.parseInt(post.getLikesNumber()) > 0) {
            holder.textViewStars.setText(post.getLikesNumber());
        }
        holder.textViewComments.setText(post.getCommentsNumber());

        holder.pCcontactId = post.getUserId();
        final String postId = holder.postId = post.getId();

        if (post.getFavouritePost()) {
            holder.imageViewCommentStar.setImageResource(R.drawable.ic_icon_star_ppdcolor);
        } else {
            holder.imageViewCommentStar.setImageResource(R.drawable.ic_icon_star_outline_ppdcolor);
        }

        holder.imageViewCommentStar.setOnClickListener(new View.OnClickListener() {

            boolean isPressed = post.getFavouritePost();

            @Override
            public void onClick(View v) {
                if (isPressed) {
                    ApiInterface apiService =
                        ApiClient.getClient().create(ApiInterface.class);

                    Call<DefaultResponse> call = apiService.removePostFavourite(currentUserId, postId);
                    call.enqueue(new Callback<DefaultResponse>() {
                        @Override
                        public void onResponse(Call<DefaultResponse> call, retrofit2.Response<DefaultResponse> response) {
                            boolean success = response.body().isSuccess();

                             onFavouriteClicked(position);
                        }

                        @Override
                        public void onFailure(Call<DefaultResponse> call, Throwable t) {

                        Log.e(TAG, t.toString());
                        }
                    });
                } else {
                    ApiInterface apiService =
                        ApiClient.getClient().create(ApiInterface.class);

                    Call<DefaultResponse> call = apiService.addPostFavourite(currentUserId, postId);
                    call.enqueue(new Callback<DefaultResponse>() {
                        @Override
                        public void onResponse(Call<DefaultResponse> call, retrofit2.Response<DefaultResponse> response) {
                            boolean success = response.body().isSuccess();

                             onFavouriteClicked(position);
                        }

                        @Override
                        public void onFailure(Call<DefaultResponse> call, Throwable t) {                           
                            Log.e(TAG, t.toString());
                        }
                    });
                }
                isPressed = !isPressed;
            }
        });

        holder.imageViewDisplayComments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        holder.imageViewReply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AlertDialog.Builder addCommentBuilder = new AlertDialog.Builder(context);
                LayoutInflater inflater = LayoutInflater.from(context);
                View addCommentView = inflater.inflate(R.layout.popup_add_comment_reply, null);
                final EditText addCommentReplyEditText = (EditText) addCommentView.findViewById(R.id.addCommentReplyEditText);
                FloatingActionButton imageViewCommentReply = (FloatingActionButton) addCommentView.findViewById(R.id.imageViewCommentReply);

                addCommentBuilder.setView(addCommentView);
                final AlertDialog addCommentReply = addCommentBuilder.create();
                addCommentReply.show();

                imageViewCommentReply.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        if (!addCommentReplyEditText.getText().toString().isEmpty()) {
                        addCommentReply.dismiss();
                        } else {
                            Toast.makeText(context,
                                    R.string.failure,
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

    private void onFavouriteClicked(int position) {
        UserPosts post = postItems.get(position);
        post.setFavouritePost(!post.getFavouritePost());
        postItems.set(position, post);
        notifyItemChanged(position);
    }

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

Cardview:

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

    <android.support.v7.widget.CardView
        android:id="@+id/userPostsCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="5dp"
        card_view:cardCornerRadius="4dp">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp">

            <TextView
                android:id="@+id/userPostsTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="1dp"
                android:paddingEnd="5dp"
                android:paddingStart="5dp"
                android:textStyle="bold"
                tools:ignore="RtlHardcoded,SpUsage" />

            <TextView
                android:id="@+id/userPostsDate"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/userPostsTitle"
                android:layout_marginTop="1dip"
                android:paddingEnd="5dp"
                android:paddingStart="5dp"
                android:textColor="@color/aluminum"
                tools:ignore="RtlHardcoded,SpUsage" />

            <TextView
                android:id="@+id/userPost"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/userPostsDate"
                android:padding="5dp"/>

            <View
                android:id="@+id/divider"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/userPost"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:background="#B6B6B6" />

            <LinearLayout
                android:id="@+id/idLLUserPostsFooter"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/divider"
                android:orientation="horizontal"
                android:weightSum="9">

                <ImageView
                    android:id="@+id/imageViewCommentStar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/star"
                    android:padding="5dp"/>

                <TextView
                    android:id="@+id/textViewStars"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="3"
                    android:padding="5dp" />

                <ImageView
                    android:id="@+id/imageViewDisplayComments"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/comments"
                    android:padding="5dp"
                    card_view:srcCompat="@drawable/comment_multiple_outline_ppdcolor" />

                <TextView
                    android:id="@+id/textViewComments"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"    
                    android:layout_weight="3"
                    android:padding="5dp" />

                <ImageView
                    android:id="@+id/imageViewReply"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/reply"
                    android:padding="5dp"
                    card_view:srcCompat="@drawable/reply_ppdcolor" />
            </LinearLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>

    <TextView
        android:id="@+id/userAppId"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/number_zero"
        android:visibility="gone" />

    <TextView
        android:id="@+id/userContactId"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/number_zero"
        android:visibility="gone" />
</LinearLayout>

0 个答案:

没有答案