当键盘出现时,Listview向上滚动

时间:2017-05-24 12:59:52

标签: android android-layout

我正在创建聊天应用。我想在键盘出现时或在键入过程中滑动列表视图内容,如果屏幕内容已满。顶部列表项与WhatsApp一样向上滑动。my app] 1

WhatsApp pic

我正在使用自定义工具栏,因此当我使用android:windowSoftInputMode =“adjustPan | adjustResize”时,工具栏也会向上滑动。        公共类MainActivity扩展AppCompatActivity实现了View.OnClickListener {

    static int i=0;
    private boolean switch_layout;
    private ImageButton switch_msg_layout;
    private ArrayList<Message> messages;
    private Toolbar _toolbar;
    private ListView _send_msg_recyclerview;
    private EditText _msg;
    private ImageButton _send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initialize();
        switch_layout = true;
        switch_msg_layout.setImageResource(R.drawable.ic_uncheckbox);
        _toolbar.setFitsSystemWindows(true);
        setSupportActionBar(_toolbar);

        _send.setOnClickListener(this);
        switch_msg_layout.setOnClickListener(this);


    }

    private void initialize() {
        messages = new ArrayList<>();
        _toolbar = (Toolbar) findViewById(R.id.toolbar);
        _send_msg_recyclerview = (ListView) findViewById(R.id.out_msg_recyclerview);
        _msg = (EditText) findViewById(R.id.text);
        _send = (ImageButton) findViewById(R.id.btn);
        switch_msg_layout = (ImageButton) findViewById(R.id.switch_msg_layout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.right_menu, menu);

        return true;
    }

    @Override
    public void onClick(View v) {

        String str = "";
        str = _msg.getText().toString();
        SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
        String currentDateandTime = sdf.format(new Date());

        switch (v.getId()) {

            case R.id.btn:

                setData(str, currentDateandTime,switch_layout);
                break;

            case R.id.switch_msg_layout:
                i++;
                if (!(i%2 ==0)){
                    switch_layout = false;
                    switch_msg_layout.setImageResource(R.drawable.ic_checkbox);
                }
                else {
                    switch_layout = true;
                    switch_msg_layout.setImageResource(R.drawable.ic_uncheckbox);
                }
                break;
        }


    }

    private void setData(String str, String currentTime,boolean switch_layout) {
        messages.add(new Message(str, currentTime,switch_layout));
        _send_msg_recyclerview.setAdapter(new MessageUIAdapter(this, messages));
        _msg.setText("");
    }
}

2 个答案:

答案 0 :(得分:1)

在清单

中的活动代码中

进行此更改 添加此属性

android:windowSoftInputMode="adjustPan|adjustResize"

答案 1 :(得分:0)

在java类中,如果它是一个Activity,请在onCreate中执行。如果它是片段,请在onViewCreated中执行。

ListView l = getListView();
l.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
l.setStackFromBottom(true);

或在xml中,listview添加此

android:stackFromBottom="true"
android:transcriptMode="normal"