单击 RecyclerView 转到另一个包含 android 中数据的活动

2024-04-15

我正在使用 RecyclerVeiw 显示一些图像,单击它应该启动新活动,显示 RecyclerView 的 id、名称。我实现了代码,但它现在可以工作,但不可点击。下面是我的适配器

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;

import zesteve.com.myapplication.Catagory;
import zesteve.com.myapplication.CatagoryVendListActivity;
import zesteve.com.myapplication.R;

/**
 * Created by Ravi Shankar on 5/13/2017.
 */

public class CatagoryAdapter extends RecyclerView.Adapter<CatagoryAdapter.MyViewHolder>{

    private Context mContext;
    private ArrayList<Catagory> CatagoryList;

    public CatagoryAdapter(Context mContext, ArrayList<Catagory> CatagoryList) {
        this.mContext = mContext;
        this.CatagoryList = CatagoryList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.catagory_card, parent, false);

        return new MyViewHolder(itemView,mContext,CatagoryList);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        Catagory catagory = CatagoryList.get(position);
        holder.title.setText(catagory.getName());
        holder.count.setText(catagory.getNumOfVend() + " Places");

        // loading album cover using Picasso library
        Picasso.with(mContext).load(catagory.getThumbnail()).into(holder.thumbnail);
    }

    @Override
    public int getItemCount() {
        return CatagoryList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView title, count;
        public ImageView thumbnail;

        ArrayList<Catagory> CatagoryList = new ArrayList<Catagory>();
        Context mContext;

        public MyViewHolder(View view , Context mContext, ArrayList<Catagory> CatagoryList ) {
            super(view);
            this.CatagoryList = CatagoryList;
            this.mContext = mContext;
            view.setOnClickListener(this);
            title = (TextView) view.findViewById(R.id.title);
            count = (TextView) view.findViewById(R.id.count);
            thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            Catagory catagory = this.CatagoryList.get(position);
            Intent intent = new Intent(this.mContext, CatagoryVendListActivity.class);
            intent.putExtra("CatId",catagory.getVendId());
            intent.putExtra("CatName",catagory.getName());
            this.mContext.startActivity(intent);


        }
    }

}

和我的 Catatgory.java

public class Catagory {
    private String name;
    private int numOfVend;
    private int thumbnail;
    private int VendId;

    public Catagory() {
    }

    public Catagory(String name, int numOfVend, int VendId , int thumbnail) {
        this.name = name;
        this.VendId = VendId;
        this.numOfVend = numOfVend;
        this.thumbnail = thumbnail;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumOfVend() {
        return numOfVend;
    }

    public void setNumOfVend(int numOfVend) {
        this.numOfVend = numOfVend;
    }

    public int getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(int thumbnail) {
        this.thumbnail = thumbnail;
    }

    public int getVendId() {
        return VendId;
    }

    public void setVendId(int VendId) {
        this.VendId = VendId;
    }
}

和我的片段

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;

import zesteve.com.myapplication.adapter.CatagoryAdapter;

/**
 * Created by Ratan on 7/29/2015.
 */
public class PrimaryFragment extends Fragment {

    FloatingActionButton fab;

    private RecyclerView recyclerView;
    private  CatagoryAdapter adapter;
    private  ArrayList<Catagory> catagoryList;

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.primary_layout,container,false);

        recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);

        fab = (FloatingActionButton) root.findViewById(R.id.post_new_event);

        catagoryList = new ArrayList<>();
        adapter = new CatagoryAdapter(getActivity(), catagoryList);

        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

         prepareCatagory();

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),PostNewEvent.class);
                startActivity(intent);
            }
        });
        return root;
    }

    private void prepareCatagory() {
        int[] covers = new int[]{
                R.drawable.album1,
                R.drawable.album2,
                R.drawable.album3,
                R.drawable.album4,
                R.drawable.album5,
                R.drawable.album6,
                R.drawable.album7,
                R.drawable.album8,
                R.drawable.album9,
                R.drawable.album10,
                R.drawable.album11
        };

        Catagory a = new Catagory("True Romance", 13, 1, covers[0]);
        catagoryList.add(a);

        a = new Catagory("Xscpae", 8, 2 ,covers[1]);
        catagoryList.add(a);

        a = new Catagory("Maroon 5",3 ,11, covers[2]);
        catagoryList.add(a);

        a = new Catagory("Born to Die",4, 12, covers[3]);
        catagoryList.add(a);

        a = new Catagory("Honeymoon",5, 14, covers[4]);
        catagoryList.add(a);

        a = new Catagory("I Need a Doctor",6, 1, covers[5]);
        catagoryList.add(a);

        a = new Catagory("Loud", 11,7, covers[6]);
        catagoryList.add(a);

        a = new Catagory("Legend", 14,8, covers[7]);
        catagoryList.add(a);

        a = new Catagory("Hello", 11,9, covers[8]);
        catagoryList.add(a);

        a = new Catagory("Greatest Hits",10, 17, covers[9]);
        catagoryList.add(a);

        adapter.notifyDataSetChanged();
    }

    /**
     * RecyclerView item decoration - give equal margin around grid item
     */
    public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

        private int spanCount;
        private int spacing;
        private boolean includeEdge;

        public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position
            int column = position % spanCount; // item column

            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

                if (position < spanCount) { // top edge
                    outRect.top = spacing;
                }
                outRect.bottom = spacing; // item bottom
            } else {
                outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                if (position >= spanCount) {
                    outRect.top = spacing; // item top
                }
            }
        }
    }

    /**
     * Converting dp to pixel
     */
    private int dpToPx(int dp) {
        Resources r = getResources();
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
    }


}

当我点击cardview时没有任何反应,也没有错误消息。以下是我的应用程序屏幕截图

如有疑问请评论。


Step-1:制作一个如下代码所示的界面:

public interface RecyclerViewItemClickListener {
    void onClickListenerForItem(int position);

   }

Step-2:实现这个接口:

 RecyclerViewItemClickListener recyclerItemClickListener = new RecyclerViewItemClickListener() {

        @Override
        public void onClickListenerForItem(int tournamentIndex, int matchIndex) {
//Todo implement your Code here
           }
}

Step-3:发送适配器类中实现的接口,如下代码:

mAdapter = new RecyclerViewAdapte(getContext(), recyclerItemClickListener);

Step-4:在 Adapter 类中实现位置回调:

 private final RecyclerViewItemClickListener mListener;
 public RecyclerViewAdapter(Context context, RecyclerViewItemClickListener mListener) {
        this.context = context;
        this.mListener = mListener;

    }

Step-5:最终在 OnBindViewHolder() 中设置位置:

holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mListener.onClickListenerForItem(position);
        }
    });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单击 RecyclerView 转到另一个包含 android 中数据的活动 的相关文章

