2013-10-19 42 views
0

我有一個自定義ListView.每當我將android:background屬性設置爲行佈局它不可點擊。此外,我爲ListView.設置了背景佈局,當我滾動它時會給出它已設置的背景,並且當它停止滾動時,它將佔用LinearLayout.的顏色。我的代碼如下所示爲ListView以及單個行佈局。我試着將android:focusable="false"放置在xml的行佈局中,但是每當我將android:background設置爲不可點擊時。每當我滾動它變白,然後變黑。另外,當我在行佈局TextView中設置android:background屬性時,它不可點擊。主要佈局如下..[已解決] ListView不可點擊與android:背景設置

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:background="#000000" 
     android:orientation="vertical" > 
     <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="#000000" 
     android:orientation="horizontal" > 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:layout_weight="1" 
     android:textColor="#ffffff" 
     android:text="hello"/> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:textColor="#ffffff" 
     android:layout_weight="1" 
     android:text="hello"/> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:layout_weight="1" 
     android:textColor="#ffffff" 
     android:text="hello"/> 
    </LinearLayout> 
    <ListView android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ></ListView> 
    </LinearLayout> 

The row layout xml 

    <?xml version="1.0" encoding="utf-8"?> 
<!-- Copyright (C) 2007 The Android Open Source Project 

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 

      http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
--> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <TextView 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:textSize="28sp" 
     android:id="@+id/text" 
     android:clickable="true" 
     android:background="#000000" //Doesn't work with this 
     android:textAlignment="center" 
     android:layout_gravity="center_vertical" 
     android:layout_width="0dip" 
     android:layout_weight="1.0" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

The MainActivity.java 



    package com.example.list; 

    import android.app.Activity; 
    import android.content.Context; 
    import android.os.Bundle; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.AdapterView; 
    import android.widget.AdapterView.OnItemClickListener; 
    import android.widget.BaseAdapter; 
    import android.widget.ListView; 
    import android.widget.TextView; 
    import android.widget.Toast; 


    /** 
    * Demonstrates how to write an efficient list adapter. The adapter used in this example binds 
    * to an ImageView and to a TextView for each row in the list. 
    * 
    * To work efficiently the adapter implemented here uses two techniques: 
    * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary 
    * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary 
    * 
    * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by 
    * getView(). This data structures contains references to the views we want to bind data to, thus 
    * avoiding calls to findViewById() every time getView() is invoked. 
    */ 
    public class MainActivity extends Activity { 

     private static class EfficientAdapter extends BaseAdapter { 
      private LayoutInflater mInflater; 

      public EfficientAdapter(Context context) { 
       // Cache the LayoutInflate to avoid asking for a new one each time. 
       mInflater = LayoutInflater.from(context); 

       // Icons bound to the rows. 
      } 

      /** 
      * The number of items in the list is determined by the number of speeches 
      * in our array. 
      * 
      * @see android.widget.ListAdapter#getCount() 
      */ 
      public int getCount() { 
       return DATA.length; 
      } 

      /** 
      * Since the data comes from an array, just returning the index is 
      * sufficent to get at the data. If we were using a more complex data 
      * structure, we would return whatever object represents one row in the 
      * list. 
      * 
      * @see android.widget.ListAdapter#getItem(int) 
      */ 
      public Object getItem(int position) { 
       return position; 
      } 

      /** 
      * Use the array index as a unique id. 
      * 
      * @see android.widget.ListAdapter#getItemId(int) 
      */ 
      public long getItemId(int position) { 
       return position; 
      } 

      /** 
      * Make a view to hold each row. 
      * 
      * @see android.widget.ListAdapter#getView(int, android.view.View, 
      *  android.view.ViewGroup) 
      */ 
      public View getView(int position, View convertView, ViewGroup parent) { 
       // A ViewHolder keeps references to children views to avoid unneccessary calls 
       // to findViewById() on each row. 
       ViewHolder holder; 

       // When convertView is not null, we can reuse it directly, there is no need 
       // to reinflate it. We only inflate a new View when the convertView supplied 
       // by ListView is null. 
       if (convertView == null) { 
        convertView = mInflater.inflate(R.layout.rowlayout, null); 

        // Creates a ViewHolder and store references to the two children views 
        // we want to bind data to. 
        holder = new ViewHolder(); 
        holder.text = (TextView) convertView.findViewById(R.id.text); 
        convertView.setTag(holder); 
       } else { 
        // Get the ViewHolder back to get fast access to the TextView 
        // and the ImageView. 
        holder = (ViewHolder) convertView.getTag(); 
       } 

       // Bind the data efficiently with the holder. 
       holder.text.setText(DATA[position]); 
       return convertView; 
      } 

      static class ViewHolder { 
       TextView text; 
      } 
     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      ListView lv = (ListView) findViewById(R.id.list); 
      lv.setFocusable(true); 
      lv.setFastScrollEnabled(true); 
      lv.setAdapter(new EfficientAdapter(this)); 
      lv.setOnItemClickListener(new OnItemClickListener() 
      { 
       @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) 
       { 
        Toast.makeText(getApplicationContext(), "Hello : " + position, Toast.LENGTH_SHORT).show(); 
       } 
      }); 

     } 

     private static final String[] DATA = { 
       "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", 
       "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", 
       "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", 
       "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese", 
       "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", 
       "Anthoriro", "Appenzell", "Aragon", "Ardi Gasna", "Ardrahan", 
       "Armenian String", "Aromes au Gene de Marc", "Asadero", "Asiago", 
       "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss", "Babybel", 
       "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", 
       "Banon", "Barry's Bay Cheddar", "Basing", "Basket Cheese", 
       "Bath Cheese", "Bavarian Bergkase", "Baylough", "Beaufort", 
       "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese", 
       "Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir", 
       "Bierkase", "Bishop Kennedy", "Blarney", "Bleu d'Auvergne"}; 
    } 
+0

你想讓textview可點擊嗎?然後將clickable屬性設置爲true。要刪除黑白問題,請將cacheColorHint屬性設置爲透明,即@android:color/transparent –

+0

謝謝@DharaShah,但可點擊必須設置爲false才能點擊。有點奇怪。 cacheColorHint解決了彩色技巧。非常感謝。 – xtreak

+0

我很高興幫助..! –

回答

1

問題已解決。對於我設置的顏色android:cacheColorHint="android:color/transparent"。爲了使它可點擊,我必須使TextView爲android:clickable="false"。我不知道爲什麼它很奇怪。

+1

並且是一個文本視圖佔用焦點,因此如果只需要觸發列表視圖的項目點擊,則必須將行佈局中的所有視圖設置爲可點擊false –