【问题标题】:Unable to access view to change background color无法访问视图以更改背景颜色
【发布时间】:2019-01-15 04:48:03
【问题描述】:

我正在构建一个简单的列表视图,它接收一系列检测到的项目并将它们添加到列表中。我希望能够在单击它们时更改这些视图的颜色。但是,我正在努力创建一种访问单个视图的方法,以便我可以更改其颜色。此代码基于 Android Studio 示例 BluetoothLeGatt。我曾尝试查看像 here 这样的地方,但无济于事。

类将检测到的设备添加到列表中

private class LeDeviceListAdapter extends BaseAdapter { 
        private ArrayList<BluetoothDevice> mLeDevices;
        private LayoutInflater mInflator;

        public LeDeviceListAdapter() {
            super();
            mLeDevices = new ArrayList<BluetoothDevice>();
            mInflator = DeviceScanActivity.this.getLayoutInflater();
        }

        public void addDevice(BluetoothDevice device) {
            if (!mLeDevices.contains(device)) {
                mLeDevices.add(device);
            }
        }

        public BluetoothDevice getDevice(int position) {
            return mLeDevices.get(position);
        }

        public void clear() {
            mLeDevices.clear();
        }

        @Override
        public int getCount() {
            return mLeDevices.size();
        }

        @Override
        public Object getItem(int i) {
            return mLeDevices.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            // General ListView optimization code.
            if (view == null) {
                view = mInflator.inflate(R.layout.listitem_device, null);
                viewHolder = new ViewHolder();
                viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
                viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            BluetoothDevice device = mLeDevices.get(i);
            final String deviceName = device.getName();
            if (deviceName != null && deviceName.length() > 0) {
                viewHolder.deviceName.setText(deviceName);
            } else {
                viewHolder.deviceName.setText(R.string.unknown_device);
            }
            viewHolder.deviceAddress.setText(device.getAddress());
            return view;
        }
    }

OnListClick 代码

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
    if (device == null) return;
    getListView().getItemAtPosition(position);
//change background color here.
}

在这里,我应该能够使用 getListView().getItemAtPosition(position); 访问各个元素,但我不知道如何使用它来更改背景颜色,因为它不提供我访问视图本身。

视图列表的 XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/device_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"/>
    <TextView android:id="@+id/device_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"/>
</LinearLayout>

如果有人能提出改变 Android Studio 实现的方法,我将不胜感激。由于我对 Android 开发不熟悉,我正在尝试使用此代码。

【问题讨论】:

    标签: java android view


    【解决方案1】:

    您拥有的 OnListClick 代码(查看 v) 使用这个 v 你可以访问与点击视图相关的属性。

    【讨论】:

      【解决方案2】:

      您在此方法中第二次获取 View 参数

      @Override
      protected void onListItemClick(ListView l, View v, int position, long id) {
        v.setBackgroundColor(Color.parseColor("#ffffff"));
      }
      

      如果你想改变 onTouch 颜色,使用这个

      在您的 drawable 文件夹中创建一个 bg.xml

      <?xml version="1.0" encoding="utf-8"?> 
        <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
            <item android:state_focused="true" android:state_pressed="true" 
                  android:drawable="@drawable/bgalt" /> 
            <item android:state_focused="false" android:state_pressed="true" 
                  android:drawable="@drawable/bgalt" /> 
            <item android:drawable="@drawable/bgnorm" /> 
        </selector>
      

      将此drawable设置为父布局元素

      <LinearLayout
          background="@drawable/bg"    
          clickable="true"
          ...>
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-15
        • 2018-06-11
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        • 2016-10-04
        相关资源
        最近更新 更多