Recyclerview在onClick方法上提供错误的位置

时间:2016-04-08 08:08:31

标签: android facebook android-recyclerview recycler-adapter

我可以根据需要查看应用内的所有项目,但使用onClick方法在Facebook上分享应用内容时,它会显示与点击的项目相关的错误项目。我正在获取图像,标题和其他相关内容,但显示错误的适配器位置。例如如果我点击第三个位置,它将在recyclerview中显示4rth项目的结果。任何人都可以指导我通过这个我在这里做错了。

以下是我的适配器代码:

private LayoutInflater inflater;
Context context;
Uri uri;
String str_uri;
List<Data> dataArray;
private int lastPosition = -1;

public RecyclerViewAdapter(Context context) {
    //this.dataArray = dataArray;
    this.context = context;
    inflater = LayoutInflater.from(context);
}

public void setDataArray(List<Data> dataArray) {
    this.dataArray = dataArray;

}

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

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    Data current = dataArray.get(position);
    holder.textView1.setText(current.heading);
    holder.textView2.setText(current.date);
    holder.textView3.setText(current.brief);

    // Using picasso to fetch image as the user scrolls down ... No need to store
    // all the images during start up.

    uri = Uri.parse(current.getLImage());
    if (current.getLlImage() == null || current.getLlImage().equalsIgnoreCase("null")) {
        Picasso.with(this.context).cancelRequest(holder.image);
        holder.image.setVisibility(View.GONE);
        HomeActivity.setParameters(current.heading, current.date, current.brief, uri);
        setAnimation(holder.relativeLayout, position);

    } else {
        Picasso.with(context).load(uri).into(holder.image);
        HomeActivity.setParameters(current.heading, current.date, current.brief, uri);
        // Animation
        setAnimation(holder.relativeLayout, position);
    }

}

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


public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView image, facebook_Sharing;
    RelativeLayout relativeLayout;
    TextView textView1, textView2, textView3;

    public CustomViewHolder(View itemView) {
        super(itemView);
        textView1 = (TextView) itemView.findViewById(R.id.heading);
        textView2 = (TextView) itemView.findViewById(R.id.date);
        textView3 = (TextView) itemView.findViewById(R.id.brief);
        image = (ImageView) itemView.findViewById(R.id.imageView);
        facebook_Sharing = (ImageView) itemView.findViewById(R.id.facebook_Sharing);
        facebook_Sharing.setOnClickListener(this);
        relativeLayout = (RelativeLayout) itemView.findViewById(R.id.Relative);
    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent(context.getApplicationContext(), HomeActivity.class);
        intent.putExtra("adapterPosition", getAdapterPosition());
        context.startActivity(intent);
    }
}


private void setAnimation(View viewToAnimate, int position) {
    // If the bound view wasn't previously displayed on screen, it's animated

    if (position > lastPosition || position < lastPosition) {

        Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }

}

以下是Facebook活动代码:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.home);
    share = (Button) findViewById(R.id.share);

    callbackManager = CallbackManager.Factory.create();
    facebookLogin = (LoginButton) findViewById(R.id.login_button);
    facebookLogin.registerCallback(callbackManager, callback);
    shareDialog = new ShareDialog(this);
    // login.setReadPermissions("public_profile email");

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

            ShareLinkContent content = new ShareLinkContent.Builder()
                    .setContentTitle(heading1)
                    .setContentDescription(brief1)
                    .setContentUrl(Uri.parse(String.valueOf(imageView1))).build();
            shareDialog.show(content);
        }
    });

}

public static void setParameters(String heading, String date, String brief, Uri imageView) {

    heading1 = heading;
    date1 = date;
    brief1 = brief;
    System.out.println("BBBBBBBBBBBBBBBBBBBB is ::::::::: " + brief1);
    imageView1 = imageView;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

2 个答案:

答案 0 :(得分:1)

尝试使用此代码,将标记设置为 facebook_Sharing 视图并获取onclik()

的位置
  private LayoutInflater inflater;
Context context;
Uri uri;
String str_uri;
List<Data> dataArray;
private int lastPosition = -1;

public RecyclerViewAdapter(Context context) {
    //this.dataArray = dataArray;
    this.context = context;
    inflater = LayoutInflater.from(context);
}

public void setDataArray(List<Data> dataArray) {
    this.dataArray = dataArray;

}

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

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    Data current = dataArray.get(position);
    holder.textView1.setText(current.heading);
    holder.textView2.setText(current.date);
    holder.textView3.setText(current.brief);
    holder.facebook_Sharing.setTag("" + position);
    // Using picasso to fetch image as the user scrolls down ... No need to store
    // all the images during start up.

    uri = Uri.parse(current.getLImage());
    if (current.getLlImage() == null || current.getLlImage().equalsIgnoreCase("null")) {
        Picasso.with(this.context).cancelRequest(holder.image);
        holder.image.setVisibility(View.GONE);
        HomeActivity.setParameters(current.heading, current.date, current.brief, uri);
        setAnimation(holder.relativeLayout, position);

    } else {
        Picasso.with(context).load(uri).into(holder.image);
        HomeActivity.setParameters(current.heading, current.date, current.brief, uri);
        // Animation
        setAnimation(holder.relativeLayout, position);
    }

}

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


public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView image, facebook_Sharing;
    RelativeLayout relativeLayout;
    TextView textView1, textView2, textView3;

    public CustomViewHolder(View itemView) {
        super(itemView);
        textView1 = (TextView) itemView.findViewById(R.id.heading);
        textView2 = (TextView) itemView.findViewById(R.id.date);
        textView3 = (TextView) itemView.findViewById(R.id.brief);
        image = (ImageView) itemView.findViewById(R.id.imageView);
        facebook_Sharing = (ImageView) itemView.findViewById(R.id.facebook_Sharing);
        facebook_Sharing.setOnClickListener(this);
        relativeLayout = (RelativeLayout) itemView.findViewById(R.id.Relative);
    }

    @Override
    public void onClick(View v) {
         int pos = Integer.parseInt("" + v.getTag());
        Intent intent = new Intent(context.getApplicationContext(), HomeActivity.class);
        intent.putExtra("adapterPosition", pos);
        context.startActivity(intent);
    }
}


private void setAnimation(View viewToAnimate, int position) {
    // If the bound view wasn't previously displayed on screen, it's animated

    if (position > lastPosition || position < lastPosition) {

        Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }

}

答案 1 :(得分:0)

如果有任何人来这里解决这个问题,这个解决方案可能对他/她有帮助。

使dataArray静态并直接在共享内容模块中使用其内容将解决此问题。在适配器类中,dataArray应声明为:

public static List<Data> dataArray;

在Facebook Activity onClick监听器中,dataArray应该像:

一样直接访问
share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ShareLinkContent content = new ShareLinkContent.Builder()
                    .setContentTitle(JSONAsync.dataArray.get(adapterPosition).heading)
                    .setContentDescription(JSONAsync.dataArray.get(adapterPosition).brief)
                    .setImageUrl(Uri.parse(JSONAsync.dataArray.get(adapterPosition).getLImage()))
                    .setContentUrl(Uri.parse(String.valueOf(Uri.parse(JSONAsync.dataArray.get(adapterPosition).getNews())))).build();
            shareDialog.show(content);
        }
    });