设计评论部分时遇到问题

时间:2019-02-07 09:59:19

标签: android android-layout

我正在尝试使“弹出对话框注释”部分。 在此“评论”部分中,我几乎做了所有事情,但面临新问题。

在这里,我使用“显示”对话框并添加新评论,但是当用户单击“输入评论”(Edittext)时。它隐藏在键盘下。因此,用户发布新评论会很麻烦。

这里是我的Xml文件:

Popup.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:background="@drawable/dialog_bg"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/popup_rcv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="bottom">
            <EditText
                android:id="@+id/new_comment_et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="Please enter Comment" />

            <Button
                android:id="@+id/comment_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Submit" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

先谢谢您

1 个答案:

答案 0 :(得分:0)

尝试一下

  

dialog_layout

<?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">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/popup_rcv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/new_comment_et"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Please enter Comment" />

        <Button
            android:id="@+id/comment_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit" />
    </LinearLayout>
</LinearLayout>
  

活动

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
public class HomeActivity extends AppCompatActivity {


    RecyclerView additionalDataList;
    ArrayList<String> arrayList = new ArrayList<>();
    DataAdapter adapter;


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

        Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_layout);

        Window window = dialog.getWindow();
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);

        dialog.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        EditText edtComment = dialog.findViewById(R.id.new_comment_et);
        Button btnComment = dialog.findViewById(R.id.comment_btn);


        additionalDataList = dialog.findViewById(R.id.popup_rcv);
        additionalDataList.setLayoutManager(new LinearLayoutManager(this));
        additionalDataList.setHasFixedSize(true);

        addDataToList();


        btnComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(edtComment.getText().toString().trim())) {
                    arrayList.add(0, edtComment.getText().toString().trim());
                    edtComment.setText("");
                    adapter.notifyDataSetChanged();
                }
            }
        });
        adapter = new DataAdapter(this, arrayList);
        additionalDataList.setAdapter(adapter);

        dialog.show();
    }

    private void addDataToList() {
        for (int i = 0; i < 5; i++) {
            arrayList.add("Comment :" + i);
        }
    }
}
  

DataAdapter

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {

    Context context;
    ArrayList<String> arrayList = new ArrayList<>();

    public DataAdapter(Context context, ArrayList<String> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.title.setText(arrayList.get(position));


    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;

        public ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.additionalInfoTitle);
        }
    }
}

OUTPUT