Reputation:
I am making a drawer having ListView
in it. But surprisingly its not getting any onClickListener
fired when I click on any list item.
I have tried these things till now
ArrayAdapter
for ListView
and get simply list.onItemClickListener
and list.onClickListener
too.OnClickListener
Also for layouts even, I have tried these ways
TextView
RelativeLayout
as parentTrying each case from above, I got nothing working till now. I guess that code is working well but might be some issue with layout, but trying everything I am still clueless.
Here is my current item layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/image"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/image"
android:text="@string/app_label"
android:paddingTop="14dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:contentDescription="@string/app_label"
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="10dp"
android:src="@drawable/thumb" />
</RelativeLayout>
Here is my Custom Adapter
/****** Depends upon data size called for each row , Create each ListView row *****/
@SuppressLint("SimpleDateFormat")
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
if (convertView == null) {
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.drawer_list_item, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.text);
holder.image = (ImageView) vi.findViewById(R.id.image);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (data.size() <= 0) {
holder.text.setText("No Music Found!");
} else {
tempValues = null;
tempValues = (ListModel) data.get(position);
String txt = tempValues.getCompanyName().toString();
holder.text.setText(txt);
vi.setOnClickListener(new OnItemClickListener(position));
// Set Values and listeners her
}
return vi;
}
/********* Called when Item click in ListView ************/
private class OnItemClickListener implements OnClickListener {
private int mPosition;
OnItemClickListener(int position) {
mPosition = position;
}
@Override
public void onClick(View arg0) {
Toast.makeText(activity, "rgrg", Toast.LENGTH_SHORT).show();
HomeActivity sct = (HomeActivity) activity;
sct.onDrawerItemClick(mPosition);
}
}
@Override
public void onClick(View arg0) {
// Default onClick
Toast.makeText(activity, "rgrg", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.i("Long", "msg");
return true;
}
And here is my activity
mPlanetTitles = getResources().getStringArray(R.array.planets);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
setListData();
DrawerAdapter adapter = new DrawerAdapter(this,
CustomListViewValuesArr, getResources());
mDrawerList.setAdapter(adapter);
mDrawerList.setClickable(true);
Here is a listener in activity even
public void onDrawerItemClick(int mPosition) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "fgsg", Toast.LENGTH_SHORT).show();
}
I am getting no Log and no Toast which shows that nothing is clicked. After trying a lot, decided to get some help from you guys!
EDIT
I have did some further look up to see what happening and put a button
in listview
. In drawer the button is not even getting onPressed
colors and hovers on pressing it. At all, the layout is very unresponsive or like not giving any feedback.
Upvotes: 0
Views: 2645
Reputation:
Thanks everyone for giving suggestion for this issue. But I have found the cause for this abnormal behaviour. It is because of RelativeLayout
(Outside Drawer Layout) defined in Activity's XML file (not item list XML). On removing that code, the click works as charm. :) Sorry for not including that code here..
You have to set a fragment in FrameLayout above drawer list view instead Relative Layout!
Upvotes: 0
Reputation: 966
You need to set you list item click listener through ListView.setOnItemClickListener(...)
Upvotes: 0
Reputation: 12368
Instead of putting the on click listener on view, do it on relativelayout after initializing it
Upvotes: 0
Reputation: 38131
In your RelativeLayout
set android:clickable="true"
. Or, a better approach would be to set an OnItemClickListener
on your ListView
.
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO: do your stuff
}
});
Upvotes: 1