为什么我的onClick在我的RecycleView片段中不起作用?

时间:2019-07-06 13:26:33

标签: android android-recyclerview

我在学习和使用RecycleView方面是新手,与Listview相比,我发现这很困难。我想要的是,当我单击RecycleView中的某个项目时,我想要显示我的片段。在下面看这张照片:

enter image description here

我已经创建了一个onClick,但是当我从RecycleView列表中单击某个项目时,我的应用会突然停止。

请检查以下代码:

public class InventoryListFragment extends Fragment implements InventoryRecyclerViewAdapter.OnInventoryListener{

    public InventoryListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_inventory_list, container, false);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.inventorylist_recycleview);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .build()); //adding a divider into the recyclerview list

        InventoryRecyclerViewAdapter adapter = new InventoryRecyclerViewAdapter();
        recyclerView.setAdapter(adapter);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    //adding a listener to the recycleview list
    @Override
    public void onInventoryClick(int position) {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        switch (position){
            case 0:
                ProductsFragment productsFragment = new ProductsFragment();
                fragmentTransaction.add(R.id.inventorylist_fragmentcontainer, productsFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                break;
            case 1:
                ServicesFragment servicesFragment = new ServicesFragment();
                fragmentTransaction.add(R.id.inventorylist_fragmentcontainer, servicesFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                break;
            case  2:
                CategoriesFragment categoriesFragment = new CategoriesFragment();
                fragmentTransaction.add(R.id.inventorylist_fragmentcontainer, categoriesFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                break;
            case 3:
                DiscountsFragment discountsFragment = new DiscountsFragment();
                fragmentTransaction.add(R.id.inventorylist_fragmentcontainer, discountsFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                break;
        }
    }

InventoryRecyclerViewAdapter.java

public class InventoryRecyclerViewAdapter extends RecyclerView.Adapter {

    private OnInventoryListener mOnInventoryListener;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.customlayout_inventorylist, viewGroup, false);
        return new InvRecycleViewHolder(view, mOnInventoryListener);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ((InvRecycleViewHolder)viewHolder).bindView(i);
    }

    @Override
    public int getItemCount() {
        return InventoryRecyclerViewDataList.label.length;
    }

    //adding listeners
    private class InvRecycleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView mPicturePath;
        private TextView mLabel;

        //added for the onInventoryListener
        OnInventoryListener onInventoryListener;

        public InvRecycleViewHolder(View itemView, OnInventoryListener onInventoryListener){ //onInventoryListener is added
            super(itemView);
            mPicturePath = (ImageView) itemView.findViewById(R.id.inventorylist_icon);
            mLabel = (TextView) itemView.findViewById(R.id.inventorylist_title);

            //added for the onInventoryListener
            this.onInventoryListener = onInventoryListener;

            itemView.setOnClickListener(this);
        }

        //onlick listener for itemview
        @Override
        public void onClick(View v) {
            //add some code here..
            onInventoryListener.onInventoryClick(getAdapterPosition());
        }

        public void bindView(int position){
            mPicturePath.setImageResource(InventoryRecyclerViewDataList.picturePath[position]);
            mLabel.setText(InventoryRecyclerViewDataList.label[position]);
        }
    }

    public interface OnInventoryListener{
        void onInventoryClick(int position);
    }

fragment_inventory.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragments.InventoryFragment"
    android:orientation="horizontal"
    android:id="@+id/inventory_content">

    <fragment
        android:id="@+id/inventorylist_fragment"
        android:name="com.example.devcash.Fragments.InventoryListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/fragment_inventory_list">

    </fragment>

    <View
        style="@style/Divider"
        android:layout_width="1dp"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/inventorylist_fragmentcontainer"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"/>
</LinearLayout>

Logcat

2019-07-06 22:01:42.871 28763-28763/com.example.devcash D/ViewRootImpl@be8c566[DashboardActivity]: ViewPostImeInputStage processPointer 1
2019-07-06 22:01:42.971 28763-28763/com.example.devcash D/ViewRootImpl@be8c566[DashboardActivity]: ViewPostImeInputStage processPointer 0
2019-07-06 22:01:43.079 28763-28763/com.example.devcash D/ViewRootImpl@be8c566[DashboardActivity]: ViewPostImeInputStage processPointer 1
2019-07-06 22:03:28.142 28763-28763/com.example.devcash D/ViewRootImpl@be8c566[DashboardActivity]: ViewPostImeInputStage processPointer 0
2019-07-06 22:03:28.166 28763-28763/com.example.devcash D/ViewRootImpl@be8c566[DashboardActivity]: ViewPostImeInputStage processPointer 1
2019-07-06 22:03:28.294 28763-28763/com.example.devcash D/ViewRootImpl@be8c566[DashboardActivity]: ViewPostImeInputStage processPointer 0
2019-07-06 22:03:28.347 28763-28763/com.example.devcash D/ViewRootImpl@be8c566[DashboardActivity]: ViewPostImeInputStage processPointer 1
2019-07-06 22:03:28.517 28763-28763/com.example.devcash D/ViewRootImpl@be8c566[DashboardActivity]: ViewPostImeInputStage processPointer 0
2019-07-06 22:03:28.560 28763-28763/com.example.devcash D/ViewRootImpl@be8c566[DashboardActivity]: ViewPostImeInputStage processPointer 1

3 个答案:

答案 0 :(得分:0)

尝试使用fragmentTransaction.replace()代替fragmentTransaction.add()。 还建议使用FrameLayout作为片段的容器。您当前使用的RelativeLayout仅存储一个视图,这浪费了内存和性能。

答案 1 :(得分:0)

尝试在ViewHolder中定义一个View变量并将其初始化为itemView并进行声明 onBindViewHolder中的onClick侦听器

public class InventoryRecyclerViewAdapter extends RecyclerView.Adapter {

    private OnInventoryListener mOnInventoryListener;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.customlayout_inventorylist, viewGroup, false);
        return new InvRecycleViewHolder(view, mOnInventoryListener);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ((InvRecycleViewHolder)viewHolder).bindView(i);
        itemView.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return InventoryRecyclerViewDataList.label.length;
    }

    //adding listeners
    private class InvRecycleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView mPicturePath;
        private TextView mLabel;
        private View v;

        //added for the onInventoryListener
        OnInventoryListener onInventoryListener;

        public InvRecycleViewHolder(View itemView, OnInventoryListener onInventoryListener){ //onInventoryListener is added
            super(itemView);
            mPicturePath = (ImageView) itemView.findViewById(R.id.inventorylist_icon);
            mLabel = (TextView) itemView.findViewById(R.id.inventorylist_title);

            //added for the onInventoryListener
            this.onInventoryListener = onInventoryListener;
            v = itemView;


        }

        //onlick listener for itemview
        @Override
        public void onClick(View v) {
            //add some code here..
            onInventoryListener.onInventoryClick(getAdapterPosition());
        }

        public void bindView(int position){
            mPicturePath.setImageResource(InventoryRecyclerViewDataList.picturePath[position]);
            mLabel.setText(InventoryRecyclerViewDataList.label[position]);
        }
    }

    public interface OnInventoryListener{
        void onInventoryClick(int position);
    }

答案 2 :(得分:0)

我认为问题出在这行代码

fragmentTransaction.add(R.id.inventorylist_fragmentcontainer, productsFragment);

您正尝试访问R.layout.fragment_inventory中定义的R.id.inventorylist_fragment容器,但您的片段正在使用布局R.layout.fragment_inventory_list。它也在那里定义吗?

View view = inflater.inflate(R.layout.fragment_inventory_list, container, false);

如果您的片段正在使用的布局中未定义它,那么您必须从可访问R.id.inventorylist_fragmentcontainer的活动或片段中调用此代码。