【问题标题】:ViewPager inside ExpandableListView click Listener not workingExpandableListView 中的 ViewPager 单击侦听器不起作用
【发布时间】:2015-11-16 14:12:17
【问题描述】:

我的 ExpandableListView 中有一个自定义 ViewPager,点击监听器没有触发,有什么想法吗?我知道我可以在适配器中添加点击侦听器,但我不想这样做。

自定义寻呼机

public class CustomPager extends ViewPager {


/**
 * the last x position
 */
private float lastX;

/**
 * if the first swipe was from left to right (->), dont listen to swipes from the right
 */
private boolean slidingLeft;

/**
 * if the first swipe was from right to left (<-), dont listen to swipes from the left
 */
private boolean slidingRight;

public CustomPager(final Context context, final AttributeSet attrs) {
    super(context, attrs);
}

public CustomPager(final Context context) {
    super(context);
}

@Override
public boolean onTouchEvent(final MotionEvent ev) {
    final int action = ev.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:

            // Disallow parent ViewPager to intercept touch events.
            this.getParent().requestDisallowInterceptTouchEvent(true);

            // save the current x position
            this.lastX = ev.getX();

            break;

        case MotionEvent.ACTION_UP:
            // Allow parent ViewPager to intercept touch events.
            this.getParent().requestDisallowInterceptTouchEvent(false);

            // save the current x position
            this.lastX = ev.getX();

            // reset swipe actions
            this.slidingLeft = false;
            this.slidingRight = false;

            break;

        case MotionEvent.ACTION_MOVE:
            /*
             * if this is the first standing_item, scrolling from left to
             * right should navigate in the surrounding ViewPager
             */
            if (this.getCurrentItem() == 0) {
                // swiping from left to right (->)?
                if (this.lastX <= ev.getX() && !this.slidingRight) {
                    // make the parent touch interception active -> parent pager can swipe
                    this.getParent().requestDisallowInterceptTouchEvent(false);
                } else {
                    /*
                     * if the first swipe was from right to left, dont listen to swipes
                     * from left to right. this fixes glitches where the user first swipes
                     * right, then left and the scrolling state gets reset
                     */
                    this.slidingRight = true;

                    // save the current x position
                    this.lastX = ev.getX();
                    this.getParent().requestDisallowInterceptTouchEvent(true);
                }
            } else
            /*
             * if this is the last standing_item, scrolling from right to
             * left should navigate in the surrounding ViewPager
             */
                if (this.getCurrentItem() == this.getAdapter().getCount() - 1) {
                    // swiping from right to left (<-)?
                    if (this.lastX >= ev.getX() && !this.slidingLeft) {
                        // make the parent touch interception active -> parent pager can swipe
                        this.getParent().requestDisallowInterceptTouchEvent(true);
                    } else {
                    /*
                     * if the first swipe was from left to right, dont listen to swipes
                     * from right to left. this fixes glitches where the user first swipes
                     * left, then right and the scrolling state gets reset
                     */
                        this.slidingLeft = true;

                        // save the current x position
                        this.lastX = ev.getX();
                        this.getParent().requestDisallowInterceptTouchEvent(true);
                    }
                }

            break;
    }

    super.onTouchEvent(ev);
    return true;
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int height = 0;
    for(int i = 0; i < getChildCount(); i++)
    {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if(h > height) height = h;
    }

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

在我的适配器内充气

        if (groupPosition == 0) {
        final HeaderHolder viewHolder;
        convertView = mInflater.inflate(R.layout.season_team_fixtures_item, null);
        viewHolder = new HeaderHolder();
        viewHolder.customIndicator = (CircleIndicator) convertView.findViewById(R.id.indicator_custom);
        viewHolder.viewPager = (CustomPager) convertView.findViewById(R.id.pager);
        viewHolder.seeAll = (TextView) convertView.findViewById(R.id.AllFixtures);
        viewHolder.viewPager.setAdapter(adapter);
        viewHolder.customIndicator.setViewPager(itemView.viewPager);

season_team_fixtures_item 布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/appBackground">

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp">

    <ProgressBar
        android:id="@+id/linlaHeaderProgress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:indeterminate="true"
        android:indeterminateDrawable="@drawable/progress_drawable"
        android:visibility="gone" />

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:descendantFocusability="blocksDescendants"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical">

        <com.redeagle07.egyptianleague.teams.season.CustomPager
            android:id="@+id/pager"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/white_frame"
            android:descendantFocusability="blocksDescendants"
            android:focusable="true"
            android:focusableInTouchMode="true" />

        <me.relex.circleindicator.CircleIndicator
            android:id="@+id/indicator_custom"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="4dp"
            android:background="@android:color/white"
            app:ci_animator="@animator/indicator_animator"
            app:ci_animator_reverse="@animator/indicator_animator_reverse"
            app:ci_drawable="@drawable/black_radius_square" />

        <TextView
            android:id="@+id/AllFixtures"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/white_frame"
            android:gravity="end"
            android:paddingLeft="2dp"
            android:textColor="@color/ColorPrimaryDark"
            android:paddingRight="2dp"
            android:text="@string/Allfixtures"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="15sp" />
    </LinearLayout>
</android.support.v7.widget.CardView>

感谢帮助,提前感谢

【问题讨论】:

  • 看看下面的例子,它有效:stackoverflow.com/questions/24083886/…。如果对您有帮助,请为答案投票。
  • @codedByMi 你了解ViewPager inside ExpandableListViewExpandableListView inside ViewPager
  • 我只是想帮忙

标签: android


【解决方案1】:

在父 FrameLayout 中使用 android:descendantFocusability="blocksDescendants" 而不是线性布局

【讨论】:

    猜你喜欢
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多