我的导航抽屉中嵌入了RecyclerView。问题是,物品不可点击,也不能对它们施加任何效果(涟漪等)。
MainActivity.java
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_drawer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<include layout="@layout/content_view" />
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_category_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|left"
android:layout_marginTop="@dimen/nav_header_height"
/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Recycler Item layout
<LinearLayout
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/abc_list_item_padding_horizontal_material"
android:paddingRight="@dimen/abc_list_item_padding_horizontal_material"
android:paddingTop="@dimen/abc_dialog_list_padding_vertical_material"
android:paddingBottom="@dimen/abc_dialog_list_padding_vertical_material"
android:background="@drawable/category_selector">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/category_icon"
android:layout_width="@dimen/post_icon_size"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginRight="@dimen/abc_action_bar_content_inset_material"
android:layout_marginEnd="@dimen/abc_action_bar_content_inset_material"
android:tint="#A0A0A0"/>
<TextView
android:id="@+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:fontFamily="sans-serif-condensed"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
android:textColor="@color/cat_text_selector"/>
</LinearLayout>
drawable-v21\category_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<ripple android:color="@color/colorAccent" />
</item>
<item android:state_activated="true"
android:drawable="@color/colorAccent" />
<item android:drawable="@android:color/transparent" />
</selector>
通过适配器填充回收站的代码工作正常。
适配器代码,其中设置了点击侦听器
public interface OnItemClickListener{
public void onClick(View view, int position);
}
public CategoryAdapter(Context context) {
super();
mContext = context;
mClickListener = (OnItemClickListener)context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public View mView;
public ViewHolder(View view) {
super(view);
mView = view;
}
}
@Override
public CategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_menu_item_category, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(CategoryAdapter.ViewHolder holder, final int position){
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mClickListener.onClick(v, position);
}
});
}
MainActivity中定义了clickhandler的代码
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>, CategoryAdapter.OnItemClickListener {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
mAdapter = new CategoryAdapter(this);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_category_menu);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
}
@Override
public void onClick(View view, int position) {
Toast.makeText(this, Integer.toString(position), Toast.LENGTH_LONG);
}
}