Android 回收器视图无法检测到这些帖子

Android 回收器视图无法检测到这些帖子,android,android-recyclerview,Android,Android Recyclerview,大家好,我有一个RecyclerView项目来自Twitch Api,但我不知道为什么RecyclerView无法检测到真实位置并显示项目两次或三次这是我的代码 片段上的我的适配器设置 JSONArray stream = response.getJSONArray("streams"); Log.i("stream",stream.toString()); TwitchApiParser parser = new TwitchApiParser(); Display display = get

大家好,我有一个RecyclerView项目来自Twitch Api,但我不知道为什么RecyclerView无法检测到真实位置并显示项目两次或三次这是我的代码 片段上的我的适配器设置

JSONArray stream = response.getJSONArray("streams");
Log.i("stream",stream.toString());
TwitchApiParser parser = new TwitchApiParser();
Display display = getActivity().getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);

float density  = getActivity().getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth  = outMetrics.widthPixels / density;
int Height = Math.round(dpHeight);
int Witdh = Math.round(dpWidth);

ArrayList items= parser.ParseTwitch(stream,getActivity().getApplicationContext(),(Witdh/2),(Height/2));
TwitchAdapter adapter = new TwitchAdapter(items,getContext(),1,getActivity());
adapter.notifyDataSetChanged();

listView.setAdapter(adapter);
这是我的适配器类

public class TwitchAdapter extends RecyclerView.Adapter<TwitchAdapter.TwitchViewHolder>  {   

    public TwitchAdapter(ArrayList<Twitch> twitch, Context context, int resId, Activity activity) {
        this.twitchs = twitch;
        this.context = context;
        this.resId = resId;
        this.activity = activity;
    }

    ArrayList<Twitch> twitchs;
    Activity activity;
    Context context;
    int resId;
    int type;
    public AppCompatTextView viewrs ;
    public AppCompatTextView language;
    public AppCompatTextView status;
    public CircleImageView profile;
    public AppCompatImageView imageView ;
    AppCompatTextView tvName ;
    RelativeLayout urlLayout ;

    @Override
    public TwitchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.twitch,parent,false);
        return new TwitchViewHolder(view);
    }

    @Override
    public void onBindViewHolder(TwitchViewHolder holder, int position) {

        final Twitch twitch = twitchs.get(position);
        int id = twitch.getId();
        String statusText = twitch.getStatus();
        urlLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url =twitch.getUrl();
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                context.startActivity(browserIntent);
            }
        });
        Display display = activity.getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics ();
        display.getMetrics(outMetrics);
        tvName.setText(twitch.getViewrs()+" در حال تماشای "+twitch.getName()+" با زبان "+twitch.getLanguage());
        float density  = activity.getResources().getDisplayMetrics().density;
        float dpHeight = outMetrics.heightPixels / density;
        float dpWidth  = outMetrics.widthPixels / density;

        if (urlLayout.getTag().equals("large") || urlLayout.getTag().equals("xlarge")){
            Picasso.with(context).load(twitch.getIconLarge()).into(imageView);
            Picasso.with(context).load(twitch.getLogoUrl()).into(profile);

        }else {
            Picasso.with(context).load(twitch.getIconUrl()).into(imageView);
            Picasso.with(context).load(twitch.getLogoUrl()).into(profile);

        }
    }

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

    public class TwitchViewHolder extends RecyclerView.ViewHolder{

        public TwitchViewHolder(View itemView) {
            super(itemView);
            urlLayout = (RelativeLayout) itemView.findViewById(R.id.url_layout);
            status = (AppCompatTextView) itemView.findViewById(R.id.streamer_status);
            profile = (CircleImageView) itemView.findViewById(R.id.logo);
            imageView = (AppCompatImageView) itemView.findViewById(R.id.twitch_icon);
            tvName = (AppCompatTextView) itemView.findViewById(R.id.streamer_name);
        }
    }
}
公共类TwitchAdapter扩展了RecyclerView.Adapter{
公共TwitchAdapter(ArrayList twitch、上下文、int resId、活动){
this.twitchs=twitch;
this.context=上下文;
这个。剩余=剩余;
这个。活动=活动;
}
ArrayList twitchs;
活动;
语境;
国际剩余;
int型;
公共应用程序和文本视图查看器;
公共文本视图语言;
公共应用程序文本视图状态;
公共环路图像视图剖面图;
公共AppCompatImageView图像视图;
AppCompatTextView tvName;
相对性布局;
@凌驾
public TwitchViewHolder onCreateViewHolder(视图组父级,int-viewType){
view=LayoutInflater.from(parent.getContext()).flate(R.layout.twitch,parent,false);
返回新TwitchViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(TwitchViewHolder,内部位置){
最终Twitch Twitch=twitchs.get(位置);
int id=twitch.getId();
字符串statusText=twitch.getStatus();
urlLayout.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
字符串url=twitch.getUrl();
Intent-browserint=新的Intent(Intent.ACTION_视图,Uri.parse(url));
背景。开始触觉(浏览内容);
}
});
Display Display=activity.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics=新的DisplayMetrics();
display.getMetrics(outMetrics);
tvName.setText(twitch.getViewrs()+“twitch.getName()+”twitch.getName()+“twitch.getLanguage()”);
浮动密度=activity.getResources().getDisplayMetrics().density;
float dpHeight=outMetrics.height像素/密度;
float dpWidth=outMetrics.widthPixels/密度;
if(urlayout.getTag().equals(“大”)| | urlayout.getTag().equals(“xlarge”)){
毕加索.with(context).load(twitch.getIconLarge()).into(imageView);
Picasso.with(context).load(twitch.getLogoUrl()).into(profile);
}否则{
毕加索.with(context).load(twitch.getIconUrl()).into(imageView);
Picasso.with(context).load(twitch.getLogoUrl()).into(profile);
}
}
@凌驾
public int getItemCount(){
返回twitchs.size();
}
公共类TwitchViewHolder扩展了RecyclerView.ViewHolder{
公共TwitchViewHolder(视图项视图){
超级(项目视图);
urlLayout=(RelativeLayout)itemView.findViewById(R.id.url\u布局);
状态=(AppCompatTextView)itemView.findViewById(R.id.streamer\u状态);
profile=(CircleImageView)itemView.findViewById(R.id.logo);
imageView=(AppCompatImageView)itemView.findViewById(R.id.twitch_图标);
tvName=(AppCompatTextView)itemView.findViewById(R.id.streamer\u名称);
}
}
}

我不知道你们是否想要我的解析器,但这是一个简单的解析,它可以工作,因为我以前使用的是ListView,但我想更改它,非常感谢noob问题,我尝试了所有方法,但都不起作用

您绑定的是适配器中的视图,而不是ViewHolder中的视图


适配器不应包含UI元素。

您绑定的是适配器中的视图,而不是ViewHolder中的视图


适配器不应包含UI元素。

尝试使用
holder.getAdapterPosition()
也清洁适配器。这类问题很少:

  • 您在构造函数中同时获得了
    上下文
    活动
    ,我认为这是无用的
  • 您没有将数据绑定到视图持有者

尝试使用
holder.getAdapterPosition()
同时清洁适配器。这类问题很少:

  • 您在构造函数中同时获得了
    上下文
    活动
    ,我认为这是无用的
  • 您没有将数据绑定到视图持有者

1-将视图添加到
视图保持架中

2-删除
活动
它是多余的

public class TwitchAdapter extends
    RecyclerView.Adapter<TwitchAdapter.TwitchViewHolder> {

ArrayList<Twitch> twitchs;

Context context;
int resId;
int type;

public TwitchAdapter(ArrayList<Twitch> twitch, Context context, int resId) {
    this.twitchs = twitch;
    this.context = context;
    this.resId = resId;
}

@Override
public TwitchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.twitch, parent, false);
    return new TwitchViewHolder(view);
}

@Override
public void onBindViewHolder(TwitchViewHolder holder, int position) {

    final Twitch twitch = twitchs.get(position);
    int id = twitch.getId();
    String statusText = twitch.getStatus();
    holder.urlLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url = twitch.getUrl();
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            context.startActivity(browserIntent);
        }
    });
    Display display = context.getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);
    holdertvName.setText(twitch.getViewrs() + " در حال تماشای " + twitch.getName() + " با زبان " + twitch.getLanguage());
    float density = activity.getResources().getDisplayMetrics().density;
    float dpHeight = outMetrics.heightPixels / density;
    float dpWidth = outMetrics.widthPixels / density;

    if (urlLayout.getTag().equals("large") || urlLayout.getTag().equals("xlarge")) {
        Picasso.with(context).load(twitch.getIconLarge()).into(holder.imageView);
        Picasso.with(context).load(twitch.getLogoUrl()).into(holder.profile);

    } else {
        Picasso.with(context).load(twitch.getIconUrl()).into(holder.imageView);
        Picasso.with(context).load(twitch.getLogoUrl()).into(holder.profile);

    }
}

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

public class TwitchViewHolder extends RecyclerView.ViewHolder {

    private AppCompatTextView viewrs;
    private AppCompatTextView language;
    private AppCompatTextView status;
    private CircleImageView profile;
    private AppCompatImageView imageView;
    private   AppCompatTextView tvName;
    private   RelativeLayout urlLayout;

    public TwitchViewHolder(View itemView) {
        super(itemView);
        urlLayout = (RelativeLayout) itemView.findViewById(R.id.url_layout);
        status = (AppCompatTextView) itemView.findViewById(R.id.streamer_status);
        profile = (CircleImageView) itemView.findViewById(R.id.logo);
        imageView = (AppCompatImageView) itemView.findViewById(R.id.twitch_icon);
        tvName = (AppCompatTextView) itemView.findViewById(R.id.streamer_name);
    }
}
}
公共类TwitchAdapter扩展
RecyclerView.适配器{
ArrayList twitchs;
语境;
国际剩余;
int型;
公共TwitchAdapter(ArrayList twitch、上下文、int resId){
this.twitchs=twitch;
this.context=上下文;
这个。剩余=剩余;
}
@凌驾
public TwitchViewHolder onCreateViewHolder(视图组父级,int-viewType){
View=LayoutInflater.from(parent.getContext()).flate(R.layout.twitch,parent,false);
返回新TwitchViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(TwitchViewHolder,内部位置){
最终Twitch Twitch=twitchs.get(位置);
int id=twitch.getId();
字符串statusText=twitch.getStatus();
holder.urlLayout.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
字符串url=twitch.getUrl();
Intent-browserint=新的Intent(Intent.ACTION_视图,Uri.parse(url));
背景。开始触觉(浏览内容);
}
});
Display Display=context.getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics=新的DisplayMetrics();
display.getMetrics(outMetrics);
holdertvName.setText(twitch.getViewrs()+“twitch.getName()+”twitch.getLanguage()+“twitch.getLanguage()”;
浮动密度=activity.getResources().getdish