从上到下滚动时如何自动停止对项目的列表视图取消选择?

Posted

技术标签:

【中文标题】从上到下滚动时如何自动停止对项目的列表视图取消选择?【英文标题】:how to stop listview deselection on items automatically when scrolling top to bottom? 【发布时间】:2016-04-09 07:48:47 【问题描述】:

我有一个列表视图并使用 BaseAdapter 添加了内容。它工作正常,但是当我要从上到下向下滚动项目时,它会自动将所选项目的阶段更改为列表视图中的默认状态。如何解决这个问题呢。请帮忙。我是安卓新手。

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

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    customDialog = new CustomDialog(TakeoutListActivity.this);
    Intent intent = getIntent();
    Obj = intent.getStringExtra("CateValue");
    Log.e("Obj", "" + Obj);

    addtocardArr = new ArrayList<HashMap<String, String>>();
    listView = (ListView) findViewById(R.id.subcategory_list);
    notificationCount = 0;
    new AsyncTask<Void, Void, Void>() 

        @Override
        protected void onPreExecute() 
            super.onPreExecute();
            customDialog.setContentView(R.layout.cus_dialog);
            customDialog.show();
        

        @Override
        protected Void doInBackground(Void... params) 

            if (Obj != null) 

                try 
                    JSONObject jsonObject = new JSONObject(Obj);
                    JSONArray jsonArray = jsonObject.getJSONArray("Details");

                    SubCateArrayList = new ArrayList<HashMap<String, String>>();

                    /*for (int i = 0; i < jsonArray.length(); i++) 
                        SubCateHashMapList = new HashMap<String, String>();
                        JSONObject DetailsObj = jsonArray.getJSONObject(i);

                        SubCateHashMapList.put("ItemName", DetailsObj.optString("ItemName"));
                        JSONArray AllItemArray = DetailsObj.getJSONArray("Allitems");
                        //Log.e("AllItemArray", "" + AllItemArray);

                        SubCateArrayList.add(SubCateHashMapList);

                        for (int j = 0; j < AllItemArray.length(); j++) 
                            SubInner = new HashMap<String, String>();
                            JSONObject AllItemObj = AllItemArray.getJSONObject(j);
                            SubInner.put("foodid", AllItemObj.getString("foodid"));
                            SubInner.put("name", AllItemObj.getString("name"));
                            SubInner.put("Description", AllItemObj.getString("Description"));
                            SubInner.put("image", AllItemObj.getString("image"));
                            SubInner.put("price", AllItemObj.getString("price"));
                            SubInner.put("favorite", AllItemObj.getString("favorite"));

                            SubCateArrayList.add(SubInner);
                            Log.e("SubCateArrayList", "" + SubCateArrayList);
                        
                    
                    Log.e("SubCateArrayList", "" + SubCateArrayList);*/


                    SubCateArrayList = new ArrayList<HashMap<String, String>>();
                    // SubCateArrayList.clear();
                    // SubCateInnerArrayList = new ArrayList<HashMap<String, String>>();

                    for (int i = 0; i < jsonArray.length(); i++) 
                        SubCateHashMapList = new HashMap<String, String>();
                        JSONObject DetailsObj = jsonArray.getJSONObject(i);

                        SubCateHashMapList.put("ItemName", DetailsObj.optString("ItemName"));
                        Log.e("SubCateHashMapList>>>>>>>>>", "" + SubCateHashMapList);
                        JSONArray AllItemArray = DetailsObj.getJSONArray("Allitems");
                        //Log.e("AllItemArray", "" + AllItemArray);

                        //SubCateArrayList.add(SubCateHashMapList);


                        for (int j=0; j<AllItemArray.length();j++)
                        
                            SubInner = new HashMap<String, String>();

                            JSONObject AllItemObj = AllItemArray.getJSONObject(j);
                            SubInner.put("foodid", AllItemObj.getString("foodid"));
                            SubInner.put("name", AllItemObj.getString("name"));
                            SubInner.put("Description", AllItemObj.getString("Description"));
                            SubInner.put("image", AllItemObj.getString("image"));
                            SubInner.put("price", AllItemObj.getString("price"));
                            SubInner.put("favorite", AllItemObj.getString("favorite"));


                            SubCateArrayList.add(SubInner);
                            Log.d("SubCateInnerArrayList2349587", "" + SubCateArrayList);
                        
                        //SubInner.putAll(SubCateHashMapList);
                    

                    Log.e("SubCateArrayList", "" + SubCateArrayList);


                 catch (JSONException e) 
                    e.printStackTrace();
                


             else 

                ErrorMsg = "JSONError";
            

            return null;
        

        @Override
        protected void onPostExecute(Void aVoid) 
            super.onPostExecute(aVoid);
            customDialog.dismiss();
            subCateListAdapter = new SubCateListAdapter(TakeoutListActivity.this, SubCateArrayList);
            subCateListAdapter.notifyDataSetInvalidated();
            listView.setFadingEdgeLength(0);
            listView.setAdapter(subCateListAdapter);


        
    .execute();




