0

I have a grid view inwhich I inflate two layouts one with {background imageview containing a imageview and two textviews aligned vertically over the image} and another layout with {background image containing three textviews aligned vertically over the image}.

I have restricted maximum number of lines in textview. Please suggest how to make the textview scrollable in the gridview and I want the gridview items to be clickable which will navigate to next screen on item click.

I tried to make the scrollable textview working inside the gridview using the requestDisallowInterceptTouchEvent method. But i didn't get the expected result. When the setscrollingmovement() method is used, the textview goes unclickable and so this is also not working.

I am not able to post the scenario as image, as I don't have the enough reputations to post image. Image uploaded in google drive.Please use below link to view the image.

https://drive.google.com/file/d/0B45x-sgFopMBMUJOaHk0NWlkMzQ/edit?usp=sharing

Durga
  • 73
  • 12
  • Upload the image some other place like dropbox or google drive and post the link in the question. – seenukarthi Sep 23 '14 at 05:00
  • Image uploaded in google drive.. Please use this link to view the image... https://drive.google.com/file/d/0B45x-sgFopMBMUJOaHk0NWlkMzQ/edit?usp=sharing – Durga Sep 23 '14 at 05:17

2 Answers2

1

I got it working :-) But partially. I used android:descendantFocusability="blocksDescendants" in the root layout of gridview item's custom layout (inflating layout). The thing is onclick works outside of the textiew.

So, the textview scrolls and clicking outside of the textview somewhere on background image fires grid view's onClickListener()

But not able to trigger gridview's onclicklistener() when clicking on textview. Need to look into this issue.

Used suggestion from the stackoverflow link: Referred Link

Edited: Scrollable and clickable textview in gridview - Got Workaround using onTouchlistener

Setting below ontouch click listener for the textview inside the adapter class for gridview will allow you to scroll text as well as fire onItemClick of GridView.

OnTouchListener aListener = new OnTouchListener() {
                                long startTouchTime = 0;

                                @Override
                                public boolean onTouch(View v,
                                        MotionEvent event) {
                                    switch (event.getAction()) {
                                    case MotionEvent.ACTION_DOWN:
                                        Log.e("ACTION_DOWN",
                                                "ACTION_DOWN, intercept true");
                                        startTouchTime = (System
                                                .currentTimeMillis());
                                        // To scroll the textview set this
                                        textview
                                                .setMovementMethod(new ScrollingMovementMethod());
                                        v.getParent()
                                                .requestDisallowInterceptTouchEvent(
                                                        true);
                                        break;
                                    case MotionEvent.ACTION_UP:
                                        Log.e("ACTION_UP",
                                                "Call onitemclick"
                                                        + ((System
                                                                .currentTimeMillis()) - startTouchTime));

                                        if ((System.currentTimeMillis())
                                                - startTouchTime < 200)
                                         // To call gridview onItemClick here
                                            myGridActivity.myGridView
                                                    .getOnItemClickListener()
                                                    .onItemClick(
                                                            null,
                                                            myGridActivity.myGridView,
                                                            (Integer) imageView
                                                                    .getTag(), // Setting the position here through imageview set tag
                                                            (long) 6767);
                                        break;
                                    }
                                    if (event.getAction() == MotionEvent.ACTION_MOVE) {
                                        Log.e("ACTION_MOVE",
                                                "ACTION_MOVE, intercept true");

                                        v.getParent()
                                                .requestDisallowInterceptTouchEvent(
                                                        true);
                                    } else {
                                        Log.e("Other", "Intercept false");
                                        v.getParent()
                                                .requestDisallowInterceptTouchEvent(
                                                        true);

                                    }
                                    return false;
                                }
                            };

                            textview.setOnTouchListener(aCBInfoListener);

Set android:scrollbars="vertical" for the textview to be scrolled.

Community
  • 1
  • 1
Durga
  • 73
  • 12
0

Try this:

Properties of your TextView in your layout's xml file:

android:maxLines = "INTEGER"

android:scrollbars = "vertical"

Then use:

yourTextView.setMovementMethod(new ScrollingMovementMethod())

in your code.

It will work fine..

or else you can put the TextView into the ScrollView:

<ScrollView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_weight="1">
    <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Testin testin testing "></TextView>
</ScrollView>
Vijay
  • 3,152
  • 3
  • 24
  • 33
  • I have used the same. But when setMovementMethod() is used, I am not able to click the gridview item. It actually navigates to another screen when the item is clicked. Can we use requestDisallowInterceptTouchEvent() for the textview ? But I don't know how to use it in the right way – Durga Sep 23 '14 at 05:24
  • Scrolling is working but the grid item is not clickable. – Durga Sep 23 '14 at 06:55
  • If you trying to click the grid you can try with `YourGrid.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView> parent, View v, int position, long id) { Intent intent = new Intent(this, MyNewActivity.class); startActivity(intent); } });` – Vijay Sep 23 '14 at 07:01
  • Yes I have included that code in the class after calling the grid setAdapter() method. Also on fast scrolling gridview, the grid becomes empty. Any idea? – Durga Sep 23 '14 at 07:16
  • So, I don't have any other idea. If you post your XML and Class you can get answers from other experts. – Vijay Sep 23 '14 at 07:20
  • Thanks for helping... The grid items are changing their positions while scrolling so that the gridview becomes empty. Any idea on this issue? – Durga Sep 23 '14 at 07:22
  • [link](http://javatechig.com/android/android-gridview-example-building-image-gallery-in-android) Try this and[link](http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/).. This may helps you.. – Vijay Sep 23 '14 at 07:28
  • I have images of same height and so I have set constant row height and the gridview items are not varying their positions now. The odd behaviour of gridview scroll is not reproduced now. – Durga Sep 23 '14 at 08:58
  • You should post your xml and class.. If it is confidential hide some important lines and post it.. – Vijay Sep 23 '14 at 09:21
  • `public View getView(int aPosition, View aConvertView, ViewGroup aParent) { View aView = aConvertView; try { if (aConvertView == null) { LayoutInflater aInflater = (LayoutInflater) myContext .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); aView = aInflater.inflate(R.layout.grid_mbp_cb_template, aParent, false); } else aView = aConvertView; ImageView aBgTemplateIV = (ImageView) aView .findViewById(R.id.grid_mbp_cb_template_IV); loadTemplate(...); } catch (Exception e) { e.printStackTrace(); } return aView; }` – Durga Sep 23 '14 at 09:32
  • In loadTemplate(), I have loaded the images and set the text and its text height... – Durga Sep 23 '14 at 09:34
  • I review your codes. But i can not get the solution. Here i suggest some more likes. See this links.. It may help you.. [link 1](http://stackoverflow.com/questions/18500524/android-gridview-item-click-opens-all-layouts).. [link 2](http://stackoverflow.com/questions/18918633/gridview-on-item-click-listener).. [link 3](http://mrbool.com/implementing-gridview-in-android/27725).. [link 4](http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html).. – Vijay Sep 23 '14 at 10:07
  • Can't we have clickable item and gridview item click listener together? Pls See this link link https://code.google.com/p/android/issues/detail?id=3414 – Durga Sep 23 '14 at 10:24