问题描述
在Recyclerview中添加EditText控件,activity的布局底部加上Button,用户在点击EditText的时候弹出软键盘,布局整体向上滑动,软键盘不覆盖布局。效果图如下:
SVID_20210218_161500_16_gif.gif
解决方案:
项目若没有引入ImmersionBar 第三方,则只需要按照下面的源码书写即可,若引入了ImmersionBar这个第三方,则需要在该页面初始化状态栏的时候添加上.keyboardEnable(true)这个属性,即可实现该效果。
源码 activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btn_submit"
android:gravity="center_vertical"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:gravity="center" />
</androidx.core.widget.NestedScrollView>
</LinearLayout>
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@color/black"
android:text="提交答案" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="@color/white"
android:gravity="center"
android:text="标题" />
</RelativeLayout>
源码 activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="600dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#769"
android:hint="内容" />
</LinearLayout>
源码 EditAdapter.java
package com.elaine.testkeysoftware;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
/**
* author: elaine
* date: 2021/2/18
*/
public class EditAdapter extends RecyclerView.Adapter<EditAdapter.EditViewHolder> {
private List<String> etString;
public EditAdapter(List<String> etString) {
this.etString = etString;
}
@NonNull
@Override
public EditViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_edit, parent, false);
return new EditViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull EditViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return etString.size();
}
public static class EditViewHolder extends RecyclerView.ViewHolder {
public EditViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
源码 MainActivity.java
package com.elaine.testkeysoftware;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.gyf.immersionbar.ImmersionBar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ImmersionBar初始化会影响软键盘的弹出,所以需要设置keyboardEnable(true)
//ImmersionBar.with(this).reset().statusBarDarkFont(false, 0.5f).keyboardEnable(true).init();
rv = findViewById(R.id.rv);
List<String> data = new ArrayList<>();
data.add("kala");
EditAdapter adapter = new EditAdapter(data);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(adapter);
}
}
网友评论