@Override
public boolean onPrepareOptionsMenu(Menu menu) 

    getMenuInflater().inflate(R.menu.cart_menu,menu);

    MenuItem item = menu.findItem(R.id.cart_add);
    drawable = (LayerDrawable)item.getIcon();

    Log.d("notificationCount", "" + notificationCount);
    BadgeHelper.setBadgeCount(this, drawable, notificationCount);


    return true;


@Override
public boolean onOptionsItemSelected(MenuItem item) 

    int id = item.getItemId();
    if (id == R.id.cart_homeID)
    
        Intent homeIntent = new Intent(TakeoutListActivity.this,HomeRestaurant.class);
        startActivity(homeIntent);
        TakeoutListActivity.this.finish();
    
    if (id==R.id.cart_add)
    
        if (notificationCount==0)
        

            AlertDialog.Builder builder = new AlertDialog.Builder(TakeoutListActivity.this);
            builder.setMessage("Please add to cart");
            builder.setNegativeButton("OK", new DialogInterface.OnClickListener() 
                @Override
                public void onClick(DialogInterface dialog, int which) 
                    dialog.dismiss();
                
            );
            builder.show();
        
        else
        
            Intent nextact = new Intent(TakeoutListActivity.this, AddCartActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("Selected", addtocardArr);
            nextact.putExtras(bundle);

            // nextact.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(nextact);

// Log.d("addtocardmap final", "********" + addtocardmap.toString());

        
        return  true;
    

    return super.onOptionsItemSelected(item);


private class SubCateListAdapter extends BaseAdapter 

    LayoutInflater layoutInflater;
    Context context;
    HashMap<String, String> display = new HashMap<String, String>();
    ArrayList<HashMap<String, String>> data;


    int count = 0;
    String strcount="";
    String[] clickedpositions;
    int locid;

    HashMap<String,String> toaddData;

    public SubCateListAdapter(Context subMenuListActivity, ArrayList<HashMap<String, String>> subCateArrayList) 

        this.context = subMenuListActivity;
        this.data = subCateArrayList;

    

    @Override
    public int getCount() 
        return data.size();
    

    @Override
    public Object getItem(int position) 
        return null;
    

    @Override
    public long getItemId(int position) 
        return 0;
    

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

        final TextView menuTitle, subMenuTitle, Price, des, countText, addCart, remove;
        ImageView itemImage, FavImage;
        ImageButton add, minus;

        toaddData = new HashMap<String,String>();


        display = data.get(position);
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view =layoutInflater.inflate(R.layout.takeout_listitem, parent, false);

        //menuTitle = (TextView)view.findViewById(R.id.menu_title);
        subMenuTitle = (TextView) view.findViewById(R.id.sub_title);
        Price = (TextView) view.findViewById(R.id.price);
        countText = (TextView) view.findViewById(R.id.count_text);
        des = (TextView) view.findViewById(R.id.menu_des);
        itemImage = (ImageView) view.findViewById(R.id.menu_Image);
        FavImage = (ImageView) view.findViewById(R.id.fav);
        addCart = (TextView)view.findViewById(R.id.addToCart);

        add = (ImageButton)view.findViewById(R.id.add);
        minus = (ImageButton)view.findViewById(R.id.minus);


        //menuTitle.setText(display.get("ItemName"));
        subMenuTitle.setText(display.get("name"));
        des.setText(display.get("Description"));
        Price.setText(display.get("price"));
        Glide.with(getApplicationContext()).load(display.get("image")).error(R.drawable.list_bg).crossFade(500).into(itemImage);

        add.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

                textCount = Integer.parseInt(countText.getText().toString());
                textCount++;
                strcount = String.valueOf(textCount);
                countText.setText(strcount);
            
        );

        minus.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                int textCount = Integer.parseInt(countText.getText().toString());
                textCount--;

                if(textCount<=0) 
                    strcount = String.valueOf(textCount);
                    countText.setText("0");
                
                else
                
                    strcount = String.valueOf(textCount);
                    countText.setText(strcount);
                

            
        );

        addCart.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

                addtocardmap = new HashMap<String, String>();
                recid = data.get(position).get("foodid");
                pdttitle = data.get(position).get("name");
                foodprice = data.get(position).get("price");
                notifycount = countText.getText().toString();
                poscount +=position;
                strposcount += String.valueOf(position) + ",";
                Log.e("strposcount", "" + strposcount);

                toaddData.put("foodid", recid);
                toaddData.put("name", pdttitle);
                toaddData.put("notifycount", notifycount);

                if(addtocardArr.contains(addtocardmap))
                
                    String quan = addtocardArr.get(position).get("qunatity");
                    int intquan = Integer.parseInt(quan)+Integer.parseInt(strcount);
                    strcount = String.valueOf(intquan);

                    // addtocardArr.remove(position - 1);
                    addtocardmap.put("foodid", recid);
                    addtocardmap.put("name", pdttitle);
                    addtocardmap.put("price", foodprice);
                    addtocardmap.put("qunatity", notifycount);
                    addtocardArr.set(position,addtocardmap);


                    Log.d("recid", "******" + recid);
                    Log.d("pdttitle", "******" + pdttitle);
                    Log.d("Quantity", "******" + notifycount);
                    Log.d("foodprice", "******" + foodprice);
                    Log.d("item position", "******" + position);
                    Log.d("addtocardmap", "********" + addtocardmap);
                    //addtocardArr.remove(position);
                    Log.d("After remove duplicates", "********" + addtocardArr);
                
                else
                
                    //in object
                    addtocardmap.put("foodid", recid);
                    addtocardmap.put("name", pdttitle);
                    addtocardmap.put("price", foodprice);
                    addtocardmap.put("qunatity", notifycount);
                    //in array
                    Log.d("recid", "******" + recid);
                    Log.d("pdttitle", "******" + pdttitle);
                    Log.d("Quantity", "******" + notifycount);
                    Log.d("foodprice", "******" + foodprice);
                    Log.d("item position", "******" + position);
                    Log.d("addtocardmap", "********" + addtocardmap);

                
                //tosendData.add(toaddData);
                addtocardArr.add(addtocardmap);
                Log.d("to add non-duplicate element", "" + addtocardArr);

                // addtocardArr.add(new SelectedItems());

                notificationCount++;
                updateNotificationCount(String.valueOf(notificationCount));

            
        );


        return view;
    

