Android 如何在片段中实现recyclerview onClick?
我的问题与此类似 我知道当用户单击片段中的按钮时,如何在相应的活动中实现onClickListener、接口和OnItemSelected方法。我有一个带有数组列表的片段。通过单击片段中数组列表中的每个项目,ExoPlayer将在新活动或片段中打开。据我所知,McClickListener不使用Recyclerview。如何将onClick方法设置为列表中的项目?另外,是否在RecyclerView外部设置onClick?这是我的RecyclerView适配器类。接口是否应采用其他参数?先谢谢你 公共类StepsAdapter扩展了RecyclerView.Adapter{Android 如何在片段中实现recyclerview onClick?,android,android-fragments,android-recyclerview,onitemclicklistener,Android,Android Fragments,Android Recyclerview,Onitemclicklistener,我的问题与此类似 我知道当用户单击片段中的按钮时,如何在相应的活动中实现onClickListener、接口和OnItemSelected方法。我有一个带有数组列表的片段。通过单击片段中数组列表中的每个项目,ExoPlayer将在新活动或片段中打开。据我所知,McClickListener不使用Recyclerview。如何将onClick方法设置为列表中的项目?另外,是否在RecyclerView外部设置onClick?这是我的RecyclerView适配器类。接口是否应采用其他参数?先谢谢
private static final String TAG = StepsAdapter.class.getSimpleName();
private ArrayList<Steps> stepsList = new ArrayList<Steps>();
private StepsAdapter.StepsAdapterOnClickHandler mClickHandler;
/**
* The interface that receives onClick messages.
*/
public interface StepsAdapterOnClickHandler {
void onClick(Steps stepClick);
}
/**
* Creates a StepsAdapter.
*
* @param clickHandler The on-click handler for this adapter. This single handler is called
* when an item is clicked.
*/
public StepsAdapter(StepsAdapterOnClickHandler clickHandler,ArrayList<Steps> stepsList) {
mClickHandler = clickHandler;
this.stepsList = stepsList;
}
/**
* Cache of the children views for a steps list item.
*/
public class StepsAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@BindView(R.id.step_short_desc)
public TextView stepShortDescription;
@BindView(R.id.step_description)
public TextView stepDescription;
public StepsAdapterViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
view.setOnClickListener(this);
}
/**
* This gets called by the child views during a click.
*
* @param v The View that was clicked
*/
@Override
public void onClick(View v) {
int adapterPosition = getAdapterPosition();
Steps stepClick = stepsList.get(adapterPosition);
mClickHandler.onClick(stepClick);
}
}
@Override
public StepsAdapter.StepsAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.steps_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
return new StepsAdapter.StepsAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(StepsAdapter.StepsAdapterViewHolder holder, int position) {
//Binding data
final Steps stepsView = stepsList.get(position);
holder.stepShortDescription.setText(stepsView.getStepShortDescription());
holder.stepDescription.setText(stepsView.getStepDescription());
}
@Override
public int getItemCount() {
return stepsList.size();
}
public void setStepsList(ArrayList<Steps> mStepsList) {
this.stepsList = mStepsList;
notifyDataSetChanged();
}
private static final String TAG=StepsAdapter.class.getSimpleName();
private ArrayList stepsList=new ArrayList();
私有StepsAdapter.StepsAdapterOnClickHandler mClickHandler;
/**
*接收onClick消息的接口。
*/
公共接口StepsAdapterOnClickHandler{
void onClick(步骤:单击);
}
/**
*创建一个StepsAdapter。
*
*@param clickHandler此适配器的单击处理程序。此单个处理程序称为
*单击项目时。
*/
公共StepsAdapter(StepsAdapterOnClickHandler clickHandler、ArrayList stepsList){
mClickHandler=clickHandler;
this.stepsList=stepsList;
}
/**
*步骤列表项的子视图缓存。
*/
公共类StepsAdapterViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
@BindView(R.id.step\u short\u desc)
公共文本视图stepShortDescription;
@BindView(R.id.step_说明)
公共文本视图步骤描述;
公共StepsAdapterViewHolder(视图){
超级(视图);
ButterKnife.bind(这个,视图);
view.setOnClickListener(这个);
}
/**
*在单击过程中,子视图将调用此函数。
*
*@param v单击的视图
*/
@凌驾
公共void onClick(视图v){
int adapterPosition=getAdapterPosition();
Steps stepClick=stepsList.get(适配器位置);
mClickHandler.onClick(stepClick);
}
}
@凌驾
public StepsAdapter.StepsAdapterViewHolder onCreateViewHolder(ViewGroup ViewGroup,int viewType){
Context=viewGroup.getContext();
int layoutIdForListItem=R.layout.steps\u list\u项;
LayoutFlater充气机=LayoutFlater.from(上下文);
布尔值shouldAttachToparentInstallent=false;
视图=充气机。充气(布局用于列表项、视图组,应立即附加到选项);
返回新的StepsAdapter.StepsAdapterViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(StepsAdapter.StepsAdapterViewHolder,内部位置){
//绑定数据
最后步骤stepsView=stepsList.get(位置);
holder.stepShortDescription.setText(stepsView.getStepShortDescription());
holder.stepDescription.setText(stepsView.getStepDescription());
}
@凌驾
public int getItemCount(){
返回stepsList.size();
}
公共无效集合步骤列表(ArrayList mStepsList){
this.stepsList=mStepsList;
notifyDataSetChanged();
}
}
相应的片段。单击方法是否正确实施
public class StepsListFragment extends Fragment implements StepsAdapter.StepsAdapterOnClickListener {
// Tag for logging
private final String TAG = StepsListFragment.class.getSimpleName();
@BindView(R.id.recyclerview_steps)
RecyclerView mRecyclerView;
ArrayList<Steps> stepsArrayList;
Recipes recipes;
// Final Strings to store state information about the list of steps and list index
public static final String STEPS_LIST_INDEX = "list_index";
// Define a new interface OnStepsClickListener that triggers a callback in the host activity
OnStepClickListener mCallback;
// OnStepsClickListener interface, calls a method in the host activity named onStepSelected
public interface OnStepClickListener {
void onClick(Steps stepClick);
}
// Override onAttach to make sure that the container activity has implemented the callback
@Override
public void onAttach(Context context) {
super.onAttach(context);
// // This makes sure that the host activity has implemented the callback interface
// // If not, it throws an exception
try {
mCallback = (OnStepClickListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnStepSelectedListener");
}
}
/**
* Mandatory empty constructor for the fragment manager to instantiate the fragment
*/
public StepsListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// //Inflate the Steps fragment layout
View rootView = inflater.inflate(R.layout.fragment_steps, container, false);
// // Bind the views
ButterKnife.bind(this, rootView);
Bundle bundle = this.getArguments();
if (bundle != null) {
recipes = getArguments().getParcelable("Recipes");
stepsArrayList = new ArrayList<>();
stepsArrayList = recipes.getRecipeSteps();
}
if (savedInstanceState != null) {
//Restore the fragment's state here
stepsArrayList = savedInstanceState.getParcelableArrayList(STEPS_LIST_INDEX);
}
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
Log.i("listSteps", stepsArrayList.size() + "");
StepsAdapter stepsAdapter = new StepsAdapter(this, stepsArrayList);
mRecyclerView.setAdapter(stepsAdapter);
// Return the root view
return rootView;
}
public void onClick(Steps stepClick){
mCallback.onClick(stepClick);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveIn`enter code here`stanceState(outState);
//Save the fragment's state here
outState.putParcelableArrayList(STEPS_LIST_INDEX, stepsArrayList);
super.onSaveInstanceState(outState);
} }
公共类StepsListFragment扩展片段实现StepsAdapter.StepsAdapterOnClickListener{
//日志记录标签
私有最终字符串标记=StepsListFragment.class.getSimpleName();
@BindView(R.id.recyclerview\u步骤)
回收视图mRecyclerView;
ArrayList-stepsArrayList;
食谱;
//用于存储有关步骤列表和列表索引的状态信息的最终字符串
公共静态最终字符串步骤\u LIST\u INDEX=“LIST\u INDEX”;
//在StepsClickListener上定义一个新接口,用于在主机活动中触发回调
OnStepClickListener McCallback;
//OnStepsClickListener接口,调用主机活动中名为OnStepsSelected的方法
步骤ClickListener上的公共接口{
void onClick(步骤:单击);
}
//重写onAttach以确保容器活动已实现回调
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
////这确保主机活动已实现回调接口
////如果不是,则抛出异常
试一试{
mCallback=(OnStepClickListener)上下文;
}catch(ClassCastException e){
抛出新的ClassCastException(context.toString()
+“必须在步骤SelectedListener上实现”);
}
}
/**
*片段管理器实例化片段时必须使用空构造函数
*/
公共StepsListFragment(){
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
////膨胀步骤片段布局
视图根视图=充气机。充气(R.layout.fragment\u步骤,容器,错误);
////绑定视图
ButterKnife.bind(这个,rootView);
Bundle=this.getArguments();
if(bundle!=null){
recipes=getArguments().getParcelable(“recipes”);
stepsArrayList=newarraylist();
stepsArrayList=recipes.getRecipes步骤();
}
如果(savedInstanceState!=null){
//在这里恢复片段的状态
stepsArrayList=savedInstanceState.getParcelableArrayList(步骤列表索引);
}
RecyclerView.LayoutManager mLayoutManager=新的LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mllayoutmanager);
Log.i(“listSteps”,stepsArrayList.size()+”);
StepsAdapter StepsAdapter=新的StepsAdapter(此,stepsArrayList);
mRecyclerView.setAdapter(stepsAdapter);
//返回根视图
返回rootView;
}
单击公共void onClick(步骤s
public interface RecyclerviewOnClickListener{
void recyclerviewClick(int position);
}
public class ChatFragment extends Fragment implements RecyclerviewOnClickListener{
.
.
}
public ChatAdapter(RecyclerviewClickListener listener, .....<other params>){
this.listener = listener;
}
ChatAdapter adapter = new ChatAdapter(this, <any additional params>);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//code here
}