简体   繁体   English

单击时如何在回收站视图中展开布局

[英]How to expand layout in recycler view while clicking on it

I have CardView that contain ImageView (size 100dp), TextView X3 that have a lot of text.我有包含 ImageView(大小 100dp)、TextView X3 的 CardView,其中有很多文本。 In recycler view, while creating the item view in my onBindViewHolder I'm setting the TextView's to visibility GONE to hide them, and displaying only image view.在回收站视图中,在我的 onBindViewHolder 中创建项目视图时,我将 TextView 设置为可见性 GONE 以隐藏它们,并仅显示图像视图。

While I'm clicking on it, I need to set visibility of TextViews to VISIBLE, but to show the text correctly i need to expand the item view ( To make it largest ).当我单击它时,我需要将 TextViews 的可见性设置为 VISIBLE,但要正确显示文本,我需要展开项目视图(使其最大)。 I can do it bu setting the height and width to wrap-content, but because my grid layout has 3 item in row, so the ItemView expands only to hight.我可以通过将高度和宽度设置为 wrap-content 来做到这一点,但是因为我的网格布局在一行中有 3 个项目,所以 ItemView 只扩展到高度。

How to make selected ItemView overlapping other items?如何使选定的 ItemView 与其他项目重叠?

Please see attachments in the end of the post.请参阅文章末尾的附件。

That what i have now:这就是我现在所拥有的:

My adapter:我的适配器:

public class LightsAdapter extends RecyclerView.Adapter<LightsAdapter.LightsViewHolder> {

    private Context context;
    private ArrayList<Light> lights;
    private OnLightImageClickListener onLightImageClickListener;
    private int selected = -1;
    private Light light;

    public LightsAdapter(Context context, ArrayList<Light> lights) {
        this.context = context;
        this.lights = lights;
    }

    public interface OnLightImageClickListener {
        void onLightImageClick(int position);
    }

    public void setOnLightImageClickListener(OnLightImageClickListener onLightImageClickListener) {
        this.onLightImageClickListener = onLightImageClickListener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull LightsViewHolder holder, int position) {
        light = lights.get(position);
        holder.rootItemView.setTag(position);
        holder.TVlightTitle.setText(light.getLampTitle());
        holder.TVLightDesc.setText(light.getLampDesc());
        holder.TVLightType.setText(String.valueOf(light.getLampType()));
        Glide.with(context)
                .load(light.getLampImageUrl())
                .placeholder(R.drawable.ic_launcher_background)
                .override(200, 200)
                .into(holder.IVLightImage);
//        setBackGround(holder);
        hideLightInfo(holder);

        if (position == selected) {
            showLightInfo(holder);
        } else {
            hideLightInfo(holder);

        }
        holder.rootItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = (int) view.getTag();
                selected = position;
                if (onLightImageClickListener != null) {
                    onLightImageClickListener.onLightImageClick(position);
                }
                notifyDataSetChanged();
            }
        });
    }

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

    public class LightsViewHolder extends RecyclerView.ViewHolder {

        private TextView TVlightTitle;
        private TextView TVLightDesc;
        private TextView TVLightType;
        private ImageView IVLightImage;
        private CardView rootItemView;

        public LightsViewHolder(@NonNull View itemView) {
            super(itemView);
            TVlightTitle = itemView.findViewById(R.id.lightTitle);
            TVLightDesc = itemView.findViewById(R.id.lightDesc);
            TVLightType = itemView.findViewById(R.id.lightType);
            IVLightImage = itemView.findViewById(R.id.lightImage);
            rootItemView = itemView.findViewById(R.id.rootItemView);
        }
    }

    public void hideLightInfo(LightsViewHolder holder) {
        holder.TVlightTitle.setVisibility(View.GONE);
        holder.TVLightDesc.setVisibility(View.GONE);
        holder.TVLightType.setVisibility(View.GONE);
        holder.IVLightImage.setVisibility(View.VISIBLE);
    }

    public void showLightInfo(LightsViewHolder holder) {
        holder.TVlightTitle.setVisibility(View.VISIBLE);
        holder.TVLightDesc.setVisibility(View.VISIBLE);
        holder.TVLightType.setVisibility(View.VISIBLE);
        holder.IVLightImage.setVisibility(View.GONE);
    }

    public void setBackGround(LightsViewHolder holder) {
        if (light.getLampType() == 1) {
            holder.rootItemView.setBackgroundColor(Color.GREEN);
        } else {
            holder.rootItemView.setBackgroundColor(Color.RED);
        }
    }

My xml file:我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootItemView"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_margin="15dp"
    android:orientation="vertical"
    android:translationZ="2dp"
    card_view:cardCornerRadius="100dp"
    card_view:cardElevation="3dp">


    <LinearLayout
        android:padding="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/lightTitle"
            tools:text="lightTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/lightDesc"
            tools:text="lightDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/lightType"
            tools:text="lightType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/lightImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</androidx.cardview.widget.CardView>

In this 2 images you can see what I have now.在这 2 张图片中,您可以看到我现在拥有的东西。 How to make the item largest and overlapping other items while clicking?单击时如何使项目最大并与其他项目重叠?

在此处输入图像描述 在此处输入图像描述

The way you are using to display the content of the grid item is wrong.您用于显示网格项目内容的方式是错误的。

You should use either thebottom sheet or can use popupwindow with an anchor view.您应该使用底部工作表或可以使用带有锚视图的弹出窗口。 That will look good and your content will be rendered properly.这看起来不错,并且您的内容将正确呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android中的Recycler View不能点击 - Recycler View in Android not Clicking 抽屉布局中的回收站视图 - Recycler View in Drawer Layout 未连接适配器; 跳过布局,同时使用回收器视图显示列表 - No adapter attached; skipping layout ,while displaying a list using recycler VIew 单击某个项目后如何关闭回收站视图持有人? - How to close recycler view holder after clicking an item? 如何使回收台视图水平放置并像桌子一样布局? - How to make recycler view horizontal and like table layout? 如何获取当前在回收站视图的线性布局中不可见的视图 - How to get the views which are not visible currently in the linear layout of the recycler view 如何在同一活动中添加回收站视图和抽屉布局? - How can I add recycler view and drawer layout in same activity? 通过单击另一个回收站视图中的按钮刷新回收站视图 - Refresh Recycler View By Clicking A Button In Another Recycler View 回收站视图:未连接适配器; 跳过布局(API 的回收站视图中的错误) - Recycler view: no adapter attached; skipping layout(Error in recycler view for API ) 选项卡布局内的水平回收器视图 - Horizontal Recycler View inside Tab Layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM