在RecyclerView中,触摸没有涟漪效果

10

我有一个包含四个网格元素(2*2)的RecyclerView,它像菜单一样工作。然而,当我点击它们时,没有出现涟漪效果。它只是直接跳转到下一个活动,没有任何视觉确认视图被按下。有人可以帮忙吗?

MainActivity

public class MainActivity extends AppCompatActivity implements MainMenuAdapter.OnItemClickListener {

    Toolbar toolbar;
    private static List<ViewModel> tileItems = new ArrayList<>();

    static {
        tileItems.add(new ViewModel("Activity", "#3F51B5", R.drawable.activity));
        tileItems.add(new ViewModel("Profile", "#E91E63", R.drawable.profile));
        tileItems.add(new ViewModel("Training", "#FF5722", R.drawable.training));
        tileItems.add(new ViewModel("Diet", "#4CAF50", R.drawable.diet));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        MainAdapter adapter = new MainAdapter(tileItems, MainActivity.this);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);


        // Toolbar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }

    @Override
    public void onBackPressed() {
        this.moveTaskToBack(true);
//        this.finishAffinity();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override public void onItemClick(View view, ViewModel viewModel) {
    }
}

主适配器

public class MainAdapter extends RecyclerView.Adapter<MainMenuAdapter.ViewHolder> implements View.OnClickListener {

    private List<ViewModel> items;
    private OnItemClickListener onItemClickListener;
    private Context context;

    // Adapter constructor
    public MainAdapter(List<ViewModel> items, Context context) {
        this.items = items;
        this.context = context;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item, parent, false);
        return new ViewHolder(v);
    }

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

        final ViewModel dataItem = items.get(position);
        viewHolder.colorBlock.setBackgroundColor(dataItem.getColor());
        viewHolder.menuName.setText(dataItem.getName());
        viewHolder.menuIcon.setImageResource(dataItem.getImage());
        viewHolder.itemView.setTag(dataItem);
        if (dataItem.getActivity() != null) {
            viewHolder.colorBlock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i = new Intent(context, dataItem.getActivity());
                    context.startActivity(i);

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    @Override public void onClick(final View v) {
        // Give some time to the ripple to finish the effect
        if (onItemClickListener != null) {
            new Handler().postDelayed(new Runnable() {
                @Override public void run() {
                    onItemClickListener.onItemClick(v, (ViewModel) v.getTag());
                }
            }, 200);
        }
    }

    /** This is our ViewHolder class */
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView menuName;
        public View colorBlock;
        public ImageView menuIcon;

        public ViewHolder(View convertView) {
            super(convertView); // Must call super() first

            menuName = (TextView) convertView.findViewById(R.id.menuName);
            colorBlock = (View) convertView.findViewById(R.id.colorBlock);
            menuIcon = (ImageView) convertView.findViewById(R.id.menuItem);
        }
    }

    public interface OnItemClickListener {

        void onItemClick(View view, ViewModel viewModel);

    }
}

活动主界面.xml

<RelativeLayout
    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"
    tools:context=".MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_below="@id/toolbar"
        android:layout_width= "match_parent"
        android:layout_height = "match_parent" />

</RelativeLayout>

主要项目.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?android:attr/selectableItemBackground">

    <View
        android:id="@+id/colorBlock"
        android:layout_width="match_parent"
        android:layout_height="170dp" />

    <ImageView
        android:id="@+id/menuItem"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_centerHorizontal="true"
        android:padding="16dp"
        />

    <TextView
        android:id="@+id/menuName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:textColor="@android:color/white"
        android:textSize="16sp"/>
</RelativeLayout>

涟漪效果不会自动发生,它只是一个可绘制对象,您需要将其放置在某些视图中。https://developer.android.com/reference/android/graphics/drawable/RippleDrawable.html - Budius
1个回答

16

如果您将main_item.xml的背景设置为?android:selectableItemBackground?selectableItemBackground,涟漪效应将发生(@Budius在他的评论中是错误的)。我找到了第二个的参考资料。但是,AndroidStudio警告说这是com.android.support:design中的私有属性。当尝试使用该私有版本时,我的应用程序崩溃了。我猜测了带有“android:”前缀的第一个选项,结果它起作用了。

也许您无意中设置了android:foreground?我尝试了一下,但我的RecyclerView项目没有发生任何变化。

更新后的RelativeLayout如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:background="?android:selectableItemBackground">

我还看到android:background="?android:attr/selectableItemBackground"可以用于触发涟漪效果。我还要提醒的是,selectableItemBackground不需要在main_item.xml的根元素上使用。我在根项中使用背景颜色,然后在嵌套的ViewGroup上设置selectableItemBackground。

我的答案来自于一个没有使用material design appcompat的参考框架。如果您使用material design appcompat支持库,则可能会有所不同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接