2017-03-01 140 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

相关问题