AppBarLayout CollapsingToolbar如何在EditText单击时禁用展开? [英] AppBarLayout CollapsingToolbar how to disable expand on EditText click?

本文介绍了AppBarLayout CollapsingToolbar如何在EditText单击时禁用展开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的测试应用中,当AppBarLayout折叠时(通过滚动RecycleView),我禁用了AppBarLayout的扩展.我通过将addOnOffsetChangedListener添加到AppBarLayout来做到这一点. 但是,当我单击EditText时,它将再次扩展,但是,我不希望它扩展. 单击EditText时如何禁用AppBarLayout的扩展?

In my test app I disable expansion of AppBarLayout when it is collapsed (by scrolling RecycleView). I do that by adding addOnOffsetChangedListener to AppBarLayout. However, when I click EditText it expands again but, I do not want it to expand. How to disable the expansion of AppBarLayout when I click EditText?

我已经放置了整个代码,以便每个人都可以复制/粘贴代码,创建新项目并快速进行测试.

I have put the whole code, so that everyone can copy/paste the code, create new project and test this fast.

.gif的外观如下

这是XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">



        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/beachcroatia"/>

        </android.support.design.widget.CollapsingToolbarLayout>


    </android.support.design.widget.AppBarLayout>



    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?attr/actionBarSize"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />



    <LinearLayout
        android:id="@+id/messages_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:layout_gravity="bottom"
        android:background="#E1F5FE"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="453dp">



        <EditText
            android:id="@+id/editText_messageBox"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_gravity="center"
            android:layout_weight="0.85"
            android:hint="Enter a message"/>

        <ImageView
            android:id="@+id/messages_sendArrow"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_weight="0.15"
            android:src="@drawable/ic_send_message_" />

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

这里是完整的MainActivity代码:

Here is full MainActivity code:

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerChat;
    private AdapterChat mAdapterChat;
    CollapsingToolbarLayout collapsingToolbarLayout;
    AppBarLayout appBarLayout;
    ImageView sendMessageImageViewButton;
    EditText editTextMessage;

    private List<Message> messagesList;


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

        collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar);
        appBarLayout = findViewById(R.id.app_bar_layout);
        sendMessageImageViewButton = findViewById(R.id.messages_sendArrow);
        editTextMessage = findViewById(R.id.editText_messageBox);

        messagesList = new ArrayList<>();
        messagesList.addAll(getMessagesList());


        mRecyclerChat = findViewById(R.id.recycleView);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRecyclerChat.setLayoutManager(manager);
        mAdapterChat = new AdapterChat(this, messagesList);
        mRecyclerChat.setAdapter(mAdapterChat);

        //move to the last item in recycleview
        mRecyclerChat.getLayoutManager().scrollToPosition(mAdapterChat.getMessagesObekt().size() - 1);

        editTextMessage.requestFocus();



        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);


        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
                    // Collapsed
                    Toast.makeText(MainActivity.this, "Collapsed", Toast.LENGTH_SHORT).show();

                    // disable expanding
                    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
                    params.setScrollFlags(0);

                } else if (verticalOffset == 0) {
                    Toast.makeText(MainActivity.this, "Extend", Toast.LENGTH_SHORT).show();
                    // Expanded
                } else {
                    // Somewhere in between
                }
            }
        });

       

      


        //sending new message and updating recycleview
        sendMessageImageViewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Message Send", Toast.LENGTH_SHORT).show();
                mAdapterChat.updateLastMessage(new Message(editTextMessage.getText().toString()));
                editTextMessage.getText().clear();
                mRecyclerChat.getLayoutManager().scrollToPosition(mAdapterChat.getMessagesObekt().size() - 1);
            }
        });
    }



    //ading some items to a list
    private List<Message> getMessagesList() {
        List<Message> mMessages = new ArrayList<>();

        for (int i = 0; i < 200; i++) {
            mMessages.add(new Message("message " + i));
        }

        return mMessages;
    }
}

这是我的消息类,我在其中添加虚拟数据以在RecycleView中显示

Here is my message class where i add dummy data to show in RecycleView

public class Message {
    private String message;

