[Android] RecyclerView click Item pop PopupWindow

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/sandalphon4869/article/details/100191612


Foreword

effect:
Here Insert Picture Description

A, LayoutInflater.from () Context Problems

The core problem is that [PopupWindow context do not know how to write? ]

the onClick () in View popview = LayoutInflater.from(mcontext).inflate(R.layout.popupwindow_layout, null, false);
the from () parameters
() and onCreateViewHolder in View view= LayoutInflater.from(mcontext).inflate(R.layout.listview_item,parent,false);the context is the same context .

You can write parent.getContext(), you can write a mcontext class member variables.

Written 1: mcontext

ItemAdapter.java:RecyclerView Custom Adapter

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

    private List<Item> mitemList;
    private Context mcontext;

    static class ViewHolder extends RecyclerView.ViewHolder{...}

    public ItemAdapter(Context context,List<Item> itemList)
    {
        this.mitemList=itemList;
        this.mcontext=context;
    }

    @Override
    public int getItemCount() {...}

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mcontext).inflate(R.layout.listview_item,parent,false);
        final ViewHolder viewHolder=new ViewHolder(view);

        final View popview=LayoutInflater.from(mcontext).inflate(R.layout.popupwindow_layout,null,false);
        ....
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {...}
}

Writing 2: parent.getContext ()

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item,parent,false);
    final ViewHolder viewHolder=new ViewHolder(view);

    final View popview=LayoutInflater.from(parent.getContext()).inflate(R.layout.popupwindow_layout,null,false);
    final PopupWindow popupWindow=new PopupWindow(
            popview,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true
    );

    viewHolder.itemBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            popupWindow.showAtLocation(popview,Gravity.CENTER,0,0);
        }
    });
    popview.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            popupWindow.dismiss();
        }
    });
    return viewHolder;
}

Second, set the text problem

: The effect
of this will be based on different click the entry you want to set different display pop-up window Chinese.

[Core issues: Can not find textView of R.id]
do not know why it R.id display options for other controls, but does not show R.id.content TextView we want. Only you break out the job.
Here Insert Picture Description

[Core issues: setText () method does not]
write separately for the job, did not write together.

Problem:
Here Insert Picture Description
Solution:

TextView textView=popview.findViewById(R.id.content);
textView.setText("hello");

Complete source code


> ItemAdapter.java

```java
package com.example.myapplication;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

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

    private List<Item> mitemList;
    private Context mcontext;

    static class ViewHolder extends RecyclerView.ViewHolder
    {
        TextView itemName;
        TextView itemProgress;
        Button itemBtn;

        public ViewHolder(View view)
        {
            super(view);
            itemName=view.findViewById(R.id.name);
            itemProgress=view.findViewById(R.id.progress);
            itemBtn=view.findViewById(R.id.show);
        }
    }

    public ItemAdapter(Context context,List<Item> itemList)
    {
        this.mitemList=itemList;
        this.mcontext=context;
    }

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

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mcontext).inflate(R.layout.listview_item,parent,false);
        final ViewHolder viewHolder=new ViewHolder(view);

        final View popview=LayoutInflater.from(mcontext).inflate(R.layout.popupwindow_layout,null,false);
        final PopupWindow popupWindow=new PopupWindow(
                popview,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true
        );

        viewHolder.itemBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos=viewHolder.getAdapterPosition();
                TextView textView=popview.findViewById(R.id.content);
                switch (pos)
                {
                    case 0:
                    {
                        textView.setText("hello");
                        break;
                    }
                    case 1:
                    {
                        textView.setText("world");
                        break;
                    }
                    default:
                        break;
                }
                popupWindow.showAtLocation(popview,Gravity.CENTER,0,0);
            }
        });
        popview.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Item item=mitemList.get(position);
        holder.itemName.setText(item.getName());
        holder.itemProgress.setText(item.getProgress());
    }
}

Guess you like

Origin blog.csdn.net/sandalphon4869/article/details/100191612