简体   繁体   English

视差效果在Android Material Design中不起作用

[英]Parallax effect not working in Android Material Design

I am trying to create a parallax effect using Android's material design. 我正在尝试使用Android的材质设计来创建视差效果。 Please see the below main xml code... 请参阅以下主要xml代码...

 <?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
    tools:context="com.mydomain.filterlistview.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:minHeight="0dp"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

    <!--<android.support.v7.widget.SearchView-->
        <!--android:id="@+id/search_view"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="wrap_content" />-->

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

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

I wanted to hide the Toolbar when the RecyclerView is scrolled up and vice versa. 当RecyclerView向上滚动时,我想隐藏工具栏,反之亦然。 But this is not happening. 但是,这没有发生。

Below is my build.gradle.. 下面是我的build.gradle ..

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:recyclerview-v7:23.2.1'
    compile 'com.android.support:cardview-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
}

Android Material Design Android Material Design Android Material Design Android材质设计

确保您使用的是com.android.support:recyclerview-v7:22.2.0或更高版本!

This Code works for me. 该代码对我有用。

activity_main.xml activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <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:fitsSystemWindows="true" > <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="180dp" android:fitsSystemWindows="true"> <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:minHeight="0dp" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <!--<android.support.v7.widget.SearchView--> <!--android:id="@+id/search_view"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="wrap_content" />--> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="5dp" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout> 

MainActivity.java MainActivity.java

public class MainActivity extends AppCompatActivity {
private RecyclerView mRecycler ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRecycler = (RecyclerView) findViewById(R.id.recycler_view);
    LinearLayoutManager manager =new LinearLayoutManager(this);
    mRecycler.setLayoutManager(manager);
    MyRecyclerAdapter adapter = new MyRecyclerAdapter(this);
    mRecycler.setAdapter(adapter);

}

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder>{
    private Context mContext ;
    private LayoutInflater inflater;
    public MyRecyclerAdapter(Context ctx){
        this.mContext = ctx;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.recycle_item,parent,false);
        MyViewHolder holder =new MyViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
       holder.txtView.setText("HELLO");
    }

    @Override
    public int getItemCount() {
        return 30;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
    private TextView txtView;
        public MyViewHolder(View itemView) {
            super(itemView);
            txtView = (TextView) itemView.findViewById(R.id.text_view);
        }
    }
}
}

manifest.xml manifest.xml

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.naveenbm.parallaxstack"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

styles.xml styles.xml

 <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources> 

recycler_item.xml recycler_item.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="hello" android:textSize="22sp" android:visibility="visible" android:textColor="#000000" android:layout_gravity="center_horizontal" /> </LinearLayout> 

NOTE: I have Removed app:popupTheme="@style/AppTheme.PopupOverlay" from your ToolBar and added app theme as Theme.AppCompat.Light.NoActionBar in menifest file. 注意:我从您的工具app:popupTheme="@style/AppTheme.PopupOverlay"删除了app:popupTheme="@style/AppTheme.PopupOverlay" ,并在Theme.AppCompat.Light.NoActionBar文件Theme.AppCompat.Light.NoActionBar应用程序主题添加为Theme.AppCompat.Light.NoActionBar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM