Long click doesn't work with ListView - java

I'm trying to set long-click listener for ListView:
final ListView gallery=(ListView)findViewById(R.id.dialogViewImagesList);
gallery.setLongClickable(true);
gallery.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View arg0) {
Log.e("event", "long");
return true;
}
});
gallery.setAdapter(new PointImagesAdapter(bitmaps));
It's my adapter:
private class PointImagesAdapter extends ArrayAdapter<Bitmap> {
private static final int LAYOUT_ID=R.layout.adapter_point_images;
private List<Bitmap> bitmaps;
private LayoutInflater inflater;
public PointImagesAdapter(List<Bitmap> bitmaps) {
super(MainActivity.this, LAYOUT_ID, bitmaps);
this.bitmaps=bitmaps;
inflater=LayoutInflater.from(MainActivity.this);
}
#Override
public View getView(int position, View view, ViewGroup group) {
if (view==null) {
view=inflater.inflate(LAYOUT_ID, null);
}
ImageView i=(ImageView)view.findViewById(R.id.adapterPointImagesItem);
i.setScaleType(ImageView.ScaleType.CENTER);
i.setImageBitmap(bitmaps.get(position));
view.setFocusable(false);
return view;
}
}
I've tried set view.setLongClickable(true), but in this case ListView items are not clickable (simple click doesn't work). It's layout code for adapter:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:orientation="vertical" >
<ImageView
android:focusable="false"
android:layout_gravity="center"
android:layout_marginTop="5dip"
android:id="#+id/adapterPointImagesItem"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I don't understand why this code doesn't work! How can I fix it?

You have to use setOnItemLongClickListener
gallery.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
});
}
Also, If your listview's adapter extends from BaseAdapter, then you also need to set convertView.setLongClickable(true); in the getView().

Related

How to reference a view inside an element from my ListView?

So I have created a custom adapter for my ListView with three Views - TextView, ImageView and a basic View:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/AllNotesFragment">
<TextView
android:id="#+id/addNoteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="25dp"
android:textSize="18sp"
android:gravity="center"
android:fontFamily="#font/ukij_qolyazma"
android:text="+new"
/>
<ImageButton
android:id="#+id/deleteNoteImageButton"
android:layout_width="50dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="30dp"
android:textSize="24sp"
android:gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:fontFamily="#font/ukij_qolyazma"
/>
<View
android:id="#+id/underlineView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="70dp"
android:background="#color/colorMainDark" />
<ListView
android:id="#+id/notesListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="75dp"
android:layout_marginBottom="0dp">
</ListView>
I have created and instantiated the ListView
notesListView = view.findViewById(R.id.notesListView);
And filled it with a bunch of my notes
notes = (ArrayList<Note>) db.getAllNotesForDay(NotesForDayActivity.getRememberDay(),
NotesForDayActivity.getRememberMonth(),
NotesForDayActivity.getRememberYear());
for (Note note : notes) {
noteTitles.add(note.getTitle());
}
NotesListAdapter adapter = new NotesListAdapter(((NotesForDayActivity) getActivity()).getContext(), notes);
notesListView.setAdapter(adapter);
With my custom adapter:
public class NotesListAdapter extends BaseAdapter {
private static final String TAG = "NotesListAdapter";
public static Context context;
private RelativeLayout notesListRelativeLayout;
private TextView noteTitleTextView;
private ImageView tickImage;
private View underlineView;
private List<Note> notes;
// !
private View listItemsView;
public NotesListAdapter(Context context, List<Note> notes) {
this.notes = notes;
this.context = context;
}
#Override
public int getCount() {
return NotesForDayActivity.getCountNotes();
}
#Override
public Object getItem(int position) {
return notes.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
listItemsView = convertView;
if (listItemsView == null) {
listItemsView = LayoutInflater.from(NotesForDayActivity.context).inflate(R.layout.notes_list_layout, null);
}
underlineView = listItemsView.findViewById(R.id.underlineView);
notesListRelativeLayout = (RelativeLayout) listItemsView.findViewById(R.id.notesListRelativeLayout);
noteTitleTextView = (TextView) listItemsView.findViewById(R.id.noteTitleTextView);
tickImage = (ImageView) listItemsView.findViewById(R.id.tickImageView);
noteTitleTextView.setText(notes.get(position).getTitle());
return listItemsView;
}
I can access an item inside my ListView with a OnItemClickListener, but I do not know how to access a View inside that particular Item of my ListView.
So If I set my OnClick like this:
notesListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
How can I reference one of the three Views in that item. For example I need to set the visibility of my imageview:
Any help is appreciated.
Try this, OnItemClickListener will return the view, so that you can get access through that view
notesListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView tickImage = view.findViewById(R.id. tickImageView);
if(tickImage!=null){
tickImage.setVisibility(View.GONE);
}
}
});

