Coordinatorlayout + collpasingtoolbar + recyclerview + editText无法按预期工作

时间:2016-08-06 04:05:15

标签: android android-layout android-recyclerview android-coordinatorlayout android-collapsingtoolbarlayout

首先很抱歉提出另一个类似的问题。但其他问题都无法解决我的挑战。我在这里创建了一个github项目 https://github.com/winster/collapsingtoolbar

activity_scrolling.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light"
        android:fitsSystemWindows="true"
        android:layout_above="@+id/footer">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/main.appbar"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:theme="@style/AppTheme.AppBarOverlay"
            android:fitsSystemWindows="true">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/main.collapsing"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginStart="48dp"
                app:expandedTitleMarginEnd="64dp">


                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:layout_collapseMode="pin"/>

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_scrolling" />


    </android.support.design.widget.CoordinatorLayout>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="5"
        android:background="@color/colorPrimary"
        android:alpha="1"
        android:layout_alignParentBottom="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <EditText android:id="@+id/data"
            android:layout_width="0dp"
            android:layout_height="24dp"
            android:hint="Enter data"
            android:paddingLeft="10dp"
            android:background="@null"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="16dp"
            android:lines="1"
            android:layout_weight="3" />

        <Button android:id="@+id/btn_send"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:text="SEND"
            android:textSize="16dp"
            android:textColor="@color/colorPrimary" />

    </LinearLayout>

</RelativeLayout>

content_scrolling.xml

<RelativeLayout 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="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="0dp"
        android:scrollbars="vertical" />
</RelativeLayout>

ScrollingActivity.java

public class ScrollingActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private MyAdapter mAdapter;
    private ArrayList<Card> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        arrayList = new ArrayList<>();
        mAdapter = new MyAdapter(this, arrayList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        for(int i=0;i<60;++i) {
            arrayList.add(new Card("TEXT "+i));
        }
        mAdapter.notifyDataSetChanged();
        if (mAdapter.getItemCount() > 1) {
            ((LinearLayoutManager)recyclerView.getLayoutManager()).setStackFromEnd(true);
            recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, null, mAdapter.getItemCount() - 1);
            //recyclerView.scrollToPosition(mAdapter.getItemCount()-1);
        }
    }        
}

的AndroidManifest.xml

<activity
        android:name=".ScrollingActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustPan">

现在有3个问题

  1. 尽管我尝试了不同的选项,例如stackFromEnd和smoothScrollToPosition

    ,但RecyclerView并未滚动到底部

    1.1 enter image description here

  2. 将appbar保持在展开模式下,如果点击EditText,工具栏不会折叠,而只是向上滚动,但工具栏的一部分显示为标题

    2.1 enter image description here

  3. 保持appbar处于折叠模式,点击EditText会将工具栏滚动到顶部并且不可见。

    3.1 enter image description here

1 个答案:

答案 0 :(得分:0)

删除第android:fitsSystemWindows="true"

并删除状态栏颜色或将颜色从透明颜色更改为某些颜色 Valus / styles.xml(v21)中的其他颜色和变化。

希望这可能有助于你

如果没有尝试更改app:layout_scrollFlags="scroll|exitUntilCollapsed"  到app:layout_scrollFlags="scroll|exitUntilCollapsed"|snap