简体   繁体   English

ListView项目单击在Tabhost中不起作用

[英]ListView item click not working in Tabhost

My issue is onItemClickListener is not working on Listview which is under Tabhost. 我的问题是onItemClickListener在Tabhost下的Listview上不起作用。 Any listview's onclick event is not working. 任何listview的onclick事件均不起作用。 i had tried everything. 我已经尝试了一切。 here is my code. 这是我的代码。 please help me with this. 请帮我解决一下这个。

My Tabhost file 我的Tabhost文件

<android.support.design.widget.TabLayout
     android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/material_blue_grey_800"
    app:tabGravity="fill"
    app:tabIndicatorColor="@color/orange"
    app:tabMode="fixed"
    app:tabSelectedTextColor="@color/orange"
    app:tabTextColor="@color/white"></android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

Then primary layout 然后是主要布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"  >

    <!--<android.support.v7.widget.RecyclerView-->
        <!--android:id="@+id/songs_recycleview"-->
        <!--android:clickable="true"-->
        <!--android:focusable="true"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent" />-->

    <!--<ListView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:id="@android:id/list"-->
        <!--android:layout_alignParentBottom="true"-->
        <!--android:layout_alignParentRight="true"-->
        <!--android:layout_alignParentEnd="true"/>-->

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/SongList"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>

Here is list adaptor 这是列表适配器

 public class LazyAdapter extends BaseAdapter {

private Activity activity;
private Fragment fragment;
private AudioListModel[] data;
private String[] test;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;

public LazyAdapter(Activity a, String[] d) {
    activity = a;
    test = d;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}
/*
  private OnItemClickListener mOnItemClickListener;

  public interface OnItemClickListener {
      public void onItemClick(View view, int position);
  }

  /*
      public LazyAdapter(Activity a,ArrayList<AudioListModel> d, OnItemClickListener OnItemClickListener) {
          activity = a;
          data = d.toArray(new AudioListModel[d.size()]);
          mOnItemClickListener=OnItemClickListener;
          inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          imageLoader = new ImageLoader(activity.getApplicationContext());
      }
  */
public LazyAdapter(Activity a, AudioListModel[] d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}


public int getCount() {
    return data.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.rawlistview, null);
    TextView text = (TextView) vi.findViewById(R.id.songname);
    //TextView song_path=(TextView)vi.findViewById(R.id.songpath);
    ImageView image = (ImageView) vi.findViewById(R.id.image);


    if (test != null) {
        text.setText(test[position]);
        imageLoader.DisplayImage(data[position].bitmap, image);
    }

    if (data != null) {
        text.setText(data[position].track);
        //song_path.setText(data[position].Song_Path);
        imageLoader.DisplayImage(data[position].bitmap, image);
    }


    return vi;
}
 }

here is primary fragment class 这是主要的片段类

  public class PrimaryFragment extends Fragment {
    RecyclerView rv;
    LazyAdapter adapter;
    ListView listView;
    View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.primary_layout, container, false);


        ArrayList<AudioListModel> songsList = SongsManager.GetSongs();

        adapter = new LazyAdapter(getActivity(), songsList.toArray(new AudioListModel[songsList.size()]));
        //rv = (RecyclerView) rootView.findViewById(R.id.songs_recycleview);

        listView = (ListView) rootView.findViewById(R.id.SongList);
        LazyAdapter ad = new LazyAdapter(getActivity(), songsList.toArray(new AudioListModel[songsList.size()]));
              /*LazyAdapter ad = new LazyAdapter(getActivity(), songsList,
                new LazyAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick( View view, int position) {
                        Toast.makeText(getActivity(), "this is on click event", Toast.LENGTH_SHORT);
                    }
                });
        */
        listView.setItemsCanFocus(false);
        listView.setAdapter(ad);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "this is on click event", Toast.LENGTH_SHORT);
            }
        });


        return rootView;//inflater.inflate(R.layout.primary_layout, null);
    }

Please help me in this. 请帮助我。 My click is not working any tab...... 我的点击没有任何标签...

Try setting your on click listener like this: 尝试像这样设置点击监听器

 listview.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?>adapter,View v, int position){ 
 Toast.makeText(getActivity(), "this is on click event", Toast.LENGTH_SHORT).show();} });

Do let me know if it changes anything for you. 让我知道它是否对您有任何改变。

EDIT: I just realised that reason you think onClick is not working could be because you are not showing your toast. 编辑:我刚刚意识到,您认为onClick不起作用的原因可能是因为您没有展示您的吐司。 You need to show toast by adding .show() like this: 您需要通过添加.show()来显示吐司,如下所示:

Toast.makeText(getActivity(), "this is on click event", Toast.LENGTH_SHORT).show();

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

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