2017-08-06 61 views
0

我最近從ListView遷移到RecyclerView ... 除了我不知道如何從RecyclerView項目獲取數據,一切似乎都很好... 在ListView Item OnClick,我以前使用getItemAtPosition,但它不存在於RecyclerView ...從RecyclerView的項目獲取的位置數據

這裏是代碼的相關部分... - MainActivity.java

private SDKP mSelectedPackage; 
RecyclerView mRecyclerView; 

mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() { 
      @Override 
      public void onItemClick(View view, int position) { 
      //I'm STUCK HERE! 
      mSelectedPackage = (SDKPackage) mRecyclerView.getItemAtPosition(position); 
        openContextMenu(view); 

    /*Toast.makeText(MainActivity.this, "Card at " + position + " is clicked", Toast.LENGTH_SHORT).show();*/ 
      } 
     })); 


@Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) 
    { 
     mSelectedPackage.inflateMenu(menu); 
     super.onCreateContextMenu(menu, v, menuInfo); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) 
    { 
     return mSelectedPackage.onMenuClick(this, item); 
    } 

這裏的inflateMenu(文本菜單菜單)方法 -

public void inflateMenu(ContextMenu menu) 
    { 
     menu.setHeaderTitle(getPackageName()); 
     if (isDownloading()) 
     { 
      menu.add(Menu.NONE, 256, Menu.NONE, "Cancel download"); 
     } 
     else if (isInstalled()) 
     { 
      menu.add(Menu.NONE, 257, Menu.NONE, "Uninstall"); 
     } 
     else 
     { 
      int i = 0; 
      for (SDKA archive : archives) 
      { 
       menu.add(Menu.NONE, i++, Menu.NONE, archive.getArchiveName()); 
      } 
     } 
    } 

    public boolean onMenuClick(Activity parent, MenuItem item) 
    { 
     if (item.getItemId() >= 256) 
      uninstall(); 
     else 
     { 
      SDKA archive = archives.get(item.getItemId()); 
      if (archive != null) 
      { 
       archive.onClick(parent); 
      } 
     } 
     return true; 
    } 

當我使用的ListView我用 -

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
      { 
       @Override 
       public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) 
       { 
        mSelectedPackage = (SDKP) mPackageList.getItemAtPosition(position); 
        openContextMenu(view); 
       } 
      }); 
+0

使用'getAdapterPosition()' –

回答

0

@鮑勃的建議,我這樣做沒有太多的代碼...:)

在我的適配器,我實現了使用自定義的方法 -

public Object getMyPos(int pos){ 
     return packages.get(pos); 
    } 

&然後在RecyclerItem的OnClick,我用它喜歡 -

mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() { 
      @Override 
      public void onItemClick(View view, int position) { 
      mSelectedPackage = (SDKP) ((ShAdapter)mRecyclerView.getAdapter()).getMyPos(position); 

        openContextMenu(view); 
      } 
     })); 
0

您應該設置View.OnClickListeneritemViewRecyclerView.ViewHolder類。

+0

然後我該如何獲得OnClickListener中的getItemAtPosition(int)方法? :) – DarShan

+0

你可以從'ViewHolder'中調用'getAdapterPosition()',它給你適配器的位置。使用該位置,您可以從您的支持數據中獲取該項目 - 它可能來自「列表」或「光標」行,具體取決於您的實施。 – Bob

+0

如果它在一個List中該怎麼辦? – DarShan

1

在recyclerview中沒有getItemAtPosition()的等價物。爲了解決這個問題,你可以在你的適配器上創建一個接口,並在你的ViewHolder上實現該接口,該接口將監聽你的onClick()

public interface OnItemClickListener { 
     public void onItemClick(View view, int position); 
    } 

然後讓你的ViewHolder實現接口。

public static class MyViewHolder extends RecyclerView.MyViewHolder implements View.OnClickListener { 

private OnItemClickListener mListener; 

public MyViewHolder(View itemView) { 
    super(itemView); 
    itemView.setOnClickListener(this); 

} 

public MyViewHolder(View itemView, OnItemClickListener listener) { 
    this(itemView); 
    mListener = listener; 
} 


@Override 
public void onClick(View v) { 
    if (mListener != null) { 
     mListener.onItemClick(v, getPosition()); 
    } 
} 

- 或者你可以使用rxjava。

0

您可以通過多種方式通過實施RecyclerView.OnItemTouchListener

import android.content.Context; 
 
import android.support.v7.widget.RecyclerView; 
 
import android.view.GestureDetector; 
 
import android.view.MotionEvent; 
 
import android.view.View; 
 

 
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { 
 
    private OnItemClickListener mListener; 
 

 
    public interface OnItemClickListener { 
 
     public void onItemClick(View view, int position); 
 
    } 
 

 
    GestureDetector mGestureDetector; 
 

 
    public RecyclerItemClickListener(Context context, OnItemClickListener listener) { 
 
     mListener = listener; 
 
     mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { 
 
      @Override 
 
      public boolean onSingleTapUp(MotionEvent e) { 
 
       return true; 
 
      } 
 
     }); 
 
    } 
 

 
    @Override 
 
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) { 
 
     View childView = view.findChildViewUnder(e.getX(), e.getY()); 
 
     if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) { 
 
      mListener.onItemClick(childView, view.getChildAdapterPosition(childView)); 
 
     } 
 
     return false; 
 
    } 
 

 
    @Override 
 
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { 
 
    } 
 
}

現在使用這個自定義類實現這個

1)項的點擊監聽器

使用接口適配器

public class NewRecyclerView extends RecyclerView.Adapter<NewRecyclerView.ViewHolder> { 
 

 
    private List<MyItem> myItems; 
 
    private ItemListener myListener; 
 

 
    public NewRecyclerView(List<MyItem> items, ItemListener listener) { 
 
     myItems = items; 
 
     myListener = listener; 
 
    } 
 

 
    public void setListener(ItemListener listener) { 
 
     myListener = listener; 
 
    } 
 

 
    @Override 
 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
 
     return new ViewHolder(LayoutInflater.from(parent.getContext()) 
 
       .inflate(R.layout.LAYOUT, parent, false)); // TODO 
 
    } 
 

 
    @Override 
 
    public int getItemCount() { 
 
     return myItems.size(); 
 
    } 
 

 
    @Override 
 
    public void onBindViewHolder(ViewHolder holder, int position) { 
 
     holder.setData(myItems.get(position)); 
 
    } 
 

 
    public interface ItemListener { 
 
     void onItemClick(MyItem item); 
 
    } 
 

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

 
     // TODO - Your view members 
 
     public MyItem item; 
 

 
     public ViewHolder(View itemView) { 
 
      super(itemView); 
 
      itemView.setOnClickListener(this); 
 
      // TODO instantiate/assign view members 
 
     } 
 

 
     public void setData(MyItem item) { 
 
      this.item = item; 
 
      // TODO set data to view 
 
     } 
 

 
     @Override 
 
     public void onClick(View v) { 
 
      if (myListener != null) { 
 
       myListener.onItemClick(item); 
 
      } 
 
     } 
 
    }

在你activty或片段實施NewRecyclerView.ItemListener會給項目處位置

recyclerView.addOnItemTouchListener( 
 
    new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() { 
 
     @Override public void onItemClick(View view, int position) { 
 
     // TODO Handle item click 
 
     } 
 
    }) 
 
);

2)獲取項目點擊位置

相關問題