滚动recylerview失去了意见

时间:2015-11-05 07:18:12

标签: android android-recyclerview recycler-adapter

我在recyclerView填充动态列表 which looks like this,现在如果您发现点击时有一个“+”图标 生成最初隐藏的视图which looks like this 我面临的问题是每当我打开超过2-3个项目并滚动时,意外地其他项目被关闭,其中的数据也被删除,我试图保持一个标记“isExpanded”以检查项目扩展但它似乎不适用于第一个和最后一个项目(即如果我打开第一个项目并向下滚动到最后一个项目并打开它,第一个项目已关闭,反之亦然)

我对我的方法完全不同的解决方案是开放的,甚至相关的解决方案都可以。

这是我的适配器类

public class Step9LaborAdapter extends RecyclerView.Adapter<Step9LaborAdapter.ChildViewHolder> implements View.OnFocusChangeListener {

List<Step9_DB.Labor> laborList;
public EditText chkFOcus;

public Step9LaborAdapter(List<Step9_DB.Labor> mlaborList) {
    this.laborList = mlaborList;
}

public List<Step9_DB.Labor> getAllItems() {
    return laborList;
}

@Override
public ChildViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_step10labor, parent, false);
    ChildViewHolder vh = new ChildViewHolder(view);
    return vh;
}

@Override
public void onBindViewHolder(ChildViewHolder holder, int position) {
    final Step9_DB.Labor laborItem = getItem(position);
    holder.tv_laborTitle.setText(laborItem.getLaborName());
    holder.s.setText(laborItem.getS());
    holder.m.setText(laborItem.getM());
    holder.m_plus.setText(laborItem.getM_plus());
    holder.l.setText(laborItem.getL());
    holder.s.setOnFocusChangeListener(this);
    holder.s.setTag(position);
    holder.m.setOnFocusChangeListener(this);
    holder.m.setTag(position);
    holder.m_plus.setOnFocusChangeListener(this);
    holder.m_plus.setTag(position);
    holder.l.setOnFocusChangeListener(this);
    holder.l.setTag(position);
    holder.tv_show_option.setTag(R.string.laboritem, holder);
    holder.tv_show_option.setTag(laborItem);
    holder.tv_show_option.setTag(R.string.labor_pos, position);
    holder.tv_show_option.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ChildViewHolder childViewHolder = (ChildViewHolder) view.getTag(R.string.laboritem);

            int pos = (int) view.getTag(R.string.labor_pos);
            Step9_DB.Labor labor = (Step9_DB.Labor) view.getTag();
            if (labor.isExpanded()) {
                childViewHolder.ll_options.setVisibility(View.GONE);
                getItem(pos).setIsExpanded(false);
            } else {
                childViewHolder.ll_options.setVisibility(View.VISIBLE);
                getItem(pos).setIsExpanded(true);
            }
           notifyDataSetChanged();

        }
    });


}

private Step9_DB.Labor getItem(int position) {
    return laborList.get(position);
}

@Override
public int getItemCount() {
    if (laborList != null)
        return laborList.size();
    else return 0;
}


public void setChkFOcus(EditText chkFOcus) {
    this.chkFOcus = chkFOcus;
}

public EditText getChkFOcus() {
    return chkFOcus;
}

public class ChildViewHolder extends RecyclerView.ViewHolder {
    public TextView tv_laborTitle, tv_show_option;
    public EditText s, m_plus, m, l;
    LinearLayout ll_options;


    public ChildViewHolder(View itemView) {
        super(itemView);
        tv_laborTitle = (TextView) itemView.findViewById(R.id.tv_labor_type);
        tv_show_option = (TextView) itemView.findViewById(R.id.tv_show_option);
        ll_options = (LinearLayout) itemView.findViewById(R.id.ll_working_days);
        s = (EditText) itemView.findViewById(R.id.et_s);
        m = (EditText) itemView.findViewById(R.id.et_m);
        m_plus = (EditText) itemView.findViewById(R.id.et_m_plus);
        l = (EditText) itemView.findViewById(R.id.et_l);

    }
}

}

请问我是否还需要其他类代码,因为我认为适配器类已经足够了。

2 个答案:

答案 0 :(得分:1)

您必须在onscroll方法中管理您的状态。请根据onscoll内的需要进行编码。在创建recyclerView的mainactivity或fragment上插入以下代码。

recylerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                visibleItemCount = layoutmanager.getChildCount();
                 ViewHolder holder=new ViewHolder();
                for (int m=0;m<visibleItemCount;m++){
                   holder.view= recyclerView.getChildAt(m);
         int position=recyclerView.getChildAdapterPosition(holder.view);

//============initialize your all viewitems like this to avoid null pointer //exception
holder.tv_show_option=(TextView)holder.view.findViewById(R.id.tv_show_option);
    holder.tv_show_option.setText(yourdataitemlikearraylist.get(positionitem));

 final Step9_DB.Labor laborItem = getItem(position);
    holder.tv_laborTitle.setText(laborItem.getLaborName());
    holder.s.setText(laborItem.getS());
    holder.m.setText(laborItem.getM());
    holder.m_plus.setText(laborItem.getM_plus());
    holder.l.setText(laborItem.getL());
    holder.s.setOnFocusChangeListener(this);
    holder.s.setTag(position);
    holder.m.setOnFocusChangeListener(this);
    holder.m.setTag(position);
    holder.m_plus.setOnFocusChangeListener(this);
    holder.m_plus.setTag(position);
    holder.l.setOnFocusChangeListener(this);
    holder.l.setTag(position);
    holder.tv_show_option.setTag(R.string.laboritem, holder);
    holder.tv_show_option.setTag(laborItem);
    holder.tv_show_option.setTag(R.string.labor_pos, position);
    holder.tv_show_option.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ChildViewHolder childViewHolder = (ChildViewHolder) view.getTag(R.string.laboritem);

            int pos = (int) view.getTag(R.string.labor_pos);
            Step9_DB.Labor labor = (Step9_DB.Labor) view.getTag();
            if (labor.isExpanded()) {
                childViewHolder.ll_options.setVisibility(View.GONE);
                getItem(pos).setIsExpanded(false);
            } else {
                childViewHolder.ll_options.setVisibility(View.VISIBLE);
                getItem(pos).setIsExpanded(true);
            }
           notifyDataSetChanged();

        }
    });

                }
                }
            });

// ======添加内部类视图

private static class ViewHolder{


View view ;
public TextView tv_laborTitle, tv_show_option;
    public EditText s, m_plus, m, l;
    LinearLayout ll_options;
}

答案 1 :(得分:0)

感谢您的回答但我通过纠正我的错误而解决了这个问题,因为我们没有首先检查isExpanded上的bindViewHolder条件。

public class Step9LaborAdapter extends RecyclerView.Adapter<Step9LaborAdapter.ChildViewHolder> implements View.OnFocusChangeListener {

List<Step9_DB.Labor> laborList;
public EditText chkFOcus;

public Step9LaborAdapter(List<Step9_DB.Labor> mlaborList) {
this.laborList = mlaborList;
}

public List<Step9_DB.Labor> getAllItems() {
return laborList;
}

@Override
public ChildViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =       LayoutInflater.from(parent.getContext()).inflate(R.layout.row_step10labor, parent, false);
ChildViewHolder vh = new ChildViewHolder(view);
return vh;
}

@Override
public void onBindViewHolder(ChildViewHolder holder, int position) {
final Step9_DB.Labor laborItem = getItem(position);
holder.tv_laborTitle.setText(laborItem.getLaborName());
holder.s.setText(laborItem.getS());
holder.m.setText(laborItem.getM());
holder.m_plus.setText(laborItem.getM_plus());
holder.l.setText(laborItem.getL());
holder.s.setOnFocusChangeListener(this);
holder.s.setTag(position);
holder.m.setOnFocusChangeListener(this);
holder.m.setTag(position);
holder.m_plus.setOnFocusChangeListener(this);
holder.m_plus.setTag(position);
holder.l.setOnFocusChangeListener(this);
holder.l.setTag(position);
holder.tv_show_option.setTag(R.string.laboritem, holder);
holder.tv_show_option.setTag(laborItem);
holder.tv_show_option.setTag(R.string.labor_pos, position);
Step9_DB.Labor labor = (Step9_DB.Labor) view.getTag();
        if (labor.isExpanded()) {
            holder.ll_options.setVisibility(View.GONE);
            getItem(pos).setIsExpanded(false);
        } else {
            holder.ll_options.setVisibility(View.VISIBLE);
            getItem(pos).setIsExpanded(true);
        }
holder.tv_show_option.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        ChildViewHolder childViewHolder = (ChildViewHolder) view.getTag(R.string.laboritem);

        int pos = (int) view.getTag(R.string.labor_pos);
        Step9_DB.Labor labor = (Step9_DB.Labor) view.getTag();
        if (labor.isExpanded()) {
            childViewHolder.ll_options.setVisibility(View.GONE);
            getItem(pos).setIsExpanded(false);
        } else {
            childViewHolder.ll_options.setVisibility(View.VISIBLE);
            getItem(pos).setIsExpanded(true);
        }
       notifyDataSetChanged();

    }
});


}

private Step9_DB.Labor getItem(int position) {
return laborList.get(position);
}

@Override
public int getItemCount() {
if (laborList != null)
    return laborList.size();
else return 0;
}


public void setChkFOcus(EditText chkFOcus) {
this.chkFOcus = chkFOcus;
}

public EditText getChkFOcus() {
return chkFOcus;
}

public class ChildViewHolder extends RecyclerView.ViewHolder {
public TextView tv_laborTitle, tv_show_option;
public EditText s, m_plus, m, l;
LinearLayout ll_options;


public ChildViewHolder(View itemView) {
    super(itemView);
    tv_laborTitle = (TextView) itemView.findViewById(R.id.tv_labor_type);
    tv_show_option = (TextView) itemView.findViewById(R.id.tv_show_option);
    ll_options = (LinearLayout) itemView.findViewById(R.id.ll_working_days);
    s = (EditText) itemView.findViewById(R.id.et_s);
    m = (EditText) itemView.findViewById(R.id.et_m);
    m_plus = (EditText) itemView.findViewById(R.id.et_m_plus);
    l = (EditText) itemView.findViewById(R.id.et_l);

}
}
}`