Android programming Listview to click expand and hide methods

  • 2020-12-05 17:22:54
  • OfStack

This article illustrates how Android programming implements Listview's click-to-expand and hide methods. To share for your reference, the details are as follows:

There's a lot of code, so I want you to post the key points, and I think it's easy for you to understand after you read it,

In listview's activity


List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>()
myAdapter = new MyAdapter(getApplicationContext(), listItems);
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
   long arg3) {
   // Click when setting the selected number in the custom adapter Set properties in selectItem
   myAdapter.setSelectItem(arg2);
   // The refresh listView
   myAdapter.notifyDataSetInvalidated();
 }
});

In the custom getView of myAdapter, what I need to hide and show is that TableLayout is hidden by default, and the definition attribute int type selectItem and sign are both -1 by default.


TableLayout info = (TableLayout)convertView.findViewById(R.id.tableLayout1);
if(position == selectItem){// Selected elements 
  if(sign == selectItem){// When selected again, it hides and initializes the tag location 
     info.setVisibility(View.GONE);
     // The transparent color is not selected 
   convertView.setBackgroundColor(Color.parseColor("#00000000"));
   sign = -1;
  }else{// When selected, it will be displayed and marked 
     info.setVisibility(View.VISIBLE);
     // Selected to set the background color 
   convertView.setBackgroundColor(Color.parseColor("#B0E2FF"));
   sign = selectItem;
 }
}else {// Unselected elements 
  info.setVisibility(View.GONE);
  convertView.setBackgroundColor(Color.parseColor("#00000000"));
}

You can click show, click Hide again, click show and only 1 element will be shown

I hope this article has been helpful in Android programming.


Related articles: