Android软键盘和panAdjust

时间:2018-07-10 19:55:46

标签: java android layout android-softkeyboard

我正在尝试实现与Todoits应用程序中类似的效果,您可以在其中单击FAB,它显示了一个用于添加新任务的浮动弹出窗口。 外观如下:

enter image description here

我最接近实现的方法是:

enter image description here

它快要出现了,但是我希望弹出窗口在键盘上方大约10dp,然后将回收查看器滚动到最后一个项目,但我无法到达那里:/当前,弹出窗口居中。

在将弹出窗口添加到屏幕底部并设置android:windowSoftInputMode =“ adjustResize”之前,我已经尝试过了,但这也使我的底部导航也移到了键盘上方。将windowsSoftInputMode设置为panAdjust会删除工具栏和列表中的少量项目。

您对如何获得与Todoist中的效果相似的任何建议吗?也许有一个DialogFragment?

以下是我当前的代码(导航栏实现仍未完成): MainActivity:

public class MainActivity extends AppCompatActivity {
  private WorkoutsListFragment workoutsListFragment;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar myToolbar = findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);
    ActionBar actionBar = getSupportActionBar();

    this.workoutsListFragment = new WorkoutsListFragment();
    getSupportFragmentManager()
        .beginTransaction()
        .add(R.id.main_layout, workoutsListFragment)
        .commit();
  }
}

WorkoutListFragment:

public class WorkoutsListFragment extends Fragment
    implements View.OnClickListener, WorkoutListViewHolderInterface {

  private RecyclerView recyclerView;
  private WorkoutListRecycleViewAdapter recycleViewAdapter;
  private AddBoxView addBoxView;
  private FloatingActionButton actionButton;
  private InputMethodManager imm;
  private WorkoutViewModel workoutViewModel;
  private boolean addBoxStatus;

  public WorkoutsListFragment() {}

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.addBoxStatus = false;
   //Irrelevant code removed (Dagger 2 DI)

    this.workoutViewModel =
        ViewModelProviders.of(this, workoutViewModelFactory).get(WorkoutViewModel.class);

    workoutViewModel.init(7);
    workoutViewModel.getWorkout().observe(this, w -> this.updateWorkoutsList(w, false));

    this.imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  }

  @Override
  public View onCreateView(
      @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_workouts_list, container, false);
    this.recyclerView = view.findViewById(R.id.workoutsList);
    //        this.recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager recyclerViewLayoutManager = new LinearLayoutManager(getContext());
    this.recyclerView.setLayoutManager(recyclerViewLayoutManager);

    this.recycleViewAdapter = new WorkoutListRecycleViewAdapter(this);
    this.recyclerView.setAdapter(recycleViewAdapter);

    this.actionButton = view.findViewById(R.id.floating_action_add_workout);
    this.actionButton.setOnClickListener(this);

    this.addBoxView = new AddBoxView(Objects.requireNonNull(getContext()));
    FrameLayout.LayoutParams addBoxLayoutParams =
        new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT,
            Gravity.CENTER);

    this.addBoxView.setLayoutParams(addBoxLayoutParams);
    this.addBoxView.getButton().setOnClickListener(this);

    return view;
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.floating_action_add_workout:
        this.addBoxView((FrameLayout) v.getParent(), v);
        break;
      case R.id.overlay_layout:
        removeAddBoxView((FrameLayout) v);
        break;
      case R.id.button:
        this.addNewWorkout(this.addBoxView.getInputText());
        break;
    }
  }


//Irrelevant code removed.... 

  private void addBoxView(FrameLayout viewGroup, View v) {
    this.addBoxStatus = true;
    viewGroup.setClickable(true);
    viewGroup.setOnClickListener(this);
    viewGroup.setBackgroundColor(
        getResources().getColor(R.color.cardview_shadow_start_color, null));
    viewGroup.removeView(v);
    viewGroup.addView(this.addBoxView);
    this.addBoxView.requestInputFocus();
    this.imm.showSoftInput(this.addBoxView.getInput(), InputMethodManager.SHOW_IMPLICIT);
  }

  private void removeAddBoxView(FrameLayout viewGroup) {
    viewGroup.setClickable(false);
    this.imm.hideSoftInputFromWindow(this.addBoxView.getInput().getWindowToken(), 0);
    viewGroup.removeView(this.addBoxView);
    viewGroup.setBackgroundColor(getResources().getColor(R.color.cardview_shadow_end_color, null));
    viewGroup.addView(this.actionButton);
    this.addBoxStatus = false;
  }
}

Fragment_workouts_list布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:background="#e1e1e1"
    android:padding="0dp"
    tools:context=".Activities.MainActivity.Fragments.Workouts.WorkoutsListFragment"
    tools:layout_editor_absoluteY="81dp">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/workoutsList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:elevation="0dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@+id/add_box_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </android.support.v7.widget.RecyclerView>
    <FrameLayout
        android:id="@+id/overlay_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="false"
        android:outlineProvider="bounds">
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/floating_action_add_workout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="40dp"
            android:layout_marginEnd="40dp"
            android:clickable="true"
            android:src="@android:color/holo_blue_dark" />
    </FrameLayout>
</FrameLayout>

MainActivity布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:id="@+id/root_view"
    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_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/AppTheme.AppBar" />

    <FrameLayout
        android:id="@+id/main_layout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/my_toolbar">
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/colorPrimary"
        android:foregroundGravity="bottom"
        android:visibility="visible"
        app:itemTextColor="@color/textColorWhite"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/my_navigation_items" />
</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案