Reputation: 205
My issue is onItemClickListener is not working on Listview which is under Tabhost. Any listview's onclick event is not working. i had tried everything. here is my code. please help me with this.
My Tabhost file
<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......
Upvotes: 0
Views: 375
Reputation: 2872
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. You need to show toast by adding .show() like this:
Toast.makeText(getActivity(), "this is on click event", Toast.LENGTH_SHORT).show();
Upvotes: 2