我想在android中单击textView来更改cardview的textView文本

时间:2017-01-18 12:41:14

标签: android

我用recyclerview创建了cardview,在单卡上我放了四个textView,我想在点击相同的textView时更改textView的文本。 当我试图这样做时,它也适用于其他卡。就像我有四张卡片,每张卡片都有像textView一样,当我将文字从喜欢改为喜欢点击像facebook一样,然后在滚动上加载更多数据。但是在加载数据之后,我发现默认情况下textView已经在新加载的卡上发生了变化。这是我的适配器:

package common;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.rate.sky.rate.LoginActivity;
import com.rate.sky.rate.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.targetActivity;
import static android.R.attr.width;
import static com.rate.sky.rate.R.attr.height;
import static com.rate.sky.rate.R.attr.title;

/**
 * Created by sky on 12/18/2016.
 */

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

    UserAuth auth;
    private Activity activity;
    private ImageLoader imageLoader;
    private ArrayList<ListItem> list;
    ListItem listItem;


    public CardAdapter(Activity activity,ArrayList<ListItem> listVal){
        super();
        this.activity = activity;
        this.list=listVal;

    }

    public ListItem getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        listItem =  getItem(position);
        try {
            imageLoader = CustomVolleyRequest.getInstance(activity.getApplicationContext())
                    .getImageLoader();
            imageLoader.get(listItem.getUrl(), ImageLoader.getImageListener(holder.thumbnail,
                    android.R.drawable.ic_dialog_alert,
                    android.R.drawable.ic_dialog_alert));
            holder.thumbnail.setImageUrl(listItem.getUrl(), imageLoader);
            holder.textViewContent.setText(listItem.getTitle());
        }catch (Exception e){
          Log.d("Error--",e.toString());
        }
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        public NetworkImageView thumbnail;
        public ImageView imageShare, imageComment, imageVote, imageDownvote;
        public TextView textViewVoteButton, textViewDownVoteButton, textViewCommentButton, textViewContent;
        public TextView txtVoteCount, txtDownVoteCount, txtCommentCount;
        public Button rateButton;
        CustomTabsIntent customTabsIntent;
        CustomTabsIntent.Builder intentBuilder;


        public ViewHolder(final View itemView) {
            super(itemView);
            //Toast.makeText(itemView.getContext(),"Hello",Toast.LENGTH_LONG).show();
            auth = new UserAuth(itemView.getContext());
            thumbnail = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
            rateButton = (Button) itemView.findViewById(R.id.button2);
            imageShare = (ImageView) itemView.findViewById(R.id.imageView4);
            imageComment = (ImageView) itemView.findViewById(R.id.imageViewComment);
            textViewContent = (TextView) itemView.findViewById(R.id.textView);
            textViewVoteButton = (TextView) itemView.findViewById(R.id.textView3);
            textViewDownVoteButton = (TextView) itemView.findViewById(R.id.textView5);
            textViewCommentButton = (TextView) itemView.findViewById(R.id.textView4);
            imageVote = (ImageView) itemView.findViewById(R.id.imageViewVote);
            imageDownvote = (ImageView) itemView.findViewById(R.id.imageViewDownvote);
            txtVoteCount = (TextView) itemView.findViewById(R.id.textViewVoteCount);
            txtDownVoteCount = (TextView) itemView.findViewById(R.id.textViewDownvote);
            txtCommentCount = (TextView) itemView.findViewById(R.id.textViewComments);
            /*Custome chrome tab view*/
            intentBuilder = new CustomTabsIntent.Builder();
            // Set toolbar(tab) color of your chrome browser
            intentBuilder.setToolbarColor(ContextCompat.getColor(itemView.getContext(), R.color.colorPrimary));
            customTabsIntent = intentBuilder.build();
            /*End*/
            /*
              Changing the text of textview 
            */
            imageVote.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (auth.getLoginWith().equals("G") || auth.getLoginWith().equals("F")) {
                        vote();
                    } else {
                        Intent intent = new Intent(v.getContext(), LoginActivity.class);
                        v.getContext().startActivity(intent);
                    }
                }
            });

            textViewVoteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (auth.getLoginWith().equals("G") || auth.getLoginWith().equals("F")) {
                        vote();
                    } else {
                        Intent intent = new Intent(v.getContext(), LoginActivity.class);
                        v.getContext().startActivity(intent);
                    }
                }
            });



        }

        public void vote() {
            RotateAnimation anim = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setInterpolator(new LinearInterpolator());
            anim.setDuration(400);
            imageVote.startAnimation(anim);
            ColorStateList mList = textViewVoteButton.getTextColors();
            int color = mList.getDefaultColor();
            if (color == Color.BLUE) {
                textViewVoteButton.setTextColor(Color.GRAY);
            } else {
                textViewVoteButton.setTextColor(Color.BLUE);

            }
        }


    }
} 

1 个答案:

答案 0 :(得分:1)

RecyclerView回收视图,这意味着当您滚动视图时,它会获取一个已存在的视图,并将其重新用于列表中的另一个条目。该视图不会被神奇地重置,它完全按照您在上次输入时使用的方式保留。

vote方法中,当用户单击该条目时,您显然会更改文本视图的颜色,但是当它重新用于其他条目时,您不会将颜色重置为默认状态。所以它只保留以前的颜色。

您需要以某种方式跟踪已经投票的条目(不是在视图或视图中,而是在模型中的某个位置),然后在onBindViewHolder方法中为每个视图设置正确的颜色

基本上,始终必须手动重置对onBindViewHolder方法中视图所做的所有更改。