在Android中滚动时Listview项目会发生变化

时间:2017-01-03 09:41:33

标签: android listview notifydatasetchanged

我有一个列表视图,其中列表项的背景在通知到达时突出显示,但问题是当我滚动列表时,项目会更改突出显示。假设当我向下滚动然后另一个项目突出显示时,最初列出了第一个项目。我也使用了notifysetdatachanged。

列表活动代码

 public class NewPageActivity extends Fragment{

//当通知到达时,这是我的通知代码

@Override
public void onResume() {
    super.onResume();


    IntentFilter intentFilter = new IntentFilter(
            "unique_name");

    mMessageReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //extract our message from intent
            String msg_for_me = intent.getStringExtra("message");
            //log our message value

            Log.e("TAG", "onReceive: boradcast" + msg_for_me);
            if (msg_for_me.contains("&")) {

                String msg1[] = msg_for_me.split("&");

                if (msg1[0].contains("Waiting for acceptance...")) {
                    edit.putInt("newTabHighlight", sp.getInt("newTabHighlight", 0) + 1);
                    edit.putString("newOrder", sp.getString("newOrder", null) + "&" + msg1[1].split("=")[1]);
                    edit.putInt("reminderTab", sp.getInt("reminderTab", 0) + 1);
                    edit.putInt("newOrderCount", 1);
                    edit.commit();

                    connectToDatabase();
                }

            }

//通过Web服务获取数据并设置适配器的代码

    if(getActivity()!= null) {
                        adapter = new customadapetr_new(dataModels, getActivity(), NewPageActivity.this);
                        list.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        setListViewHeightBasedOnChildren(list);
                    }

      }

//背景突出显示的适配器代码

 private void setNewCardBackground(final String Order_id, final RelativeLayout MenuCard, String OrderFlag)
{

    Log.e("TAG", "setNewCardBackground: " );

    newOrder = sp.getString("newOrder", "null");


    if (newOrder.contains("&")) {


        newOrderArray = newOrder.split("&");

        Log.e("TAG", "setNewCardBackground:order "+newOrderArray);

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


            if (Order_id.equals(newOrderArray[i])) {


                MenuCard.setBackgroundResource(R.drawable.highlightedorder);
                MenuCard.setPadding(20,10,20,10);
                newOrderlength++;


                break;
            }

//自定义适配器获取视图代码

      @Override
         public View getView(final int position, View vv, ViewGroup parent) {


    menuModels= new ArrayList<>();
    final ViewHolder viewHolder; // view lookup cache stored in tag
    final View result;
    final  DataModel data=dataSet.get(position);



    if (vv == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        vv = inflater.inflate(R.layout.adapter_listview_newpage_card, parent, false);

        viewHolder.rupee=(TextView)vv.findViewById(R.id.rupee);
        viewHolder.dateandtime=(TextView)vv.findViewById(R.id.dateandtime);
        viewHolder.modeofpayment=(TextView)vv.findViewById(R.id.modeofpayment);
        viewHolder.location=(TextView)vv.findViewById(R.id.location);
        viewHolder.customerName=(TextView)vv.findViewById(R.id.customerName);
        viewHolder.orderId = (TextView)vv.findViewById(R.id.orderId);
        viewHolder.order_time = (TextView) vv.findViewById(R.id.order_time);
        viewHolder.menucard = (RelativeLayout) vv.findViewById(R.id.menucard);
        viewHolder.accept = (ImageView) vv.findViewById(R.id.accept);
        viewHolder.statustext = (TextView)vv.findViewById(R.id.statustext);
        viewHolder.orderstatus = (TextView) vv.findViewById(R.id.orderstatus);
        viewHolder.order_card_layout =(RelativeLayout) vv.findViewById(R.id.order_card_layout);
        viewHolder.progressBar = (PlayGifView) vv.findViewById(R.id.progressBar);
        viewHolder.progressBar.setImageResource(R.drawable.renewedloader);
        viewHolder.showhide =(ImageView) vv.findViewById(R.id.showhide);
        viewHolder.comments = (TextView) vv.findViewById(R.id.comments);
        viewHolder.expanded_menulist =(ListView) vv.findViewById(R.id.menulist1);
        viewHolder.expanded_menulist.setVisibility(View.GONE);


        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        height = metrics.heightPixels;
        width = metrics.widthPixels;

        sp =mContext.getSharedPreferences("afewtaps", Context.MODE_PRIVATE);


        face = Typeface.createFromAsset(mContext.getAssets(), "fonts/Nunito-Regular.ttf");
        face1 = Typeface.createFromAsset(mContext.getAssets(), "fonts/Rupee_Foradian_2.ttf");

        int size = dataSet.size();


        String menu=data.getMenu();
        viewHolder.listview=(ListView)vv.findViewById(R.id.menulist);
        setListViewHeightBasedOnChildren(viewHolder.listview);

        String paynt = data.getPaymemt_method();
        final  String order_id = data.getOrder_id();
        final String Order_flag = data.getOrder_flag();
        setNewCardBackground(order_id, viewHolder.menucard,Order_flag );


        String orderEscalation = data.getOrder_escalated();
        setEscalation(viewHolder.statustext, viewHolder.accept, viewHolder.orderstatus, order_id, orderEscalation);
        int time = Integer.parseInt(data.getTimer_time());
        setTime(time, viewHolder.order_time);


        // menucard item

        comment_value = data.getOrder_comments();


        viewHolder.accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeListItem(viewHolder.order_card_layout, position);
                objNewPageActivity.SendOrder(data.getOrder_id());

            }
        });





        viewHolder.customerName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getCustomerProfile(order_id, mContext);
            }
        });


        try {


            JSONArray jsonArray1 = new JSONArray(menu);
            String orderMenu[][] = new String[jsonArray1.length()][4];

            int j;
            for (j = 0; j < jsonArray1.length(); j++) {
                menumodel = new MenuItemModel();

                JSONObject ob2 = jsonArray1.getJSONObject(j);
                // String menu_name, menu_qty, menu_price;
                String menu_veg_type = ob2.getString("type");
                menumodel.setMenuitem_type(menu_veg_type);
                String  menu_name = ob2.getString("item");
                menumodel.setMenuitem_name(menu_name);
                String  menu_qty = ob2.getString("qty");
                menumodel.setMenuitem_qty(menu_qty);
                String    menu_price = ob2.getString("price");
                menumodel.setMenuitem_price(menu_price);
                menuModels.add(menumodel);
            }
            viewHolder.listview.setAdapter(new MyAdapter(mContext,menuModels));
            setListViewHeightBasedOnChildren(viewHolder.listview);
            viewHolder.expanded_menulist.setAdapter(new Expanded_Menu_Adapter(mContext,menuModels));
            //setListViewHeightBasedOnChildren(viewHolder.expanded_menulist);
        }catch (Exception e)
        {

        }

        int menuArrayLenghth = menuModels.size();

        if(menuArrayLenghth > 2)
        {
            viewHolder.showhide.setVisibility(View.VISIBLE);
        }
        else
        {
            viewHolder.showhide.setVisibility(View.GONE);
        }

        result=vv;
        vv.setTag(viewHolder);
    }



    else {
        viewHolder = (ViewHolder) vv.getTag();
        result=vv;
    }

    lastPosition = position;

    viewHolder.dateandtime.setText(data.getOrder_time());
    viewHolder.modeofpayment.setText(data.getPaymemt_method());
    viewHolder.location.setText(data.getCustomer_location());
    // amount
    String amounttext = "` " + data.getPrice();
    SpannableStringBuilder mySpannable = new SpannableStringBuilder(amounttext);
    mySpannable.setSpan(new CustomTypefaceSpan("", face1), 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    mySpannable.setSpan(new CustomTypefaceSpan("", face), 2, data.getPrice().length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    viewHolder.rupee.setText(mySpannable);


    viewHolder.customerName.setText(data.getCustomer_name());
    viewHolder.orderId.setText(data.getOrder_id());
    if(!(comment_value.equals("0"))){
        viewHolder.comments.setVisibility(View.VISIBLE);
        viewHolder.comments.setText(data.getOrder_comments());

    }
    else
    {
        viewHolder.comments.setVisibility(View.GONE);
    }





    viewHolder.showhide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(viewHolder.listview.getVisibility() == View.VISIBLE)
            {
                viewHolder.expanded_menulist.setVisibility(View.VISIBLE);
                viewHolder.listview.setVisibility(View.GONE);
                viewHolder.showhide.setImageResource(R.drawable.blackarrrowup);
                if(!(comment_value.equals("0"))){
                    viewHolder.comments.setVisibility(View.VISIBLE);
                    viewHolder.comments.setText(data.getOrder_comments());

                }
                else
                {
                    viewHolder.comments.setVisibility(View.GONE);
                }
            }

            else if(viewHolder.expanded_menulist.getVisibility() == View.VISIBLE) {

                viewHolder.expanded_menulist.setVisibility(View.GONE);
                viewHolder.listview.setVisibility(View.VISIBLE);
                viewHolder.showhide.setImageResource(R.drawable.blackarrow);
                viewHolder.comments.setVisibility(View.GONE);
            }
        }
    });



    return vv;
}

1 个答案:

答案 0 :(得分:0)

问题可能是由Views回复Adapter引起的:当您向下/向上滚动时,突出显示的View会被回收并在ListView的其他位置使用}。您可以将每个View的背景颜色设置为适配器getView中的默认颜色。这样,回收的View的背景颜色将在再次显示之前设置为恢复正常。