无法让工具栏崩溃

时间:2015-10-29 04:58:07

标签: android android-recyclerview android-collapsingtoolbarlayout

更新了问题。我尝试从strings.xml中的字符串数组设置适配器,然后将适配器设置为recyclerview(而不是从游标加载的适配器),但它仍然无法正常工作。我在这里再次发布了2个文件(activity_article_list.xml - 主布局文件和ArticleListActivity.java):

activity_article_list.xml

<android.support.design.widget.CoordinatorLayout
    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.support.design.widget.AppBarLayout
        android:id="@+id/my_appbar_container"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:background="@color/primary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="@color/primary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/days_list_3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

ArticleListActivity.java

public class ArticleListActivity extends AppCompatActivity {

private Toolbar mToolbar;
private SwipeRefreshLayout mSwipeRefreshLayout;
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;

Toolbar toolbar;
RecyclerView planets_list;
RecyclerView.LayoutManager layout_manager;
RecyclerViewAdapter adapter;
CollapsingToolbarLayout collapsing_container;

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

    toolbar = (Toolbar) findViewById(R.id.technique_three_toolbar);
    planets_list = (RecyclerView) findViewById(R.id.days_list_3);
    collapsing_container = (CollapsingToolbarLayout) findViewById(R.id.collapsing_container);

    layout_manager = new LinearLayoutManager(this);
    planets_list.setLayoutManager(layout_manager);

    adapter = new RecyclerViewAdapter(getResources().getStringArray(R.array.days_names));
    planets_list.setAdapter(adapter);

    setSupportActionBar(toolbar);
    collapsing_container.setTitle(getString(R.string.app_name));

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}
}

RecyclerViewAdapter.java

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

    String data[];

    public RecyclerViewAdapter(String data[]){
        this.data = data;
    }

    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.day_item, parent, false);

        ViewHolder vh = new ViewHolder(v);

        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
        holder.text_view.setText(data[position]);
    }

    @Override
    public int getItemCount() {
        return data.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView text_view;
        public ViewHolder(View v) {
            super(v);
            text_view = (TextView) v.findViewById(R.id.planet_name);
        }
    }
}

的strings.xml:

<resources>

    <string name="app_name">XYZ Reader</string>
    <string name="welcome_xyz_reader">Welcome to XYZ Reader!</string>

    <!-- Days -->
    <string-array name="days_names">
        <item>Monday</item>
        <item>Tuesday</item>
        <item>Wednesday</item>
        <item>Thursday</item>
        <item>Friday</item>
        <item>Saturday</item>
        <item>Sunday</item>
    </string-array>

</resources>

那些已完成Make Your App Material项目的人,请给我一些关于如何在主要活动中添加功能齐全的折叠工具栏的提示。

感谢。

2 个答案:

答案 0 :(得分:0)

你在CoordinatorLayout中包装了整个视图吗? e.g。

Display Value=21

我删除了参数。

此滚动行为仅由CoordinatorLayout控制。任何其他容器都无法做到这一点。

答案 1 :(得分:0)

根据以下内容更改您的AppBarLayout代码...您的代码中缺少一些行...这对我来说非常合适。

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="112dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|enterAlwaysCollapsed|enterAlways">

        <android.support.v7.widget.Toolbar
            android:id="@+id/anim_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/index6"
            android:fitsSystemWindows="true"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways">



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

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