当点击项目时,为什么这个ListView会显示涟漪? (AppCompat 22)

时间:2015-05-02 23:59:52

标签: android listview appcompat-v7-r22.1

我继承了一个没有AppCompat编写的Android项目。在我使用AppCompat更新它(给它一个Material主题)的努力中,我发现列表视图中的涟漪并不起作用。原作者今天推了a commit以某种方式解决了应用程序某个区域缺少涟漪的问题,但我们都不知道为什么会发生这种情况。现在我试图在首选项屏幕上添加涟漪(完全是Java)。

我在SO上发现了一些建议将listSelector设置为透明drawable以禁用纹波的帖子,但我不确定如何设置它以启用纹波。

这里是PreferencesActivity的样子(跳过东西):

public class PreferencesActivity extends IHWActivity implements ListAdapter {
private ListView mainList;
private ArrayList<String> titles;
private ArrayList<String> subtitles;
private int newCampus;
private int newYear;
private TextView yearText;
private CheckBox notificationBox;

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

    titles = new ArrayList<String>(5);
    subtitles = new ArrayList<String>(5);
    titles.add(getString(R.string.pref_notifications));
    subtitles.add(getString(R.string.pref_notifications_desc));
    titles.add(getString(R.string.pref_change));
    subtitles.add(getString(R.string.pref_change_desc));
    titles.add(getString(R.string.pref_redownload));
    subtitles.add(getString(R.string.pref_redownload_desc));
    titles.add(getString(R.string.pref_disclaimer));
    subtitles.add(getString(R.string.pref_disclaimer_desc));
    titles.add(getString(R.string.pref_about));
    subtitles.add("");
    mainList = (ListView) findViewById(R.id.preference_list);
    mainList.setDivider(this.getResources().getDrawable(android.R.drawable.divider_horizontal_bright));
    mainList.setAdapter(this);
    newCampus = Curriculum.getCurrentCampus();
    newYear = Curriculum.getCurrentYear();


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        this.finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = this.getLayoutInflater().inflate(R.layout.list_item_preferences, null);
        TextView titleView = ((TextView) convertView.findViewById(R.id.text_preference_title));
        TextView subtitleView = ((TextView) convertView.findViewById(R.id.text_preference_subtitle));
        titleView.setText(titles.get(position));
        subtitleView.setText(subtitles.get(position));
        if (subtitles.get(position).equals("")) subtitleView.setVisibility(View.GONE);
        if (position == 0) {
            notificationBox = new CheckBox(this);
            notificationBox.setChecked(Curriculum.getNotificationsEnabled());
            notificationBox.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
            notificationBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    toggleNotifications(isChecked);
                }
            });
            ((ViewGroup) convertView).addView(notificationBox);
        }
    }
    convertView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int i = mainList.indexOfChild(v);
            if (i == 0) {
                toggleNotifications(!notificationBox.isChecked());
            } else if (i == 1) {
                showYearOptions();
            } else if (i == 2) {
                showRedownloadOptions();
            } else if (i == 3) {
                showDisclaimer();
            } else if (i == 4) {
                showAbout();
            }
        }
    });
    return convertView;
}

这里是相关的XML(所有list_item_preferences.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical"
    android:paddingRight="16dp">

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:gravity="center_vertical|left"
        android:minHeight="64dp"
        android:orientation="vertical"
        android:paddingBottom="8dp"
        android:paddingTop="8dp">

        <TextView
            android:id="@+id/text_preference_title"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:ellipsize="end"
            android:gravity="center_vertical"
            android:linksClickable="false"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textIsSelectable="false" />

        <TextView
            android:id="@+id/text_preference_subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:ellipsize="end"
            android:gravity="center_vertical"
            android:linksClickable="false"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </LinearLayout>
</LinearLayout>

有什么东西可以抑制涟漪吗?是否应该有代码打开它?原始作者在首次编写应用程序时使用自动点击选择器添加自己的一个(颜色与应用程序匹配)(因此他将获得默认的Holo蓝色选择器)。

0 个答案:

没有答案