简体   繁体   中英

Q: How to use OnClickListener with HorizontalScrollView

OnClickListener not working with ScrollView because "extends Fragment". How can I use it with this? An example enough. Thanks..

UPDATE Fragment1.java

package intizamyazilim.example;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 26.02.2015.
 */
public class Fragment1 extends Fragment {

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState) {
        if (container == null) {

           return null;
        }
        return (LinearLayout) inflater.inflate(R.layout.fragment1, container, false);
    }
}

You can use horizontallscrollviews onTouchListener as in this example. I just quickly used Android Studio's default activity with fragment but I hope it gives you an idea :)

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        HorizontalScrollView horizontalScrollView = (HorizontalScrollView) getActivity().findViewById(R.id.horizontalScrollView);

        horizontalScrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Toast.makeText(getActivity(), "Touched", Toast.LENGTH_LONG).show();
                }
                return false;
            }
        });
    }
}

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