2015-06-19 151 views
0

我正在處理一個項目,其中需要在recyclerView項目單擊後更改recyclerView的數據。我曾嘗試在setOnClickListener方法onBindViewHolder:更改RecyclerView項目上的DataSet點擊

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 
    private List<Dataset> mDataset = Collections.emptyList(); 
    private static Context sContext; 
    private SharedPreferences sharedPrefs; 

    // Create new views (invoked by layout manager) 
    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View v = null; 

     sharedPrefs = Functions.getShPreferences(parent.getContext(), "selected_view"); 
     viewType = sharedPrefs.getInt("selected_view", 0); 
     switch (viewType) { 
      case 0: 
       v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view, parent, false); 
       break; 
      case 1: 
       v = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view, parent, false); 
       break; 
     } 

     // set the view's size, margins, paddings and layout parameters 

     return new ViewHolder(v); 
    } 

    // Replace the contents of a view (invoked by layout manager) 
    @Override 
    public void onBindViewHolder(final ViewHolder holder, int position) { 
     // - get element from your dataset at this position 
     // - replace the contents of the view with that element 
     final Dataset current = mDataset.get(position); 

     holder.mName.setText(current.getName()); 
     holder.mIcon.setImageResource(current.getIconId()); 
     holder.mLayout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       // make your click listener here 
       String f = Functions.getRoot(current.getFile()); 

       mDataset = Functions.getData(f); 
       new FileAdapter(sContext, f); 
       notifyDataSetChanged(); 
       Toast.makeText(v.getContext(), f, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

    } 

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

    @Override 
    public int getItemViewType(int position) { 

     return super.getItemViewType(position); 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder { 
     TextView mName; 
     ImageView mIcon; 
     RelativeLayout mLayout; 
     //TextView mFileDateModified; 

     public ViewHolder(View v) { 
      super(v); 
      mLayout = (RelativeLayout) v.findViewById(R.id.recycler_view_layout); 
      mName = (TextView) v.findViewById(R.id.file_name); 
      mIcon = (ImageView) v.findViewById(R.id.file_icon); 


     } 
    } 

    public MyAdapter(Context context, String ph) { 
     context = sContext; 
     mDataset = Functions.getData(ph); 
    } 
} 

我已經把這個代碼onBindViewHolder。但它不工作。任何幫助將不勝感激。謝謝。

+0

是什麼*但它不工作*是什麼意思? – Blackbelt

+0

我想說的是我試圖通過調用適配器來改變數據,但它不工作 – Apurva

+0

'holder.layout.setOnClickListener'應該在你的適配器中,我錯了嗎? – Blackbelt

回答

3

你可以在你的ViewHolder內部類中設置你的監聽器。代碼會是這樣的;

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    public ViewHolder(View itemView) { 
     super(itemView); 
     itemView.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     mDataset = Functions.getData(); 
     new FileAdapter(); 
     notifyDataSetChanged(); 

     int position = getAdapterPosition(); 
     // or 
     position = getLayoutPosition(); 
     // both worked for me 
    } 
} 
+0

如何獲取當前數據對象? – Apurva

+0

在你的onClick方法中使用'getPosition()' – iamkaan

+0

getPosition()被折舊 – Apurva

0

我想打電話notifyDataSetChanged(); 是不是一個好主意,因爲它改變和刷新所有可能給你帶來麻煩,如果有長期數據的數據,因此建議使用

notifyItemChanged(getAdapterPosition()); 

我ViewHolder看起來像這個:

public class AttendanceListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    TextView rollNo, name,isPresent; 


    public AttendanceListViewHolder(View itemView) { 
     super(itemView); 
     if (isRecyclerViewClickable) { 
      isPreset.setOnClickListener(this); 
     } 

    } 

    @Override 
    public void onClick(View v) { 



     data.get(getAdapterPosition()).wasPresent = "false"; 


     notifyItemChanged(getAdapterPosition()); 
    } 
} 
相關問題