Adapter's class getView() function not called for Android ListView

I am trying to create a ListView having a list of items, each having three components.
Title (SezonNo)
Description
An Image (SezonThumb)
Following is the code for the layout file "layout_album", how will each item in the list be looked like:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.SeasonsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imgThumbnail"
android:clickable="true"
/>
<TextView
style="#style/style_textviewsProfile"
android:layout_margin="0dp"
android:textStyle="bold"
android:id="#+id/textViewSeasonNo"
android:text="Season No: " />
<TextView
android:id="#+id/textViewDescription"
style="#style/style_textviewsProfile"
android:text="Description: " />
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#color/Gray"></View>
</LinearLayout>
</ScrollView>
The following is the code for the Activity:
public void onCreate(){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seasons);
listView_seasons=findViewById(R.id.list_seasons);
txtTitle = findViewById(R.id.txtTitle);
String[] seasonNos=new String[]{"1","2","3","4","5"};
String[] seasonDescriptions=new String[]{"a","b","c","d","e"};
int[] seasonThumbs= new int[]{R.drawable.sezon1, R.drawable.sezon2, R.drawable.flag, R.drawable.flag, R.drawable.flag};
adapter=new ListAdapter(this,seasonNos,seasonDescriptions,seasonThumbs);
listView_seasons.setAdapter(adapter);
}
//____________________________Adapter Class________________________//
public class ListAdapter extends ArrayAdapter<String> {
Context context;
String sezonNo[];
String desc[];
int[] imgs;
public ListAdapter(#NonNull Context context, String[] sezonNo, String[] desc, int[] imgs) {
super(context,R.layout.layout_album,R.id.textViewSeasonNo,sezonNo);
this.context=context;
this.sezonNo = sezonNo;
this.desc = desc;
this.imgs = imgs;
Log.d("List","Adapter");
}
#Override
public int getCount() {
return 0;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
Log.d("List","setting bindings...");
view=getLayoutInflater().inflate(R.layout.layout_album,null);
TextView txtSezonNo=view.findViewById(R.id.textViewSeasonNo);
TextView txtDescription=view.findViewById(R.id.textViewDescription);
ImageView imgThumbnail=view.findViewById(R.id.imgThumbnail);
progressDialog.hide();
txtSezonNo.setText(seasonNos[position]);
txtDescription.setText(seasonDescriptions[position]);
imgThumbnail.setImageResource(seasonThumbs[position]);
return view;
}
}
The Constructor of Adapter class is getting called but getView() not called at all.
your getView() is not called because the size you returned in getCount() is 0
please in your getCount() put this :
#Override
public int getCount() {
return sezonNo.length;
}
also getItemId should not be 0 for all, simple solution could be return position of item in the list. return position like
#Override
public long getItemId(int position) {
return position;
}

setOnItemClickListener not working

