英文:
My linear layout only flashses a background color instead of persisting in Android
问题
我有一个带有线性布局列表的回收视图,应该可以点击。一旦选定,我希望背景颜色会改变,但是我在做这个时遇到了问题。根据我现在的代码,选定的线性布局只会闪烁一下颜色,然后恢复为白色/透明。
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listcontentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/selector">
选择器背景可绘制(主要深色颜色会闪烁一下):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 选中行 -->
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="@color/colorAccent" />
<!-- 按下行 -->
<item android:state_pressed="true" android:drawable="@color/colorPrimaryDark" />
</selector>
线性布局也在主/详细流程的回收视图中,这是onClick方法:
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
DummyContent.DummyItem item = (DummyContent.DummyItem) view.getTag();
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, item.id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
mParentActivity.getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
// ... 其他逻辑 ...
}
}
};
谢谢!如果需要更多信息,请告诉我。
英文:
I have a recycler view with a list of linearlayouts that should be clickable. Once selected I want the background color to change, but I'm having trouble doing that. With the code I have right now, the selected linearlayout just flashes a color, and then reverts back to white/transparent.
How can I make it so that once it's selected, the color stays?
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listcontentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/selector">
selector background drawable (the primary dark color is getting flashed):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active Row-->
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="@color/colorAccent" />
<!-- Pressed Row -->
<item android:state_pressed="true" android:drawable="@color/colorPrimaryDark" /> />
</selector>
The linear layout is inside of a recycler view for the master/detail flow as well, this is the onClick method
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
DummyContent.DummyItem item = (DummyContent.DummyItem) view.getTag();
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, item.id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
mParentActivity.getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
Thanks! Let me know if you need more info.
(This is what I want it to look like)
答案1
得分: 1
在LinearLayout的onClick方法中,设置linearLayout.setSelected(true)。
如果你已经为LinearLayout设置了点击监听器,请使用以下代码片段:
@Override
public void onClick(View view) {
view.setSelected(true);
//你的代码
}
英文:
On your onClick method of the LinearLayout set linearLayout.setSelected(true)
If you have set clicklistener to the LinearLayout then use the following snippet,
@Override
public void onClick(View view) {
view.setSelected(true)
//Your code
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论