在解析JSON数据后将onClick添加到RecyclerView

时间:2017-01-21 17:14:15

标签: javascript android json

我有一个片段,其中包含RecyclerView。我从网页解析了JSON数据并使用教程在此RecyclerView中显示:https://www.learn2crack.com/2016/02/recyclerview-json-parsing.html

现在我想在RecyclerView项目中添加一个onClick。我知道如何对静态RecyclerView项目这样做,但在这种情况下不知道setOnClickListener。对于静态recyclerview项目,我将此类用于onClick事件。

package com.parassidhu.cdlumaths;

import android.support.v7.widget.RecyclerView;
import android.view.View;

public class ItemClickSupport {
    private final RecyclerView mRecyclerView;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
                mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
            }
        }
    };
    private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (mOnItemLongClickListener != null) {
                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
                return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);
            }
            return false;
        }
    };
    private RecyclerView.OnChildAttachStateChangeListener mAttachListener
            = new RecyclerView.OnChildAttachStateChangeListener() {
        @Override
        public void onChildViewAttachedToWindow(View view) {
            if (mOnItemClickListener != null) {
                view.setOnClickListener(mOnClickListener);
            }
            if (mOnItemLongClickListener != null) {
                view.setOnLongClickListener(mOnLongClickListener);
            }
        }

        @Override
        public void onChildViewDetachedFromWindow(View view) {

        }
    };

    private ItemClickSupport(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        mRecyclerView.setTag(R.id.item_click_support, this);
        mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
    }

    public static ItemClickSupport addTo(RecyclerView view) {
        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
        if (support == null) {
            support = new ItemClickSupport(view);
        }
        return support;
    }

    public static ItemClickSupport removeFrom(RecyclerView view) {
        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
        if (support != null) {
            support.detach(view);
        }
        return support;
    }

    public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
        return this;
    }

    public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
        mOnItemLongClickListener = listener;
        return this;
    }

    private void detach(RecyclerView view) {
        view.removeOnChildAttachStateChangeListener(mAttachListener);
        view.setTag(R.id.item_click_support, null);
    }

    public interface OnItemClickListener {

        void onItemClicked(RecyclerView recyclerView, int position, View v);
    }

    public interface OnItemLongClickListener {

        boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
    }
}

我的适配器类是:

package com.parassidhu.cdlumaths;

import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.parassidhu.cdlumaths.R;
import com.parassidhu.cdlumaths.Android;

import java.util.ArrayList;

public class NoticesData extends RecyclerView.Adapter<NoticesData.ViewHolder> {

    private ArrayList<Android> mAndroidList;
    private RecyclerView recyclerView;
    public NoticesData(ArrayList<Android> androidList, RecyclerView rcl) {
        mAndroidList = androidList;
        recyclerView=rcl;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notices_row, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mTvName.setText(mAndroidList.get(position).getName());
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
            //I can't call Toast or startIntent here
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView mTvName;
        public ViewHolder(View view) {
            super(view);
            mTvName = (TextView)view.findViewById(R.id.tv_name);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在您的Activity或Fragment类中创建一个类似下面的方法

public void showToast(int position) {
  Toast.makeText(getActivity(), "Clicked position is " + position, Toast.LENGTH_SHORT).show();
}

(我认为您正在使用Fragment,因此我使用了getActivity()

现在像这样更新你的适配器构造函数

...
...
private YourFragment frag;

public NoticesData(ArrayList<Android> androidList, RecyclerView rcl, YourFragment fragment) {
    mAndroidList = androidList;
    recyclerView = rcl;
    frag = fragment;
}

在Fragment更新适配器初始化中,如下所示

NoticesData notice = NoticesData(androidList, rcl, YourFragment.this);

现在调用适配器

中的showToast()方法,如下所示
ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
        @Override
        public void onItemClicked(RecyclerView recyclerView, int position, View v) {

           frag.showToast(position);  // it's like callback method

        }
    });

注意:此处YourFragment表示托管RecyclerView的片段。

答案 1 :(得分:0)

您必须为该回收站视图的适配器设置回收站视图中每个项目的单击侦听器。当您为recycler视图中的项目(视图)设置每个数据时,您也可以为该视图添加onClickListener。