问:如何将OnClickListener与Horizo​​ntalScrollView一起使用

时间:2015-02-26 06:34:15

标签: java android scrollview onclicklistener horizontalscrollview

OnClickListener不使用ScrollView,因为"扩展了Fragment"。我怎么能用它呢?一个例子就够了。感谢..

更新 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);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以在此示例中使用onTouchListener上的horizo​​ntallscrollviews。我只是快速使用Android Studio的默认活动片段,但我希望它能给你一个想法:)

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;
            }
        });
    }
}