ListView项目中的横向滚动视图?

6

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

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

我该如何使HorizontalScrollView的点击事件调用ListView列表项的点击事件?


请在ListView的点击事件周围使用try-catch块,并将错误打印到日志中。请将您的错误发布在此处,以便我们确切地知道出了什么问题。 - Aamirkhan
我没有任何错误...问题与视图的行为有关,因为它们是以这种方式设置的。 - startupsmith
好的,您能发布您正在使用作为自定义适配器的XML文件吗? - Aamirkhan
2个回答

1

HorizontalScrollView没有"onClick()"方法,可以参考http://developer.android.com/reference/android/widget/HorizontalScrollView.html

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

因此,您可以将其用作点击事件。请参考我准备的以下演示。

//      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,每个触摸事件都会触发吗?我仍然希望能够滚动HorizontalScrollView。只有在用户点击/轻敲HorizontalScrollView的情况下,我希望它具有正常的listview项目单击行为。 - startupsmith
抱歉,我没有完全理解您的意思。如果您有时间,能否详细地向我解释一下? - Chintan Raghwani

0
将以下代码添加到适配器类中,添加布尔变量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;
                 }
         });
    }
}

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接