Android 直播 聊天輸入法彈出 視頻不擠壓 解決方案

參考網上已有方案,即使用DialogFragment佈局聊天列表、聊天框、禮物等內容。

需要兩個Fragment,底下Fragment放視頻View,上層即DialogFragment。

注意,經過測試視頻層必須放置在Fragment裏邊,若直接視頻放置在Activity佈局,達不到我們要的效果!!!

網上其他DialogFragment方案均說到直播Activity在清單中鍵盤彈出方式爲 android:windowSoftInputMode="adjustPan" ,經過我的測試無需此設置

demo效果:

效果展示

完整demo代碼:

AndroidManifest.xml

        <activity
            android:name=".fragment3.LiveVideo3Activity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
activity 容器 
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;

import cn.smiles.myapplication.R;

/**
 * activity 容器
 *
 * @author kaifang
 */
public class LiveVideo3Activity extends AppCompatActivity {

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

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.hide();

    }
}

activity 佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment3.LiveVideo3Activity">

    <fragment
        android:id="@+id/fragment4"
        android:name="cn.smiles.myapplication.fragment3.VideoFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:tag="VideoFragment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

視頻顯示層Fragment

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.smiles.myapplication.R;

/**
 * 底層視頻顯示
 *
 * @author kaifang
 */
public class VideoFragment extends Fragment {

    private View video;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_video, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        video = view.findViewById(R.id.iv_video);
        //在view佈局完成後,顯示上層聊天等彈窗的 DialogFragment
        video.post(new Runnable() {
            @Override
            public void run() {
                ChatFragment.newInstance("p").show(getChildFragmentManager(), "ChatFragment");
            }
        });
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public VideoFragment() {
        // Required empty public constructor
    }

}

視頻層Fragment佈局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".fragment.VideoFragment">

    <ImageView
        android:id="@+id/iv_video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/bg_video"/>

</FrameLayout>
聊天列表、聊天輸入框、禮物框等顯示DialogFragment
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import cn.smiles.myapplication.R;

/**
 * 聊天列表、聊天輸入框、禮物框等顯示Fragment
 *
 * @author kaifang
 */
public class ChatFragment extends DialogFragment implements View.OnClickListener {
    private View layout_gift;
    private Context context;
    private RecyclerView rec_view;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //設置dialog無title
        Window window = getDialog().getWindow();
        if (window != null)
            window.requestFeature(Window.FEATURE_NO_TITLE);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_chat, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        layout_gift = view.findViewById(R.id.layout_gift);
        view.findViewById(R.id.btn_gift).setOnClickListener(this);
        view.findViewById(R.id.btn_send).setOnClickListener(this);
        view.findViewById(R.id.click_hide).setOnClickListener(this);

        rec_view = view.findViewById(R.id.rec_view);
        initChatList();
    }

    @Override
    public void onStart() {
        super.onStart();
        //設置dialog顯示時 按後退鍵後的操作
        Dialog dialog = getDialog();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                    if (layout_gift.getVisibility() == View.VISIBLE) {
                        layout_gift.setVisibility(View.GONE);
                        return true;
                    } else {
                        ((Activity) context).finish();
                    }
                }
                return false;
            }
        });

        //設置dialog爲透明背景、寬高填充屏幕
        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(android.R.color.transparent);

            WindowManager.LayoutParams windowParams = window.getAttributes();
            windowParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            windowParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            windowParams.dimAmount = 0.0f;
            window.setAttributes(windowParams);
        }
    }

    /**
     * 聊天列表
     */
    private void initChatList() {
        rec_view.setHasFixedSize(true);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(context);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rec_view.setLayoutManager(mLayoutManager);
        ArrayList<Spanned> myDataset = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            if ((int) (Math.random() * 2) == 0) {
                Spanned spanned = Html.fromHtml("<font color='green'>小米:</font>" + UUID.randomUUID().toString());
                myDataset.add(spanned);
            } else {
                Spanned spanned = Html.fromHtml("<font color='green'>小花:</font>" + UUID.randomUUID().toString());
                myDataset.add(spanned);
            }
        }
        MyRecAdapter mAdapter = new MyRecAdapter(myDataset);
        rec_view.setAdapter(mAdapter);
    }

    /**
     * 列表適配器
     */
    public class MyRecAdapter extends RecyclerView.Adapter<MyRecAdapter.MyViewHolder> {
        private final List<Spanned> myDataset;

        MyRecAdapter(List<Spanned> myDataset) {
            this.myDataset = myDataset;
        }

        @NonNull
        @Override
        public MyRecAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item2, parent, false);
            return new MyRecAdapter.MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyRecAdapter.MyViewHolder holder, int position) {
            Spanned msg = myDataset.get(position);
            holder.mTextView.setText(msg);
        }

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

        class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            TextView mTextView;

            MyViewHolder(View v) {
                super(v);
                mTextView = v.findViewById(R.id.textView2);
                v.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                int position = rec_view.getChildLayoutPosition(v);
                Spanned spanned = myDataset.get(position);
                Toast.makeText(mTextView.getContext(), "點擊位置 " + position + "\n" + spanned.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }

    public ChatFragment() {
        // Required empty public constructor
    }

    public static ChatFragment newInstance(String param1) {
        ChatFragment fragment = new ChatFragment();
        Bundle args = new Bundle();
        args.putString("param1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                Toast.makeText(context, "發送。。。", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_gift:
                hideKeyboardFrom(context);
                layout_gift.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout_gift.setVisibility(View.VISIBLE);
                    }
                }, 300);
                break;
            case R.id.click_hide:
                hideKeyboardFrom(context);
                if (layout_gift.getVisibility() == View.VISIBLE)
                    layout_gift.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 隱藏鍵盤
     *
     * @param context
     */
    public void hideKeyboardFrom(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm != null) {
            View view = getView();
            if (view != null) {
                imm.hideSoftInputFromWindow(view.getRootView().getWindowToken(), 0);
            }
        }
    }
}
DialogFragment層佈局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="@android:color/transparent"
             android:orientation="vertical"
             tools:context=".fragment.ChatInputFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <View
            android:id="@+id/click_hide"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"/>

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="輸入聊天內容。。。"
                android:textColor="#FFF0"
                android:textColorHint="#00F">

                <requestFocus/>
            </EditText>

            <Button
                android:id="@+id/btn_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="發送"/>

            <TextView
                android:id="@+id/btn_gift"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="3dp"
                android:paddingRight="10dp"
                android:paddingBottom="3dp"
                android:text="禮物"
                android:textColor="#FF00FF"/>
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/layout_gift"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_gravity="bottom"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:text="禮物區域"
        android:visibility="gone"/>

</FrameLayout>

以上↑↑↑

最後附上demo中二次元人物圖片

 

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