安卓 - 向图像添加视图不会更新 - onClick

总结

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

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

我的假设是将位置传递给方法,以便在调用数组时,它将获得与Sashido类相同的位置,但事实并非如此。我觉得在用户单击新项目后适配器一定无法正确更新。getTagInformation()getLayoutPosition()objects.get(position)

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);
....
}

随着传递到方法中,并且我还手动输入了一个匹配的标记,它仍然只会生成属于该的标记。它似乎不是这个查询正在遍历所有对象。只需从该对象获取标签信息,然后使用这些标签设置所有图像即可。imageIdobjectIdobjectId

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


答案 1

嗨@BIW请按照下面的链接链接

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

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类中,我们需要传递一个,或者我们可以编写在单击项时需要执行的代码。positionrecyclerView


答案 2

解决了问题。与其说位置错误,不如说是因为由于Parse的回调,我试图在填充图像列表之前找到标签,它被填充得太晚了。FindInBackground()

解决方案是,我得到了在填充原始图像列表的原始查询中包含标记名称,x坐标和y坐标的。因此,它们将同时执行,然后我将值传递到 中,然后将其传递给 .JSONArrayJSONArraymodelbind()Adapter

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

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

回收商视图支架:

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());
                }
            }
        });
    }

感谢您的帮助。


推荐