My Listview app gets its data and background color of itemview from custom adapter ListAdapter.class.i also need to set the currently selected list items value in a textview below listview,but the setOnItemClickListener in MAinActivity is not executing.pls help.
This is my list view app:
Layout image
MainActivity.java
public class MainActivity extends Activity {
private static ListAdapterclass adapter;
ListView lv;
TextView tv2;
private final String android_versions[]={
"Donut",
"Eclair",
"Froyo",
"Gingerbread",
"Honeycomb",
"Ice Cream Sandwich",
"Jelly Bean",
"KitKat",
"Lollipop",
"Marshmallow"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
lv = (ListView) findViewById(R.id.listView1);
tv2 = (TextView) findViewById(R.id.selected);
adapter = new ListAdapterclass(getApplicationContext(), android_versions);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), "hiiiiiiiii", Toast.LENGTH_SHORT).show();
System.out.println("********************** INSIDE ONITEMCLICKLISTNER IN MAIN ACTIVITY ******************");
String ver_name = (lv.getItemAtPosition(i)).toString();
tv2 = (TextView) findViewById(R.id.selected);
tv2.setText(ver_name);
}
});
}
}
ListAdapter.class
public class ListAdapterclass extends ArrayAdapter implements AdapterView.OnItemClickListener{
private String android_versionNames[];
Context mContext;
public int row_index=-1;
#Override
public void onItemClick(AdapterView<?> adapterView, View v, int i, long l) {
int position=(Integer)v.getTag();
String ver_name=getItem(position).toString();
}
private static class ViewHolder{
TextView tv;
LinearLayout LL;
TextView tv2;
}
public ListAdapterclass(Context context,String android_versionnames[]) {
super(context, R.layout.list_item,android_versionnames);
this.android_versionNames=android_versionnames;
this.mContext=context;
System.out.println(" ???????????????????????? Inside dataadapter,Android names : ?????????????????????????????\n ");
for(int i=0;i<android_versionnames.length;i++){
System.out.println("\n"+android_versionnames[i]);
}
}
private int lastPosition=-1;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
String ver_name=getItem(position).toString();
final ViewHolder viewHolder;
final View result;
if(convertView==null){
viewHolder=new ViewHolder();
LayoutInflater inflater=LayoutInflater.from(getContext());
convertView=inflater.inflate(R.layout.list_item,parent,false);
viewHolder.tv=(TextView)convertView.findViewById(R.id.label);
viewHolder.LL=(LinearLayout) convertView.findViewById(R.id.linearLayout_1);
viewHolder.tv2=(TextView)convertView.findViewById(R.id.selected);
result=convertView;
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
result=convertView;
}
lastPosition=position;
viewHolder.tv.setText(ver_name);
viewHolder.LL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
row_index=position;
notifyDataSetChanged();
}
});
if(row_index==position){
viewHolder.LL.setBackgroundColor(Color.parseColor("#409de1"));
viewHolder.tv.setTextColor(Color.parseColor("#ffffff"));
}
else
{
viewHolder.LL.setBackgroundColor(Color.parseColor("#ffffff"));
viewHolder.tv.setTextColor(Color.parseColor("#000000"));
}
return convertView;
}
}
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.cybraum.test.listviewcolorchange.MainActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:clickable="true"
android:layout_weight="1"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView1"
>
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2"
android:id="#+id/linearLayout_2"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected : "
android:textStyle="bold"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="text"
android:id="#+id/selected"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/linearLayout_1"
android:padding="10dp">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center">
</TextView>
</LinearLayout>
what is the problem?
Remove viewHolder.LL.setOnClickListener listener from adapter and
In your adapter add a method to update row_index:
public void changeIndex(int rowIndex){
this.row_index = rowIndex;
notifyDataSetChanged();
}
Call this method from onItemClickListener event:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.changeIndex(i);//This will give you the same result of viewHolder.LL.setOnClickListener as you are doing
//Do whatever you are doing previously
}
});
If you take click event from adapter then listview itemclick could not work if you need adapter click event and listview item click please refer the link,
How to make imageView clickable from OnItemClickListener?
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long viewId = view.getId();
if (viewId == R.id.button1) {
Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
} else if (viewId == R.id.button2) {
Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "ListView clicked" + id, Toast.LENGTH_SHORT).show();
}
}
In adapter:
viewHolder.Btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
Use:
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), "hiiiiiiiii", Toast.LENGTH_SHORT).show();
System.out.println("********************** INSIDE ONITEMCLICKLISTNER IN MAIN ACTIVITY ******************");
String ver_name = (lv.getItemAtPosition(i)).toString();
tv2 = (TextView) findViewById(R.id.selected);
tv2.setText(ver_name);
}
});
And from adapter remove
implements AdapterView.OnItemClickListener
You need to remove your adaptor from the setOnClickListner()
Try to change Your method with
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object o = prestListView.getItemAtPosition(position);
prestationEco str=(prestationEco)o;//As you are using Default String Adapter
Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
}
});
change listview xml
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"></ListView>
remove AdapterView.OnItemClickListener from adapter class
public class ListAdapterclass extends ArrayAdapter {
}
change listview xml
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true">
</ListView>
remove AdapterView.OnItemClickListener from adapter class
public class ListAdapterclass extends ArrayAdapter {
}

