gpt4 book ai didi

android-edittext - AppBarLayout CollapsingToolbar 如何禁用在 EditText 单击时展开?

转载 作者:行者123 更新时间:2023-12-01 05:54:13 32 4
gpt4 key购买 nike

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

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

这是 .gif 的样子

enter image description here

这是 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 代码:

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 中显示的虚拟数据

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 适配器代码:

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)

<?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但是,这没有帮助。这就是我尝试的方式:
     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 do not know is the best solution, but it works nice. If someone has better solution, feel free to write it.


确实有更好、更清洁、更简单的解决方案。
导致这个“问题”的罪魁祸首其实是Google在 AppBarLayout.ScrollingViewBehavior里面添加的一个功能。 .那是您通过 app:layout_behavior="@string/appbar_scrolling_view_behavior" 设置的.在那个类(class)里面, onRequestChildRectangleOnScreen电话 setExpanded(false, !immediate)每当显示键盘时。您只需覆盖此方法并返回 false 以禁用此默认行为。将此类添加到您的项目中:
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.google.android.material.appbar.AppBarLayout;

public class FixedScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior {

public FixedScrollingViewBehavior() {
super();
}

public FixedScrollingViewBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean onRequestChildRectangleOnScreen(@NonNull CoordinatorLayout parent,
@NonNull View child, @NonNull Rect rectangle, boolean immediate) {
return false;
}
}
然后只需通过更改 app:layout_behavior="@string/appbar_scrolling_view_behavior" 来使用这个新类至 app:layout_behavior="your.source.package.FixedScrollingViewBehavior" .

关于android-edittext - AppBarLayout CollapsingToolbar 如何禁用在 EditText 单击时展开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50774518/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com