在我的应用程序中,当我要在ScrollView中选择文本时,要滚动我的Spinner到一个特定的TextView,该文本包含与使用Java的Android Studio中选定的Spinner文本相同的文本。 (您也可以检查图像)。在这里,我的微调器是动态的,而我的文本视图也是动态的。java - ScrollView应该滚动到特定的TextView,同时在Spinner中选择相同的名称-LMLPHP

我的代码-适用于微调

private void spinnerbind() {
        JSONObject request = new JSONObject();
        try {
            request.put("action", "get_spinner");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsArrayRequest = new JsonObjectRequest
                (Request.Method.POST, url,request, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {

                            JSONArray dataArray  = response.getJSONArray("data");
                            if(response.optString("status").equals("1")){

                                goodModelArrayList = new ArrayList<>();
                                //JSONArray dataArray  = obj.getJSONArray("data");

                                for (int i = 0; i < dataArray.length(); i++) {

                                    PlayerModel playerModel = new PlayerModel();
                                    JSONObject dataobj = dataArray.getJSONObject(i);

                                    playerModel.setid(dataobj.getString("id"));
                                    playerModel.setTitle(dataobj.getString("specialist"));

                                    goodModelArrayList.add(playerModel);

                                }

                                for (int i = 0; i < goodModelArrayList.size(); i++){
                                    names.add(goodModelArrayList.get(i).getTitle().toString());
                                }
                                spinnerArrayAdapter = new ArrayAdapter<String>(DoctorsActivity.this, simple_spinner_item, names);
                                spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
                                mySpinner.setAdapter(spinnerArrayAdapter);
                                mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
                                    {
                                        PlayerModel playerModel = goodModelArrayList.get(position);
                                        myid=playerModel.getId();
                                        scrollView.scrollTo(0, 200);

                                    }
                                    public void onNothingSelected(AdapterView<?> parent) {
                                    }
                                });

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                //displaying the error in toast if occurrs
                                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });

        MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
    }


我的代码-用于文本绑定

public void detailsbind(){
        JSONObject docrequest = new JSONObject();
        try {
            myrequest.put("action", "get_doctors");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsArrayRequest = new JsonObjectRequest
                (Request.Method.POST, url,myrequest, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            if(response.optString("status").equals("1")){
                                // Toast.makeText(getApplicationContext(),response.getString("data"), Toast.LENGTH_SHORT).show();
                                JSONArray dataSpecArray  = response.getJSONArray("data");
                                LayoutInflater inflater = getLayoutInflater();
                                for (int k = 0; k < dataSpecArray.length(); k++) {
                                    PlayerModel Specialist = new PlayerModel();
                                    JSONObject specJSONobj = dataSpecArray.getJSONObject(k);
                                    TextView text1 = new TextView(MyActivity.this);
                                    text1.setText(specJSONobj.getString("specialist"));
                                        mainlayout.addView(text1);

                                }

                            }else{
                                Toast.makeText(DoctorsActivity.this, response.optString("message"), Toast.LENGTH_SHORT).show();

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        pDialog.dismiss();

                        //Display error message whenever an error occurs
                        Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();

                    }
                });

        // Access the RequestQueue through your singleton class.
        MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
    }

最佳答案

scrollview.scrollTo(0, getRelativeTop(textView))

private int getRelativeTop(View textView) {
    if (textView.getParent() == textView.getRootView())
        return textView.getTop();
    else
        return textView.getTop() + getRelativeTop((View) textView.getParent());
}

10-08 15:05