在recyclerView视图中,如何停止多个视图的重叠?

时间:2018-07-10 14:22:50

标签: android android-recyclerview adapter android-viewholder multiple-views

我创建了一个回收商列表视图,其中包含两个视图-(i)内容(ii)原生广告(位置3)。一切运行正常,但是内容项目与广告容器重叠,我的意思是说内容项目并未在每个第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 个答案:

答案 0 :(得分:0)

更改一些逻辑,也可以在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);
        }
    }
}