style.xml主题中设置
- true
//透明的状态栏
- true
//透明的导航图
- true
//设置应用布局时是否考虑系统窗口布局
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//判断,如果当前设备android版本大于或等于api4.4,就将status bar设置为透明的
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// Translucent status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
//这里其实可以通过values-v19 的sytle.xml里设置windowTranslucentStatus
//属性为true来进行设置,但是在某些手机会不起效,所以采用代码的形式进行设置
}
}
需要在toolbar的父控件中设置一个属性:
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
将这个属性设置在viewpager中,其表示viewpager始终在AppBarLayout下面。
1、一定要在tablayout中设置以下属性:
app:layout_scrollFlags="scroll|enterAlways"
scroll 表示向下滚动时,这个View会被滚出屏幕范围直到隐藏.
2、如果是在CoodinatorLayout外面再套了一层DrawerLayout,出现toolbar隐藏失效。
只需要在CoodinatorLayout外层包裹一层LinearLayout,然后将
android:fitsSystemWindows="true"
写在LinearLayuot中即可。
是因为给左侧菜单布局的时候,左侧菜单的FrameLayout写在了主要内容LinearLayout之前,只要调整下布局就好了。
正确的布局,
正确代码:
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(Test1Activity.this, "刷新成功", Toast.LENGTH_SHORT).show();
}
}, 2000);
}
});
关键代码:
/**
* Created by Administrator on 2016/1/8 20:46.
*/
public class TestAdapter extends RecyclerView.Adapter {
private static final int TYPE_LIST = 0;
private static final int TYPE_STAGGERED = 1;
private static final int TYPE_HEADER = 2;
private static final int TYPE_INFO = 3;
List mHeight;
List mDatas;
public Context mContext;
public TestAdapter(List datas) {
this.mDatas = datas;
mHeight = new ArrayList<>();
for (int i = 0; i < mDatas.size(); i++) {
mHeight.add((int) (100 + Math.random() * 300));
}
}
@Override
public int getItemViewType(int position) {
//前半部分是list,后半部分是瀑布流
if (position == 0) {
return TYPE_HEADER;
} else if (position == 2 || position == 3 || position == 5 || position == 6) {
return TYPE_INFO;
} else if (position < 8) {
return TYPE_LIST;
}
return TYPE_STAGGERED;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
if (viewType == TYPE_LIST) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item1, parent, false);
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
layoutParams.setFullSpan(true);
view.setLayoutParams(layoutParams);
return TestViewHolder1.newInstance(view);
} else if (viewType == TYPE_STAGGERED) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item1, parent, false);
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
layoutParams.setFullSpan(false);
view.setLayoutParams(layoutParams);
return TestViewHolder1.newInstance(view);
} else if (viewType == TYPE_HEADER) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_header, parent, false);
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
layoutParams.setFullSpan(true);
view.setLayoutParams(layoutParams);
return HeaderHolder.newInstance(mContext, view);
} else if (viewType == TYPE_INFO) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_info, parent, false);
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
layoutParams.setFullSpan(true);
view.setLayoutParams(layoutParams);
return InfoViewHolder.newInstance(view);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
if (viewType == TYPE_LIST) {
//要减去header的1
position = position - 1;
TestViewHolder1 testViewholder = (TestViewHolder1) holder;
testViewholder.loadData(mDatas.get(position));
} else if (viewType == TYPE_STAGGERED) {
//要减去header的1
position = position - 1;
TestViewHolder1 testViewholder = (TestViewHolder1) holder;
ViewGroup.LayoutParams layoutParams = testViewholder.cardView.getLayoutParams();
layoutParams.height = mHeight.get(position);
testViewholder.cardView.setLayoutParams(layoutParams);
testViewholder.loadData(mDatas.get(position));
} else if (viewType == TYPE_HEADER) {
HeaderHolder headerHolder = (HeaderHolder) holder;
headerHolder.loadData();
} else if (viewType == TYPE_INFO) {
}
}
@Override
public int getItemCount() {
// +1(header)
return mDatas.size() + 1;
}
}
这个问题害我找了好久啊,最后居然是因为inflate的方式不对。
如果给条目inflate的时候没有指定父容器(recyclerview),就会显示一点点。至于为什么会这样,还有待探究。。
LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.item_test_1, parent,false);
//LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.item_test_1, null);