В представлении recyclerView, как остановить перекрытие для нескольких представлений?

Я создал представление списка переработчика, которое содержит два представления (i) для контента (ii) для нативной рекламы (позиция 3). Все работает отлично, но элемент контента перекрывается с рекламным контейнером, я имею в виду, что элемент контента отображается не на каждой третьей позиции. Любое предложение будет очень благодарным.

Вот мой класс адаптера

package com.example.something.rcviewwithads;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    RecyclerView.ViewHolder holder;

    private List<ListItem> listItems;
    private Context context;

    public MyAdapter(List<ListItem> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 3 == 0) {
            return R.layout.ads_layout;
        } else {
            return R.layout.item_layout;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        switch (viewType) {
            case R.layout.ads_layout:
                v = LayoutInflater.from(context).inflate(R.layout.ads_layout, parent, false);
                holder = new AdsHolder(v);
                break;

            case R.layout.item_layout:
                v = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
                holder = new ContentHolder(v);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ContentHolder) {
            final ListItem listItem = listItems.get(position);

            // to load Heading and description
            ((ContentHolder) holder).txt1.setText(listItem.getTitle());    // get...() methods are in ListItem.java
            ((ContentHolder) holder).txt2.setText(listItem.getDescription());

            // onItem clicked open another activity
            ((ContentHolder) holder).linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent myInt = new Intent(context, DetailActivity.class);
                    myInt.putExtra("about", listItem.getDetail());
                    myInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(myInt);
                }
            });
        } else if (holder instanceof AdsHolder) {
            ((AdsHolder) holder).adsTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "You have clicked on ad", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

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

    // Content ViewHolder
    public class ContentHolder extends RecyclerView.ViewHolder {

        public TextView txt1, txt2;
        public LinearLayout linearLayout;

        public ContentHolder(View itemView) {
            super(itemView);

            txt1 = (TextView) itemView.findViewById(R.id.txt1_id);
            txt2 = (TextView) itemView.findViewById(R.id.txt2_id);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linear_id);

        }
    }

    // Advertisement ViewHolder
    public class AdsHolder extends RecyclerView.ViewHolder {

        public ImageView adsLogo;
        public TextView adsTitle;

        public AdsHolder(View view) {
            super(view);
            adsLogo = (ImageView) view.findViewById(R.id.adsLogo_id);
            adsTitle = (TextView) view.findViewById(R.id.ads_title_id);
        }
    }
}

1 ответ

Измените небольшую логику и добавьте код в onBindViewholder.

package com.example.something.rcviewwithads;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    RecyclerView.ViewHolder holder;

    private List<ListItem> listItems;
    private Context context;

    public MyAdapter(List<ListItem> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 3 == 0) {
            return R.layout.ads_layout;
        } else {
            return R.layout.item_layout;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;
        switch (viewType) {
            case R.layout.ads_layout:
                v = LayoutInflater.from(context).inflate(R.layout.ads_layout, parent, false);
                holder = new AdsHolder(v);
                break;

            case R.layout.item_layout:
                v = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
                holder = new ContentHolder(v);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
       if (position % 3 != 0) {
            final ListItem listItem = listItems.get(position);

            // to load Heading and description
            ((ContentHolder) holder).txt1.setText(listItem.getTitle());    // get...() methods are in ListItem.java
            ((ContentHolder) holder).txt2.setText(listItem.getDescription());

            // onItem clicked open another activity
            ((ContentHolder) holder).linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent myInt = new Intent(context, DetailActivity.class);
                    myInt.putExtra("about", listItem.getDetail());
                    myInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(myInt);
                }
            });
        } else 
            ((AdsHolder) holder).adsTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "You have clicked on ad", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

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

    // Content ViewHolder
    public class ContentHolder extends RecyclerView.ViewHolder {

        public TextView txt1, txt2;
        public LinearLayout linearLayout;

        public ContentHolder(View itemView) {
            super(itemView);

            txt1 = (TextView) itemView.findViewById(R.id.txt1_id);
            txt2 = (TextView) itemView.findViewById(R.id.txt2_id);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linear_id);

        }
    }

    // Advertisement ViewHolder
    public class AdsHolder extends RecyclerView.ViewHolder {

        public ImageView adsLogo;
        public TextView adsTitle;

        public AdsHolder(View view) {
            super(view);
            adsLogo = (ImageView) view.findViewById(R.id.adsLogo_id);
            adsTitle = (TextView) view.findViewById(R.id.ads_title_id);
        }
    }
}
Другие вопросы по тегам