    public Message(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

这是RecycleView适配器代码:

Here is RecycleView Adapter code:

package com.example.petar.collapsingtolbartestiramo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class AdapterChat extends RecyclerView.Adapter<AdapterChat.ChatHolder> {
    private LayoutInflater mInflater;
    private List<Message> messagesObekt;

    public AdapterChat(Context context, List<Message> listOfMassages) {
        mInflater = LayoutInflater.from(context);
        messagesObekt = new ArrayList<>();
        messagesObekt.addAll(listOfMassages);
        notifyDataSetChanged();
    }

    public void updateChat(List<Message> porukeObjekt){
        this.messagesObekt.addAll(porukeObjekt);
        notifyDataSetChanged();
    }
    public void updateLastMessage(Message porukeObjekt) {
        this.messagesObekt.add(porukeObjekt);
        //notifyDataSetChanged();
        notifyItemInserted(messagesObekt.size());
    }

    public List<Message> getMessagesObekt() {
        return messagesObekt;
    }

    @Override
    public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.list_message, parent, false);
        ChatHolder holder = new ChatHolder(view, messagesObekt);
        return holder;
    }

    @Override
    public void onBindViewHolder(ChatHolder holder, int position) {
        holder.textViewMessage.setText(messagesObekt.get(position).getMessage());
    }

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



    public static class ChatHolder extends RecyclerView.ViewHolder {

        TextView textViewMessage;

        public ChatHolder(View itemView, List<Message> messagesObekt) {
            super(itemView);

            textViewMessage = itemView.findViewById(R.id.rv_message);
        }
    }
}

这是RecycleView项目XML代码(list_message.xml)

Here is RecycleView item XML code (list_message.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/rv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Poruka Ovdje"
        android:textSize="14sp"/>

</LinearLayout>

:

编辑

我尝试将焦点侦听器添加到EditText,但这没有帮助.这就是我尝试的方式:

I tried adding focus listener to EditText but, that did not helped. This is how i try:

     editTextMessage.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    appBarLayout.setExpanded(false);
                    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
                    params.setScrollFlags(0);
                }
            }
        });

推荐答案

我找到了答案.我不知道这是最好的解决方案,但是效果很好.如果有人有更好的解决方案,请随时编写.

I have find the answer. I do not know is the best solution, but it works nice. If someone has better solution, feel free to write it.

所以,我要做的是,当我单击EditTextEditText获得焦点时,将整个AppBarLayout参数设置为0. 因此,当单击EditText时,AppBarLayout仍然存在,但是现在他的高度为0,因此它是不可见的.

So, what i done is i put the whole AppBarLayout params to 0 when i click EditText or when EditText get focus. So, when EditText is clicked, AppBarLayout is still there but, now his height is 0 and it is not visible.

这可以解决问题:

 //Collaps AppBarLayout
    public void collapsAppBarLayout(){
        CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        params.height = 0; // setting new param height to 0
        //setting params to appbarLayout (now AppBarLayout is 0)
        appBarLayout.setLayoutParams(params);
        appBarLayout.setExpanded(false);
    }

此外,如果要具有扩展选项,则需要保存先前参数的高度,然后再次添加该高度.

Also, if you want to have expand option, you need to save height of previous params and then just add that height again.

所以,这是我如何执行的代码: 拳头,当我滚动RecycleView时,我会使用addOnOffsetChangedListener禁用appBarLayout的扩展.但是,我还保存了params的高度,以备将来使用.

So, this is a code how i done it: Fist, when i scroll RecycleView i disable expand of appBarLayout using addOnOffsetChangedListener. However i also save params height for future use.

int sizeParams; //save params height 


appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
                    // Collapsed
                    Toast.makeText(MainActivity.this, "Collapsed", Toast.LENGTH_SHORT).show();
                    // disable expanding and scroll
                    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
                    params.setScrollFlags(0);
                    //saving height for future use
                    sizeParams =  params.height;
                } else if (verticalOffset == 0) {
                    Toast.makeText(MainActivity.this, "Extend", Toast.LENGTH_SHORT).show();
                    // Expanded
                } else {
                    // Somewhere in between
                }
            }
        });

现在我制作了这两种方法,当我需要扩展/折叠时可以调用.

Now i make this 2 method which i call when i need expand/collaps.

   //Collaps AppBarLayout
    public void collapsAppBarLayout(){
        CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        sizeParams =  params.height;//save params height for future use
        params.height = 0; // setting new param height to 0
        //setting params to appbarLayout (now AppBarLayout is 0
        appBarLayout.setLayoutParams(params);
        appBarLayout.setExpanded(false);
    }
    //Expand AppBarLayout
    public void expandAppBarLayout(){
        CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        params.height = 3*sizeParams; // HEIGHT
        appBarLayout.setLayoutParams(params); //add height to appbarlayout
        //enable scroll and expand 
        AppBarLayout.LayoutParams params1 = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
        params1.setScrollFlags(1);
        appBarLayout.setExpanded(true);//expand
    }

这篇关于AppBarLayout CollapsingToolbar如何在EditText单击时禁用展开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