2013-07-09 2 views
0

각기 SeekBar가 들어있는 ListView (또는 GridView, 화면 크기에 따라 다름) 사용자 지정 항목이 있습니다. OnSeekBarChangeListener를 설정 중이며 문제는 버튼을 놓은 후 또는 엄지 위치를 변경 한 후에 onStartTrackingTouch가 실행된다는 것입니다 (프레스 이후이어야 함). 나는 ListView, 또는 ListView의 행이 이벤트를 도용하고 있다고 생각했지만 많은 다른 솔루션을 시도했지만 아무런 효과가 없었다.ListView의 SeekBar가 onStartTrackingTouch에 올바르게 응답하지 않습니다.

onStartTrackingTouch도 사용하고 싶지 않습니다. 이 작업이 필요한 이유는 사용자가 엄지 손가락을 누를 때 상태가 바뀌므로 (사용자가 엄지 손가락을 드래그하기 시작했음을 알기 때문입니다). 이제 엄지 손가락은 위치가 바뀌었을 때만 상태를 변경합니다.

MainActivity.java

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ListView list = (ListView) findViewById(R.id.seekbarList); 
    list.setAdapter(new Adapter()); 

    SeekBar sb = (SeekBar) findViewById(R.id.seekBarOutsideTheList); 
    sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 
      ((TextView) findViewById(R.id.outsideListLbl)) 
        .setText("onStop"); 
      Log.d("seekBarOutsideTheList", "onStop"); 

     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 
      ((TextView) findViewById(R.id.outsideListLbl)) 
        .setText("onStart"); 
      Log.d("seekBarOutsideTheList", "onStart"); 

     } 

     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, 
       boolean fromUser) { 
      ((TextView) findViewById(R.id.outsideListLbl)) 
        .setText("onChange"); 
      Log.d("seekBarOutsideTheList", "onChange"); 

     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public class Adapter extends BaseAdapter { 
    LayoutInflater layoutInflter; 

    public Adapter() { 
     layoutInflter = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    class ViewHolder { 
     TextView label; 
     SeekBar seek; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = layoutInflter.inflate(R.layout.list_row, null); 
      ViewHolder viewHolder = new ViewHolder(); 
      viewHolder.label = (TextView) convertView 
        .findViewById(R.id.insideListLbl); 
      viewHolder.seek = (SeekBar) convertView 
        .findViewById(R.id.seekbarInLIst); 
      convertView.setTag(viewHolder); 
     } 

     ViewHolder viewHolder = (ViewHolder) convertView.getTag(); 

     viewHolder.seek 
       .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 

        @Override 
        public void onStopTrackingTouch(SeekBar seekBar) { 
         ((TextView) findViewById(R.id.insideListLbl)) 
           .setText("onStop"); 
         Log.d("seekBarInsideTheList", "onStop"); 
        } 

        @Override 
        public void onStartTrackingTouch(SeekBar seekBar) { 
         ((TextView) findViewById(R.id.insideListLbl)) 
           .setText("onStart"); 
         Log.d("seekBarInsideTheList", "onStart"); 
        } 

        @Override 
        public void onProgressChanged(SeekBar seekBar, 
          int progress, boolean fromUser) { 
         ((TextView) findViewById(R.id.insideListLbl)) 
           .setText("onChange"); 
         Log.d("seekBarInsideTheList", "onChange"); 
        } 
       }); 

     return convertView; 
    } 

    @Override 
    public int getCount() { 
     return 1; 
    } 

    @Override 
    public Integer getItem(int arg0) { 
     return arg0; 
    } 

} 

} 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SeekBar outside the list" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <SeekBar 
      android:id="@+id/seekBarOutsideTheList" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <TextView 
      android:id="@+id/outsideListLbl" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

    </LinearLayout> 

</LinearLayout> 

<ListView 
    android:id="@+id/seekbarList" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

list_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="SeekBar inside the list" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <SeekBar 
     android:id="@+id/seekbarInLIst" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <TextView 
     android:id="@+id/insideListLbl" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

</LinearLayout> 

답변

0

파기 및 테스트를 한 후 버그 또는 오류가 아닌 것으로 나타났습니다.

우선, 사용자는 아무 문제없이 목록을 스크롤 할 수 있어야합니다. 따라서 seekbar에 손가락을 대고 가로로 움직이지 않으면 seekbar의 이벤트가 실행되지 않았기 때문에 목록을 스크롤 할 수 있습니다. 그것이 바로 이런 식으로 행동하는 이유입니다.

