1

我已經創建了一些代碼,它從數據庫中提取數據,然後填充cardView。 cardView行有一個隱藏的(不見的)textView,我想在單擊卡時啓用,因此展開視圖。 所有這些似乎工作正常,但由於某種原因,它不僅擴展了項目1,而且還擴展了另一個項目,我明顯不希望看到擴展的原因。Recycleview擴展多個iterms

這裏是我的適配器

import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.blackout.kfzquiz.model.Product; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Sundown on 01/10/2016. 
*/ 

public class MyAdapter2 extends RecyclerView.Adapter<MyAdapter2.ViewHolder> { 
    private String[] kennzeichen_array; 
    private String[] stadt_array; 
    private int length; 

    // Provide a reference to the views for each data item 
    // Complex data items may need more than one view per item, and 
    // you provide access to all the views for a data item in a view holder 
    public static class ViewHolder extends RecyclerView.ViewHolder{ 
     //each data item is just a string in this case 
     public TextView kennzeichen; 
     public TextView hiddenTextView; 

     public ViewHolder(View v){ 
      super(v); 
      kennzeichen = (TextView) v.findViewById(R.id.tv); 
      hiddenTextView = (TextView) v.findViewById(R.id.textView8); 
     } 
    } 

    // Provide a suitable constructor (depends on the kind of dataset) 
    public MyAdapter2(List<Product> myItems){ 
     length = myItems.size(); 

     String[] mStrings = new String[myItems.size()]; 
     String[] stadtStrings = new String[myItems.size()]; 
     for(int i=0;i<myItems.size();i++) { 
      mStrings[i] = myItems.get(i).getkfz(); 
      stadtStrings[i] = myItems.get(i).getStadt(); 
     } 
     kennzeichen_array = mStrings; 
     stadt_array = stadtStrings; 

    } 

    // Create new views (invoked by the layout manager) 
    @Override 
    public MyAdapter2.ViewHolder onCreateViewHolder(ViewGroup parent, 
                int viewType) { 
     // create a new view 
     LayoutInflater inflater = LayoutInflater.from(
       parent.getContext()); 
     View v = 
       inflater.inflate(R.layout.my_text_view, parent, false); 

     // set the view's size, margins, paddings and layout parameters 
     MyAdapter2.ViewHolder vh = new MyAdapter2.ViewHolder(v); 
     return vh; 
    } 

    // Replace the contents of a view (invoked by the layout manager) 
    @Override 
    public void onBindViewHolder(final MyAdapter2.ViewHolder holder, final int position) { 
     // - get element from your dataset at this position 
     // - replace the contents of the view with that element 
     holder.kennzeichen.setText(kennzeichen_array[position]); 
     holder.hiddenTextView.setText(stadt_array[position]); 

     holder.itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.d("Clicked", String.valueOf(holder.getAdapterPosition())); 
       if (holder.hiddenTextView.getVisibility() == View.VISIBLE){ 
        holder.hiddenTextView.setVisibility(View.GONE); 
       } 
       else{ 
        holder.hiddenTextView.setVisibility(View.VISIBLE); 
       } 
      } 
     }); 
    } 

    // Return the size of your dataset (invoked by the layout manager) 
    @Override 
    public int getItemCount() { 
     return length; 
    } 
} 

而且一些截圖提前

Nothing ExpandedClicked on first itemAnother also expanded??

感謝

回答

2

回收站視圖再利用項目 - 它創建它們的固定量適合屏幕,並在您滾動時重複使用它們。因此,例如,如果您必須顯示30個項目,recyclerView將只創建7個項目,並在您滾動時重新使用它們。因此,項目1也將是項目8,項目15等。當您在特定項目上顯示文字視圖時,它在滾動時保持可見狀態,因爲它是相同的項目。

解決方案是跟蹤有關特定TextView在給定位置的可見性的信息,並在滾動到該位置時應用它。

所以,你需要做什麼:

創建一個布爾數組,將在指定位置保存有關項目的知名度信息

private boolean[] shouldBeHidden; 

接下來,在構造函數初始化:

length = myItems.size(); 

    String[] mStrings = new String[myItems.size()]; 
    String[] stadtStrings = new String[myItems.size()]; 
    shouldBeHidden = new boolean[length]; 
    for(int i=0;i<myItems.size();i++) { 
     mStrings[i] = myItems.get(i).getkfz(); 
     stadtStrings[i] = myItems.get(i).getStadt(); 
     shouldBeHidden[i] = true; 
    } 

最後,在OnBindViewHolder方法中設置可見性:

 holder.kennzeichen.setText(kennzeichen_array[position]); 
     holder.hiddenTextView.setText(stadt_array[position]); 
     final TextView myHiddenText = holder.hiddenTextView; 

     if (shouldBeHidden[position]){ 
      myHiddenText.setVisibility(View.GONE); 
     } 
     else{ 
      myHiddenText.setVisibility(View.VISIBLE); 
     } 

    holder.itemView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Log.d("Clicked", String.valueOf(holder.getAdapterPosition())); 
     if (myHiddenText.getVisibility()==View.VISIBLE){ 
      myHiddenText.setVisibility(View.GONE); 
      shouldBeHidden[position] = true; 
     } 
     else{ 
      myHiddenText.setVisibility(View.VISIBLE); 
      shouldBeHidden[position] = false; 
     } 

    } 
}); 
+0

天才隊友,感謝您的快速和有益的答案!甚至沒有想過這樣做。似乎挺直。 – T1M