How to make card style menu in android

I want to add card style in my app like this
i use in my app mysql database so i need to make like this cards and put my data from database in it now i use ListView with this code
public void listAllItme() {
ListAdapter lA = new listAdapter(listitems);
listView.setAdapter(lA);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent open = new Intent(R_arabic.this, rewaya_show.class);
open.putExtra("name", listitems.get(position).name);
open.putExtra("url", listitems.get(position).url);
open.putExtra("img", listitems.get(position).img);
open.putExtra("num", listitems.get(position).num);
startActivity(open);
}
}
});
}
class listAdapter extends BaseAdapter {
ArrayList<listitem_gib> lista = new ArrayList<listitem_gib>();
public listAdapter(ArrayList<listitem_gib> lista) {
this.lista = lista;
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position).name;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.row_item_gib, null);
TextView name = (TextView) view.findViewById(R.id.textView_gib);
ImageView img = (ImageView) view.findViewById(R.id.imageView_gib);
TextView num = (TextView) view.findViewById(R.id.textView_gib2);
TextView size = (TextView) view.findViewById(R.id.textView_gib3);
name.setText(lista.get(position).name);
num.setText(lista.get(position).num);
size.setText(lista.get(position).size);
Picasso.with(R_arabic.this).load("http://grassyhat.com/android/image/" + lista.get(position).img).into(img);
return view;
}
}
first i want to know how i can make like this card style
second how i can use this code with card menu not listview
sorry im new in android and sorry for my bad english
What do you mean by card menu? because the example in the image is a recyclerview with a cardview item, you can achieve this by doing something like this
This will be your activity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
//Just your list of objects, in your case the list that comes from the db
List<Items> itemsList = new ArrayList<>();
CardAdapter adapter = new CardAdapter(this, itemsList);
//RecyclerView needs a layout manager in order to display data so here we create one
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//Here we set the layout manager and the adapter to the listview
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
Inside the layout file you just have to place the recyclerview like this
<RelativeLayout
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="jsondh.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Then your adapter will be something like this
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private List<Items> itemsList;
private Activity activity;
public CardAdapter(Activity activity, List<Items> items){
this.activity = activity;
this.itemsList = items;
}
#Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = activity.getLayoutInflater().inflate(R.layout.cardview_layout, parent, false);
return new CardViewHolder(itemView);
}
#Override
public void onBindViewHolder(CardViewHolder holder, int position) {
//Here you bind your views with the data from each object from the list
}
#Override
public int getItemCount() {
return itemsList.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
public ImageView bookImage;
public TextView bookLabel01, bookLabel02;
public CardViewHolder(View itemView) {
super(itemView);
bookImage = (ImageView)itemView.findViewById(R.id.image);
bookLabel01 = (TextView)itemView.findViewById(R.id.label01);
bookLabel02 = (TextView)itemView.findViewById(R.id.label02);
}
}
And the last one will be the layout from each item on the list, like this
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="15dp"
app:cardBackgroundColor="#3369Ed">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="150dp"
android:layout_height="130dp"/>
<TextView
android:id="#+id/label01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
<TextView
android:id="#+id/label02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LongerLabel"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</android.support.v7.widget.CardView>
You also have to add this to your gradle file:
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
Hope it helps!
It's pretty simple. You will have to use a RecyclerView with GridLayoutManager and add a cardView to it.
Then, use an Adapter and ViewHolder to feed the data.
I suggest you to check this out:
https://developer.android.com/training/material/lists-cards.html

