My app is coded with a RecyclerView that is contained within a fragment. I'm able to successfully populate the RecyclerView with a list of mock data, however I don't how how to send an Intent to another Activity when one of the items is clicked. When I try to call StartActivity
from within my fragment, Android Studio cannot resolve the method.
My onItemClick
is implemented via an interface in my viewholder class. The interface also generates an error inner classes cannot have static declarations
. I'm wondering if I'm unable to call StartActivity
from the fragment because neither my RecyclerView adapter or viewholder classes extend Activity
.
What the appropriate implementation to send an Intent when an item in a RecyclerView is clicked?
PlaceHolderFragment
public static class PlaceholderFragment extends Fragment {
Activity mActivity;
RecyclerView mRecyclerView;
TaskAdapter taskAdapter;
public PlaceholderFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = (Activity) activity;
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.cardList);
taskAdapter = new TaskAdapter(createList(30));
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView.setAdapter(taskAdapter);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//Cannot resolve method
taskAdapter.TaskViewHolder.SetItemClickListener(new TaskAdapter.TaskViewHolder.OnItemClickListener() {
@Override
public void onItemClick(View v, int position) {
Intent detailIntent = new Intent(v.getContext(), DetailActivity.class);
//Cannot resolve method
StartActivity(detailIntent);
}
});
}
private List<Task> createList(int size) {
List<Task> result = new ArrayList<>();
for (int i = 1; i<=size; i++) {
Task task = new Task();
task.title = Task.TITLE_PREFIX + i;
task.updated = Task.UPDATED_PREFIX +i;
task.notes = task.NOTES_PREFIX + i;
result.add(task);
}
return result;
}
}
TaskAdapter
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {
private List<Task> taskList;
TaskViewHolder.OnItemClickListener mItemClickListener;
public TaskAdapter(List<Task> taskList) {
this.taskList = taskList;
}
@Override
public int getItemCount() {
return taskList.size();
}
@Override
public void onBindViewHolder(TaskViewHolder taskViewHolder, int i) {
Task task = taskList.get(i);
taskViewHolder.mTitle.setText(task.title);
taskViewHolder.mUpdated.setText(task.updated);
taskViewHolder.mStatus.setChecked(false);
}
@Override
public TaskViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.tasklist_layout, viewGroup, false);
return new TaskViewHolder(itemView);
}
public class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected TextView mTitle;
protected TextView mUpdated;
protected CheckBox mStatus;
public TaskViewHolder(View view) {
super(view);
mTitle = (TextView) view.findViewById(R.id.title);
mUpdated = (TextView) view.findViewById(R.id.updated);
mStatus = (CheckBox) view.findViewById(R.id.status);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition());
}
}
//Inner Classes cannot have static declarations
public interface OnItemClickListener {
void onItemClick(View view , int position);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
//cannot resolve symbol
this.mItemClickListener = mItemClickListener;
}
}
}
Aucun commentaire:
Enregistrer un commentaire