Android - 向图像添加视图无法更新 - onClick

时间:2016-09-23 14:54:46

标签: java android parse-platform android-recyclerview

摘要

当用户点击RecyclerView项目时,我想根据存储在BaaS [Sashido]中的信息为该图像添加标签(X坐标,Y坐标和标签)名称)。但是,我遇到的问题是没有得到这个位置。单击图像时创建一个Toast,它显示与视图本身对应的正确位置。 (开头为零,依旧等等)

但是一旦用户点击列表中的另一个项目,如何更新位置,以便与Sashido数组中的位置对应的标记与RecyclerView中的位置匹配,因为此刻Sashido类的第一行用该行的标签填充所有图像。

我的假设是使用getTagInformation()将位置传递给getLayoutPosition()方法,这样当调用objects.get(position)数组时,它将获得Sashido类的相同位置但它不是“T。我觉得在用户点击新项目后,必须不能正确更新适配器。

onBindViewHolder:

@Override
public void onBindViewHolder(RecyclerViewHolderPreviousPosts holder, int position) {
    holder.bind(previousPostsList.get(position), listener);
}

onBind:

void bind(final PreviousPostsDataModel model, final OnItemClickListener listener) { ...

uploadedImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        if (count == 0) {
                            imageid = model.getImageId();
                            Toast.makeText(App.getContext(), "Image ID: " + imageid, Toast.LENGTH_SHORT).show();
                            Toast.makeText(App.getContext(), "Position: " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
                            getTagInformation(getLayoutPosition());
                        } else {
                            Log.e("qwert", "" + imageid);
                            imageContainer.removeAllViews();
                            imageContainer.addView(uploadedImage);
                            count = 0;
                        }
                }
            });
... }

getTagInformation:

private void getTagInformation(final int position) {
                ParseQuery<ParseObject> query = ParseQuery.getQuery("FashionFeed");
                query.findInBackground(new FindCallback<ParseObject>() {
                    @Override
                    public void done(List<ParseObject> objects, ParseException e) {
                        if (e == null) {
                            Toast.makeText(context, "" + position, Toast.LENGTH_SHORT).show();
                            JSONArray tagNamesArray = objects.get(position).getJSONArray("tagName");
                            JSONArray posXArray = objects.get(position).getJSONArray("tagPointX");
                            JSONArray posYArray = objects.get(position).getJSONArray("tagPointY");

                            for (int i = 0; i < tagNamesArray.length(); i++) {
                                for (int t = 0; t < tagNamesArray.length(); t++) {
                                    tagNames.add(tagNamesArray.optString(t));
                                    tagXPositions.add(posXArray.optString(t));
                                    tagYPositions.add(posYArray.optString(t));

                                }

                                for (int o = 0; o < tagNamesArray.length(); o++) {
                                    tag = new TextView(App.getContext());
                                    tag.setX(Float.parseFloat(tagXPositions.get(o)));
                                    tag.setY(Float.parseFloat(tagYPositions.get(o)));
                                    tag.setText(tagNames.get(o));
                                    tag.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                                    tag.setMaxLines(1);
                                    tag.setTextSize(11);
                                    tag.setClickable(true);
                                    tag.setHintTextColor(Color.WHITE);
                                    tag.setTextColor(Color.WHITE);
                                    tag.setBackgroundResource(R.drawable.tags_rounded_corners);
                                    imageContainer.addView(tag);
                                    count = 1;
                                }
                            }
                        } else {
                            Toast.makeText(context, "" + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                });
        }

我也试过

public void getTagInformation(String imageid) {
ParseQuery query = ParseQuery.getQuery("FashionFeed");
query.WhereEqualTo("objectId", imageId);
....
}

imageId传递给方法,并且我手动输入将匹配的objectId,它仍然只会生成属于objectId的标记。它似乎并不是这个查询通过所有对象。只需从该对象获取标签信息,然后使用这些标签设置所有图像。

如果您需要我提供更多代码,我非常乐意。

3 个答案:

答案 0 :(得分:4)

您好@BIW请点击以下链接link

onBindViewHolder中,您每次都要添加侦听器,因此每次返回相同的对象时,recyclelerView ViewHolder模式使用相同的对象来呈现onBindViewHolder中的recyclelerView项。因此,您需要在创建持有者对象时添加并设置侦听器,以便获得正确的位置。

package com.subbu.moviemasti.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.subbu.moviemasti.Constants;
import com.subbu.moviemasti.R;
import com.subbu.moviemasti.entities.Movie;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by subrahmanyam on 25-11-2015.
 */
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {

    private final List<Movie> movieList;
    private onRecyclerViewItemClickListener mItemClickListener;

    public MovieAdapter(List<Movie> movieList) {
        this.movieList = movieList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.grid_item, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Movie movie = movieList.get(position);
        String imageUrl = Constants.MOVIE_POSTER_BASE_URL + movie.getPosterPath();
        if (imageUrl != null) {
            Picasso.with(holder.posterImage.getContext()).load(imageUrl).
                    placeholder(R.drawable.img_default).
                    into(holder.posterImage);
        }
    }

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

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

    public void setOnItemClickListener(onRecyclerViewItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    public interface onRecyclerViewItemClickListener {
        void onItemClickListener(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.poster)
        ImageView posterImage;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            mItemClickListener.onItemClickListener(v, getAdapterPosition());
        }
    }

}

在我们创建adaper对象的地方,我们需要设置监听器 比如adapter.setOnItemClickListener(this)

class MyActivity extendsActivity implements onRecyclerViewItemClickListener {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    manager = new GridLayoutManager(getActivity(), cols);
            gridView.setLayoutManager(manager); 
gridView.setAdapter(adapter);
adapter.setOnItemClickListener(this);
}
 @Override
        public void onItemClickListener(View view, int position) {
            //Do wantever you want to do
        }
}

在ViewHolder类中,我们需要传递position的{​​{1}},或者我们可以编写单击项目时需要执行的代码。

答案 1 :(得分:1)

解决了这个问题。这不是因为位置错误,而是因为我在填充图片列表之前试图找到标签,因为Parse的FindInBackground()回调,它的填充太晚了。

解决方案是我得到了JSONArray,其中包含填充原始图像列表的原始查询中的标记名称,x-coordindates和y坐标。所以它们会同时执行,然后我将JSONArray值传递给model,然后传递给bind()中的Adapter函数。

之前,问题是适配器将所有标记绑定到每个图像上,因此当图像上发生click事件时,它只会将标记添加到当前选中的imageContainer(在当前位置),而不是任何特定位置的任何特定视图。

这样就可以在每次单击图像时始终找到数据库中的第一个对象并将其分配给所选图像。 [由于点击事件触发getTagInformation()功能)。 通过getTagInformation Bind函数中ViewHolder方法的自动化,我能够使用正确的标记填充每个图像,并使用分配的onClickListener进一步操作它到图像如下所示:

RecyclerView持有人:

public class RecyclerViewHolderPreviousPosts extends RecyclerView.ViewHolder implements View.OnClickListener {
        // View holder for gridview recycler view as we used in listview
        public TextView createdAt;
        public ImageView uploadedImage;
        public TextView caption;
        TextView number_of_likes;
        TextView number_of_comments;
        TextView number_of_tags;
        public ImageView comments;
        public RelativeLayout imageContainer;


    RecyclerViewHolderPreviousPosts(View view) {
        super(view);
        // Find all views ids
        this.createdAt = (TextView) view
                .findViewById(R.id.created_date);
        this.uploadedImage = (ImageView) view
                .findViewById(R.id.image);
        this.caption = (TextView) view
                .findViewById(R.id.caption_post);
        this.number_of_likes = (TextView) view
                .findViewById(R.id.number_of_likes);
        this.number_of_comments = (TextView) view
                .findViewById(R.id.number_of_comments);
        this.number_of_tags = (TextView) view
                .findViewById(R.id.number_of_tags);
        this.comments = (ImageView) view
                .findViewById(R.id.comments_image);
        this.imageContainer = (RelativeLayout) view
                .findViewById(R.id.image_container);
        view.setOnClickListener(this);
    }

    void bind(PreviousPostsDataModel model1, final int position) { ....
        model = previousPostsList.get(position);
        getTagInformation();
....}

    private void getTagInformation() {
        for (int o = 0; o < model.getTagSize(); o++) {
            tag = new TextView(App.getContext());
            tag.setX(Float.parseFloat(model.getXpoints(o)));
            tag.setY(Float.parseFloat(model.getYpoints(o)));
            Log.e("x", "" + tag.getX());
            Log.e("y", "" + tag.getY());
            tag.setText(model.getTagName(o));
            tag.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            tag.setMaxLines(1);
            tag.setTextSize(11);
            tag.setClickable(true);
            tag.setHintTextColor(Color.WHITE);
            tag.setTextColor(Color.WHITE);
            tag.setBackgroundResource(R.drawable.tags_rounded_corners);
            imageContainer.addView(tag);
            tags.add(tag);
        }
    }

    @Override
    public void onClick(View v) {
        if (count == 0) {
            for (int i = 0; i < tags.size(); i++) {
                tags.get(i).setVisibility(View.INVISIBLE);
            }
            count = 1;
        }
        else {
            for (int j = 0; j < tags.size(); j++) {
                tags.get(j).setVisibility(View.VISIBLE);
            }
            count = 0;
        }
    }
}

个人资料片段[原始查询]:

private void populateSelectedUserRecyclerView(String objectid) {
        ParseQuery<ParseObject> query = ParseQuery.getQuery("FashionFeed");
        query.whereEqualTo("uploader", ParseObject.createWithoutData("_User", objectid));
        query.orderByDescending("createdAt");
        Log.e("get order", "ordered");
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                Log.e("gets done", "gets into done");
                if(e == null) {
                    if (objects.size() > 0) {
                        Log.e("does it get here", "it got here");

                        latestPostList = new ArrayList<>();
                        for (ParseObject j : objects) {
                            JSONArray tagNamesArray = j.getJSONArray("tagName");
                            JSONArray posXArray = j.getJSONArray("tagPointX");
                            JSONArray posYArray = j.getJSONArray("tagPointY");
                            latestPostList.add(new PreviousPostsDataModel(tagNamesArray, posXArray, posYArray));
                        }
                    }
                    else {
                        no_follow_display.setVisibility(View.VISIBLE);
                        no_follow_display.setText(R.string.no_posts);
                        no_follow_display.bringToFront();
                        recyclerView.setVisibility(View.GONE);
                    }

                    adapter = new RecyclerViewAdapterPreviousPosts(getActivity(), latestPostList, listener);
                    recyclerView.setAdapter(adapter);// set adapter on recyclerview

                    adapter.notifyDataSetChanged();
                }
                else {
                    Log.e("failed", "failed" + e.getMessage());
                }
            }
        });
    }

感谢您的帮助。

答案 2 :(得分:0)

如果我正确理解问题,那么ParseQuery不会以相同的顺序返回对象。因此,您的RecycleView中对象的位置与ParseDatabase中相同的假设是错误的。

如果你想从Sashido获得TAG你应该首先与图像建立一些关系,例如附加列。然后使查询白色额外参数如:

<%@page language="java" session="true" %>

或者像你现在一样得到所有ParseObjects并找到与你的图像相对应的TAG,然后获取信息。