我有设置为自动滚动的recycleview。我希望实现onclicklistener,以便打开新活动。
这是我的回收站
final int duration = 10;
final int pixelsToMove = 30;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private final Runnable SCROLLING_RUNNABLE = new Runnable() {
@Override
public void run() {
top.smoothScrollBy(pixelsToMove, 0);
mHandler.postDelayed(this, duration);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
top = (RecyclerView) view.findViewById(R.id.top);
final LinearLayoutManager llm = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false);
top.setLayoutManager(llm);
top.setHasFixedSize(true);
staggeredBooksAdapter = new TopAdapter(this, bookslist);
top.setAdapter(staggeredBooksAdapter);
//Recycleview
top.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState); }
@Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastItem = llm.findLastCompletelyVisibleItemPosition();
if(lastItem == llm.getItemCount()-1){
mHandler.removeCallbacks(SCROLLING_RUNNABLE);
Handler postHandler = new Handler();
postHandler.postDelayed(new Runnable() {
@Override
public void run() {
top.setAdapter(null);
top.setAdapter(staggeredBooksAdapter);
mHandler.postDelayed(SCROLLING_RUNNABLE, 6000);
}}, 6000);
}
}
});
mHandler.postDelayed(SCROLLING_RUNNABLE, 6000);
我的适配器类是
public class TopAdapter extends RecyclerView.Adapter<TopAdapter.MyViewHolder> {
ArrayList<location> bookslist;
CardView cv;
location g;
private Home context;
public TopAdapter(Home context, ArrayList<location> bookslist){
this.bookslist = bookslist;
this.context = context; // add this as a field in your adapter class.
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_top,parent,false);
return new MyViewHolder(v);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView teacher_name,teacher_location;
LinearLayout profile_details;
ImageView iv;
MyViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.teacher_name);
teacher_location = (TextView)
}
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
database = FirebaseDatabase.getInstance();
g = bookslist.get(position);
holder.teacher_name.setText(g.getSellername());
holder.profile_details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
g = bookslist.get(position);
Intent intent = new Intent(v.getContext(), gender_details.class);
intent.putExtra(KEY_NAME, g.getSellername());
v.getContext().startActivity(intent);
}
});
@Override
public int getItemCount() {
return bookslist.size();
}
}
当我重复单击时,我可以根据位置打开包含详细信息的新活动,但我不知道它是如何发生的,只有重复单击某些项目后,才能再现相同的效果。但这并不经常发生。当我单击一次recycleview列表中的任何项目时,我正在寻找永久解决方案以打开新活动。
提前致谢。
最佳答案
您的onClickListener设置为名称为profile_details的视图,并且仅当您(完全)点击该视图时才会触发它。您的某些元素可能与该视图重叠,因此您的点击无法正常工作。它说明了在项目的不同部分随机点击了几次之后,startActivity触发了什么。
尝试调试视图上的点击并将此onItemClickListener设置为适当的View / ViewGroup
附言:
另外我的建议是不要在适配器内存储任何与数据库相关的对象(这违反了单一职责)。
将活动启动逻辑存储在适配器中也是一种不好的做法。查看此答案:Can't resolve the Context or Application while navigating from Adapter of a fragment(A) to another Fragment (B)
希望它会有所帮助:)