列表视图onClickListener未在抽屉布局工作 [英] Listview onClickListener is not working in Drawer Layout
问题描述
我制作具有的ListView
中有一个抽屉里。但出乎意料的是它没有得到任何 onClickListener
当我点击任何一个列表项被解雇了。
I am making a drawer having ListView
in it. But surprisingly its not getting any onClickListener
fired when I click on any list item.
我曾尝试这些事情到现在为止
- 将
ArrayAdapter
为的ListView
,并得到根本list.onItemClickListener
和list.onClickListener
了。 - 与自定义适配器实施
OnClickListener
- Making a
ArrayAdapter
forListView
and get simplylist.onItemClickListener
andlist.onClickListener
too. - A custom adapter with a implemented
OnClickListener
也为布局就算了,我已经试过这些方式
Also for layouts even, I have tried these ways
- 与父母布局
的TextView
- 布局与
RelativeLayout的
父
- A layout with a parent
TextView
- Layout with
RelativeLayout
as parent
试图从上面每一种情况下,我没有什么工作至今。我想,code运作良好,但可能有一些问题与布局,但乱投医我仍然毫无头绪。
Trying 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;
}
这是我的活动
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);
下面是活性听众甚至
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!
修改
我已经做了一些进一步抬头,看看有什么发生的事情,把按钮
在列表视图
。在抽屉中的按钮甚至没有获得上pressed
颜色和徘徊在pressing它。在所有的,布局是非常无响应或像不给任何反馈。
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.
推荐答案
感谢大家给予的建议针对此问题。但我发现这种异常行为的原因。这是因为在活动的XML文件(而不是项目列表XML)定义的 RelativeLayout的
(外抽屉布局)。在去除code,点击工作原理魅力。 :)对不起,不包括code在这里..
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..
您需要的设置中的FrameLayout 抽屉上面的列表视图,而不是相对布局!
You have to set a fragment in FrameLayout above drawer list view instead Relative Layout!
这篇关于列表视图onClickListener未在抽屉布局工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!