List onItemClickListener not working with

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:focusable="false"
android:focusableInTouchMode="false">
<android.support.v7.widget.CardView
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="60dp"
android:padding="5dp"
android:elevation="6dp">
<TextView
android:id="#+id/major_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/black"
android:textSize="#dimen/text_size_large"
android:text="Computer Science"
android:gravity="center" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/subject_scroll"
android:paddingBottom="8dp"
android:paddingTop="10dp"
android:paddingLeft="15dp"
android:paddingStart="15dp"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
`This is dropdown.xml which is the view that will be inside the listview.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/courses_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:orientation="vertical"
android:clickable="false">
</ListView>
</LinearLayout>
main.xml
the listview with id couses_list with contain the dropdown.xml view
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("ListView", "onItemClick: true");
});
I have tried setting the setFocusable() to false to everyview inside listview and other method shown in precious solutions in stack overflow but nothing worked for me.What wrong am i doing here..
ListView uses MajorAdapter which extends BindableAdapter
public class MajorAdapter extends BindableAdapter<Major> {
public class ViewHolder {
TextView majorName;
private boolean isOpen = false;
private RecyclerView subjectRecyclerView;
Major major;
List<Subject> subjects = new ArrayList<>();
ViewHolder(View view){
majorName = (TextView)view.findViewById(R.id.major_name);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
subjectRecyclerView = (RecyclerView)view.findViewById(R.id.subject_scroll);
subjectRecyclerView.setLayoutManager(linearLayoutManager);
SubjectAdapter subjectAdapter = new SubjectAdapter(subjects);
subjectRecyclerView.setAdapter(subjectAdapter);
subjectRecyclerView.addItemDecoration(new RecyclerListDecorater(view.getContext()));
}
public void toggle(){
if(this.isOpen)
this.isOpen = false;
else
this.isOpen = true;
}
public boolean isOpen(){
return this.isOpen;
}
public Model getMajor(){
return major;
}
public RecyclerView getRecyclerView(){
return this.subjectRecyclerView;
}
}
public MajorAdapter(Context context){
super(context);
}
#Override
public View getNewView(LayoutInflater inflater, int position, ViewGroup container) {
View view = inflater.inflate(R.layout.majors,null,false);
view.setFocusable(false);
ViewHolder holder = new ViewHolder(view);
view.setTag(holder);
return view;
}
#Override
public void bindView(Major item, int position, View view) {
ViewHolder holder= (ViewHolder)view.getTag();
holder.major = item;
holder.majorName.setText(item.getCourseName());
}
}
here is the bindable adapter
public abstract class BindableAdapter<T> extends ArrayAdapter<T> {
private LayoutInflater inflater;
public BindableAdapter(Context context){
super(context,0);
setup(context);
}
private void setup(Context context){
inflater = LayoutInflater.from(context);
}
#Override
public final View getView(int position, View view, ViewGroup container){
if(view == null){
view = getNewView(inflater, position, container);
if(view == null)
throw new IllegalStateException("View created cannot be null");
}
bindView(getItem(position), position, view);
return view;
}
public abstract View getNewView(LayoutInflater inflater, int position, ViewGroup container);
public abstract void bindView(T item, int position, View view);
#Override
public View getDropDownView(int position, View view, ViewGroup parent){
return getView(position, view, parent);
}
}

Categories