Recycler视图无法检测到位置

时间:2018-02-16 07:52:55

标签: android android-recyclerview recycler-adapter

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

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

我不知道你们是否想要我的解析器,但它是一个简单的解析和它的作品,因为我曾经使用 ListView ,但我想改变它感谢很多抱歉noob问题我尝试了一切但它不起作用

3 个答案:

答案 0 :(得分:1)

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

适配器不应包含UI元素。

答案 1 :(得分:1)

1-在ViewHolder内添加您的观点。

2-删除activity这是多余的。

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

答案 2 :(得分:0)

尝试使用holder.getAdapterPosition()同时清理适配器。几乎没有问题:

  • 你在构造函数中得到ContextActivity,我认为这是无用的
  • 您没有将数据绑定到您的观看者。