禁用项目单击并启用recyclelerview onclick

时间:2016-12-18 14:05:48

标签: android android-recyclerview

这是我的布局xml.If relativelayout click也很好。没有获得除项目点击之外的任何点击事件。

        <RelativeLayout
                android:id="@+id/layoutItems"
                android:clickable="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/viewDivider">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/item_content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="false"
                    android:padding="8dp"></android.support.v7.widget.RecyclerView>

               </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

有一些不同的方法可以做到这一点。第一种方法是这样做:

 RecyclerView recyclerView = findViewById(R.id.recycler);
 recyclerView.addOnItemTouchListener(
 new RecyclerItemClickListener(context, recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
  @Override public void onItemClick(View view, int position) {
    // do whatever
  }

  @Override public void onLongItemClick(View view, int position) {
    // do whatever
  }
})

);

或者您可以在ViewHolder中执行此操作。

  1. 我们需要一个监听器接口。

     public interface OnItemClickListener {
           public void onClick(View view, int position);
             }
    
  2. 在RecyclerView适配器的ViewHolder类中,实现View.OnClickListener,将侦听器绑定到视图。在onClick方法中,调用OnItemClickListener接口的onClick方法。这应该从你的RecycyclerView的构造函数传入。 onclick事件的实际实现将来自包含此RecyclerView的活动或片段。这里重要的一行是clickListener.onClick(view,getPosition());其中clickListener是您的RecyclerView类中的全局变量,同样它应该从您的RecyclerView的构造函数传入。

        public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView cityName;
        public ViewHolder(View view) {
          super(view);
          cityName = (TextView) view.findViewById(R.id.city_name);
          itemView.setOnClickListener(this); // bind the listener
       }
       @Override
       public void onClick(View view) {
           clickListener.onClick(view, getPosition()); // call the onClick in the OnItemClickListener
    

    } }

  3. Activity类中的onClick实现,这里重要的一行是mAdapter.setClickListener(this);和onClick方法。 onClick方法从您的RecyclerView类中的ViewHolder的onClick方法触发,该方法传递所单击项目的视图和位置。

        public class CityActivity extends Activity implements ItemClickListener {
    
        private RecyclerView mRecyclerView;
        private CityAdapter mAdapter;
        private List<City> cities;
    
        @Override
         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_city);
         cities = CityManager.getInstance(this.getApplicationContext()).getCites();
         mRecyclerView = (RecyclerView)findViewById(R.id.list);
         mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
         mRecyclerView.setItemAnimator(new DefaultItemAnimator());
         mAdapter = new CityAdapter(cities, R.layout.row_city, this);
         mRecyclerView.setAdapter(mAdapter);
    
         mAdapter.setClickListener(this); // Bind the listener
         }
    
         @Override
         public void onClick(View view, int position) {
         // The onClick implementation of the RecyclerView item click
         final City city = cities.get(position);
          Intent i = new Intent(this, CityviewActivity.class);
          i.putExtra("city", city.name);
          i.putExtra("desc", city.description);
          i.putExtra("image", city.imageName);
          Log.i("hello", city.name);
          startActivity(i);
        }
       }