【问题讨论】:

使用视图持有者,请分享代码以供审核 这是我的代码,我也使用了视图支架。它不工作。 【参考方案1】:

查看我的适配器并像这样更改您的适配器

 @Override
public View getView(int position, View convertView, ViewGroup parent) 
    View view = convertView;
    ViewHolder holder = null;
    if (view == null) 
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_list_item, null);
        holder = new ViewHolder();
        holder.title = (TextView) view.findViewById(R.id.tvFoodItemTitle);
        holder.desc = (TextView) view.findViewById(R.id.tvFoodItemDesc);
        holder.image = (ImageView) view.findViewById(R.id.ivFoodItem);
        view.setTag(holder);
     else 
        holder = (ViewHolder) view.getTag();
    

    holder.title.setText(getItem(position).getName());
    holder.desc.setText(getItem(position).getDescription());
    String imagePath = "assets://" + getItem(position).getImagePath();
    ImageLoader.getInstance().displayImage(imagePath, holder.image);

    return view;


class ViewHolder 
    TextView title;
    TextView desc;
    ImageView image;

【讨论】:

【参考方案2】:

这个例子是使用数组列表来完成的这个逻辑

在你的 pojo 类中添加布尔标志说

Boolean isSelected;

在其中应用 getter 和 setter。 最初设置 isSelected = false 的所有值,以便不选择列表项

现在在 onItem 单击期间将当前对象的 isSelected 设置为 true 像这样

在 onItemClick 内应用这个

OnItemClickListener listViewOnItemClick = new OnItemClickListener() 

    @Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) 

for(int i =0;i  < lstcategory.size();i++
lstcategory.get(i).setIsSelected(false);


lstcategory.get(position).setIsSelected(true); 
            mAdapter.notifyDataSetChanged();

    
;

现在在您的适配器类的 getview 中检查列表项是否已检查。

@Override
public View getView(int position, View convertView, ViewGroup parent) 
    final View view = View.inflate(context, R.layout.item_list, null);

    if (lstcategory.get(position).getIsSelected()) 
        // set selected your color
    else
       //set default color
    


    return view;

【讨论】:

以上是关于从上到下滚动时如何自动停止对项目的列表视图取消选择?的主要内容,如果未能解决你的问题,请参考以下文章

单击同一个div时如何从上到下和从下到上缓慢滚动?

如何使用coredata从上到下对tableview项目进行排序

如何让容器从上到下向下滚动

如何在像快门一样从上到下加载时为 UIView 设置动画?

浮动布局 - 项目从上到下运行,而不是并排?

UITextView 可以从上到下呈现吗?