小编nab*_*bir的帖子

有没有更好的方法从适配器获取对父级RecyclerView的引用?

我有一个用例,我需要RecyclerView从适配器内部引用父,特别是在onBindViewHolder方法内.到目前为止,我正在做的是将它分配给onCreateViewHolder传递viewGroup parentarg 的方法中的私有类成员,如下所示:

private ViewGroup mParent;

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // inflater logic.
    mParent = parent;
}
Run Code Online (Sandbox Code Playgroud)

并引用父RecyclerViewonBindViewHolder这样的:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // binder logic.
    ((RecyclerView)mParent).blahBlahBlah();
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法呢?也许RecyclerView.Adapter有一种我可能错过的方式?

android android-adapter android-recyclerview

72
推荐指数
1
解决办法
2万
查看次数

将ListView或RecyclerView添加到新的NavigationView

我正在使用Google 支持库版本22.2.0中的新NavigationView.它可以很好地生成使用菜单res填充的导航抽屉.

我想知道是否可以将ListView或RecyclerView添加到导航抽屉,以便可以使用我的自定义适配器代码填充它,这允许比菜单资源更大的灵活性.

这是我目前的XML:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/main_toolbar" />

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_drawer_header"
        app:menu="@menu/menu_navigation_drawer" />


</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

在我的XML中,我会添加ListView或RecyclerView吗?

编辑

根据Basant的建议,我将ListView嵌套到NavigationView中.你失去了从菜单res(据我所知)膨胀的能力,但它成功地按照我的意愿去做.标头XML未更改,它只包含在XML中.

新代码:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/main_toolbar" />

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/navigation_drawer_header_include"
                layout="@layout/navigation_drawer_header" />

            <ListView
                android:id="@+id/navigation_drawer_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/navigation_drawer_header_include"/>

        </RelativeLayout>

    </android.support.design.widget.NavigationView> …
Run Code Online (Sandbox Code Playgroud)

android listview android-recyclerview navigationview

20
推荐指数
3
解决办法
3万
查看次数

onClick从ImageView中删除涟漪效果

我有一个简单的ImageView包装在FrameLayout中,以给它一个涟漪效果:

<FrameLayout
    android:id="@+id/container"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:src="@drawable/image"/>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

这非常适合添加涟漪效果.然而,当我向ImageView添加onClick侦听器时,纹波效果不再起作用,考虑到涟漪效应是可以选择/单击的元素,这很奇怪!

ImageView imageView = (ImageView)findViewById(R.id.image_view);
imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Logic.
    }
});
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何保持onClick和涟漪?

android imageview

1
推荐指数
1
解决办法
1076
查看次数