-2

I am trying to implement an onItemClick within a fragment, when clicking on an element, but I have no result yet someone can help me?

public class FotoFragment extends Fragment {


    private List<DataPictures> mediaList = new ArrayList<>();
    private RecyclerView mRecyclerView;
    private MediaRVAdapter adapter;


    public FotoFragment() {
    }



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

        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        mRecyclerView.setPadding(10,10,10,10);

        mRecyclerView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(v.getContext(), "Intent onLongClick= ", Toast.LENGTH_SHORT).show();

                return false;
            }
        });
        return view;
    }

this is my xml fragment_blank

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

enter image description here

I'm trying to make a multiselection photography power but I've seen that you can do it with the onItemClick method, doing so

public void onItemClick(AdapterView parent, View view, int position, long id) {

How can I get the position of that element? With the onLongClick method?

Devix
  • 335
  • 4
  • 13

1 Answers1

0

Try this

OnCLick can be achived using Custom Interface

interface Listener {
    void performOperation(int Position);
}

in Your Adapter

private Listener mListener;

In your Adapter initialize your listner in constructor like this

mListener = (Listener) context;

In your bindViewHolder method

holder.yourView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mListener.performOperation(position);
        }
    });

Now in your Fragment implement that interface like this

public class FotoFragment extends Fragment implements listner {

@Override
public void performOperation(int Position) {
    //Do whatever you want to do with this position
}

}
AbhayBohra
  • 1,827
  • 15
  • 31