随机推荐

  • 调用类指针上的方法[重复]

    这个问题在这里已经有答案了 并不是说我有问题 而是我发现以下事实很奇怪 Class Shape class Shape protected int width height public Shape int a 0 int b 0 widt
  • C# Begin/EndReceive - 如何读取大数据?

    当以 1024 字节为单位读取数据时 如何继续从接收大于 1024 字节的消息的套接字读取数据 直到没有剩余数据为止 我是否应该仅使用 BeginReceive 来读取数据包的长度前缀 然后在检索到该前缀后 使用 Receive 在异步线程
  • RxJS v5 中的速率限制和计数限制事件,但也允许传递

    我有很多事件要发送到服务 但请求有速率限制 每个请求都有计数限制 每秒 1 个请求 bufferTime 1000 每个请求 100 个活动项目 bufferCount 100 问题是 我不确定如何以有意义的方式将它们组合起来 允许通过 让
  • 在 wine (linux) 下运行的 Windows 应用程序的屏幕截图

    正如标题所说 我想截取wine下运行的窗口应用程序的屏幕截图 不是整个桌面 C 是首选 但也可以使用 java 或 Pascal 提前致谢 使用 imagemagick 的导入命令抓取窗口并将其转储到文件中 进口文件 http www im
  • 远程过程调用认证

    我正在使用远程过程调用 RPC 在本地计算机上通信数据 我的要求是使用 RPC 在两个处理之间通信数据 但服务器应该通过某种方式对客户端进行身份验证 我遇到了 RpcBindingSetAuthInfo 它设置身份验证和授权信息 第四个参数
  • 如何获取类属性的名称?

    无论如何我可以获得类属性的名称IntProperty public class ClassName public static int IntProperty get return 0 something like below but I
  • 在Python中按多个值对列表列表进行排序

    我需要对列表列表进行排序 其中每个列表entry外部列表是一个由三个整数组成的列表 如下所示 3 1 0 1 2 3 3 2 0 3 1 1 诀窍是我需要对其进行排序entry 0 如果有平局 则按以下顺序对它们进行排序entry 1 如果
  • OSX - 始终隐藏某些文件

    我知道如何在终端中显示和隐藏隐藏文件 但是有没有办法在显示隐藏文件时隐藏某些文件 例如 DS STORE 可以这么说 使某些文件超级隐藏吗 Use chflags与隐藏选项 ie chflags hidden fileToHide从 Fin
  • XMI 2.1.1 的 XSD

    我必须使用 JAXB 解析 XMI 文件 xmi 版本 2 1 1 为此 我必须生成与 XMI 文件相对应的 Java 类 因此 我需要 xmi 文件的 shema 定义才能使用 jxc 工具执行此操作 我希望有人知道在哪里可以找到这个文件
  • `AVCaptureVideoDataOutput` 消耗的内存是 `AVCaptureMovieFileOutput` 的三倍

    Issue 我正在使用 AVFoundation 来实现一个相机 它能够在运行特殊的人工智能处理时录制视频 拥有一个AVCaptureMovieFileOutput 用于视频录制 and a AVCaptureVideoDataOutput
  • 当由不同模块导入时,如何访问 Python 2.7 中的相对路径

    目标 使用从各种 python 模块调用的通用实用程序函数时访问 写入相同的临时文件 背景 我正在使用 python Unittest 模块来运行一组自定义测试 这些测试通过 pySerial 与仪器接口 因为我使用的是unittest模块
  • AudioConverterNew 返回 -50

    我有一个关于使用 AudioQueue 服务的小问题 我已按照 Apple 网站上提供的指南进行操作 但是当我启动并运行音频队列时 我收到消息告诉我 AudioConverterNew 返回 50 现在 我知道 50 错误代码意味着存在错误
  • node.js可以导入java库吗

    我有一个 Nodejs 应用程序 它有一些昂贵的计算 我正在考虑用 java 来完成这部分 这样我就可以更轻松地利用线程和数学库 有没有一种简单的方法可以让nodejs与外部java库对话 java 库将包含一个频繁调用 javascrip
  • Hive 分区表上的 Spark 行为

    我用的是 Spark 2 实际上我不是执行查询的人 所以我不能包含查询计划 数据科学团队问过我这个问题 我们将 Hive 表划分为 2000 个分区并以 parquet 格式存储 当在 Spark 中使用相应的表时 执行器之间恰好执行了 2
  • 如何查找没有自己登录名的 sqlserver 域用户的登录名、数据库用户名或角色?

    我创建了一个名为 MYDOMAIN Domain Users 的登录名和数据库用户 我需要找到登录的域用户具有哪些角色 但所有获取当前用户的调用都返回域用户名 例如 MYDOMAIN username 不是数据库用户名 例如 MYDOMAI
  • Thymeleaf 中链接绝对 URL 时 th:href 和 href 之间的区别

    就在 Thymeleaf 的开头文档 http www thymeleaf org doc articles standardurlsyntax html关于标准url语法有两个例子 但没有说明它们之间的区别 a a href http w
  • 比特率 JWplayer

    我无法让 jwplayer 以不同的比特率工作 对于每个视频 我都会创建具有不同后缀且比特率较低的新输出文件 例如 输出1 高比特率 test original mp4 输出 2 中等比特率 test medium mp4 输出 2 低比特
  • 如何列出 Git 存储库中的目录以及每个目录条目的最新提交信息?

    我想列出 Git 存储库中的目录以及每个目录条目的最新提交信息 类似于 GitHub 如何显示目录或 viewvc 如何显示 SVN CVS 存储库中的目录 目前我这样做 获取目录条目git ls tree master并从输出中解析目录结
  • 如何在 JavaScript 的“if”语句中指定多个条件[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 这是我试图提及两个条件的方式如果这个或这个 但它不起作用 if Type 2 PageCount 0 Type 2 PageCou
  • 单击 RecyclerView 转到另一个包含 android 中数据的活动

    我正在使用 RecyclerVeiw 显示一些图像 单击它应该启动新活动 显示 RecyclerView 的 id 名称 我实现了代码 但它现在可以工作 但不可点击 下面是我的适配器 import android content Conte