当我更改搜索条时,onProgressChanged方法没有调用?

时间:2016-08-31 08:39:18

标签: android android-seekbar

以下是我正在使用的代码:

import android.support.v4.app.Fragment; 
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;

/**
 * Created by Arsalan on 8/29/2016.
 */
public class ToolbarFragmnet extends Fragment implements        
SeekBar.OnSeekBarChangeListener {

    @Nullable
    private  static int s=10;
    private  static EditText editText;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.toolbar_fragment, container, false);
        editText = (EditText) view.findViewById(R.id.edittext1);
        final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekBar1);
        final Button button = (Button) view.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buttonClicked(view);
            }
        });
        return view;
    }

    ToolbarListner callBackActivity;
    public interface ToolbarListner {
        public void onButtonClick(int position, String text);
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            callBackActivity = (ToolbarListner) context;
        } catch (ClassCastException c) {
            throw new ClassCastException(context.toString() + "must be implement");
        }
    }

    private void buttonClicked(View view) {

        callBackActivity.onButtonClick(s,editText.getText().toString());

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        s= i
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}

2 个答案:

答案 0 :(得分:1)

在将listener设置为seekbar

之前,它永远不会被调用

将他放入onCreateView()方法

seekBar.setOnSeekBarChangeListener(this);

答案 1 :(得分:0)

在onCreateView

中替换它
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

    @Override       
    public void onStopTrackingTouch(SeekBar seekBar) {      
        // TODO Auto-generated method stub      
    }       

    @Override       
    public void onStartTrackingTouch(SeekBar seekBar) {     
        // TODO Auto-generated method stub      
    }       

    @Override       
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {     
        // TODO Auto-generated method stub      

      s= i

    }