Android中ViewPager+TabLayout+RecyclerView+点击事件的传递
来源:互联网 发布:h5 css js手机开发 编辑:程序博客网 时间:2024/06/05 09:01
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>
0 0
- Android中ViewPager+TabLayout+RecyclerView+点击事件的传递
- Android RecyclerView 的点击事件
- Android中TabLayout+ViewPager的简单使用
- android 控件 翻页 ViewPager TabLayout RecyclerView
- Android AppBarLayout+TabLayout+RecyclerView+ViewPager+Fragment
- Android中RecyclerView简单的点击事件(不推荐)
- Android中RecyclerView添加item的点击事件
- Android中为RecyclerView添加item的点击事件
- android点击事件的传递
- Android中RecyclerView使用,RecyclerView-Item点击事件设置
- android 开发 TabLayout+ViewPager 点击+滑动效果
- 【Android】RecyclerView的OnItemClickListener点击事件处理
- RecyclerView中点击事件的添加
- 重写TabLayout的点击事件
- RecyclerView的点击事件
- RecyclerView的点击事件
- RecyclerView的点击事件
- RecyclerView的点击事件
- ANR问题记录
- leetcode解题之160# Intersection of Two Linked Lists Java版 (两个链表的第一个公共结点)
- 明明安装了Java8,启动Tomcat报错no JDK or JRE found
- Deposit Promotion 30 Free 50 by iBET Malaysia Only(deposit bonus, Deposit Promotion, Free Register i
- Java中的设计模式与原则
- Android中ViewPager+TabLayout+RecyclerView+点击事件的传递
- spring-boot-druid-demo
- mybatis中Oracle数据库的批量增删改操作小结
- B1006
- Android 简单实现缓存机制(反射和数据库)
- 3D—touch
- ubuntu下安装redis
- 关于Linux下Apache的站点开启方法
- git的优秀博客收藏