首先添加依赖:compile 'com.android.support:design:23.0.0'
布局文件:
关键属性(组件可以隐藏)[布局]:fuck:layout_scrollFlags="scroll|enterAlwaysCollapsed"
关键点:解决ListView嵌套在其他布局里面,有可能只显示一行,需要重写<span style="font-family: Consolas;">onMeasure方法!</span>
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fuck="http://schemas.android.com/apk/res-auto"
tools:context="com.android.coordinatorlayoutdemo.MyActivity">
<!-- 实现悬浮控件置顶-->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="@mipmap/bb7"
fuck:layout_scrollFlags="scroll|enterAlwaysCollapsed"
/>
<LinearLayout
android:id="@+id/hoveringLayout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#fff"
>
<RadioGroup
android:id="@+id/item_list_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="4">
<RadioButton
android:id="@+id/item_list_b1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:drawableBottom="@drawable/match_toolbar_main"
android:gravity="center"
android:text="热门"
android:textColor="@color/main_text_yes" />
<RadioButton
android:id="@+id/item_list_b2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="@drawable/match_toolbar_main"
android:gravity="center"
android:text="关注"
android:textColor="@color/main_text_no" />
<RadioButton
android:id="@+id/item_list_b3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="@drawable/match_toolbar_main"
android:gravity="center"
android:text="红人"
android:textColor="@color/main_text_no" />
<RadioButton
android:id="@+id/item_list_b4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="@drawable/match_toolbar_main"
android:gravity="center"
android:text="线下店"
android:textColor="@color/main_text_no" />
</RadioGroup>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
fuck:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Activity里面的JAVA代码:
public class MyActivity extends AppCompatActivity implements View.OnClickListener {
private FrameLayout layout;
private RadioGroup radioGroup;
private MyFragment fragment1, fragment2, fragment3, fragment4;
private Fragment cunnFragment;
private Bundle bundle;
private String[] strs = {"热门碎片", "关注碎片", "红人碎片", "线下店碎片"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
initView();
}
private void initView() {
layout = (FrameLayout) findViewById(R.id.layout);
radioGroup = (RadioGroup) findViewById(R.id.item_list_group);
for (int i = 0; i < radioGroup.getChildCount(); i++) {
//遍历单选组里面的控件,设置点击事件
((RadioButton) radioGroup.getChildAt(i)).setOnClickListener(this);
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
bundle = new Bundle();
bundle.putString("text", strs[0]);
fragment1 = new MyFragment();
fragment1.setArguments(bundle);
transaction.add(R.id.layout, fragment1);
cunnFragment = fragment1;
transaction.commit();
}
private int counnt;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.item_list_b1:
counnt = 0;
isitFragment(1, fragment1);
break;
case R.id.item_list_b2:
counnt = 1;
isitFragment(2, fragment2);
break;
case R.id.item_list_b3:
counnt = 2;
isitFragment(3, fragment3);
break;
case R.id.item_list_b4:
counnt = 3;
isitFragment(4, fragment4);
break;
}
for (int i = 0; i < radioGroup.getChildCount(); i++) {
RadioButton button = (RadioButton) radioGroup.getChildAt(i);
if (i == counnt) {
button.setTextColor(getResources().getColor(R.color.main_text_yes));
continue;
}
button.setTextColor(getResources().getColor(R.color.main_text_no));
}
}
public void isitFragment(int counnt, Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (fragment == null) {//碎片对象为空
fragment = new MyFragment();
bundle = new Bundle();
bundle.putString("text", strs[counnt - 1]);
fragment.setArguments(bundle);
}
if (fragment.isAdded()) {//碎片如果存在
transaction.hide(cunnFragment);
transaction.show(fragment);
} else {
transaction.hide(cunnFragment);
transaction.add(R.id.layout, fragment);
}
transaction.commit();
cunnFragment = fragment;
}
}
Fragment碎片(布局文件为一个自定义的ListView)
public class MyFragment extends Fragment {
private MyListView listView;
private List<String>strs;
private String text;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text=getArguments().getString("text");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
listView = (MyListView) view.findViewById(R.id.listview);
setAdapter();
return view;
}
private void setAdapter() {
strs=new ArrayList<>();
for (int i = 0; i <30 ; i++) {
strs.add(text+"的"+(i+1)+"号数据");
}
listView.setAdapter(new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,strs));
}
}
自定义的ListView,重点: 重写onMeasure方法,告诉父容器自己的高度(这里给一个很大的值)
public class MyListView extends ListView{
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}