2015-11-05 42 views
0

我正在填充在我的回收站視圖 which looks like this的動態列表,現在如果你發現有哪個上點擊 生成最初是隱藏which looks like this 我面臨的問題美景的「+」圖標是每當我打開超過2-3項目和滾動,意外其他項目被關閉,其中的數據也被抹去,我已經嘗試維護一個標誌「isExpanded」來保持項目擴大檢查,但它似乎並沒有工作與第一個和最後一個項目(即如果我打開第一個項目,並向下滾動到最後一個項目,並打開它的第一個是封閉的,反之亦然)觀點失去了滾動recylerview

我打開完全不同的解決方案,我的應用程序蟑螂甚至相關的解決方案都可以。

這裏是我的Adpater類

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); 

    } 
} 

}

請問我是否還需要其他類的代碼,因爲我覺得適配器類是不夠的。

+0

對於子視圖和父視圖您可以有兩個不同的視圖,您可以通過getViewType()來設置視圖。欲瞭解更多信息,請訪問http://doublewong.com/2014/create-recyclerview-with-multiple-view-type/ –

回答

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); 

} 
} 
}` 
1

你必須在onscroll方法內管理你的狀態。請根據你的需要在onscoll中進行編碼。插入下面您mainactivity或片段在您recyclerview創建

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(); 

     } 
    }); 







       } 
       } 
      }); 

// ======添加內部類viewholder

private static class ViewHolder{ 


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