简体   繁体   中英

How to get RecyclerView onClick working in a Fragment

After many days getting Fragment to actually display I now find myself struggling to be able to allow clicking items in recyclerview. I have read numerous postings on how to do it but I must be missing something. First let me say my Background has been years as a VB developer.

Here is what I currently have in my Fragment Java.

    package com.procatdt.stockright.activities;
    import android.app.Fragment;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    import com.procatdt.stockright.R;
    import com.procatdt.stockright.models.ItemsListModel;
    import com.procatdt.stockright.utils.DatabaseMgr;
    import java.util.List;

    public class SelectItemFragment extends  Fragment {
    private RecyclerAdapter mRecyclerAdapter;
    private String mParam1;
    private Bundle args;
    private RecyclerView mRecyclerView;
    private List <ItemsListModel> myItems;
    private ItemsListModel mItem;
    private Button btnSelect;


    public SelectItemFragment newInstance(String param1) {
        SelectItemFragment fragment = new SelectItemFragment();
        args = new Bundle();
        args.putString("valuesArray", param1);
        String mParam1 = param1;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        mParam1 = bundle.toString();
        String name = this.getArguments().getString("valuesArray").toString();

        String newParam1 = name;
        View view = inflater.inflate(R.layout.fragment_select_item, container, false);
        btnSelect = (Button)view.findViewById(R.id.btnSelectItem);
        myItems = DatabaseMgr.getItemsList(newParam1);

        if (myItems.size() > 0) {
            myItems.get(0).setSelected(true);
            mItem = new ItemsListModel(myItems.get(0));
        }
        mRecyclerView = (RecyclerView) view.findViewById(R.id.lRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        mRecyclerAdapter = new RecyclerAdapter(myItems);
        mRecyclerView.setAdapter(mRecyclerAdapter);

        return (view);
    }
    private class RecyclerAdapter extends RecyclerView.Adapter<RecyclerHolder> {
        private RecyclerViewClickListener mListener;
        private List <ItemsListModel> items;


        RecyclerAdapter(RecyclerViewClickListener listener){
            mListener=listener;
        }

        public RecyclerAdapter(List <ItemsListModel> itemList) {

          items = itemList;
        }

        @Override
        public   RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_select_item, parent, false);
            return new RecyclerHolder(view);
        }
        public void onBindViewHolder(RecyclerHolder recyclerholder, int position) {
            ItemsListModel item = items.get(position);
            if (item.isSelected()) {
                recyclerholder.itemView.setBackgroundColor(getResources().getColor(R.color.colorYellow));
            } else {
                recyclerholder.itemView.setBackgroundColor(getResources().getColor(R.color.colorWhite));
            }
            recyclerholder.bindRecyclerData(item);
        }
        @Override
        public int getItemCount() {
            return items.size();
        }
    }

    public class RecyclerHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView mlItem;
        private TextView mLVL;
        private TextView mLVLd;
        private TextView mDesc;
        private ItemsListModel mItems;


        public RecyclerHolder(View itemView) {
            super(itemView);
            mlItem = (TextView) itemView.findViewById(R.id.text_view_item);
            mLVL = (TextView) itemView.findViewById(R.id.text_view_LVL);
            mLVLd = (TextView) itemView.findViewById(R.id.text_view_LVLd);
            mDesc = (TextView) itemView.findViewById(R.id.text_view_desc);
        }

        public void bindRecyclerData(ItemsListModel newItem) {
            mItems = newItem;
            mlItem.setText(mItems.getiItem());
            mLVL.setText(mItems.getiLVL());
            mLVLd.setText(mItems.getiLVLd());
            mDesc.setText(mItems.getiDesc());
        }

        @Override
        public void onClick(View view) {
            for (int i = 0; i < myItems.size(); i++) {
                if (myItems.get(i).equals(mItem)) {
                    myItems.get(i).setSelected(true);
                    mItem = new ItemsListModel(myItems.get(i));
                } else {
                    myItems.get(i).setSelected(false);
                }
            }
            mRecyclerAdapter.notifyDataSetChanged();
        }
        public void onbtnClick(View view){

        }

    }

    public interface RecyclerViewClickListener {
        void onClick(View view, int position);
    }
}

The XML for the fragment is.

    <FrameLayout 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="com.procatdt.stockright.activities.SelectItemFragment">

    <!-- TODO: Update blank fragment layout -->

    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

  <!--      <FrameLayout
            android:id="@+id/newFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true">-->


        <TextView
            android:id="@+id/text_view_item"
            android:layout_width="65dp"
            android:layout_height="35dp"
            android:gravity="start|right"
            android:text="Item" />

        <TextView
            android:id="@+id/text_view_LVL"
            android:layout_width="40dp"
            android:layout_height="35dp"
            android:layout_toRightOf="@+id/text_view_item"
            android:text="LVL" />

        <TextView
            android:id="@+id/text_view_LVLd"
            android:layout_width="60dp"
            android:layout_height="35dp"
            android:layout_toRightOf="@+id/text_view_LVL"
            android:text="LvlDesc" />

        <TextView
            android:id="@+id/text_view_desc"
            android:layout_width="100dp"
            android:layout_height="35dp"
            android:layout_toRightOf="@+id/text_view_LVLd"
            android:text="Description" />


        <android.support.v7.widget.RecyclerView
            android:id="@+id/lRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="115dp"

            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:paddingTop="8dp"
            android:scrollbars="horizontal" />


        <Button
            android:id="@+id/btnSelectItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="220dp"
            android:onClick="onbtnClick"
            android:text="Select Item" />

        <!--</FrameLayout>-->
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorBackgroundGray"
        android:layout_below="@id/layout"
        style="?android:listSeparatorTextViewStyle"
        />
</FrameLayout>

When the I click on any of the items in recyclerview nothing happens. I would like the item I click to be highlighted and let me know what row in recyclerview has been selected. I have been not having issues with recyclerviews in activities but fragments only.

Any pointers would be most appreciated. Tom

I think you forget itemView.setOnClickListener(this);

Try this:

public RecyclerHolder(View itemView) {
            super(itemView);
            mlItem = (TextView) itemView.findViewById(R.id.text_view_item);
            mLVL = (TextView) itemView.findViewById(R.id.text_view_LVL);
            mLVLd = (TextView) itemView.findViewById(R.id.text_view_LVLd);
            mDesc = (TextView) itemView.findViewById(R.id.text_view_desc);
            itemView.setOnClickListener(this);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM