【问题标题】:Horizontal Scrollview inside ListView item?ListView 项内的水平滚动视图?
【发布时间】:2014-07-11 05:46:27
【问题描述】:

我有一个ListView 项目布局,它在中心使用HorizontalScrollView。我在我的父 LinearLayout 上使用了 android 属性“android:descendantFocusability="blocksDescendants"”,因此 ListView 项目仍然可以选择。

我遇到的问题是,当单击 ListView 项目的部分 HorizontalScrollView 时,不会调用 ListView 项目单击事件。

如何获取HorizontalScrollView的点击事件调用ListView列表项点击事件?

【问题讨论】:

  • 请使用 try catch blog surroung listview click event 并在日志中打印错误并在此处发布您的错误,以便我们可以准确地知道出了什么问题。
  • 我没有任何错误...问题与视图的行为有关,因为它们是以这种方式设置的。
  • 好的,您可以将您的 xml 文件发布为您使用的自定义 adatper 吗?

标签: android android-layout


【解决方案1】:

Horizo​​ntalScrollView 没有 "onClick()",看这个 http://developer.android.com/reference/android/widget/HorizontalScrollView.html

它支持手势并且有"onTouchEvent(MotionEvent ev)"

因此您可以将其用作点击。请看我准备的followin demo。

//      Followin  code will not work  for HorizontalScrollView
        /*hsv1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(HorizontalListActivity.this, tvMiddle.getText().toString().trim(), Toast.LENGTH_SHORT).show();
            }
        });*/

        hsv1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Toast.makeText(YourActivity.this, "Your Msg", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

【讨论】:

  • 使用 onTouch 每次触摸都会触发事件?我仍然希望能够滚动 Horizo​​ntalScrollView。只有在用户单击/点击 Horizo​​ntalScrollView 的情况下,我才希望它与正常的列表视图项单击行为一起行动。
  • 对不起,我没有正确理解你。如果你有时间,能不能详细解释一下。
【解决方案2】:

如下向适配器类添加布尔变量 touchDown 和 touchUp 似乎效果很好:

private class MyListAdapter extends ArrayAdapter<MyObject>{
    ...
    //touch down + touch up with no other motion events in between = click
    boolean touchDown = false;
    boolean touchUp = false;
    private int iHostViewID;
    ...

    public MyListAdapter(Context context,int viewResourceId, List<MyObject> objects) {
        super(context, textViewResourceId, objects);
        iHostViewID = viewResourceId;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        View itemView = convertView;

        //iff we cannot re-use a view
        if(itemView == null){
            LayoutInflater inflater = (
            (LayoutInflater)hContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemView = inflater.inflate(iHostViewID, null);
        }

        final View hItemView = itemView;
        final int hPosition = pos;
        ...
        final HorizontalScrollView textDataSV = 
        (HorizontalScrollView)itemView.findViewById(R.id.widget_hsv);
        textDataSV.setOnTouchListener(new OnTouchListener(){

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    if(event.getAction() == MotionEvent.ACTION_DOWN){

                        touchDown = true;
                    }
                    else if(event.getAction() == MotionEvent.ACTION_UP){

                        touchUp = true;
                     }

                    else{

                        touchDown = false;
                        touchUp = false;
                    }

                    if(touchDown && touchUp){
                        //click 
                        //mMyListView is the reference to the list view
                        //instantiated in the view controller class responsible
                        //for setting this adapter class as the list view's adapter
                        mMyListView.performItemClick(hItemView, hPosition, 
                        hItemView.getId());
                    }



                    return false;
                 }
         });
    }
}

它远非完美,但应该适用于大多数标准用例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2013-07-18
    • 1970-01-01
    相关资源
    最近更新 更多