2017-03-01 107 views
0

我有三個類即viz。一個片段,一個RecyclerViewAdapter和一個RecyclerViewHolder。他們以通常的方式工作。但是現在我遇到了一個問題,我需要從該Activity中的ViewHolder中調用一個方法。如何從Activity/Fragment調用ViewHolder方法

我想使用回調,但我試過了,它不工作。這是我迄今所做的:

接口

public interface TreeBottomListener { 
    enum Status {FAILED, SUCCESS, PRE_REQUEST} 
    void onResponse(Status status); 
} 

的ViewHolder

public class TreeBottomViewHolder extends RecyclerView.ViewHolder implements TreeBottomListener{ 
    @BindView(R.id.tree_bottom_progress) ProgressBar bottomProgress; 
    @BindView(R.id.tree_error_button) Button moreRetryButton; 
    private TreeAdapterListener listener; 
    private final String TAG = "TreeBottomViewHolder"; 

    public TreeBottomViewHolder(View itemView, final TreeAdapterListener listener) { 
     super(itemView); 
     ButterKnife.bind(this, itemView); 
     this.listener = listener; 
     setUpViews(); 
    } 

    private void setUpViews() { 
     bottomProgress.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor 
       (itemView.getContext(), R.color.colorAccent), PorterDuff.Mode.SRC_IN); 

     moreRetryButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       v.setVisibility(View.GONE); 
       listener.onLoadMoreClicked(); 
      } 
     }); 
    } 

    @Override 
    public void onResponse(Status status) { 
     processViews(status); 
    } 

    private void processViews(Status status) { 
    // This method is never called 
     Log.i(TAG, "onResponse: with status " + status); 
     if (status == Status.FAILED) { 
      bottomProgress.setVisibility(View.GONE); 
      moreRetryButton.setVisibility(View.VISIBLE); 

     } else if (status == Status.SUCCESS) { 
      bottomProgress.setVisibility(View.GONE); 

     } else if (status == Status.PRE_REQUEST) { 
      bottomProgress.setVisibility(View.VISIBLE); 
     } 
    } 
} 

片段

private TreeBottomListener bottomListener = bottomCallbacks; 

private static TreeBottomListener bottomCallbacks = new TreeBottomListener() { 
     @Override 
     public void onResponse(Status status) { 

     } 
    }; 

// I do this say when a network request is about to be sent 
bottomListener.onResponse(TreeBottomListener.Status.PRE_REQUEST); 
+0

請參考此鏈接(http://stackoverflow.com/questions/32720702/how-to-call-a-mainactivity-method-from-viewholder-in-recyclerview-adapter)供您參考 –

+0

@vijaychhalotre,謝謝。我曾看到過,但我想要做的是反過來,即從Fragment/Activity調用ViewHolder方法。 – X09

+0

你不應該這樣做。這會使你的代碼緊密耦合並且非常複雜。 適配器任務應該是顯示列表或recyclerView中的項目。活動不應直接與Adapter或ViewHolder交互。 如果您想刷新列表中的項目,請更改適配器數據列表並調用notifydatasetChange方法。 – nnn

回答

2

你可以調用此方法(YourAdapter.TreeBottomViewHolder)recyclerView.findViewHolderForLayoutPosition(position); 返回ViewHolder,您可以通過使用下面的代碼查詢有關該項目的位置:

private boolean isCurrentListViewItemVisible(int position) { 
    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); 
    int first = layoutManager.findFirstVisibleItemPosition(); 
    int last = layoutManager.findLastVisibleItemPosition(); 
    return first <= position && position <= last; 
} 

那麼你就可以做到以下幾點:

TreeBottomViewHolder holder = (YourAdapter.TreeBottomViewHolder)recyclerView.findViewHolderForLayoutPosition(position); 

if(isCurrentListViewItemVisible(position)){ 
    // you can access your views here 
} 

我想,如果它幫助你。

+0

由於某些未知原因,它返回null。 – X09

相關問題