Android 片段页面适配器中未触发Onclick listner
我试图显示从手机到回收视图的所有联系人,当用户按下任何项目时,它应该做一些工作。当我单击Android 片段页面适配器中未触发Onclick listner,android,android-recyclerview,Android,Android Recyclerview,我试图显示从手机到回收视图的所有联系人,当用户按下任何项目时,它应该做一些工作。当我单击recyclerView项onClicklistener未执行时,我在片段页面适配器内的片段中有一个联系人列表 我在以前的应用程序中使用了相同的代码,但是,onclicklisteners正在触发 public class ListOfContacts extends Fragment implements OnContactListener { private static final St
recyclerView
项onClicklistener
未执行时,我在片段页面适配器内的片段中有一个联系人列表
我在以前的应用程序中使用了相同的代码,但是,onclicklisteners
正在触发
public class ListOfContacts extends Fragment implements OnContactListener {
private static final String TAG = "ListOfContacts";
ArrayList<ContactModel> arrayList = new ArrayList<>();
RecyclerView mRecyclerview;
ListOfContactsRAdapter listOfContactsRAdapter;
Cursor cursor;
public static ListOfContacts getInstance(ArrayList<ContactModel> arrayList) {
ListOfContacts listOfContacts = new ListOfContacts();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("listofcontacts", arrayList);
listOfContacts.setArguments(bundle);
return listOfContacts;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_of_contacts, container, false);
mRecyclerview = view.findViewById(R.id.loc_r_view);
listOfContactsRAdapter = new ListOfContactsRAdapter(getActivity(), this, arrayList);
mRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerview.setAdapter(listOfContactsRAdapter);
mRecyclerview.addItemDecoration(new DividerItemDecoration(mRecyclerview.getContext(), DividerItemDecoration.VERTICAL));
mRecyclerview.setHasFixedSize(true);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
arrayList.addAll(Objects.requireNonNull(getArguments().<ContactModel>getParcelableArrayList("listofcontacts")));
Log.d(TAG, "contact recived " + arrayList.get(0).getContactName());
} else {
if (getArguments().getStringArrayList("listofcontacts") != null)
arrayList.addAll(getArguments().<ContactModel>getParcelableArrayList("listofcontacts"));
}
}
}
@Override
public void onDueListener(int position) {
Log.d(TAG, "onDueListener: ");
ContactModel contactModel = new ContactModel();
contactModel.setContactName(arrayList.get(position).getContactName());
contactModel.setContactNumberp(arrayList.get(position).getContactNumberp());
contactModel.setState(true);
Intent intent = new Intent(getActivity(), AddDue.class);
intent.putExtra("userDetails", contactModel);
startActivity(intent);
}
}
公共类ListOfContacts扩展片段实现OnContactListener{
私有静态最终字符串TAG=“ListOfContacts”;
ArrayList ArrayList=新的ArrayList();
回收视图mRecyclerview;
ContactsAdapter列表ContactsAdapter列表;
光标;
公共静态联系人列表getInstance(ArrayList ArrayList){
ListOfContacts ListOfContacts=新的ListOfContacts();
Bundle=新Bundle();
bundle.putParcelableArrayList(“联系人列表”,arrayList);
setArguments(bundle);
返回合同清单;
}
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u联系人列表,容器,错误);
mRecyclerview=view.findviewbyd(R.id.loc\u R\u视图);
listOfContactsRAdapter=新的listOfContactsRAdapter(getActivity(),this,arrayList);
setLayoutManager(新的LinearLayoutManager(getActivity());
mrecycerview.setAdapter(ContactsAdapter列表);
addItemDecoration(新的DividerItemDecoration(mRecyclerview.getContext(),DividerItemDecoration.VERTICAL));
mRecyclerview.setHasFixedSize(true);
返回视图;
}
@凌驾
创建时的公共void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.KITKAT){
addAll(Objects.requirennull(getArguments().getParcelableArrayList(“listofcontacts”));
Log.d(标记“contact received”+arrayList.get(0.getContactName());
}否则{
if(getArguments().getStringArrayList(“listofcontacts”)!=null)
addAll(getArguments().getParcelableArrayList(“列表联系人”);
}
}
}
@凌驾
公共无效onDueListener(int位置){
Log.d(标记“onDueListener:”);
ContactModel ContactModel=新的ContactModel();
contactModel.setContactName(arrayList.get(position.getContactName());
contactModel.setContactNumberp(arrayList.get(position.getContactNumberp());
contactModel.setState(真);
Intent Intent=新的Intent(getActivity(),AddDue.class);
intent.putExtra(“用户详细信息”,contactModel);
星触觉(意向);
}
}
contactsradapter的公共列表(上下文上下文、OnContactListener a、ArrayList ArrayList){
Log.d(标记,“ListofContactsAdapter:+”+arrayList.size());
ContactListener=a;
this.arrayList=新的arrayList();
if(arrayList!=null)
this.arrayList.addAll(arrayList);
mContext=上下文;
}
@非空
@凌驾
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup ViewGroup,int i){
View View=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.loc\R\u容器,viewGroup,false);
customViewHolder=新的customViewHolder(视图,ContactListener);
返回customViewHolder;
}
@凌驾
BindViewHolder上的公共无效(@NonNull CustomViewHolder CustomViewHolder,int i){
customViewHolder.contactName.setText(arrayList.get(i).getContactName());
customViewHolder.contactNumber.setText(arrayList.get(i).GetContactNumberRP());
customViewHolder.contactDp.setText(String.valueOf(arrayList.get(i).getContactName().charAt(0));
}
@凌驾
public int getItemCount(){
Log.d(标记“getItemCount:”);
返回arrayList.size();
}
静态类CustomViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
OnContactListener侦听器;
TextView联系人姓名;
文本视图联系人号码;
TextView contactDp;
按钮;
线性布局线性布局;
CustomViewHolder(@NonNull View itemView,OnContactListener mOnContactListener){
超级(项目视图);
contactName=itemView.findViewById(R.id.contact\u name);
contactNumber=itemView.findViewById(R.id.contact_编号);
button=itemView.findViewById(R.id.action);
contactDp=itemView.findViewById(R.id.contactDp);
linearLayout=itemView.findviewbyd(R.id.container);
linearLayout.setOnClickListener(此);
Listener=mOnContactListener;
setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
Log.d(标记“onClick:”);
onDueListener(getAdapterPosition());
}
}
}我所做的是在您的RecyclerView适配器中创建一个接口:
public interface OnClickItem { // create an interface
void onClickItem(int position); // create callback function
}
然后在OnBindViewHolder中:
customViewHolder.(yourView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContactListener.onClickItem(customViewHolder.getAdapterPosition());
}
});
public class ListOfContacts extends Fragment implements ListOfContactsRAdapter.OnClickItem { ...
@Override
public void onClickItem(int position) {
...
}
在你的片段中:
customViewHolder.(yourView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContactListener.onClickItem(customViewHolder.getAdapterPosition());
}
});
public class ListOfContacts extends Fragment implements ListOfContactsRAdapter.OnClickItem { ...
@Override
public void onClickItem(int position) {
...
}
实施方法:
customViewHolder.(yourView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContactListener.onClickItem(customViewHolder.getAdapterPosition());
}
});
public class ListOfContacts extends Fragment implements ListOfContactsRAdapter.OnClickItem { ...
@Override
public void onClickItem(int position) {
...
}
我已经使用了这个代码没有任何工程…我的完整项目源代码好的,我会让你知道,如果我可以修复这个。