0

我有一个自定义项目的 ListView(或 GridView,它取决于屏幕大小),每个项目都包含 SeekBar。我正在设置 OnSeekBarChangeListener ,问题是,onStartTrackingTouch 在释放按钮或更改拇指位置后触发(应该在按下后)。我认为 ListView 或 ListView 行正在窃取事件,但我尝试了许多不同的解决方案,但没有任何效果。

我什至不想使用 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>

4

2 回答 2

0

经过一番挖掘和测试,我发现这不是错误或错误。

首先,用户应该能够毫无问题地滚动列表。因此,如果他将手指放在搜索栏上,但没有水平移动,他可以滚动列表,因为搜索栏的事件还没有被触发。这就是为什么它的行为如此。

其次,行为因 API 版本而异。我测试了 API 7、8 和 10,并在它们上立即触发onStartTrackingTouch。但在 API 16 上,第一个是列表滚动。

总之:我想要实现的行为是不良行为。用户应该能够轻松滚动列表。我宁愿想,如何改变较低 API 的行为,但这不是这个问题的主题。

于 2013-07-10T07:05:53.073 回答
0

如果您希望行项目单击和拇指进度更改,请在行项目布局中添加 android:descendantFocusability="blocksDescendants"。

list_row.xml

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

}

活动视图.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);

        }

    });

我试图告诉你我从你的任务中理解的 whtevr……如果需要,请发表评论。并且onStartTrackingTouch在拇指更改时被调用,而onStopTrackingTouch在我释放拇指时被调用。

于 2013-07-09T08:47:41.770 回答