评论功能

来源:互联网 发布:广州淘宝二手汽车网 编辑:程序博客网 时间:2024/05/16 08:05

1.布局

<ListView    android:id="@+id/listView"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_above="@+id/ll"    android:visibility="visible"></ListView><LinearLayout    android:id="@+id/ll"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_alignParentBottom="true"    android:visibility="visible">    <Button        android:layout_width="wrap_content"        android:layout_height="50dp"        android:text="发送"        android:onClick="send"/>    <EditText        android:id="@+id/et"        android:layout_width="match_parent"        android:layout_height="50dp" /></LinearLayout>

2.逻辑代码

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_list);        linearLayout = (LinearLayout) findViewById(R.id.ll);        onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                //判断窗口可见区域大小                Rect r = new Rect();                getWindow().getDecorView().getWindowVisibleDisplayFrame(r);                //如果屏幕高度和Window可见区域高度差值大于整个屏幕高度的1/3,则表示软键盘显示中,否则软键盘为隐藏状态。                heightDifference = 1920 - (r.bottom - r.top);//                Toast.makeText(ListActivity.this, "键盘高度:" + heightDifference, Toast.LENGTH_SHORT).show();            }        };        //注册布局变化监听        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);        listView = (ListView) findViewById(R.id.listView);        MyAdapter adapter = new MyAdapter();        listView.setAdapter(adapter);        editText = (EditText) findViewById(R.id.et);        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) {                tanchu();                new Handler().postDelayed(new Runnable() {                    @Override                    public void run() {                        listView.setSelectionFromTop(position, 1920 - heightDifference - dip2px(50 + 130));                    }                }, 400);            }        });    }    public int dip2px(float dpValue) {        final float scale = this.getResources().getDisplayMetrics().density;        return (int) (dpValue * scale + 0.5f);    }    @Override    protected void onDestroy() {        //移除布局变化监听        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {            getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);        } else {            getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(onGlobalLayoutListener);        }        super.onDestroy();    }    private void tanchu() {        linearLayout.setVisibility(View.VISIBLE);        editText.requestFocus();        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);        imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);    }    public void send(View view) {        linearLayout.setVisibility(View.INVISIBLE);//        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);//        imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);    }
注解:主要是测量软键盘的高度, 例外还有一个关键方法:
listView.setSelectionFromTop(position, y);

0 0
原创粉丝点击