評論功能

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);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章