我的线性布局只是在安卓中闪烁背景颜色,而不是保持不变。

huangapple go评论65阅读模式
英文:

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:

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:id=&quot;@+id/listcontentlayout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;horizontal&quot;
android:background=&quot;@drawable/selector&quot;&gt;

selector background drawable (the primary dark color is getting flashed):

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;selector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
&lt;!--  Active Row--&gt;
&lt;item android:state_selected=&quot;true&quot; android:state_focused=&quot;false&quot;
    android:state_pressed=&quot;false&quot; android:drawable=&quot;@color/colorAccent&quot; /&gt;
&lt;!--  Pressed Row --&gt;
&lt;item android:state_pressed=&quot;true&quot; android:drawable=&quot;@color/colorPrimaryDark&quot; /&gt; /&gt;

</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
 }

huangapple
  • 本文由 发表于 2020年10月7日 00:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64230233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定