2014-10-02 57 views
2

我有一個ListView與自定義ArrayAdapter,創建項目列表。我試圖做到這一點,因此點擊列表項上的任何位置都會執行某些操作。但是,唯一可點擊的區域位於文本區域之外。TextView阻止單擊ListView

紅色的左側,右側和頂部之外是可點擊的。

http://i.stack.imgur.com/CJuLm.png

主要活動XML:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffd7d7d7"> 
    <!-- The main content view --> 

     <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent"> 
      <ListView 
       android:layout_gravity="center" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/cities_list" 
       android:drawSelectorOnTop="true" 
       android:divider="@android:color/transparent" 
       android:smoothScrollbar="true" 
       android:descendantFocusability="blocksDescendants"/> 

     </FrameLayout> 


    <!-- The navigation drawer --> 
    <ListView android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="4dp" 
     android:background="#ffd7d7d7"/> 

</android.support.v4.widget.DrawerLayout> 

項目XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:clickable="false" 
    android:focusable="false" 
    > 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="2dp" 
     android:showDividers="beginning" 
     android:background="@drawable/city_list_item_background" 
     android:baselineAligned="true" 
     android:padding="8dp" 
     android:clickable="false" 
     android:focusable="false" 
     > 


     <TextView 
      android:id="@+id/tvCity" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="City" 
      android:textSize="24sp" 
      android:clickable="false" 
      android:focusable="false" 
      android:background="#FF0000" 
      android:enabled="false" 
      android:editable="false" 
      android:textColor="#000000" /> 

     <TextView 
      android:id="@+id/tvCountry" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Country" 
      android:clickable="false" 
      android:focusable="false" 
      android:background="#FF0000"/> 

    </LinearLayout> 
</RelativeLayout> 

適配器類別:

public class CitiesAdapter extends ArrayAdapter<NewCity> { 
    public CitiesAdapter(Context context, ArrayList<NewCity> cities) { 
     super(context, R.layout.item_city, cities); 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 




     // get the data item for this position 
     NewCity cities = getItem(position); 

     // Check if an existing view is being reused, otherwise inflate the view 
     if (convertView == null) { 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_city, parent, false); 
     } 

     // lookup for data population 
     TextView tvCity = (TextView) convertView.findViewById(R.id.tvCity); 

     TextView tvCountry = (TextView) convertView.findViewById(R.id.tvCountry); 

     // populate the data into the template view using the ata object 
     tvCity.setText(cities.city); 
     tvCountry.setText(cities.country); 




     // return the completed view to render on screen 
     return convertView; 

    } 



    @Override 
    public boolean areAllItemsEnabled() { 
     return true; 
    } 

    @Override 
    public boolean isEnabled(int arg0) { 
     return true; 
    } 
} 

主要活動:

public class MainActivity 
     extends Activity { 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerList; 
    private ActionBarDrawerToggle mDrawerToggle; 

    private CharSequence mDrawerTitle; 
    private CharSequence mTitle; 
    private String[] mMenuItems; 

    public static class MyPlacesFragment 
      extends Fragment { 

     public MyPlacesFragment() { 

      // Empty constructor required for fragment subclasses 
     } 


     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.my_places, container, false); 
      String menu_title = getResources().getStringArray(R.array.menu_items)[0]; 

      getActivity().setTitle(menu_title); 
      return rootView; 
     } 

    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction ft = fragmentManager.beginTransaction(); 

     //Fragment f0 = new MyPlacesFragment(); 
     //ft.replace(R.id.content_frame, f0); 
     //ft.commit(); 

     populateCitiesList(); 


     mTitle = mDrawerTitle = getTitle(); 
     mMenuItems = getResources().getStringArray(R.array.menu_items); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.left_drawer); 

     // set a custom shadow that overlays the main content when the drawer opens 
     mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
     // set up the drawer's list view with items and click listener 
     mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.drawer_list_item, mMenuItems)); 
     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

     // enable ActionBar app icon to behave as action to toggle nav drawer 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 

     // ActionBarDrawerToggle ties together the the proper interactions 
     // between the sliding drawer and the action bar app icon 
     mDrawerToggle = new ActionBarDrawerToggle(
       this,     /* host Activity */ 
       mDrawerLayout,   /* DrawerLayout object */ 
       R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ 
       R.string.drawer_open, /* "open drawer" description for accessibility */ 
       R.string.drawer_close /* "close drawer" description for accessibility */ 
     ) { 
      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(mTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 

      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(mDrawerTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 
     }; 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     if (savedInstanceState == null) { 
      selectItem(0); 
     } 
    } 

    private void populateCitiesList() { 
     // Construct the data source 
     ArrayList<NewCity> arrayOfUsers = NewCity.getUsers(); 
     // Create the adapter to convert the array to views 
     CitiesAdapter adapter = new CitiesAdapter(this, arrayOfUsers); 
     // Attach the adapter to a ListView 
     ListView listView = (ListView) findViewById(R.id.cities_list); 
     listView.setAdapter(adapter); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View v, int position, 
            long id) 
      { 
       //Tweet theTweet = (Tweet)parent.getAdapter().getItem(position); 
       //saved.insert(theTweet); 
       Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 

任何意見,將不勝感激。

回答

1

我想你需要從你的listView中刪除android:descendantFocusability =「blocksDescendants」。如果我理解正確,我相信它會阻止textview獲取焦點/點擊並將其傳遞給父項。也許試着將它設置爲「beforeDescendants」。

http://developer.android.com/reference/android/view/ViewGroup.html#FOCUS_BEFORE_DESCENDANTS

如果你不能找到適合自己的解決方案,一個解決辦法將是一個OnClickListener設置爲執行相同的功能的ListView TextView的。

0

設置android:clickable="false"android:focusable="false"刪除這些,試試這個新:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    > 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="2dp" 
     android:showDividers="beginning" 
     android:background="@drawable/city_list_item_background" 
     android:baselineAligned="true" 
     android:padding="8dp" 
     > 


     <TextView 
      android:id="@+id/tvCity" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="City" 
      android:textSize="24sp" 
      android:background="#FF0000" 
      android:enabled="false" 
      android:editable="false" 
      android:textColor="#000000" /> 

     <TextView 
      android:id="@+id/tvCountry" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Country" 
      android:background="#FF0000"/> 

    </LinearLayout> 
</RelativeLayout>