简体   繁体   中英

Using both a CardScrollView and a ScrollView

I have a CardScrollView with multiple cards. Swiping left and right moves between the cards.

Some of the cards have a lot of content on them. I used a ScrollView so the user can scroll through the card to see the content.

Glass doesn't know whether it should scroll to a different card or scroll on the card it is on when the user swipes their finger for obvious reasons. It chooses to scroll to a different card.

To differentiate, I want to use the GestureDetector to make a one finger scroll scroll cards, and a two finger scroll scroll on the selected card. Seems easy enough, so I made the createGestureDetector() method and put if statements for each case.

Now I have a problem...I do not know how to tell the CardScrollView to advance or go back a card, and I dont know how to make the ScrollBody scroll based on the gesture.

I looked through all of the available methods and nothing stuck out to me as particularly helpful. Does anyone have any idea how to do this?

Bonus question: I saw a lot of "dispatch" commands, like dispatchGenericMotionEvent. What do dispatch methods do?

EDIT:

Here is my code after Jean Vacca's suggestion:

private GestureDetector createGestureDetector(Context context) {
        GestureDetector gestureDetector = new GestureDetector(context);

        gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
            @Override
            public void onFingerCountChanged(int previousCount, int currentCount) {
                if(currentCount == 2){
                    mCardScrollView.deactivate();
                }else{

                    mCardScrollView.activate();
                }
            }
        });
            return gestureDetector;
    }

and the xml for my views is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ScrollView 
        android:id="@+id/scrollBody"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false">

        <LinearLayout
            android:id="@+id/scrollLinearLayout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">           
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

Which is filled with TextViews in this code segment located in the CardScrollAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        int nextID = 3;

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.my_card, null);
            holder = new ViewHolder();

            MyClass mine = mMyList.get(position);

            LinearLayout ll = (LinearLayout)convertView.findViewById(R.id.scrollLinearLayout);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

            holder.name = new TextView(this.context);
            holder.name.setId(nextID);
            nextID++;
            holder.name.setTextSize(50);
            holder.name.setText(mine.getName());
            holder.name.setLayoutParams(lp);
            holder.name.setGravity(Gravity.CENTER);
            ll.addView(holder.name);

            holder.infoTextViews = new ArrayList<TextView>(mine.getInfo().size());
            for(int i = 0; i < mine.getInfo().size(); i++)
            {
                holder.infoTextViews.add(new TextView(this.context));
                TextView tv = holder.infoTextViews.get(i);
                tv.setId(nextID);
                nextID++;
                tv.setTextSize(24);
                tv.setText(mine.getInfo().get(i));
                tv.setLayoutParams(lp);
                tv.setGravity(Gravity.CENTER);
                ll.addView(tv);
            }
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        return convertView;
    }

I hope these edits help!

I had a similar problem on my project. To resolve it, I had to handle the "onFingerCountChanged" in the GestureDetector and desactivate the CardScrollView when i have 2 fingers count. So when I use one finger the cardscrollview scroll normaly and when I use two finger on can scroll in my card using the gestureDetector.

You code should look something like this :

    gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
        @Override
        public void onFingerCountChanged(int previousCount, int currentCount) {
            if(currentCount == 2){
                yourCardScrollView.deactivate();
            }else{

                yourCardScrollView.activate();
            }

        }
        ....
    }

EDIT: I notice that my first answer is not work. I have another one. You can use following code in your Activity.

@Override
public boolean dispatchGenericMotionEvent(MotionEvent ev) {
    if (ev.getPointerCount() > 1) {
        return true;
    }
    return super.dispatchGenericMotionEvent(ev);
}

When dispatchGenericMotionEvent actives, detect if pointer is greater than one (muit-touch), return true and consume it.

I tested, but still have bug. https://code.google.com/p/google-glass-api/issues/detail?id=632
Reply me if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM