Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClickListener of Child preventing the swipe of Android ViewPager

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

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

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.

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.

like image 748
Abu Saad Papa Avatar asked Sep 07 '12 17:09

Abu Saad Papa


1 Answers

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

See the 'apply single line' source here: https://github.com/android/platform_frameworks_base/blob/7bfb1e7f46d2ed3df6cc467bccec95b5999d9cc4/core/java/android/widget/TextView.java#L7877

like image 55
Abu Saad Papa Avatar answered Oct 11 '22 18:10

Abu Saad Papa