ListView 软键盘与item底部对齐

在项目中遇到一个需求,ListView的某项点击后底部弹出输入框和软键盘,要使被点击的Item底部与输入框顶部保持对齐。本文介绍了实现这一功能的方法,包括监听布局变化、计算滚动距离,并提供了相关代码示例。不过,当列表的最后一项出现时,可能会被输入框遮挡一部分,这是一个尚未解决的问题。对于有header的ListView,需要考虑header的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引出:最近项目中ListView Item中有个回复的按钮,点击后会从屏幕底部弹出回复的输入框+软键盘,同时被点击item的底部与输入框顶部对齐。

////////////////////////////////////////////////////////////////////////////////////////////////////

预览图:

这里写图片描述


这里写图片描述


这里写图片描述

///////////////////////////////////////////////////////////////////////////////////////////////////

高能代码区:

主布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#cccccc"
        android:gravity="center"
        android:text="Title Bar" />

    <com.qiyi.guianxiang.testlistview.CustomListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/title" />

    <LinearLayout
        android:id="@+id/input_layer"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:background="#555555"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/comment_et"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:hint="please input..." />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0"
            android:text="send" />
    </LinearLayout>
</RelativeLayout>

自定义ListView,监听布局尺寸变化

package com.qiyi.guianxiang.testlistview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * Created by guianxiang on 16-9-26.
 */

public class CustomListView extends ListView {

    public void setmListener(OnResizeListener mListener) {
        this.mListener = mListener;
    }

    private OnResizeListener mListener;



    public CustomListView(Context context) {
        super(context);
    }

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

    public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mListener.OnResize(w, h, oldw, oldh);
    }
}

adapter中计算变化前后需要滚动的距离
可以使用scrollListBy(>=API 19) 或者 setSelectionFromTop(>=API 21)

package com.qiyi.guianxiang.testlistview;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by guianxiang on 16-9-23.
 */

public class myAdapter extends BaseAdapter {
    private Context mContext;
    private View commentLayer;
    private CustomListView mlistView;
    private int[] datas;
    private int clickbottom = 0;
    private int offset = 0;
    private int clickPosition;
    private int clickItemHeight;
    private int newH = 0;

    @Override
    public int getCount() {
        return datas.length;
    }

    @Override
    public Object getItem(int i) {
        return datas[i];
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public myAdapter(Context context, CustomListView listView, int[] datas) {
        mContext = context;
        mlistView = listView;
        this.datas = datas;
        initInputBar();

        mlistView.setmListener(new OnResizeListener() {
            @Override
            public void OnResize(int w, int h, int oldw, int oldh) {
                Log.e("test", "listOnResize: " + w + "," + h + "," + oldw + "," + oldh);
                //键盘弹出
                if (oldh > h) {
                    //(clicked item's bottom to the listview's bottom after keyboard appear) - (clicked item's bottom to the listview's bottom before keyboard appear)
                    offset = (oldh - h + commentLayer.getHeight()) - (oldh - clickbottom);
                    newH = h;
                    mlistView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mlistView.scrollListBy(offset);
                            //following is another way to realize but require a higher API version
//                            mlistView.setSelectionFromTop(clickPosition, newH - commentLayer.getHeight() - clickItemHeight);
                        }
                    }, 20);
                }
            }
        });
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        final int position = i;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.list_item, viewGroup, false);
            holder = new ViewHolder();
            holder.icon = (ImageView) view.findViewById(R.id.icon);
            holder.name = (TextView) view.findViewById(R.id.name);
            holder.reply = (TextView) view.findViewById(R.id.reply);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.name.setText("android:" + datas[i]);

        holder.reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showInputBar();
                //attention: getChildAt()is to get the visible item in ListView area
                clickbottom = mlistView.getChildAt(position - mlistView.getFirstVisiblePosition()).getBottom();
                clickItemHeight = mlistView.getChildAt(position - mlistView.getFirstVisiblePosition()).getHeight();
                clickPosition = position;
            }
        });
        return view;
    }

    public static class ViewHolder {
        public ImageView icon;
        public TextView name;
        public TextView reply;
    }

    private void showInputBar() {
        commentLayer.setVisibility(View.VISIBLE);
        EditText comment_et = (EditText) commentLayer.findViewById(R.id.comment_et);
        keyboardUtils.openSoftKeyboard(comment_et);
    }

    private void initInputBar() {
        commentLayer = ((Activity) (mContext)).findViewById(R.id.input_layer);
        commentLayer.setVisibility(View.GONE);
    }
}

最后附上github链接:https://github.com/tobevan/Keyboard-align-ListView-item-bottom


存在的问题:最后一条Item会被输入框挡住一部分,目前没有解决


拓展:如果ListView有header,记得在adapter中listview.getchildat(xxx+mlistview.getHeaderViewCounts())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值