我无法从RecyclerView
单击Activity
到新的RecyclerViewAdapter
。
我在这里打ItemClick
。
DayAdapter.java :
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
openProgramActivity(view, position);
}
});
}
此功能将打开一个新的Activity
:
public void openProgramActivity(View view, int position) {
//Intent openProgramActivity = new Intent(context, ProgramActivity.class);
Intent openProgramActivity = new Intent(view.getContext(), ProgramActivity.class);
openProgramActivity.putExtra("index",position);
view.getContext().startActivity(openProgramActivity);
}
FragmentDay30.java :
public class FragmentDay30 extends Fragment {
private View view;
public static FragmentDay30 newInstance() {
FragmentDay30 fragment = new FragmentDay30();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_30day, container,false);
ViewPager slideViewPager = (ViewPager) view.findViewById(R.id.slideViewPager);
SlideAdapter slideAdapter = new SlideAdapter(getActivity());
slideViewPager.setAdapter(slideAdapter);
RecyclerView fragment30datRecyclerView = (RecyclerView) view.findViewById(R.id.fragment30dayRecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
fragment30datRecyclerView.setLayoutManager(linearLayoutManager);
DayAdapter dayAdapter = new DayAdapter(getActivity());
fragment30datRecyclerView.setAdapter(dayAdapter);
return view;
}
我尝试使用getActvity()
和getContext()
,但不使用新的Activity
。
答案 0 :(得分:4)
像这样将contaxt传递给recyclerview适配器构造器
Context context;
MyAdapter(Context context, .....){
this.context=context;
}
通话活动
context.startActivity(......);
答案 1 :(得分:0)
虽然您可以通过Context传递自Adapter类的Activity ,但据记录是
我宁愿建议您在Adapter类中定义一个接口,该接口将由Fragment类实现。片段类通过它的引用来初始化适配器,您可以键入Cast来像这样接口
DayAdpater.class
public class DayAdapter extends RecyclerView.Adapter<DayAdapter.ViewHolder> {
private OnActionListener mListener;
DayAdapter(OnActionListener listener){
this.mListener=listener;
}
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
mListener.startActivity(position);
}
});
interface OnActionListener{
public void startActivity(int position);
}
}
FragmentDay30.class
public class FragmentDay30 extends Fragment implements DayAdapter.OnActionListener{
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_30day, container,false);
RecyclerView fragment30datRecyclerView = (RecyclerView) view.findViewById(R.id.fragment30dayRecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
fragment30datRecyclerView.setLayoutManager(linearLayoutManager);
DayAdapter dayAdapter = new DayAdapter(getActivity(), this);
fragment30datRecyclerView.setAdapter(dayAdapter);
return view;
}
/**
* this is the place where you should start a new activity
*/
public void startActivity(int position) {
//Intent openProgramActivity = new Intent(context, ProgramActivity.class);
Intent openProgramActivity = new Intent(getActivity(), ProgramActivity.class);
openProgramActivity.putExtra("index",position);
getActivity.startActivity(openProgramActivity);
}
}
这是您的adpater类与片段类交互的方式。
希望这会有所帮助。
答案 2 :(得分:0)
在context
适配器构造函数中传递RecyclerView
,您将其用于setAdapter
,如下所示:
Context context;
MyCustomAdapter(Context context, .....){
this.context=context;
}
对于使用的通话活动:
context.startActivity(......);