儿童$ P $ OnClickListener pventing的Andr​​oid ViewPager的刷卡儿童、OnClickListener、pventing、ViewPager

2023-09-04 13:04:36 作者:听余音未散

我使用的是ViewPager动态视图,如本链接。我加入的每个页面的viewpager。的code创建的每一页并把它添加到该寻呼机适配器是如下

I am using a ViewPager with Dynamic Views as shown in this link. I am adding each page to the viewpager. The code to create each page and add it to the pager adapter is as follows

public View createPage( )
{   
TableLayout table = new TableLayout(mycontext);
TableRow row = new TableRow(mycontext);

for(int i=0; i <= num_of_views; i++)
{

        TableLayout catTable = new TableLayout(mycontext);

        TableLayout.LayoutParams params = new TableLayout.LayoutParams();   

        params.height = LayoutParams.MATCH_PARENT;

        TableRow catName = new TableRow(mycontext);
        catName.setGravity(0x03);
        catName.setLayoutParams(params);

        TextView cat = new TextView(mycontext);
        TableRow.LayoutParams RowParams= 
            new TableRow.LayoutParams(pixeltoDp(0),pixeltoDp(85));
        RowParams.weight = 1;
        cat.setLayoutParams(RowParams);
        cat.setPadding(35, 0, 5, 0);
        cat.setGravity(0x13);
        cat.setSingleLine(true);
        cat.setHorizontallyScrolling(true);
        cat.setFocusable(true);
        cat.setFocusableInTouchMode(true);
        cat.setLines(1);
        cat.setText(data.getCats().get(i).getName());
        cat.setTextColor(color);
        cat.setSelected(true);

        catName.addView(cat);
        catTable.addView(catName);
        for(int j=0; j < 5; j++)
        {
            TableRow trChild = new TableRow(mycontext);
            trChild.setLayoutParams(params);
            trChild.setGravity(0x11);
            for (int k=0; k<4; k++)
            {
                TextView app = new TextView(mycontext);
                TableRow.LayoutParams tableRowParams= 
                    new     TableRow.LayoutParams(pixeltoDp(0),pixeltoDp(80));
                margin = pixeltoDp(11);

                tableRowParams.weight = 1;
                app.setLayoutParams(tableRowParams);

                app.setGravity(0x11);
                app.setSingleLine(true);
                app.setTextSize(pixeltoDp(12));
                app.setTextColor(Color.BLACK);
                app.setTypeface(verdana);


                int pos = j+(k*5);
                if((pos < mysize))
                {
                        Drawable icon = getMyDrawable();

                        app.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
                        app.setText("some Name");
                        app.setTag(i+":"+pos);
                        app.setEllipsize(TextUtils.TruncateAt.END);
                        app.setOnClickListener(onMyClick);

                }

                trChild.addView(app);
            }
            catTable.addView(trChild);              
        }   
        TableRow.LayoutParams tableRowParams=
              new TableRow.LayoutParams
                  (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
        margin = pixeltoDp(5);
        tableRowParams.setMargins(margin,0,margin, pixeltoDp(18));
        catTable.setLayoutParams(tableRowParams);       
}           
row.setGravity(0x03);
table.addView(row);     
return table;           
}

的OnClickListener对于在页中的TextView如下

The OnClickListener for the textview in the page is as follows

    private View.OnClickListener onAppClick=new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String gettext = (String)v.getTag();
        String[] splittext = gettext.split(":");

        Log.d("TAG","Click Event is Triggered");
    }
};

我现在面临的问题是,viewpager幻灯片时,我轻扫屏幕上,但如果我的手指开始轻扫从与onclicklistener页面没有滑动,也click事件不叫文字。如果我点击的文本视图的单击事件被调用。我无法弄清楚,为什么viewpager在没有翻动页面,如果刷卡开始从与click事件的文本视图。

The problem I am facing is that the viewpager slides when I swipe on the screen but if my finger starts the swipe from the text with the onclicklistener the page does not slide, also the click event is not called. If I click on the text view the click event is called. I am not able to figure why the viewpager in not flipping the pages if the swipe starts from the text view with click event.

要找出我添加了一个ontouchlistener到viewpager,发现viewpager的触摸事件时,刷一下发生在页面的一部分在没有文字观和触摸事件是被称为错误的原因不叫所有,如果刷卡开始的文本视图的顶部。如果我删除的TextView的onclicklistener那么整个viewpager翻转正常,没有任何问题。所以问题在于TextView的的点击事件被停止viewpager的触摸事件。请告诉我,如果我做错了什么在我创建网页code。这是我在这个函数创建视图通过pageadapter加入viewpager所示,我在开头提到的教程。

To find out the reason for the bug I added a ontouchlistener to the viewpager and found that the touch event of the viewpager is called when the swipe happens on the part of the page where there is no text view and the touch event is not called at all if the swipe starts on top of the text view. If I remove the onclicklistener of the textview then the entire viewpager flips properly without any issue. So the problem is that the click event of the textview is stopping the touch events of the viewpager. Please tell me if I am doing something wrong in my create page code. The view which I create in this function is added to the viewpager through the pageadapter as shown in the tutorial I mentioned in the beginning.

推荐答案

最后,我发现有在ViewPager的最新版本,其中有一个click事件一个TextView,单线设置为真和重力设置的错误为中心是不是通过触摸事件至ViewPager如果手指移动开始在TextView的顶部。如果我们让SINGLELINE为假或取出中心重力则事件被传递给ViewPager那么它可以扔。我们不能同时使用重心和setsingleline(真)的TextView的或sinlgeline一个按钮设置为true。我报告这是一个错误here

Finally I found that there is a bug in the latest version of ViewPager where a TextView with a click event, singleLine set as true and gravity set as center is not passing the touch events to the ViewPager if the finger is moved starting on the top of the textview. If we make the singleLine as false or remove the Center gravity then the events are passed to the ViewPager then it can fling. We can't use both the center gravity and setsingleline(true) for the textview or a button with sinlgeline set as true. I have reported this as a bug here