在Recyclerview适配器中实现onClick

时间:2016-09-17 20:18:25

标签: java android android-recyclerview

  1. 我想知道如何为我的每个项目实现onClick grid(使用Recycler View和Card视图)启动新的 activity.Below是我的ViewHolder类和RecyclerView适配器 类

    这是我的RecyclerView适配器类

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.nfc.Tag;
    import android.nfc.TagLostException;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Toast;
    
    import java.util.List;
    
    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
    
        private List<ItemObject> itemList;
        private Context context;
    
        public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
            this.itemList = itemList;
            this.context = context;
        }
    
        @Override
        public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
    
            View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
            RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
            return rcv;
        }
    
        @Override
        public void onBindViewHolder(RecyclerViewHolders holder, final int position) {
            holder.countryName.setText(itemList.get(position).getName());
            holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());
            holder.countryPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(view.getContext(),"This is Maps",Toast.LENGTH_SHORT).show();
    
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return this.itemList.size();
        }
    }
    
    //This is my Recycler View Holder Class
    
    
    import android.content.Intent;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {
    
        public TextView countryName;
        public ImageView countryPhoto;
    
        public RecyclerViewHolders(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            countryName = (TextView) itemView.findViewById(R.id.country_name);
            countryPhoto = (ImageView) itemView.findViewById(R.id.country_photo);
        }
    
        @Override
        public void onClick(View view) {
            Toast.makeText(view.getContext(), "Clicked Country Position = " + getPosition(), Toast.LENGTH_SHORT).show();
    
    
        }
    }
    

3 个答案:

答案 0 :(得分:0)

我使用Interface作为Callback来处理这种情况。

首先,我在Adapter中创建一个接口类。

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

    private List<ItemObject> itemList;
    private Context context;
    private ItemClickListener mItemClickListener;

    public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

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

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
        RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
        return rcv;
    }

    public void addItemClickListener(ItemClickListener listener) {
        mItemClickListener = listener;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolders holder, final int position) {
        holder.countryName.setText(itemList.get(position).getName());
        holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());
        holder.countryPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });
    }

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

    //Define your Interface method here
    public interface ItemClickListener {
        void onItemClick(int position);
    }
}

然后,让Activity或Fragment实现该Interface并添加监听器。

public class MyFragment extends Fragment
    implements RecyclerViewAdapter.ItemClickListener {

    private List<ItemObject> mItemList;
    ...
     @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             final Bundle savedInstanceState) {
       ...
       //Make sure mItemList not NULL
       RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), mItemList);
       //Add Item Click listener
       adapter.addItemClickListener(this);
    }


    @Override
    public void onItemClick(int position) {   
        Toast.makeText(.getContext(),"Click on item: " + position,Toast.LENGTH_SHORT).show();
    }
}

希望这会有所帮助!

答案 1 :(得分:0)

你的代码看起来很好,如果要求如何创建打开新活动的意图,那么你应该改变

这是懒惰的方式不推荐最佳实践

在你的Mian活动中制作一个静态变量女巫正在举行某种类似的东西

        public class MainActivity extends AppCompatActivity

 public static Activity activity;


            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                getSupportActionBar().setElevation(0);

                activity=this;

        }
    }

然后点击

...

          @Override
            public void onClick(View v) {
Toast.makeText(.getContext(),"Click on item: " + position,Toast.LENGTH_SHORT).show();
                startActivity(new Intent(MainActivity.activity, MyOtherActivity.class));
            }

...

答案 2 :(得分:0)

我从您的初始帖子中我不完全清楚您的问题是什么,但如果您只想在RecyclerViewAdapter中实现OnClickListener,只需添加以下内容:

首先,将您的类声明更改为:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> implements OnClickListener {

然后,将onClick方法添加到类中,以完成界面。

 @Override
public void onClick(View view) {   
    Toast.makeText(this.getContext(),"Code to change activity goes here." ,Toast.LENGTH_SHORT).show();
}

最后,您需要在适配器类中设置OnClickListener。

@Override
public void onBindViewHolder(RecyclerViewHolders holder, final int position) {
    holder.setOnClickListener(this);
    holder.countryName.setText(itemList.get(position).getName());
    holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());
    holder.countryPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(position);
            }
        }
    });
}

现在,我个人不会这样做。我要做的是在你的适配器中添加一个私有内部类,如下所示:

private AdapterOnClickListener implements OnClickListener() {
    private String message;

    public AdapterOnClickListener (String toast) {
         message = toast;
    }

    public void onClick (View v) {
       Toast.makeText(this.getContext(),toast ,Toast.LENGTH_SHORT).show();
    }

}

然后在适配器中调用:

@Override
public void onBindViewHolder(RecyclerViewHolders holder, final int position) {
    holder.setOnClickListener(new AdapterOnClickListener ("Instead of a string, I can be an intent. Then in your listener, you can call (If I recall correctly) getContext().startActivity(intent), and have an activity.");
    holder.countryName.setText(itemList.get(position).getName());
    holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());
    holder.countryPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(position);
            }
        }
    });
}