activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="#cbd1d1" > <ListView android:id="@+id/lv" android:layout_width="175dp" android:layout_height="wrap_content" android:background="#a5afa3" /> <!-- android:listSelector Drawable used to indicate the currently selected item in the list. May be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name". May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb". This corresponds to the global attribute resource symbol listSelector. --> <!-- android:cacheColorHint Indicates that this list will always be drawn on top of solid, single-color opaque background. This allows the list to optimize drawing. Must be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb". This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type. This corresponds to the global attribute resource symbol cacheColorHint. --> <!-- android:listSelector="@android:color/transparent" Only this attribute is work fine to disable ListView item click highlight --> <ListView android:id="@+id/lv_second" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_toRightOf="@id/lv" android:background="#d113b7" android:listSelector="@android:color/transparent" android:cacheColorHint="@android:color/transparent" /> <ListView android:id="@+id/lv_third" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_toRightOf="@id/lv_second" android:background="#3ea93d" /> </RelativeLayout>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.RelativeLayout; import android.widget.ListView; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity { private Context mContext; private Activity mActivity; private RelativeLayout mRelativeLayout; private ListView mListView; private ListView mListViewSecond; private ListView mListViewThird; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the application context mContext = getApplicationContext(); // Get the activity mActivity = MainActivity.this; // Get the widgets reference from XML layout mRelativeLayout = (RelativeLayout) findViewById(R.id.rl); mListView = (ListView) findViewById(R.id.lv); mListViewSecond = (ListView) findViewById(R.id.lv_second); mListViewThird = (ListView) findViewById(R.id.lv_third); // Initialize an array of animals String[] animals = new String[]{ "Red deer", "Salamander", "Sea urchin", "Sea lion", "Squirrel", "Turkey" }; // Create a list from string array elements List<String> animals_list = new ArrayList<String>(Arrays.asList(animals)); // Initialize an ArrayAdapter object from the list ArrayAdapter<String> adapter = new ArrayAdapter<String>( mContext,android.R.layout.simple_list_item_1, animals_list ); // Populate the ListView widgets with ArrayAdapter mListView.setAdapter(adapter); mListViewSecond.setAdapter(adapter); mListViewThird.setAdapter(adapter); /* public void setSelector (int resID) Set a Drawable that should be used to highlight the currently selected item. Parameters resID int: A Drawable resource to use as the selection highlight. */ // Disable the ListView item click highlight programmatically mListViewThird.setSelector(new ColorDrawable(Color.TRANSPARENT)); } }
Komentar
Posting Komentar