[英]How to do you implement a recyclerview onClick in a Fragment?
我的問題與此類似。
在Fragment / Activity和RecyclerView.Adapter之間進行通信的最佳方法?
我知道當用戶單擊Fragment中的按鈕時,如何在相應的活動中實現onClickListener,Interface和OnItemSelected方法。 我有一個帶有數組列表的片段。 通過單擊片段中數組列表中的每個項目,ExoPlayer將在新活動或片段中打開。 據我了解,onItemClickListener不適用於Recyclerview。 如何將onClick方法設置為列表中的項目? 另外,我是否在RecyclerView之外設置onClick? 這是我的RecyclerView適配器類。 接口是否應該使用其他參數? 先感謝您。
公共類StepsAdapter擴展了RecyclerView.Adapter {
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();
}
}
相應的片段。 click方法是否正確實施?
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);
} }
在recyclerview Viewholder中填充視圖時,在recyclerview的每個項目上實現點擊功能的最佳方法是初始化onClickListener。 然后在onClick方法中,使用自定義界面/監聽器來捕獲父片段/活動中的點擊活動。
例如:創建一個這樣的自定義界面;
public interface RecyclerviewOnClickListener{
void recyclerviewClick(int position);
}
現在,在包含recyclerview的父活動/片段中實現此接口。 假設您的片段名稱是ChatFragment。 然后,
public class ChatFragment extends Fragment implements RecyclerviewOnClickListener{
.
.
}
這將在片段中實現onClick(int position)函數。 在您的適配器構造函數中,您應該為RecyclerviewOnClickListener創建一個字段。 假設您的適配器名稱是ChatAdapter,然后
適配器構造函數。
public ChatAdapter(RecyclerviewClickListener listener, .....<other params>){
this.listener = listener;
}
在片段中,您可以像下面那樣初始化適配器
ChatAdapter adapter = new ChatAdapter(this, <any additional params>);
您還應該將相同的“偵聽器”實例傳遞給您的觀看者,並在那里也初始化監聽器
現在,您可以在recyclerview ViewHolder中設置view.setOnClickListener(new OnClickListener{ this.listener.recyclerviewClick(getAdapterPosition()) });
getAdapterPosition()
函數返回單擊在recyclerview中的位置,這將在片段的recyclerviewClick()函數中獲得回調。
關於要傳遞的參數數量,可以根據需要使用任意數量,但是對於recyclerview中的click函數,理想的方法是僅使用該位置的一個參數。 現在,您可以修改從片段傳遞到適配器的列表中的內容,並調用notifyDataSetChanged()來更新recyclerview。 希望清楚。
在OnBindviewHolder中嘗試一下
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//code here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.