Android列表视图刷新闪烁

时间:2016-05-26 07:45:23

标签: android listview

我有一个来自服务器的listview数据,当listview刷新眨眼时如何使其listview没有闪烁。 这是我的listview代码。我使用处理程序刷新listview

list = (ListView) findViewById(R.id.listView);
    personList = new ArrayList<HashMap<String, String>>();
    adapter = new SimpleAdapter(
            DataSendActivity.this, personList, R.layout.layout_chat,
            new String[]{TAG_DATA, TAG_CREATED_AT},
            new int[]{R.id.data, R.id.created}
    );
    list.setAdapter(adapter);

   final Handler handler = new Handler();
    handler.postDelayed( new Runnable() {

        @Override
        public void run() {
            //showList();
            isInternetPresent = cd.isConnectingToInternet();
            if (isInternetPresent) {
                personList.clear();
                try{
                    getData();
                }catch (Exception e){}

                adapter.notifyDataSetChanged();
                list.invalidateViews();
            } else {
                showAlertDialog(DataSendActivity.this, "No Internet Connection",
                        "You don't have internet connection.", false);
            }

            //list.refreshDrawableState();
            handler.postDelayed( this, 8000 );
        }
    }, 8000 );

这是显示数据的功能。

    protected void showList() {
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            peoples = jsonObj.getJSONArray(TAG_RESULTS);

            for (int i = 0; i < peoples.length(); i++) {
                JSONObject c = peoples.getJSONObject(i);
                String data = c.getString(TAG_DATA);
                String created_at = c.getString(TAG_CREATED_AT);
// final String dataaaa = rcdata.getText().toString().trim();
                HashMap<String, String> user_data = new HashMap<String, String>();
                user_data.put(TAG_DATA, data);
                user_data.put(TAG_CREATED_AT, created_at);


                personList.add(user_data);

            }
            try{
                adapter.notifyDataSetChanged();
            }catch (Exception e){

            }

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

    }

这是getdata代码。

public void getData() {
    class GetDataJSON extends AsyncTask<String, Void, String> {
        //String recID = ;
        //String userID = email;
        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HashMap<String, String> user = db.getUserDetails();

            //String name = user.get("name");
           // Semail = user.get("email");

            String semail  = user.get("email");
            final String remail = rremail;

            HttpPost httppost = new HttpPost("http://samplechatapp.gear.host/myphpfile.php?sender_email="+semail+"&reciver_email="+remail+"&fatch_server_data=true");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            Log.i("","processing entity");
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                    Log.i("",line);
                }
                result = sb.toString();
                Log.i("",result);
            } catch (Exception e) {
                // Oops
            } finally {
                try {
                    if (inputStream != null) inputStream.close();
                } catch (Exception squish) {
                }
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            myJSON = result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}

listview工作正常,但问题是当listview刷新它的闪烁我想要listview刷新而不闪烁。提前谢谢

1 个答案:

答案 0 :(得分:0)

list.invalidateViews(); - 这是为了什么?它会重新绘制视图 - 可能会眨眼。

adapter.notifyDataSetChanged(); - 这个更好的调用ui thread runOnUIThread

更新列表后,连接到适配器的U只需要调用 adapter.notifyDataSetChanged();

下面:

personList.clear();
try{
    getData();
}catch (Exception e){}
adapter.notifyDataSetChanged();
list.invalidateViews();

在加载数据之前调用notify和invalidate,因为你启动asynctask,所以你在加载数据之前更新listview