Onitemclicklistener and Onitemlongclicklistener working together

Asked

Viewed 152 times

0

I have a listview inside a Fragment and from each item I open a different Activity, but after I put a Onitemlongclicklistener to open a Alertdialog when each item is pressed the Onlistitemclick "stopped" from working. As it is a custom Array Adapter I have a Relativelayout with 16dp padding, and the Onlistitemclick only works by clicking outside the Relative Layout

This is XML with custom items

 <RelativeLayout
    android:id="@+id/text_container"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:longClickable="true"
    android:clickable="true"
    android:background="@color/lista_background"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txt_item_list"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="40dp"
        android:fontFamily="cursive"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="43sp"
        android:textStyle="bold"
        tools:text="@string/txt_lista" />

    <ImageView
        android:id="@+id/image_list"
        android:layout_width="@dimen/list_item_height"
        android:layout_height="@dimen/list_item_height"
        android:layout_alignParentRight="true"
        android:layout_marginRight="40dp"
        android:layout_marginTop="20dp"/>

</RelativeLayout>   

That part with Onitemclicklistener and Onitemlongclicklistener

        View rootView = inflater.inflate(R.layout.my_list, container, false);
    ArrayList<Itens> itens = new ArrayList<Itens>();
    itens.add(new Itens("Primeira", R.drawable.img));
    itens.add(new Itens("Segunda", R.drawable.img));
    itens.add(new Itens("Terceira", R.drawable.img));

    MyAdapter adapter = new MyAdapter(getActivity(), itens, R.color.lista_background);

    ListView listView = (ListView) rootView.findViewById(R.id.list);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
            if(position == 0) {
                Intent intent = new Intent(getActivity(), PrimeiraActivity.class);
                startActivity(intent);
            }

            if(position == 1) {
                Intent intent = new Intent(getActivity(), SegundaActivity.class);
                startActivity(intent);
            }

            if(position == 2) {
                Intent intent = new Intent(getActivity(), TerceiraActivity.class);
                startActivity(intent);
            }
        }
    });

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
            if(position == 0) {
                PrimeiroDialogFragment dialogFragment = new PrimeiroDialogFragment();
                dialogFragment.show(getChildFragmentManager(), "sobre");
            }
            if(position == 1) {
                SegundoDialogFragment dialogFragment = new SegundoDialogFragment();
                dialogFragment.show(getChildFragmentManager(), "sobre");


            }
            if(position == 2) {
                TerceiroDialogFragment dialogFragment = new TerceiroDialogFragment();
                dialogFragment.show(getChildFragmentManager(), "sobre");
            }

            return true;
        }
    });

    return rootView;
  • I don’t understand what you want! xD

  • I want when I click the item to have a behavior (open an Activity) and when I hold it to have another one (display the Dialogfragment). It’s working, but the problem is that I have a Relativelayout (it has a yellow background) to customize the items, the problem is that it only works qnd I click out of this yellow rectangle, because it has a 16dp padding. I don’t know if there’s any way to send photo here, it would be better to explain

  • Solved. It was just take android:clickable and android:longClickable

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.