Android标题栏随着滑动显示隐藏

这次实现标题栏随着上滑下滑显示隐藏

实现这个效果需要,Support Design库中的CoordinatorLayout和AppBarLayout进行配合才行。

dependencies {
    ...
    implementation 'com.android.support:design:27.1.1' //必须添加
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'
   
}

xml布局代码



    
        

        
    
    

    


Activity代码

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    RecyclerView recyclerView;
    private BaseQuickAdapter baseQuickAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=findViewById(R.id.toolbar);
        recyclerView=findViewById(R.id.recycle);
        setSupportActionBar(toolbar);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final ArrayList strings = new ArrayList<>();
        for(int x=0;x<10;x++){
            strings.add("我是条目"+x);
        }
        baseQuickAdapter = new BaseQuickAdapter(R.layout.item, strings) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                helper.setText(R.id.tv_title, item);
            }
        };
        baseQuickAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
//        baseQuickAdapter.isFirstOnly(false);
        baseQuickAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        recyclerView.setAdapter(baseQuickAdapter);
    }
}

还需要在主题中将标题去掉

 

扩展一:将Toolbar换成ImageView和TabLayout实现图片显示隐藏

xml布局代码



    
        
        
            
            
            
        
    
    

    


你可能感兴趣的:(Android标题栏随着滑动显示隐藏)