簡體   English   中英

ScrollView 應該滾動到特定的 TextView 在 Spinner 中選擇相同的名稱時

[英]ScrollView should scroll to a particular TextView While selecting the same name in Spinner

在我的應用程序中,當我在Spinner中選擇一個文本時,我想滾動我的ScrollView到一個特定的TextView ,其中包含與使用 ZD52387880E1EA22817A72D375921381 在 Android Studio 中選擇的Spinner文本相同的文本。 (您也可以檢查圖像)。 在這里,我的微調器是動態的,我的文本視圖也是動態的。 在此處輸入圖像描述

我的代碼 - 對於 Spinner

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM