更新recyclerview会导致活动被启用两次

时间:2017-02-27 14:20:24

标签: android android-studio android-recyclerview cardview

您好我已在我的应用中实施了recyclerview。单击recyclerview中的元素后,将启动一个活动。但是我注意到一个问题,在更新了recyclerview之后,点击一个元素后,某个活动会被启动两次。

以下是我用来启动recyclerview的代码:

private void initViews(){


    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);

    recyclerView.setHasFixedSize(false);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);


    RecyclerView.Adapter adapter = new customadaptertest(stoiska, logoarray,getApplicationContext(), namestoiska);
    recyclerView.setAdapter(adapter);

    recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        GestureDetector gestureDetector = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() {

            @Override public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

        });

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if(child != null && gestureDetector.onTouchEvent(e)) {
                int position = rv.getChildAdapterPosition(child);
                Intent showmap = new Intent(Search_Engine.this, (Class) activities.get(position));
                startActivity(showmap);
                //Toast.makeText(getApplicationContext(), (CharSequence) stoiska.get(position), Toast.LENGTH_SHORT).show();
            }

            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }

    });

此initViews()用于onCreate以及单击后将使用不同参数创建recyclerview的按钮。 recyclerview列表已正确更新,但问题是每个活动都打开了两次

更新,更改了我的适配器,但现在活动根本无法启用:

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {

    viewHolder.tv_country.setText(countries.get(i));
    viewHolder.nazwafirmy.setText(countries2.get(i));
    viewHolder.tv_country.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent showmap = new Intent(v.getContext(), (Class) activities.get(i));
            v.getContext().startActivity(showmap);
        }
    });
}

4 个答案:

答案 0 :(得分:0)

如果OnClick是您需要的唯一功能,则应尝试向适配器本身添加onClickListener接口。

因此,在CustomAdapterTest类中,onBindViewHolder()方法内部将onclick功能设置为行的根视图。

实施例。将rootRowLayout视为mCardView

holder.mCardView.setOnClickListener(new OnClick....

答案 1 :(得分:0)

您不必使用捕捉触摸事件来编写所有这些复杂的内容。 创建监听器类

public interface RecyclerListener {

    void onClick();
    void onLongClick();

}

将此侦听器放入适配器构造函数

 public HistoryAdapter(List<YourData> Datas, RecyclerListener listener){
    this.orderlistDatas=orderlistDatas;
    this.listener = listener
}

在适配器调用内的onBindViewHolder方法

holder.itemView.setOnClickLisnter
holder.itemView.setOnLongLickListener

将此侦听器实现到您调用适配器的活动

答案 2 :(得分:0)

在适配器类中尝试此操作

@Override
     public void onBindViewHolder(ViewHolder holder, final int position) {
     holder.stoiska.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent showmap = new Intent(Search_Engine.this, (Class) activities.get(position));
                startActivity(showmap);
             }
     }
}

答案 3 :(得分:0)

创建一个监听器interface,然后再点击Activity,然后点击RecyclerView

所以在RecyclerViewAdapter添加:

private onRecyclerViewItemClickListener mItemClickListener;

public void setOnItemClickListener(onRecyclerViewItemClickListener mItemClickListener) {
  this.mItemClickListener = mItemClickListener;
}

public interface onRecyclerViewItemClickListener {
  void onItemClickListener(View view, int position);
}

然后在ViewHolder(我在适配器中添加内部类)中添加onClickListener到您要点击的项目,即tv_country。示例ViewHolder将是:

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

    public TextView tv_country;

    public RecyclerViewHolder(View itemView) {
        super(itemView);
        this.tv_country = (TextView) itemView.findViewById(R.id.tv_country);
        this.tv_country.setOnClickListener(this);
    }

    @Override
    public boolean onClick(View v) {
       if (mItemClickListener != null) {
         mItemClickListener.onItemClickListener(v, getAdapterPosition());
       }
    }
}

然后,在初始化Adapter时,只需在您的活动中,您现在可以拨打OnItemClickListener上的RecyclerView

adapter.setOnItemClickListener(new RecyclerViewAdapter.onRecyclerViewItemClickListener() {
                            @Override
                            public void onItemClickListener(View v, int position) {
                                switch (v.getId()) {
                                    case R.id.tv_country:
                                       Intent showmap = new Intent(Search_Engine.this, (Class) activities.get(position));
            startActivity(showmap);
                                        break;
                                }
                            }
                        });

所以这样做,将视图和位置从适配器传递回活动列表中的Activity,然后使用从适配器传递的位置来获取列表中的正确活动。这应该会带你到正确的相应事件。

进一步阅读: