I have a ListView with a custom ArrayAdapter that creates a list of items. I am trying to make it so clicking anywhere on the list item will do something. However the only clickable area is outside the text area.
Outside the left, right, and top of the red is clickable.
Main Activity XML:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://ift.tt/nIICcg"
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>
Item XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:focusable="false"
>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
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>
Adapter Class:
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;
}
}
Main Activity:
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();
}
} );
}
Any suggestions would be appreciated.
No comments:
Post a Comment