这是布局1
<android.support.constraint.ConstraintLayout 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:background="@color/c_f1f1f1"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.my01.MainActivity"
tools:showIn="@layout/activity_main">
<!--android:background="@android:color/white"-->
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_tl"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="2dp"
android:background="@color/c_ffffff"
app:layout_constraintTop_toTopOf="parent"
app:tabIndicatorColor="@color/c_5183d9"
app:tabSelectedTextColor="@color/c_5183d9"
app:tabTextColor="@color/c_606060" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/tablayout_tl">
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
<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" tools:context="com.example.my01.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/AppTheme.ActionBar" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /></android.support.design.widget.CoordinatorLayout>下面是嵌套的drawable
cb_d_bg_scsubscribe.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/cb_d_bg_sp_subscribe_checked" android:state_checked="true" /> <item android:drawable="@drawable/cb_d_bg_sp_subscribe_normal" android:state_checked="false" /></selector>cb_d_bg_sp_subscribe_checked.xml
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/c_ffffff" /> <corners android:radius="2dp" /> <stroke android:color="@color/c_5183d9" android:width="2dp" /></shape>cb_d_bg_sp_subscribe_normal.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/c_ffffff" /> <stroke android:width="2dp" android:color="@color/c_ccc9c9" /> <corners android:radius="2dp" /></shape>cb_d_sc_subscribe.xml 这个里面有两张图片
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/cb_d_ic_subscribe_normal" android:state_checked="true" /> <item android:drawable="@drawable/cb_d_ic_subscribe_checked" android:state_checked="false" /></selector>主布局
public class MainActivity extends AppCompatActivity { protected TabLayout tablayoutTl; protected ViewPager viewpagerVp; private String TAG = this.getClass().getSimpleName(); private ArrayList<Fragment> fragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle("分类选择"); toolbar.setTitleTextColor(getResources().getColor(R.color.c_606060)); setSupportActionBar(toolbar); //实例化 initView(); } private void initView() { tablayoutTl = (TabLayout) findViewById(R.id.tablayout_tl); fragments = new ArrayList<>(); //添加了两个碎片 fragments.add(new Fragment0()); fragments.add(new Fragment1()); viewpagerVp = (ViewPager) findViewById(R.id.viewpager_vp); tablayoutTl.setupWithViewPager(viewpagerVp);//tablayout与viewpager绑定 viewpagerVp.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));//viewpager和适配器进行绑定 } //系统菜单 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } //确定按钮的触发 @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) {//action_settings确定按钮的名字 BaseFragment fragment = (BaseFragment) fragments.get(tablayoutTl.getSelectedTabPosition()); Toast.makeText(this, "勾选了:" + fragment.getCheckedInfo(), Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); } //title直接和适配器绑定 String[] tabs = {"科室", "兴趣"}; //适配器 class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } //和 String[] tabs = {"科室", "兴趣"}; 绑定在一起 @Override public CharSequence getPageTitle(int position) { return tabs[position]; } }
interface UC { String URL_TAB0 = "https://raw.githubusercontent.com/zhang721788/testmaterial/master/doctor0.json"; String URL_TAB1 = "https://raw.githubusercontent.com/zhang721788/testmaterial/master/doctor1.json";}
public class Fragment0 extends BaseFragment { @Override protected String getUrl() { return UC.URL_TAB0; }}
public class Fragment1 extends BaseFragment { @Override protected String getUrl() { return UC.URL_TAB1; }}
public abstract class BaseFragment extends Fragment { protected DoctorInfo doctorInfo; private MyRecyclerAdapter adapter; private float density; private int widthPixels; public BaseFragment() { } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); density = getContext().getResources() .getDisplayMetrics() .density; DisplayMetrics displayMetrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); //屏幕宽度px widthPixels = displayMetrics.widthPixels; Log.i(TAG, "onCreate: 屏幕的宽度是***" + widthPixels); int densityDpi = displayMetrics.densityDpi; Log.i(TAG, "onCreate: densityDpi***" + densityDpi); float density = displayMetrics.density; Log.i(TAG, "onCreate: density***" + density); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_recycler, null); initView(view); request(); return view; } private void initView(View rootView) { recyclerRv = (RecyclerView) rootView.findViewById(R.id.recycler_rv); GridLayoutManager manager = new GridLayoutManager(this.getActivity(), 2); recyclerRv.setLayoutManager(manager); recyclerRv.setHasFixedSize(true); adapter = new MyRecyclerAdapter(); recyclerRv.setAdapter(adapter); //设置列的跨度 manager.setSpanSizeLookup(new SectionedSpanSizeLookup(adapter, manager)); } protected View rootView; protected String TAG = this.getClass().getSimpleName(); protected RecyclerView recyclerRv; //请求数据 private void request() { gson = new Gson(); new OkHttpClient.Builder() .build().newCall(new Request.Builder().url(getUrl()).build()) .enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); Log.i(TAG, result); doctorInfo = gson.fromJson(result, DoctorInfo.class); Log.i(TAG, "doctorInfo: ***" + doctorInfo.toString()); BaseFragment.this.getActivity().runOnUiThread(new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); } }); } }); } //调用的路径 protected abstract String getUrl(); protected Gson gson; //recycleView适配器 class MyRecyclerAdapter extends SectionedRecyclerViewAdapter<MyHeaderViewHolder, MyViewHolder, MyFootHeader> { int itemSpaceWidth; int cbShouldWidth; public MyRecyclerAdapter() { itemSpaceWidth = dp2px(15); cbShouldWidth = (widthPixels - 3 * itemSpaceWidth) / 2; } public MyRecyclerAdapter(int dpValue) { itemSpaceWidth = dp2px(dpValue); } @Override protected int getSectionCount() { return doctorInfo == null ? 0 : doctorInfo.getData().size(); } @Override protected int getItemCountForSection(int section) { return doctorInfo == null ? 0 : doctorInfo.getData().get(section).getChilds().size(); } @Override protected boolean hasFooterInSection(int section) { return false; } @Override protected MyHeaderViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_header, null); return new MyHeaderViewHolder(view); } @Override protected MyFootHeader onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) { return null; } @Override protected MyViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_normal, null); return new MyViewHolder(view); } //头部的全选按钮 @Override protected void onBindSectionHeaderViewHolder(final MyHeaderViewHolder holder, final int section) { holder.titleTv.setText(doctorInfo.getData().get(section).getDataName()); holder.titleCheckeAllCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { holder.titleCheckeAllCb.setText(R.string.cb_select_all_cancel); for (DoctorInfo.DataBean.ChildsBean bean : doctorInfo.getData().get(section).getChilds()) { bean.setIsFocus(1); } } else { holder.titleCheckeAllCb.setText(R.string.cb_select_all_checed); for (DoctorInfo.DataBean.ChildsBean bean : doctorInfo.getData().get(section).getChilds()) { bean.setIsFocus(0); } } notifyDataSetChanged(); } }); } //中间视图的数据传输 以及点击事件全选和取消全选 @Override protected void onBindItemViewHolder(final MyViewHolder holder, final int section, final int position) { holder.itemSelectCb.setText(doctorInfo.getData().get(section).getChilds().get(position).getDepName()); LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.bg_ll.getLayoutParams(); layoutParams.width = cbShouldWidth; if (position % 2 == 0) { layoutParams.leftMargin = itemSpaceWidth; } else if (position % 2 == 1) { layoutParams.leftMargin = itemSpaceWidth / 2; } holder.bg_ll.setLayoutParams(layoutParams); holder.itemSelectCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { holder.bg_ll.setBackgroundDrawable(holder.bg_ll.getResources().getDrawable(R.drawable.cb_d_bg_sp_subscribe_checked)); doctorInfo.getData().get(section).getChilds().get(position).setIsFocus(1); } else { holder.bg_ll.setBackgroundDrawable(holder.bg_ll.getResources().getDrawable(R.drawable.cb_d_bg_sp_subscribe_normal)); doctorInfo.getData().get(section).getChilds().get(position).setIsFocus(0); } } }); if (doctorInfo.getData().get(section).getChilds().get(position).getIsFocus() == 0) { holder.itemSelectCb.setChecked(false); } else { holder.itemSelectCb.setChecked(true); } } @Override protected void onBindSectionFooterViewHolder(MyFootHeader holder, int section) { } } public int dp2px(int dpValue) { int resultValue = (int) (dpValue * density + 0.5); Log.i(TAG, "dp2px: dp转换px***" + resultValue); return resultValue; } //将选中的东西传入到主MainActivity中的系统菜单中然后由 确定按钮 进行触发 public String getCheckedInfo() { if ( doctorInfo != null) { checkedBuilder.setLength(0); for (int i = 0; i < doctorInfo.getData().size(); i++) { for (int j = 0; j < doctorInfo.getData().get(i).getChilds().size(); j++) { if (doctorInfo.getData().get(i).getChilds().get(j).getIsFocus() == 1) { checkedBuilder.append(doctorInfo.getData().get(i).getChilds().get(j).getDepName()); checkedBuilder.append(","); } } } } return checkedBuilder.toString(); } //创建一个StringBuilder进行字符串拼接 private StringBuilder checkedBuilder = new StringBuilder();}
创建一个menu 主布局中的确定按钮
<menu 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" tools:context="com.example.my01.MainActivity"> <item android:id="@+id/action_settings" android:title="@string/action_settings" app:showAsAction="always" /></menu>