둘째, API의 버전에 따라 동작이 다릅니다. API 7, 8 및 10을 테스트했으며 그 중 onStartTrackingTouch이 즉시 실행됩니다. 그러나 API 16에서 첫 번째는 목록 스크롤입니다.

결론 : 구현하려는 동작이 좋지 않습니다. 사용자는 목록을 쉽게 스크롤 할 수 있어야합니다. 오히려 더 낮은 API에서 동작을 변경하는 방법을 생각해야합니다.하지만이 질문의 주제는 아닙니다.

0

안드로이드를 추가 descendantFocusability = "블록을 ksDescendants "행 항목 레이아웃에서 두 행 항목을 모두 클릭하려는 경우 & 엄지 진행 변경.

list_row.xml 난 유 내가 당신의 탐구에서 이해 whtevr ans와 주려고 노력

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:descendantFocusability="blocksDescendants" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/lbl" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

<SeekBar 
    android:id="@+id/seekbar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:max="100" 
    android:progress="50" /> 

AdapterSeek.java

LayoutInflater layoutInflter; 

    /** 
    * 
    */ 
    public AdapterSeek() { 
     layoutInflter = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.widget.Adapter#getCount() 
    */ 
    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return seekbarval.length; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.widget.Adapter#getItem(int) 
    */ 
    @Override 
    public Integer getItem(int position) { 
     // TODO Auto-generated method stub 
     return seekbarval[position]; 
    } 

    class PlaceHolder { 
     TextView labell; 
     SeekBar seek; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.widget.Adapter#getItemId(int) 
    */ 
    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see android.widget.Adapter#getView(int, android.view.View, 
    * android.view.ViewGroup) 
    */ 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     PlaceHolder placeHolder; 
     if (convertView == null) { 
      convertView = layoutInflter.inflate(R.layout.list_row, null); 
      placeHolder = new PlaceHolder(); 
      placeHolder.labell = (TextView) convertView 
        .findViewById(R.id.lbl); 
      placeHolder.seek = (SeekBar) convertView 
        .findViewById(R.id.seekbar); 
      convertView.setTag(placeHolder); 
     } else 
      placeHolder = (PlaceHolder) convertView.getTag(); 
     placeHolder.labell.setText(" No. " + position); 
     placeHolder.seek.setProgress(seekbarval[position]); 
     placeHolder.seek 
       .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 

        @Override 
        public void onStopTrackingTouch(SeekBar seekBar) { 
         Log.e("onStopTrackingTouch", 
           "" + seekBar.getProgress()); 
        } 

        @Override 
        public void onStartTrackingTouch(SeekBar seekBar) { 
         Log.e("onStartTrackingTouch", 
           "" + seekBar.getProgress()); 
        } 

        @Override 
        public void onProgressChanged(SeekBar seekBar, 
          int progress, boolean fromUser) { 
         if (fromUser) 
          Log.e("onProgressChanged", "" + progress); 
        } 
       }); 
     return convertView; 
    } 

} 

ActivityView.java

private int[] seekbarval = new int[] { 100, 20, 80, 50, 40, 10, 0, 70, 30, 
     90, 15, 35, 65, 75, 70, 100, 25, 89 }; 

/* 
* (non-Javadoc) 
* 
* @see android.app.Activity#onCreate(android.os.Bundle) 
*/ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.testscreen); 
    list = (ListView) findViewById(R.id.seekbarList); 
    list.setAdapter(new AdapterSeek()); 
    list.setOnItemClickListener(new OnItemClickListener() { 

     /* 
     * (non-Javadoc) 
     * 
     * @see 
     * android.widget.AdapterView.OnItemClickListener#onItemClick(android 
     * .widget.AdapterView, android.view.View, int, long) 
     */ 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      Log.e("onItemClick", "" + arg2); 

     } 

    }); 

... 떠나라. req 경우 주석. 및 onStartTrackingTouch이 엄지 손가락으로 변경 될 때 호출됩니다. & onStopTrackingTouch은 엄지 손가락을 뗄 때 호출됩니다.

+0

답장을 보내 주셔서 감사합니다.하지만 말씀대로 : "onStartTrackingTouch는 엄지 손가락 변경에 호출됩니다." 이건 내가 원하는 행동이 아니야. ListView가 아닌 ​​단일 SeekBar를 사용하면 엄지 손가락을 눌렀을 때 StartTrackingTouch가 실행됩니다.목록에서 원하는이 기본 동작 및 왜 다르게 동작하는지 알 수 없습니다. –

관련 문제