这是我的MainActivity RecyclerView addOnItemTouchListener实现。
recyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(this, recyclerView, new NewAlbumAdapter.RecyclerViewClickListener() {
@Override
public void onClick(View view, int position) {
String[] allImagePathListInAnAlbum;
Log.e("Item Clicked is","++++ "+allAlbumsName[position]);
Log.e("Item Number is", "++++ " +String.valueOf(position));
allImagePathListInAnAlbum=filePathToAllAlbums[position].list();
Intent insideAlbumIntent=new Intent(getApplicationContext(), ImagesInsideAlbumMainActivity.class);
insideAlbumIntent.putExtra("AllImagePath",allImagePathListInAnAlbum);
startActivity(insideAlbumIntent);
}
@Override
public void onLongClick(View view, int position) {
Toast.makeText(getApplicationContext(),allAlbumsName[position]+" Is Long clicked\nAt Position: "+String.valueOf(position),Toast.LENGTH_SHORT).show();
}
}));
这是我实施的RecyclerViewTouchListener。
public class RecyclerViewTouchListener implements RecyclerView.OnItemTouchListener{
private GestureDetector gestureDetector;
private NewAlbumAdapter.RecyclerViewClickListener clickListener;
public RecyclerViewTouchListener(Context context, final RecyclerView recyclerView, final NewAlbumAdapter.RecyclerViewClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) { }
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { }
}
当我点击以前在RecyclerView中添加的项目时,点击工作正常。只调用一次onClick。
但是当我将一个项目添加到RecyclerView并单击新创建的项目时,会调用onClick方法两次。
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
从中返回False。它在RecyclerView中使用此方法: 我运行了调试器,我评论了一些事情的价值观:
if (action != MotionEvent.ACTION_DOWN) //action:1
{
final int listenerCount = mOnItemTouchListeners.size(); //listenerCount=2
//While for previously added items it was 1 only.
for (int i = 0; i < listenerCount; i++) {
final OnItemTouchListener listener = mOnItemTouchListeners.get(i);
if (listener.onInterceptTouchEvent(this, e)) {//e:MotionEvent {action==ACTION_UP
mActiveOnItemTouchListener = listener;
return true;
}
但是,如果点击之前添加的项目,那么它的效果非常好。
我只想要一次调用OnClick。但它被称为2次。
我正在将文件夹添加到目录并尝试通过以下方式更新recyclerView的内容:
public void onFinishEditDialog(String inputText) {
newAlbumName = inputText;
initAllVariables();
initRecyclerViewForAlbumView();
}
重新初始化所有变量和
public void initRecyclerViewForAlbumView() {
RecyclerView recyclerView;
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this, 2);
//recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount,spacing,includeEdge));
recyclerView.setLayoutManager(gridLayoutManager);
newAlbumAdapter = new NewAlbumAdapter(recyclerView.getContext(),filePathToAllAlbums,allAlbumsName,countOfAlbums,firstImageOfAllFile);
recyclerView.setAdapter(newAlbumAdapter);
recyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(this, recyclerView, new RecyclerViewClickListener() {
@Override
public void onClick(View view, int position) {
String[] allImagePathListInAnAlbum;
Log.e("Item Clicked is", "++++ " + allAlbumsName[position]);
Log.e("Item Number is", "++++ " + String.valueOf(position));
allImagePathListInAnAlbum = filePathToAllAlbums[position].list();
Intent insideAlbumIntent = new Intent(getApplicationContext(), ImagesInsideAlbumMainActivity.class);
insideAlbumIntent.putExtra("AllImagePath", allImagePathListInAnAlbum);
startActivity(insideAlbumIntent);
}
@Override
public void onLongClick(View view, int position) {
Toast.makeText(getApplicationContext(), allAlbumsName[position] + " Is Long clicked\nAt Position: " + String.valueOf(position), Toast.LENGTH_SHORT).show();
}
}));
}
我有两个问题:
1&GT;有没有其他方法可以更新RecyclerView并在Activity上显示新添加的目录。 (我使用了所有通知方法,但它们似乎不起作用)。
2 - ;如果我在OnTouchItemListener
中重新初始化RecyclerView,则onFinishEditDialog
中的onClick会被调用两次。如果我不这样做,OnClick工作正常,但新创建的目录将不会显示在Activity上。
请帮助解决此问题。 如果有人能提供帮助,我将非常感激。