如何在android studio java的RecyclerView中显示/隐藏主Activity中的布局?

new9mtju  于 2023-02-16  发布在  Android
关注(0)|答案(1)|浏览(178)

我有一个活动,其中包括一个用于生成按钮的回收器视图(来自不同的布局文件)和一个包含用于确认的元素的布局。
我想让它在我点击按钮“新游戏”(在Recyclerview类中)时,confirmationLayout(在MainMenu Activity中)显示出来。但是我不能,有人能帮我吗?
主菜单类

public class MainMenu extends AppCompatActivity {

    private ImageView profileIcon;
    private TextView username;

    private RelativeLayout newGameConfirmationLayout;
    private TextView yesButton, noButton;

    private RecyclerView buttonRecyclerView;
    private ButtonRecyclerAdapter adapter;

    private DataHandler dataHandler;

    private ArrayList<String> buttonNames = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);

        newGameConfirmationLayout = findViewById(R.id.newGameConfirmationLayout);
        yesButton = findViewById(R.id.yesButton);
        noButton = findViewById(R.id.noButton);

        buttonRecyclerView = findViewById(R.id.buttonRecyclerView);
        buttonRecyclerView.setLayoutManager(new LinearLayoutManager(MainMenu.this));

        if(General.infoSaved){buttonNames.add("CONTINUE GAME");}

        buttonNames.add("NEW GAME");
        buttonNames.add("CHANGE USER");
        buttonNames.add("PRACTICE");
        buttonNames.add("EXERCISE LIST");

        adapter = new ButtonRecyclerAdapter(buttonNames, MainMenu.this);
        buttonRecyclerView.setAdapter(adapter);

        yesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
                startGame();
            }
        });

        noButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newGameConfirmationLayout.setVisibility(View.INVISIBLE);
            }
        });
    }

    private void startGame(){}

    public void show_confirmationLayout(){
        newGameConfirmationLayout.setVisibility(View.VISIBLE);
    }

回收器视图类

public class ButtonRecyclerAdapter extends RecyclerView.Adapter<ButtonRecyclerAdapter.buttonHolder>{

    private ArrayList<String> buttonNames;
    private Context context;
 

    public ButtonRecyclerAdapter(ArrayList<String> loginButtonNames, Context context) {
        this.buttonNames = loginButtonNames;
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull buttonHolder holder, int position) {

        holder.button.setText(buttonNames.get(position));

        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String buttonName = holder.button.getText().toString();

                switch (buttonName){

                    case "NEW GAME":
                        
                        MainMenu mainMenu = new MainMenu();
                        mainMenu.show_confirmationLayout();
                        break;
                }
            }
        });
    }

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

    public class buttonHolder extends RecyclerView.ViewHolder{
        private Button button;

        public buttonHolder(@NonNull View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.buttonblock);
        }
    }
}

我尝试将其作为类添加(如上所述),但我得到的错误是布局是空对象/然而,当我尝试直接在Activity中编写ame行代码时,它工作正常。

svgewumm

svgewumm1#

我个人在我的RecyclerView'sAdapterViewHolder里面有一个方法就是把你物品的ViewGroupwidthheight设置成0
下面是您可以添加到适配器类中的方法,它占用了您想要隐藏的项位置:

public void hideFeedItem(int position){
        RecyclerView.ViewHolder holder = getViewHolderAtPosition(position);
        if(holder != null){
            ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
            params.height=0;
            params.width= 0;
            holder.itemView.setLayoutParams(params);
        }
}

其中itemview是根ViewGroup,在您的情况下,应该已经在ViewHolder中定义了buttonHolder

相关问题