簡體   English   中英

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

[英]Why is my onClick not working in my RecycleView Fragments?

我是學習和使用RecycleView ,與Listview相比,我發現它很難。 我想要的是,當我單擊RecycleView中的某個項目時,我想要顯示我的片段。 這里看下面這張照片:

在此處輸入圖片說明

我已經創建了一個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

嘗試使用fragmentTransaction.replace()代替fragmentTransaction.add() 還建議將FrameLayout用作片段的容器。 您當前正在使用RelativeLayout ,它僅存儲一個視圖,這浪費了內存和性能。

嘗試在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);
    }

我認為問題出在這行代碼中

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

您正在嘗試訪問R.layout.fragment_inventory中定義的R.id.inventorylist_fragmentcontainer,但是您的片段正在使用布局R.layout.fragment_inventory_list。 它也在那里定義嗎?

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

如果未在您的片段使用的布局中定義它,則必須從可訪問R.id.inventorylist_fragmentcontainer的活動或片段中調用此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM