在项目中设置visiblity时,RecyclerView会搞乱

时间:2017-07-01 08:30:27

标签: android android-recyclerview

我有一个RecyclerView,每个项目都有一个button。当按button时,它将隐藏或显示其他项目。当用户按下按钮并且我设置可见性visible时,我从服务器获取数据,如果再按一次,它将隐藏。它工作正常,

但问题是,当我按button时,请说pos 3它显示数据,但当我向下滚动pos 7项时,同样的数据也会显示!我真的很困惑。为什么会这样?是的,我搜索了一些文章,我还设置了其他部分也设置了可见性。任何帮助将不胜感激!

以下是一些代码:

public void onBindViewHolder(final pending_order_viewHolder holder, final int position) 
{
         final model_completed timeLineModel = mFeedList.get(position);
        //setting some data on start inlusding a button
        holder.mOrder.setText(timeLineModel.ordno +"");
        holder.mMarchant.setText(timeLineModel.olnm);

       //button click to expand and show data also hide when press agine
        holder.ButtonHide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View m) {
             if(holder.ClickToHide.getVisibility() == View.VISIBLE){

                //hiding if press the button agine
                 holder.ClickToHide.setVisibility(View.GONE);
                 holder.mDate.setVisibility(View.GONE);
             holder.mOrder.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
           holder.mMarchant.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
         }else {
              loader = new ProgressDialog(mContext);
              loader.setCancelable(false);
              loader.setMessage(mContext.getString(R.string.wait_msg));
              loader.show();

              //getting data when user expand
             ComplatedOrder(timeLineModel, holder, position);

         //setting visble with data
       holder.ClickToHide.setVisibility(View.VISIBLE);
        holder.mDate.setVisibility(View.VISIBLE);
      holder.mOrder.setCompoundDrawablesWithIntrinsicBounds( R.drawable.order_id, 0, 0, 0);
    holder.mMarchant.setCompoundDrawablesWithIntrinsicBounds( R.drawable.pending_outlets, 0, 0, 0);
       }
   }
});

获取服务器数据类:

 private void ComplatedOrder(final model_completed timeLineModel, final pending_order_viewHolder holder,final int position){

     //here getting some data

    //setting data
holder.mTime.setText(mContext.getString(R.string.dilvered_at) + timeLineModel.deltime + "");
    holder.collected_cash.setText("₹ " + timeLineModel.amtcollect + "");
    holder.Custmer_name.setText(timeLineModel.custname + "");
    holder.mDeliver_at.setText(timeLineModel.custaddr + " ");
    holder.mDate.setText(timeLineModel.dltm + "");
    loader.hide();
    }

3 个答案:

答案 0 :(得分:1)

问题出在循环视图中它将重用该对象,因此如果将pos 3设置为hide,它会尝试在pos7使用相同的对象。

我们可以通过在适配器中存储隐藏位置并在绑定视图中每次根据位置显示或隐藏来解决。

像这样

SELECT *
FROM (
    select *
    , ROW_NUMBER() OVER (PARTITION BY ExternalID ORDER BY ModifiedDate DESC) rnk
    from accounts 
) i
WHERE i.rnk = 1

和ComplatedOrder方法

    @Override
    public void onBindViewHolder(final pending_order_viewHolder holder, final int position) {

        final model_completed timeLineModel = mFeedList.get(position);


//setting order no. and marchant name on start
        holder.mOrder.setText(timeLineModel.ordno + "");
        holder.mMarchant.setText(timeLineModel.olnm);


//click to expand and hide when press agine
        holder.ButtonHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View m) {
                if (holder.ClickToHide.getVisibility() == View.VISIBLE) {
                    holder.ClickToHide.setVisibility(View.GONE);
                    holder.mDate.setVisibility(View.GONE);
                    holder.mOrder.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    holder.mMarchant.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

                    timeLineModel.isEnabled = false;
                } else {
                    if (timeLineModel.custaddr == null) {
                        loader = new ProgressDialog(mContext);
                        loader.setCancelable(false);
                        loader.setMessage(mContext.getString(R.string.wait_msg));
                        loader.show();

//getting data when user expand
                        ComplatedOrder(timeLineModel, holder, position);

                    } else {
                        timeLineModel.isEnabled = true;
                    }

                    notifyDataSetChanged();
//                    holder.ClickToHide.setVisibility(View.VISIBLE);
//                    holder.mDate.setVisibility(View.VISIBLE);
//                    holder.mOrder.setCompoundDrawablesWithIntrinsicBounds(R.drawable.order_id, 0, 0, 0);
//                    holder.mMarchant.setCompoundDrawablesWithIntrinsicBounds(R.drawable.pending_outlets, 0, 0, 0);
                }
            }
        });
        holder.collected_cash.setEnabled(false);


        if (timeLineModel.isEnabled) {


            holder.ClickToHide.setVisibility(View.VISIBLE);
            holder.mDate.setVisibility(View.VISIBLE);
            holder.mOrder.setCompoundDrawablesWithIntrinsicBounds(R.drawable.order_id, 0, 0, 0);
            holder.mMarchant.setCompoundDrawablesWithIntrinsicBounds(R.drawable.pending_outlets, 0, 0, 0);
        } else {
            holder.ClickToHide.setVisibility(View.GONE);
            holder.mDate.setVisibility(View.GONE);
            holder.mOrder.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            holder.mMarchant.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);


        }
    }

答案 1 :(得分:0)

  1. 我建议从

    中删除FINAL

    onBindViewHolder(final pending_order_viewHolder holder,final int position)

  2. 如果您处于紧张状态,请尝试使用

    holder.setIsRecyclable(假); 在适配器中。

  3. 希望这些建议能帮到你。

答案 2 :(得分:0)

您的模型类是model_completed,您可以在模型中创建一个布尔字段来存储可见性。

将以下属性添加到您的模型中:

    public class model_completed {
       ...
       ...
       ...
       //Add this property
       public boolean isVisible;
    }

并修改您的onBindViewHolder代码,如下所示:

    holder.ButtonHide.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View m) {
         if(timeLineModel.isVisible){

            //hiding if press the button again

             //Set property to false
             timeLineModel.isVisible = false                 

             holder.ClickToHide.setVisibility(View.GONE);
             holder.mDate.setVisibility(View.GONE);
         holder.mOrder.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
       holder.mMarchant.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
     }else {
          loader = new ProgressDialog(mContext);
          loader.setCancelable(false);
          loader.setMessage(mContext.getString(R.string.wait_msg));
          loader.show();

          //getting data when user expand
         ComplatedOrder(timeLineModel, holder, position);

     //setting visble with data

    //Set property to true

   timeLineModel.isVisible = true;

   holder.ClickToHide.setVisibility(View.VISIBLE);
    holder.mDate.setVisibility(View.VISIBLE);
  holder.mOrder.setCompoundDrawablesWithIntrinsicBounds( R.drawable.order_id, 0, 0, 0);
holder.mMarchant.setCompoundDrawablesWithIntrinsicBounds( R.drawable.pending_outlets, 0, 0, 0);
   }
  }     
});