Android中ViewPager+TabLayout+RecyclerView+点击事件的传递
2017-03-14 15:04
537 查看
Android中ViewPager+TabLayout+RecyclerView+点击事件的传递
这是一套通用的组合,大多数时候都在使用。在这里主要看点击事件的传递,如何将Fragment中RecyclerView的点击事件传递到Activity中去实现其逻辑代码Activity代码:
public class DownLoadedActivity extends AppCompatActivity implements DownLoadedFragment.onRecyclerItemClick { @BindView(R.id.down_image_view) ImageView mDownImageView; @BindView(R.id.down_tab_layout) TabLayout mDownTabLayout; @BindView(R.id.down_view_pager) ViewPager mDownViewPager; @BindView(R.id.down_delete_iamge) ImageView mDownDeleteIamge; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_down); ButterKnife.bind(this); initView(); } private void initView() { List<DownLoadedBaseFragment> fragments = new ArrayList<>(); fragments.add(new DownLoadedFragment()); fragments.add(new DownLoadedFragment()); DownloadedAdapter adapter = new DownloadedAdapter(getSupportFragmentManager(), fragments); mDownViewPager.setAdapter(adapter); mDownTabLayout.setupWithViewPager(mDownViewPager); } @Override public void onRecyclerItemClick(int message) { Toast.makeText(this, String.valueOf(message), Toast.LENGTH_SHORT).show(); } @OnClick({R.id.down_image_view, R.id.down_delete_iamge}) public void onClick(View view) { switch (view.getId()) { case R.id.down_image_view: break; case R.id.down_delete_iamge: break; } } }
DownloadedAdapter代码:
public class DownloadedAdapter extends FragmentPagerAdapter { private List<DownLoadedBaseFragment> mFragments; public DownloadedAdapter(FragmentManager fm, List<DownLoadedBaseFragment> fragments) { super(fm); mFragments = fragments; } @Override public Fragment getItem(int position) { return mFragments.get(position); } @Override public int getCount() { int ret = 0; if(mFragments != null){ ret = mFragments.size(); } return ret; } @Override public CharSequence getPageTitle(int position) { return mFragments.get(position).getFragmentTitle(); } }
DownLoadedBaseFragment 代码
public abstract class DownLoadedBaseFragment extends Fragment { public DownLoadedBaseFragment(){ } public abstract String getFragmentTitle(); }
DownLoadedFragment代码
public class DownLoadedFragment extends DownLoadedBaseFragment { private onRecyclerItemClick mListener; @BindView(R.id.down_recycler_view) RecyclerView mDownRecyclerView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_downloaded, null); ButterKnife.bind(this,view); initView(); return view; } private void initView() { DownloadedRecycylerAdapter adapter = new DownloadedRecycylerAdapter(getActivity()); adapter.setOnRecyclerItemClick(new DownloadedRecycylerAdapter.onRecyclerItemClick() { @Override public void onItemClick(View view, int position) { mListener.onRecyclerItemClick(position); Log.d("Test", "onItemClick: " + position); } }); mDownRecyclerView.setAdapter(adapter); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); mDownRecyclerView.setLayoutManager(layoutManager); } @Override public String getFragmentTitle() { return "Play"; } public interface onRecyclerItemClick{ public void onRecyclerItemClick(int message); } public void onAttach(Activity activity){ super.onAttach(activity); try { mListener = (DownLoadedFragment.onRecyclerItemClick) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + "must implement OnGridViewSelectedListener"); } } }
RecyclerView适配器DownloadedRecycylerAdapter代码:
public class DownloadedRecycylerAdapter extends RecyclerView.Adapter<DownloadedRecycylerAdapter.MyViewHolder>{ private Context mContext; private onRecyclerItemClick mOnRecyclerItemClick; public DownloadedRecycylerAdapter(Context context) { mContext = context; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.down_loaded_recycler_view_item, parent, false); ButterKnife.bind(view); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { if (mOnRecyclerItemClick != null){ if(!holder.itemView.hasOnClickListeners()){ holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int pos = holder.getPosition(); mOnRecyclerItemClick.onItemClick(v,pos); } }); } } } @Override public int getItemCount() { return 12; } public static class MyViewHolder extends RecyclerView.ViewHolder{ public MyViewHolder(View itemView) { super(itemView); } } public interface onRecyclerItemClick { void onItemClick(View view, int position); } public void setOnRecyclerItemClick(onRecyclerItemClick mOnRecyclerItemClick) { this.mOnRecyclerItemClick = mOnRecyclerItemClick; } }
Activity的布局文件activity_down.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" tools:context="com.home.quhong.quhong.TV.DownLoadedActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:id="@+id/down_image_view" android:layout_weight="1" android:src="@drawable/back" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="25dp"/> <TextView android:text="DownLoads" android:textSize="20sp" android:layout_weight="6" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/down_delete_iamge" android:src="@drawable/delete" android:layout_weight="1" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <android.support.design.widget.TabLayout android:id="@+id/down_tab_layout" android:layout_width="match_parent" app:tabIndicatorColor="#080707" android:layout_height="@dimen/tab_layout_default_height"/> <android.support.v4.view.ViewPager android:id="@+id/down_view_pager" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
Fragment的布局文件fragment_downloaded.xml
<LinearLayout 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" android:orientation="vertical" tools:context="com.home.quhong.quhong.TV.fragments.DownLoadedFragment"> <!-- TODO: Update blank fragment layout --> <android.support.v7.widget.RecyclerView android:id="@+id/down_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
RecyclerView中的down_loaded_recycler_view_item.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/down_loaded_cover" android:src="@drawable/one6" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/down_loaded_tv_title" android:textSize="25sp" android:text="Sing_Part2" android:layout_weight="4" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/down_loaded_tv_size" android:text="Unknown Size Wating" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <ProgressBar android:id="@+id/down_loaded_pb" android:max="100" android:layout_weight="1" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="15dp"/> </LinearLayout> </LinearLayout>
相关文章推荐
- Toolbar,DrawerLayout,PagerSlidingTabStrip,AndroidImageSlider,RecyclerView结合使用
- 介绍三个Android支持库控件:TabLayout+ViewPager+RecyclerView
- android ViewPager实现App主界面Tab菜单页面切换和点击事件
- android 开发 TabLayout+ViewPager 点击+滑动效果
- android material design之Tablayout,Recyclerview,Fragment,Viewpager搭配使用(四)
- Android TabLayout、ViewPager实现顶部和底部Tab导航 点击滑动切换Tab页面
- android app主题UI界面框架搭建 tablayout+Viewpager+fragment 导航栏滑动和点击切换界面
- android 控件 翻页 ViewPager TabLayout RecyclerView
- Android AppBarLayout+TabLayout+RecyclerView+ViewPager+Fragment
- 介绍三个Android支持库控件:TabLayout+ViewPager+RecyclerView
- android:viewpager实现图片循环滑动+索引点击事件
- Android ViewPager+Fragment滑动选项卡,tab点击选项卡
- Android特效专辑(四)——APP主页框架TabHost绑定ViewPager的替换者TabLayout
- Android进阶-多个ViewPager嵌套时事件传递的处理
- 安卓官方ViewPager与android.support.design.widget.TabLayout双向交互联动切换 。
- Android ViewPager与子控件点击事件冲突的解决方案
- Android TabLayout定制CustomView与ViewPager交互双向联动
- Android-RecyclerView-Item点击事件设置
- 在Layout响应点击事件但不传递给内部的childView
- Android-RecyclerView-Item点击事件设置