为List< ListView项创建Ripple效果< &安培; > 21

时间:2016-06-30 20:04:35

标签: java android xml listview ripple

我有以下的XML,并试图在Click上创建一个连锁反应。 ListView中每个项目的背景都是黑色的,理想情况下,涟漪效应会产生某种灰色:

@Value("${spring.jpa.properties.hibernate.default_schema}")
private String HIBERNATE_DEFAULT_SCHEMA;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setMappingLocations(HIBERNATE_HBM_RESOURCES);

    Properties hibernateProperties = new Properties();
    hibernateProperties.put("hibernate.dialect", HIBERNATE_DIALECT);
    hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
    hibernateProperties.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
    hibernateProperties.put("hibernate.default_schema", HIBERNATE_DEFAULT_SCHEMA);

    sessionFactoryBean.setHibernateProperties(hibernateProperties);

    return sessionFactoryBean;
}

代码创建以下内容。请注意,我想要涵盖棒棒糖前后:

enter image description here

1 个答案:

答案 0 :(得分:2)

尝试为api pre Lollipop创建两个背景一个纹波而另一个没有纹波 在drawable中创建

ripple_gray.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_pressed="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="3dp"/>
        <solid android:color="@android:color/white"/>
    </shape>
</item>

<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="3dp"/>
        <solid android:color="@android:color/black"/>
    </shape>
</item>

在drawable-v21中创建 ripple_gray.xml

<?xml version="1.0" encoding="utf-8"?>
   <ripple xmlns:android="http://schemas.android.com/apk/res/android"
      android:color="@android:color/white">
   <item android:drawable="@android:color/black"/>
</ripple>

使用创建的ripple_gray更改XML布局背景并添加id

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/white" />

<LinearLayout
    android:id="@+id/viewId"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@drawable/ripple_gray"
    android:orientation="horizontal">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:src="@drawable/placeholder" />

<TextView
    android:id="@+id/latest_item_question"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:textSize="12sp" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/white" />

</LinearLayout>

在onCreate()或你需要的地方

的ACtivity中
findViewById(R.id.viewId).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });