ListView项目单击无法在Tabhost中工作

时间:2016-09-30 02:22:44

标签: android android-layout listview android-studio android-fragments

我的问题是onItemClickListener无法在Tabhost下的Listview上运行。任何listview的onclick事件都不起作用。我曾尝试过一切。这是我的代码。请帮帮我。

我的Tabhost文件

Character.isLetter()

然后是主要布局

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

这是列表适配器

<?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>

这里是主要的片段类

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

请帮帮我。我的点击没有任何标签......

1 个答案:

答案 0 :(得分:2)

尝试设置点击监听器,如下所示:

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

如果它改变了你的任何内容,请告诉我。

编辑:我刚刚意识到您认为onClick不起作用的原因可能是因为您没有显示您的祝酒词。您需要通过添加 .show()来显示祝酒词:

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