当前位置:  开发笔记 > Android > 正文

将CardView置于RecyclerView中,只有一个元素

如何解决《将CardView置于RecyclerView中,只有一个元素》经验,为你挑选了1个好方法。

我正在使用包含带有TextView和ImageView的CardView的RecyclerView(每张卡代表一个城市).我在每张卡片上都有一个onClickListener,可以将我带到城市的博物馆列表中.(RecyclerView由ArrayList填充).该列表是由垂直滚动的同一Cardview组成的RecyclerView.

当一个城市只有一个博物馆时,如何在屏幕中央显示独特的CardView?

这是活动xml:

    
 

    

这是RecyclerView xml:

 

    
        

            

            

        

    


`

谢谢你的帮助.

所以这就是MainActivity.城市列表.一切都很好. 在此输入图像描述

当我点击只有一个博物馆的Parigi时,我想在屏幕中央显示单个卡片视图

在此输入图像描述

(如果有可能我想从屏幕中心开始,而不是从顶部开始.如果我打开活动时中央卡片视图始终位于中心.例如,在中心翻译NationalGallery另一个的顺序)

在此输入图像描述



1> Konstantin L..:

我已经实现了简单的HelloWorld应用程序,该应用程序显示城市列表并根据它拥有的博物馆数量 - 显示全尺寸城市卡或其中心包装版本.

在此输入图像描述

(是的,我不擅长艺术:-))

这就是我做到的.

TL; DR:

关键部分是ItemDecoration:设置适当的物品抵消,你会得到你需要的东西; 这是我如何做到的:

    RecyclerView recyclerViewMuseum = (RecyclerView)findViewById(R.id.recyclerViewMuseum);
    recyclerViewMuseum.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    recyclerViewMuseum.setAdapter(adapter);
    recyclerViewMuseum.addItemDecoration(new RecyclerView.ItemDecoration() {

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (view instanceof CityWithOneMuseumCardView) {
                int totalWidth = parent.getWidth();
                int cardWidth = getResources().getDimensionPixelOffset(R.dimen.small_card_width);
                int sidePadding = (totalWidth - cardWidth) / 2;
                sidePadding = Math.max(0, sidePadding);
                outRect.set(sidePadding, 0, sidePadding, 0);
            }
        }
    });

这是我的模型 - CityMuseum类:

public class Museum {
    public String title;
    public Museum(String title) {
        this.title = title;
    }
}

public class City {
    public String title;
    public int imageRes;
    public List museums = new ArrayList<>();

    public City(String title, int imageRes) {
        this.title = title;
        this.imageRes = imageRes;
    }
}

然后观看:CityWithManyMuseumsCardViewCityWithOneMuseumCardView.他们都使用helper-interface IItemDisplayer.

public class CityWithOneMuseumCardView extends CardView implements IItemDisplayer {

    public CityWithOneMuseumCardView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.one_museum_layout, this);
    }

    @Override
    public void displayItem(City city) {
        TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView);
        cityTitleTextView.setText(city.title);
    }
}

public class CityWithManyMuseumsCardView extends CardView implements IItemDisplayer {

    public CityWithManyMuseumsCardView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.many_museums_layout, this);
    }

    @Override
    public void displayItem(City city) {
        ImageView cityBackgroundImageView = (ImageView)findViewById(R.id.cityBackgroundImageView);
        cityBackgroundImageView.setImageResource(city.imageRes);
        TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView);
        cityTitleTextView.setText(city.title);
        TextView cityNumberOrMuseumsTextView = (TextView)findViewById(R.id.cityNumberOrMuseumsTextView);
        cityNumberOrMuseumsTextView.setText(String.valueOf(city.museums.size()));
    }
}

public interface IItemDisplayer {
    public void displayItem(TItem item);
}

他们的布局:





    

    





    

    

    

然后我们需要为我们创建一个适配器 RecyclerView CityAdapter.java

public class CityAdapter  extends RecyclerView.Adapter {

    final static int ITEM_TYPE_MANY_MUSEUMS = 0;
    final static int ITEM_TYPE_ONE_MUSEUM = 1;

    private List items;

    public CityAdapter(List items) {
        this.items = items;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        switch (viewType) {
            case ITEM_TYPE_MANY_MUSEUMS:
                return new ViewHolder(new CityWithManyMuseumsCardView(viewGroup.getContext()));
            case ITEM_TYPE_ONE_MUSEUM:
                return new ViewHolder(new CityWithOneMuseumCardView(viewGroup.getContext()));
            default:
                throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (items == null || items.size() < position) {
            throw new IllegalArgumentException("Wrong position!");
        }

        if (items.get(position).museums.size() > 1) {
            return ITEM_TYPE_MANY_MUSEUMS;
        } else if (items.get(position).museums.size() == 1){
            return ITEM_TYPE_ONE_MUSEUM;
        }

        throw new IllegalArgumentException("Wrong number of museums!");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((IItemDisplayer) holder.itemView).displayItem(items.get(position));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}

我已将此项目上传到我的Dropbox - 随时查看!希望能帮助到你.


好的,谢谢,我会将你的答案标记为正确
推荐阅读
有风吹过best
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有