简体   繁体   中英

listview is jerking on scrolling

i hv an application in which i am getting data from api,when data get loads and i tried to scroll the listview it get starts jerking,i tried to find the solution but get nothing.please help me to sort it out.

InboxActivity.java

    list=(ListView)rootView.findViewById(R.id.list);
     catagery = new ArrayList<ProfileInbox>();

     String fontPath = "font/Roboto-Regular.ttf";

    // Loading Font Face
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), fontPath);
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View arg1,
                int position, long id) {
            // TODO Auto-generated method stub
            //arg0.getp.setBackgroundColor(Color.WHITE);
            //parent.getChildAt(position).setBackgroundColor(Color.BLUE);

         IsRead=true;
         catagery.get(position).setRead(IsRead);


            new Task().execute(url);

            adapter.notifyDataSetChanged();
            msg=catagery.get(position).getMessage();
            dateFrom=catagery.get(position).getSentDate();
            sub=catagery.get(position).getSubject();
            Intent i= new Intent(getActivity(),InboxDetail.class);
            startActivity(i);

        }
    });

    return rootView;
}


 private void loadNextPageOfReviews()
 {
  page_no_count += 1;



  new JSONAsyncTask().execute(loadMoreUrl);
 }  

class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Please Wait, Loading...");

        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            // ------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("inbox");

                String unread_msg= jsono.getString("unread_msg");
                Log.i("unreadMsg", unread_msg);

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject c = jarray.getJSONObject(i);
                    ProfileInbox category = new ProfileInbox();


                    String id = c.getString("msg_id");

                    String sub = c.getString("subject");
                    String name = c.getString("message");
                    String imageSetter=c.getString("sent_on");
                    //Log.i("id", id);
                    //Log.i("name", name);
                    //Log.i("imageSetter", imageSetter);

                    category.setMsgId(((JSONObject) c).getString("msg_id"));
                    if(unread_msg.contains(id)){
                        category.setRead(false);
                    }
                    else{
                        category.setRead(true);
                    }
                    category.setSubject(((JSONObject) c).getString("subject"));
                    category.setMessage(((JSONObject) c).getString("message"));
                    category.setSentDate(((JSONObject) c).getString("sent_on"));
                    //Log.i("category", category.toString());

                    catagery.add(category);
                    //Log.i("category", category.toString());
                }
                return true;
            }

            // ------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        dialog.cancel();

        if (result == false){
            Toast.makeText(getActivity(),
                    "Unable to fetch data from server", Toast.LENGTH_LONG)
                    .show();
            }
        else if(Blank.notice.equals("true")){

                msg=catagery.get(0).getMessage();
                dateFrom=catagery.get(0).getSentDate();
                sub=catagery.get(0).getSubject();
                adapter = new InboxAdaptor(getActivity(),
                        catagery);
                list.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                Intent i= new Intent(getActivity(),InboxDetail.class);
                startActivity(i);
            }
        else if(Blank.notice.equals("false"))
            {
                //adapter.notifyDataSetChanged();
            adapter = new InboxAdaptor(getActivity(),
                    catagery);
            list.setAdapter(adapter);
            adapter.notifyDataSetChanged();

            }

    }

InboxAdapter

public class InboxAdaptor extends BaseAdapter {
    private List<ProfileInbox> originalData;
    private List<ProfileInbox> filteredData;
    private Context context;
    public static String url;
    public static String bussinessId;
    public InboxAdaptor(Context context, ArrayList<ProfileInbox> Data) {
        this.context = context;
        this.originalData = Data;
        //Log.i("originalData", Data.toString());
        filteredData = new ArrayList<ProfileInbox>();
        filteredData.addAll(this.originalData);
        //Log.i("filterData", filteredData.toString());


    }

    @Override
    public int getCount() {
        return filteredData.size();
    }

    @Override
    public Object getItem(int position) {
        return filteredData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.inboxlist, null,false);
            holder.coloredlay=(RelativeLayout)convertView.findViewById(R.id.coloredlay);
            holder.txtWelcom = (TextView) convertView.findViewById(R.id.txtWelcom);
            holder.dateTime = (TextView) convertView.findViewById(R.id.dateTime);
            holder.txtdetails = (TextView) convertView.findViewById(R.id.txtdetails);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }


        if(filteredData.get(position).getRead()==true)
        {

            holder.coloredlay.setBackgroundColor(Color.WHITE);
             notifyDataSetChanged();
        }else{
            holder.coloredlay.setBackgroundColor(Color.parseColor("#E5E4E2"));
        }

       // holder.img.setTag(position);
        String fontPath = "font/Roboto-Regular.ttf";

        // Loading Font Face
        Typeface tf = Typeface.createFromAsset(convertView.getContext().getAssets(), fontPath);





        holder.txtWelcom.setText(filteredData.get(position).getSubject());
        holder.txtWelcom.setTypeface(tf);
        holder.dateTime.setText(filteredData.get(position).getSentDate());
        holder.dateTime.setTypeface(tf);
        holder.txtdetails.setText(filteredData.get(position).getMessage());
        holder.txtdetails.setTypeface(tf);



 /*       if(Blank.notice.equals("true")){
            holder.coloredlay.setBackgroundColor(Color.WHITE);
             notifyDataSetChanged();
        }
        */
       notifyDataSetChanged();
        return convertView;
    }

    public static class ViewHolder {
        public RelativeLayout coloredlay;
        public TextView txtdetails;
        public TextView dateTime;
        public TextView txtWelcom;

    }

从getView()中删除notifyDataSetChanged() ,当您为适配器提供的数据已更改时,notifyDataSetChanged()将更新适配器,但是在更改列表视图时,您将使用该错误。

Remove notifyDataSetChanged() from getView().

You dot need that here. Rather have Remove notifyDataSetChanged() in onPostExecute() call back of JSONAsyncTask

Use cachecolorHint for come out of your solution:

<ListView
    android:id="@+id/listNewsMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="@android:color/transparent" >
</ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM