Android Horizo​​ntalScrollView 中项目的点击事件在滚动后不响应

发布于 2024-12-08 01:01:34 字数 3050 浏览 0 评论 0原文

我有一个 Horizo​​ntalScrollView,我在 Horizo​​ntalScrollView 内的 LinearLayout 中添加按钮,并将 onClickListener 设置为按钮。

当我滚动 ScrollView立即单击任何按钮时,它永远不会响应 onClick,但滚动后 3-5 秒,它会响应 onClick 。

请解释一下我怎样才能摆脱这个困境。我错过了什么吗?

<HorizontalScrollView android:layout_height="wrap_content"
        android:layout_width="600dip"
            android:layout_centerInParent="true" android:gravity="left"
            android:id="@+id/horizontalScrollView1" 

            >
            <LinearLayout android:orientation="horizontal"
                android:id="@+id/llChnnelCategoriesLayout" android:layout_height="wrap_content"
                android:layout_width="wrap_content">
                <TextView android:text="All " android:id="@+id/optionText1"
                    style="@style/CategoriesText"></TextView>
                <TextView android:text="l"
                    style="@style/CategoriesDeviderText"></TextView>
                <TextView android:text="Business News" android:id="@+id/optionText2"
                    style="@style/CategoriesText"></TextView>
                <TextView android:text="l"
                    style="@style/CategoriesDeviderText"></TextView>
                </LinearLayout>
        </HorizontalScrollView>

在“活动”中,我添加按钮并设置“onClickListener”。

  LinearLayout llChannelCategoriesLayout;
    arrChannelCategory = new String[] {"All Channels","Business News","Entertainment", "News","Cartoon","Music","Other"};
    llChannelCategoriesLayout =(LinearLayout)findViewById(R.id.llChnnelCategoriesLayout);
    llChannelCategoriesLayout.removeAllViews();
    for(int i=0; i<arrChannelCategory.length;i++)
    {
        Button btnCategory = new Button(this);
        TextView tvDevider = new TextView(this);
        tvDevider.setTextAppearance(getApplicationContext(), R.style.ChannelCategoriesDeviderText);


        btnCategory.setTextAppearance(getApplicationContext(), R.style.ChannelCategoriesText);
        //btnCategory.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

        btnCategory.setText(arrChannelCategory[i]);
        btnCategory.setPadding(Math.round(15*density), Math.round(5*density), Math.round(15*density), Math.round(5*density));
        btnCategory.setClickable(true); 
        btnCategory.setBackgroundColor(Color.TRANSPARENT);

        btnCategory.setId(i+3000);

        if(i != arrChannelCategory.length-1)
        {
            tvDevider.setText("  l  ");
        }

        btnCategory.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                changeColor(v.getId());
                Log.v("Channel Category Clicked ==","***"+arrChannelCategory[v.getId()-3000]);
            }
        });         

        llChannelCategoriesLayout.addView(btnCategory);     
        llChannelCategoriesLayout.addView(tvDevider);       

    }   

    //changeColor(3000);

}

I have an HorizontalScrollView and I am adding buttons in LinearLayout inside HorizontalScrollView and had set onClickListener to buttons.

When I scroll the ScrollView and immediately click on any button it never responds to onClick, but 3-5 seconds after scrolling, it responds to onClick.

Please explain how can I get ride of this. Am I missing something?

<HorizontalScrollView android:layout_height="wrap_content"
        android:layout_width="600dip"
            android:layout_centerInParent="true" android:gravity="left"
            android:id="@+id/horizontalScrollView1" 

            >
            <LinearLayout android:orientation="horizontal"
                android:id="@+id/llChnnelCategoriesLayout" android:layout_height="wrap_content"
                android:layout_width="wrap_content">
                <TextView android:text="All " android:id="@+id/optionText1"
                    style="@style/CategoriesText"></TextView>
                <TextView android:text="l"
                    style="@style/CategoriesDeviderText"></TextView>
                <TextView android:text="Business News" android:id="@+id/optionText2"
                    style="@style/CategoriesText"></TextView>
                <TextView android:text="l"
                    style="@style/CategoriesDeviderText"></TextView>
                </LinearLayout>
        </HorizontalScrollView>

And in Activity I am adding buttons and setting 'onClickListener'.

  LinearLayout llChannelCategoriesLayout;
    arrChannelCategory = new String[] {"All Channels","Business News","Entertainment", "News","Cartoon","Music","Other"};
    llChannelCategoriesLayout =(LinearLayout)findViewById(R.id.llChnnelCategoriesLayout);
    llChannelCategoriesLayout.removeAllViews();
    for(int i=0; i<arrChannelCategory.length;i++)
    {
        Button btnCategory = new Button(this);
        TextView tvDevider = new TextView(this);
        tvDevider.setTextAppearance(getApplicationContext(), R.style.ChannelCategoriesDeviderText);


        btnCategory.setTextAppearance(getApplicationContext(), R.style.ChannelCategoriesText);
        //btnCategory.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

        btnCategory.setText(arrChannelCategory[i]);
        btnCategory.setPadding(Math.round(15*density), Math.round(5*density), Math.round(15*density), Math.round(5*density));
        btnCategory.setClickable(true); 
        btnCategory.setBackgroundColor(Color.TRANSPARENT);

        btnCategory.setId(i+3000);

        if(i != arrChannelCategory.length-1)
        {
            tvDevider.setText("  l  ");
        }

        btnCategory.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                changeColor(v.getId());
                Log.v("Channel Category Clicked ==","***"+arrChannelCategory[v.getId()-3000]);
            }
        });         

        llChannelCategoriesLayout.addView(btnCategory);     
        llChannelCategoriesLayout.addView(tvDevider);       

    }   

    //changeColor(3000);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文