可以处理适配器但不可滚动的视图

时间:2014-02-10 06:11:28

标签: android android-view android-adapter android-adapterview

是否存在可以处理适配器(例如ArrayAdapter)但不可滚动的视图?

我不想进入解决方法(例如禁用ListView的滚动功能)。

3 个答案:

答案 0 :(得分:1)

当然可以。在适配器上调用getView将返回与适配器的View方法提供的信息相对应的getItem。所以你可以这样做:

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear_layout);
BaseAdapter adapter = new CustomAdapter(context, itemList);

for (int i = 0; i < itemList.size(); i++) {
    // you can pass in a recycled view instead of null
    View itemView = adapter.getView(i, null, layout);
    layout.addView(itemView);
}

LinearLayout之类的ViewGroups本身不可滚动,所以这应该可行。我不确定你为什么要在这种情况下使用Adapter,但这取决于你。

答案 1 :(得分:0)

您可以使用以下内容或参考上述链接

listView.setScrollContainer(false);

参考文献: Disable scrolling of a ListView contained within a ScrollView

Disable scrolling in listview

答案 2 :(得分:0)

// try this wau
1. Create your custom Listview like below and use it rather default ListView

public class MyCustomListView extends ListView {

    @SuppressWarnings("unused")
    private static final int SWIPE_MIN_DISTANCE = 50;
    @SuppressWarnings("unused")
    private static final int SWIPE_THRESHOLD_VELOCITY = 100;
    @SuppressWarnings("unused")
    private GestureDetector gDetector;
    @SuppressWarnings("unused")
    private boolean isFling;

    private float mDiffX;
    private float mDiffY;
    private float mLastX;
    private float mLastY;

    /**
     * Overrides method
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // reset difference values
            mDiffX = 0;
            mDiffY = 0;

            mLastX = ev.getX();
            mLastY = ev.getY();
            break;

        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            mDiffX += Math.abs(curX - mLastX);
            mDiffY += Math.abs(curY - mLastY);
            mLastX = curX;
            mLastY = curY;

            // don't intercept event, when user tries to scroll vertically
            if (mDiffX > mDiffY) {
                return false; // do not react to horizontal touch events, these
                                // events will be passed to your list item view
            }
        }

        return super.onInterceptTouchEvent(ev);
    }

    public MyCustomListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public MyCustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyCustomListView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context mContext) {
    }
}