android - 实现 setOnClickListener

标签 android

我有这个示例,我正在尝试在 subview 中实现对项目的点击

我有这两个xml文件

这是 subview .xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
            android:id="@+id/textLabel"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:textSize="50dip"
            android:textColor="#00FF00"
            android:textStyle="bold"/>

</LinearLayout>

这是 scrollview.xml View :

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="none">

    <LinearLayout
            android:layout_width="fill_parent"
            android:id="@+id/scrollviewlinearlayout"
            android:layout_height="fill_parent">
    </LinearLayout>
</HorizontalScrollView>

这是 Activity :

public class TestTwo extends Activity {
Context mContext;
HorizontalScrollView mScrollView;
LinearLayout mLinearLayout;
LinearLayout.LayoutParams mLinearLayoutParams;
Display mDisplay;
// scroll behaviour
private int mScrollStartPosition;
private static final float SCROLL_MARGIN = 0.2f;

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        // load layout from xml and get references to sub-views
        setContentView(R.layout.scrollview);
        mScrollView = (HorizontalScrollView) findViewById(R.id.scrollview);
        mLinearLayout = (LinearLayout) findViewById(R.id.scrollviewlinearlayout);
        // get a display reference (used to find screen size)
        mDisplay = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        // get the layout parameters to apply to the sub-views
        mLinearLayoutParams = new LayoutParams(mDisplay.getWidth(), mDisplay.getHeight());
        // add some views to the ScrollView
        addViewsToScrollView();

}

private void addViewsToScrollView() {
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < 3; i++) {
            // inflate view from xml
            View child = inflater.inflate(R.layout.subview, null);
            // give it a number
            final TextView text = (TextView) child.findViewById(R.id.textLabel);
            text.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    text.setText("Test");
                }
            });
            text.setText("" + (i + 1));
            // give it a colour
            text.setBackgroundColor(Color.rgb((int) (Math.random() * 255), (int) (Math.random() * 255),
                            (int) (Math.random() * 255)));
            // apply layout parameters, and add it
            child.setLayoutParams(mLinearLayoutParams);
            mLinearLayout.addView(child);
    }
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
        int viewWidth = mDisplay.getWidth(); // width of each view
        int triggerWidth = (int) (SCROLL_MARGIN * viewWidth); // amount user has to scroll to move to next view
        int pos = mScrollView.getScrollX();
        int diff = pos % viewWidth; // offset of current scroll from leftmost view's snap position
        int posLeftView = pos - diff; // absolute snap position of the leftmost view on screen
        switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                        // Record the starting scroll position. This is used to decide the scroll direction.
                        mScrollStartPosition = pos;
                        break;
                case MotionEvent.ACTION_UP:
                        if (pos > mScrollStartPosition) {
                                // Scrolling right
                                if (diff > triggerWidth) mScrollView.smoothScrollTo(posLeftView + viewWidth, 0);
                                else mScrollView.smoothScrollTo(posLeftView, 0);
                        } else {
                                // Scrolling left
                                if (diff > (viewWidth - triggerWidth)) mScrollView.smoothScrollTo(posLeftView + viewWidth, 0);
                                else mScrollView.smoothScrollTo(posLeftView, 0);
                        }
                        // replacing our scrollTo command with it's own
                        return true;
        }
        return super.dispatchTouchEvent(ev);
}

我尝试调试它,但它似乎没有触发 onClick 事件 你能帮我解决这个问题吗?

最佳答案

你正在做一堆疯狂的事情,我会在下次离开。在您的 final TextView 文本

上尝试 text.setClickable(true)

关于android - 实现 setOnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8218646/

相关文章:

android - 通过android发送arraylist作为请求参数

android - changeCursor 导致 CalledFromWrongThreadException 异常......但只有一次

java - 在NotificationListenerService中过滤应用程序

java - 如何在 GridView 中交换图像 (Android)

java - 如何将 Paint 或 Color 传递给使用 Activity 中的 onDraw() 方法的新对象?

android - Intent.setClass错误

android - AVD在Android Studio 2.3.1中自动关闭

java - Xamarin Studio 中的 Android - 错误 : package com. google.android.gms.tagmanager 不存在

Android:AudioTrack 开始时发出咔哒声

安卓模拟器 : Failed to allocate memory: 8 even with 8MB RAM