我有一个带有自定义ArrayAdapter的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();
}
} );
}
任何建议都将不胜感激。
答案 0 :(得分:1)
我认为你需要删除android:descendantFocusability =&#34; blocksDescendants&#34;从你的listView。如果我理解正确,我相信它会阻止textview获得焦点/点击并将其传递给父母。也许尝试将它设置为&#34; beforeDescendants&#34;。
http://developer.android.com/reference/android/view/ViewGroup.html#FOCUS_BEFORE_DESCENDANTS
如果找不到适合您的解决方案,可以尝试将OnClickListener设置为执行与ListView相同功能的textview。
答案 1 :(得分: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>