毕加索和回收视图的问题 - 图像在滚动时刷新,但在错误的图像视图中



我正在开发一个简单的应用程序,我在其中搜索电视节目,该应用程序会生成对TMDB的GET请求。从那里,我尝试在卡片视图中显示结果的图像和详细信息。我正在使用毕加索从TMDB加载图像,它工作正常。问题是当我滚动时,首先加载的图像会刷新到其他图像视图,而不是显示特定图像的图像视图。

这是我的异步任务代码

class JsonTask extends AsyncTask<String,String,String[][]>{
    private List<FeedItem> persons;
    String [][] Finalarray = new String[200][3];
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected String[][] doInBackground(String... params) {
        HttpURLConnection urlConnection = null;
        StringBuffer buffer = new StringBuffer();
        String line;
        BufferedReader reader = null;
        try{
            URL Request1 = new URL(params[0]);
            urlConnection = (HttpURLConnection) Request1.openConnection();
            urlConnection.setRequestProperty("Accept", "application/json");
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoInput(true);
            urlConnection.connect();
            int responseCode = urlConnection.getResponseCode();
            Log.d("DEBUG_TAG", "The response code is: " + responseCode + " " + urlConnection.getResponseMessage());
            InputStream in = urlConnection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(in));
            while ((line = reader.readLine())!=null){
                Log.i("check","Line is"+line);
                buffer.append(line);
            }
            String jsonobjectt= buffer.toString();
            JSONObject parentJson = new JSONObject(jsonobjectt);
            JSONArray parentArray = (JSONArray) parentJson.get("results");
            dataobtained=parentJson.getInt("total_results");
            for(int i = 0; i<parentJson.getInt("total_results"); i++){
                JSONObject temp = parentArray.getJSONObject(i);
                Finalarray[i][0] = temp.getString("original_name");
                Finalarray[i][1] = temp.getString("first_air_date");
                Finalarray[i][2] = temp.getString("poster_path");
            }
        }
        catch (MalformedURLException e){
            Log.i("I do", "eee1");
            e.printStackTrace();
        } catch (IOException e) {
            Log.i("I do", "eee2");
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            Log.i("I do", "eee3");
            if(urlConnection!=null)
                urlConnection.disconnect();
            try {
                if(reader!=null)
                    reader.close();
            } catch (IOException e) {
                Log.i("I do", "eee4");
                e.printStackTrace();
            }
        }
        return Finalarray;
    }
    @Override
    protected void onPostExecute(String[][] result) {
        persons = new ArrayList<>();
        for(int i=0;i<dataobtained;i++){
            //if(!result[i][0].isEmpty()&&result[i][2]!=null)
        persons.add(new FeedItem(result[i][0], result[i][1],"http://image.tmdb.org/t/p/w500"+result[i][2],searchpagee.this));
            }
        new Thread(new Runnable() {
            @Override
            public void run() {
            }
        }).start();
        mRecyclerView.setLayoutManager(llm);
        RVAdapter adapter = new RVAdapter(persons);
        mRecyclerView.setAdapter(adapter);
    }
}
}

这是我的回收器适配器代码

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
List<FeedItem> persons;
RVAdapter(List<FeedItem> persons){
    this.persons = persons;
}
public static class PersonViewHolder extends RecyclerView.ViewHolder {
    public static ImageView personPhoto;
    CardView cv;
    TextView personName;
    TextView personAge;
    PersonViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.cv);
        personName = (TextView)itemView.findViewById(R.id.titleCir);
        personAge = (TextView)itemView.findViewById(R.id.contentsCir);
        personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
    }
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardv, viewGroup, false);
    PersonViewHolder pvh = new PersonViewHolder(v);
    return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
    personViewHolder.personName.setText(persons.get(i).title);
    personViewHolder.personAge.setText(persons.get(i).content);
    Picasso.with(persons.get(i).context).load(persons.get(i).photoId).placeholder(R.drawable.placeholder).fit().into(PersonViewHolder.personPhoto);
}
@Override
public int getItemCount() {
    return persons == null ? 0 : persons.size();
}

}

这是在滚动之前滚动时。图像重复

这样在PersonViewHolder中添加一个bindView()

public static class PersonViewHolder extends RecyclerView.ViewHolder {
    public static ImageView personPhoto;
    CardView cv;
    TextView personName;
    TextView personAge;
    PersonViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.cv);
        personName = (TextView)itemView.findViewById(R.id.titleCir);
        personAge = (TextView)itemView.findViewById(R.id.contentsCir);
        personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
    }
    public void bindView(int i) {
        personName.setText(persons.get(i).title);
        personAge.setText(persons.get(i).content);
        Picasso.with(persons.get(i).context).load(persons.get(i).photoId).placeholder(R.drawable.placeholder).fit().into(personPhoto);
    }
}

bindViewHolder中,您需要调用 bindView() 以将列表的每个元素与适当的元素绑定。

@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
    try {
        personViewHolder.bindView(i);
    } catch(Exception e) {e.printStackTrace();}
}

最新更新