2012-06-30 269 views
6

我有一个ListView项目布局,使用中心的HorizontalScrollView。我在我的父母LinearLayout上使用了android属性“android:descendantFocusability="blocksDescendants"”,因此ListView项目仍可选。ListView项中的水平滚动视图?

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

我如何获得HorizontalScrollView的点击事件以调用ListView列表项单击事件?

+0

工作,请使用try catch blog surroung listview点击事件并在日志中打印错误,并在这里发布我们的错误,以便我们能够快速地知道发生了什么问题。 – Aamirkhan

+0

我没有任何错误......问题在于如何以这种方式设置视图的行为。 – startupsmith

+0

好的,你可以使用自定义adatper来发布你的xml文件吗? – Aamirkhan

回答

1

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

它支持的手势,并有“的onTouchEvent(MotionEvent EV)”

所以,你可以使用它作为点击。请参阅我准备的followin演示。

//  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; 
      } 
     }); 
+0

随着onTouch的事件触发每一次触摸?我仍然希望能够滚动Horizo​​ntalScrollView。只有在用户点击/点击Horizo​​ntalScrollView的情况下,我是否希望它能够处理正常的listview项目点击行为。 – startupsmith

+0

请原谅,我没有正确理解你。如果你有时间,你能详细解释一下吗? –

0

添加布尔变量着陆和润色的适配器类如下似乎运作得相当好:

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; 
       } 
     }); 
    } 
} 

它远非完美,但也应根据大多数标准使用情况