2015-05-27 17 views
1

我有一個recyclerview,其中onLongClick()是一個項目,我正在顯示一個按鈕。但when scroll down the recycler view and scrolling back, that button is showing on top of another item or sometimes it is not showing在所有。 這裏是我的代碼Android RecyclerView在向後滾動時未保存其狀態

public static class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{ 
    public LinearLayout enq_layout; 
    public LinearLayout item_layout; 
    public TextView enquire; 
    public int position; 

    public TextViewHolder(View itemView) { 
     super(itemView); 
     item_layout= (LinearLayout) itemView.findViewById(R.id.item_layout); 
     enq_layout= (LinearLayout) itemView.findViewById(R.id.enq_layout); 
     enquire=(TextView) itemView.findViewById(R.id.enquire); 
     //position=getLayoutPosition(); 
    } 
} 

@Override 
public int getItemViewType(int position) { 
    return product.get(position)!=null? VIEW_ITEM: VIEW_PROG; 
} 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View v = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.cardlayout_product, parent, false); 
     RecyclerView.ViewHolder vh = new TextViewHolder(v); 
    return vh; 
} 
@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
    // Toast.makeText(act, "onBindViewHolder" +position, Toast.LENGTH_LONG).show(); 
     final ProductDetails item = product.get(position); 
     final TextViewHolder hold=((TextViewHolder)holder); 

     //hold.position=position; 
     // hold.item_layout.setTag(position); 

     hold.item_layout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
      // go to next activity 
      } 
     }); 
     hold.item_layout.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View view) { 
       // show enquiry button 
       hold.enq_layout.setVisibility(View.VISIBLE); 
      } 
     }); 
     hold.enquire.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //do some operation 
       int productid = Integer.parseInt(product.get(item.getPosition()).getProduct_id()); 
      } 
     }); 
} 
@Override 
public int getItemCount() { 
    return product.size(); 
} 

我試過這樣Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview?,但我不能夠訪問onCreateViewHolder的onclick方法裏面的意見。

+1

必須存儲在長按項目位置上,並嘗試顯示按鈕隱藏/顯示按鈕的基礎onBindViewHolder()上的存儲位置。 –

+1

謝謝。現在它正在工作.. – n1m1

+0

很高興幫助你,你做了很好的工作...,嘗試上傳你的改變,讓其他人也可以從中獲得想法。 –

回答

0

你將不得不創建一個名爲例如布爾shouldShowView這樣的全局變量:在onBindViewHolder使用可以使用下面的代碼

public static class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{ 
    public LinearLayout enq_layout; 
private boolean shouldShowView; 

然後:在您的onLongClick

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
    // Toast.makeText(act, "onBindViewHolder" +position, Toast.LENGTH_LONG).show(); 
     final ProductDetails item = product.get(position); 
     final TextViewHolder hold=((TextViewHolder)holder); 

if (shouldShowView) { 
    hold.enq_layout.setVisibility(View.VISIBLE); 
    } else { 
    hold.enq_layout.setVisibility(View.INVISIBLE); 
    } 

然後方法只需在長時間點擊時將shouldShowView變量設置爲true即可。當你想隱藏按鈕時,你也應該把它設置爲false。您可能還必須跟蹤在全局變量狀態中選擇了哪個項目,並檢查當前位置是否等於所選位置。

您可能還想將clickShowView檢查移到點擊偵聽器下方。

讓我知道如果您有任何問題。

+0

如果你可以選擇正確的答案,它會很好:-) – Smashing

0

你可以試試這個

 public View getViewByPosition(int position, ListView lv1) { 
     final int firstListItemPosition = lv1.getFirstVisiblePosition(); 
     final int lastListItemPosition = firstListItemPosition +  
     lv1.getChildCount() - 1; 
     if (position < firstListItemPosition || position > 
     lastListItemPosition) { 
      return lv1.getAdapter().getView(position, null, lv1); 
     } else { 
      final int childIndex = position - firstListItemPosition; 
      return lv1.getChildAt(childIndex); 
     } 
    } 

而在你的聲明像這樣

   selectedids =""; 
       for (int i = 0; i < len; i++){ 

        //View childv = lv1.getChildAt(i); 
        View childv=getViewByPosition(i, lv1); 
0

我有同樣的問題,解決了這個。您需要使用RecyclerView的方法並設置項目緩存大小: mRecyclerView.setItemViewCacheSize(300);