简体   繁体   English

Listview onClickListener在抽屉式布局中不起作用

[英]Listview onClickListener is not working in Drawer Layout

I am making a drawer having ListView in it. 我正在制作一个具有ListView的抽屉。 But surprisingly its not getting any onClickListener fired when I click on any list item. 但是令人惊讶的是,当我单击任何列表项时,没有触发任何onClickListener

I have tried these things till now 直到现在我都尝试过这些东西

  • Making a ArrayAdapter for ListView and get simply list.onItemClickListener and list.onClickListener too. ListView制作一个ArrayAdapter ,也可以简单地获取list.onItemClickListenerlist.onClickListener
  • A custom adapter with a implemented OnClickListener 具有实现的OnClickListener自定义适配器

Also for layouts even, I have tried these ways 甚至对于布局,我也尝试过这些方法

  • A layout with a parent TextView 具有父TextView布局
  • Layout with RelativeLayout as parent RelativeLayout为父级的布局

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;
}

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 . 我做了一些进一步的查找,看看发生了什么,并在listview放了一个button In drawer the button is not even getting onPressed colors and hovers on pressing it. 在抽屉中的按钮甚至没有得到onPressed上按下它的颜色和徘徊。 At all, the layout is very unresponsive or like not giving any feedback. 根本上,布局非常无响应,或者就像没有提供任何反馈一样。

In your RelativeLayout set android:clickable="true" . 在您的RelativeLayout设置android:clickable="true" Or, a better approach would be to set an OnItemClickListener on your ListView . 或者,更好的方法是在ListView上设置OnItemClickListener

yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO: do your stuff
    }
});

与其在视图上放置单击侦听器,不如在初始化后在relativelayout上进行操作

您需要通过ListView.setOnItemClickListener(...)设置列表项单击侦听器

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). 这是因为在Activity的XML文件(不是项目列表XML)中定义了RelativeLayout (外部抽屉布局)。 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! 您必须抽屉列表视图上方的FrameLayout中设置一个片段,而不是相对布局!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM