赞
踩
适配器中添加点击事件
向中间布局添加的数据
@Override protected void onBindItemViewHolder(VholderActivity holder, final int section, final int position) { holder.tv2.setText(data.get(section).getDepatmentTitle().get(position).getType()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mOnHolderClickListener != null) { mOnHolderClickListener.onHolderClick(view, section, position); } } }); } //点击事件的接口回调 private OnHolderClickListener mOnHolderClickListener; public interface OnHolderClickListener { void onHolderClick(View view, int section, int position); } public void setOnHolderClickListener(OnHolderClickListener listener) { mOnHolderClickListener = listener; }
//主布局的应用
myAdapter.setOnHolderClickListener(new MyAdapter.OnHolderClickListener() { @Override public void onHolderClick(View view, int section, int position) { Toast.makeText(MainActivity.this,"--" +department.get(section).getDepatmentTitle().get(position).getType(), Toast.LENGTH_SHORT).show(); } });
section是头部
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"; }
//两个Fragment中的一个
public class Fragment0 extends BaseFragment { public Fragment0() { } @Override protected String getUrl() { return UC.URL_TAB0; } }
//布局之一
public class MyHeaderViewHolder extends RecyclerView.ViewHolder{//xml布局TextView tv1; public VhHead(View itemView) { super(itemView); tv1 = itemView.findViewById(R.id.tv1); }}
//主方法Tablayout+Viewpager<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/c_f1f1f1" android:orientation="horizontal" android:paddingBottom="5dp" android:paddingLeft="15dp" android:paddingRight="15dp" android:paddingTop="5dp"> <TextView android:id="@+id/title_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <CheckBox android:checked="false" android:background="@android:color/transparent" android:id="@+id/title_checke_all_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@null" android:text="@string/cb_select_all_checed" android:textColor="@color/cb_c_sc_check_all" /> </LinearLayout>
public class MainActivity extends AppCompatActivity { protected TabLayout tablayoutTl; protected ViewPager viewpagerVp; protected FloatingActionButton fab; 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); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); initView(); } @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(); // action_settings上边确定点击吐司 if (id == R.id.action_settings) { BaseFragment fragment = (BaseFragment) fragments.get(tablayoutTl.getSelectedTabPosition()); Toast.makeText(this, "勾选了:" + fragment.getCheckedInfo(), Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); } 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); viewpagerVp.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); fab = (FloatingActionButton) findViewById(R.id.fab); } String[] tabs = {"科室", "兴趣"}; class MyPagerAdapter extends FragmentPagerAdapter { @Override public CharSequence getPageTitle(int position) { return tabs[position]; } public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } } }//适配器中的内容 使用包
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() { //请求数据OkHTTP 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; //适配器 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; } 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(); } private StringBuilder checkedBuilder = new StringBuilder(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。