如何在配置更改时保存和恢复NetworkImageView和Textview

时间:2016-04-16 22:00:57

标签: android android-volley networkimageview android-savedstate

我使用volley从网上获取数据并在TextViews和NetworkImageView中显示结果。

我成功保存并恢复了textview的状态,但是遇到了NetworkImageView的问题。

FruitDetails

public class FruitDetails extends AppCompatActivity {

    private final String TAG = "FruitDetails";


    TextView fruitTitle, fruitContent;
    NetworkImageView authorImg;
    ImageLoader AuthImgLoader;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fruit_details);

        fruitTitle = (TextView) findViewById(R.id.dfruit_title);
        fruitContent = (TextView) findViewById(R.id.dfruit_content);
        authorImg = (NetworkImageView) findViewById(R.id.author_img);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        if (savedInstanceState != null) {
            fruitTitle.setText(savedInstanceState.getString("fruitTitle"));
            fruitContent.setText(savedInstanceState.getString("fruitContent"));
        } else {

                loadFruit();

        }
    }

    private void loadFruit() {
        Log.d(TAG, "loadFruit called");

        final ProgressBar progressBar;
        progressBar = (ProgressBar) findViewById(R.id.progress_circle);
        progressBar.setVisibility(View.VISIBLE);


        int news_id = getIntent().getIntExtra("FruitId", -1);
        Log.d(TAG, "You clicked fruit id " + fruits_id);

        final JsonObjectRequest jsonObjReq = new JsonObjectRequest( DetailConfig.GET_DURL + news_id, null,


                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("Debug", response.toString());

                        //Dismissing progressbar;
                        if (progressBar != null) {
                            progressBar.setVisibility(View.GONE);
                        }

                        //Calling method to parse json array
                        parseFruit(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d("", "Error: " + error.getMessage());
                    }
                });

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to queue
        requestQueue.add(jsonObjReq);
    }

    //This method will parse json data of fruit
    private void parseFruit(JSONObject jsonObject) {

        Log.d(TAG, "Parsing fruit array");


            try {
                String title = jsonObject.getString(DetailConfig.TAG_DFRUIT_TITLE);
                fruitTitle.setText(Html.fromHtml(title));

                JSONObject pAuthor = jsonObject.getJSONObject("author");
                String authorimg = pAuthor.getString("avatar");

                AuthImgLoader = VolleyRequest.getInstance(getApplicationContext()).getImageLoader();
                AuthImgLoader.get(authorimg, ImageLoader.getImageListener(authorImg, R.drawable.ic_author, R.drawable.ic_author));
                authorImg.setImageUrl(authorimg, AuthImgLoader);


                String content = jsonObject.getString(DetailConfig.TAG_DFRUIT_CONTENT);
                fruitContent.setText(Html.fromHtml(content));

            } catch (JSONException w) {
                w.printStackTrace();
            }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("fruitTitle", fruitTitle.getText().toString());
        outState.putString("fruitContent", fruitContent.getText().toString());
        //outState.putInt("authorImg", authorImg // I got into problem here
    }

}

虽然我根本无法恢复NetworkImageView的那个,但是fruitContent有一个异常现象:当我旋转屏幕时,文本的样式(例如粗体)就会丢失,即&#39; s恢复正常。

修改 我发现造型丢失的原因是因为我正在设置并将其作为字符串;无论如何我可以使用Parcelables吗?

1 个答案:

答案 0 :(得分:0)

将url保存到onSaveInstanceState中的包中,并在onRestoreInstanceState中将其还原。这假设图像不需要将旋转更改为更合适尺寸的图像。