Скрыть и показать представления в ListView Android

Я создал и приложение для Android в том, что хочу скрыть и показать виды в ListView с помощью Custom Adapter.

Мой код:

@Override public View getView(final int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View vi = convertView; if (convertView == null) { vi = inflater.inflate(R.layout.item_estimate, null); btn_dots = (ImageButton) vi.findViewById(R.id.btn_dots_itemEstimate); ll_2 = (LinearLayout) vi.findViewById(R.id.ll_2_itemEstimate); } btn_dots.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (ll_2.getVisibility() == View.GONE) { ll_2.setVisibility(View.VISIBLE); } else { ll_2.setVisibility(View.GONE); } } }); return vi; } 

Введите описание изображения здесь

При нажатии на точки это не скрывает макет.

Благодарю.

Да, я столкнулся с такой же проблемой и решил ее с помощью ExpandableListView. Я вставляю код ниже

 public DefaultContactListAdapter(Context context, ArrayList<Cont> List2) { super(); this.context = context; this.contactList = contactList2; @Override public Object getChild(int parent, int child) { return child; // return glossariesList.get(Sub_List.get(parent) ).get(child); } @Override public long getChildId(int parent, int child) { // TODO Auto-generated method stub return child; } @Override public View getChildView(int parent, int child, boolean lastChild, View convertview, ViewGroup parentview) { // String child_title=(String) getChild(parent, child); if(convertview==null) { LayoutInflater inflator=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertview=inflator.inflate(R.layout.layout_default_contact_screen_child, parentview, false); } return convertview; } @Override public int getChildrenCount(int groupPosition) { return 1; } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return contactList.get(groupPosition); } @Override public int getGroupCount() { // TODO Auto-generated method stub return contactList.size(); } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentview) { // TODO Auto-generated method stub //String group_title=(String) getGroup(parent); if(convertView==null) { LayoutInflater inflator=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); convertView=inflator.inflate(R.layout.layout_default_screen, parentview,false); } return convertView; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return false; } } 

Если вы удовлетворены этим ответом, пожалуйста, примите мой ответ.

Попробуйте расширение списка. Эта ссылка может помочь вам

http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

  import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class First_Activity extends Activity { ListView listView; ArrayList<Item> arrayList= new ArrayList<Item>(); Adapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.first_actiivty); listView =(ListView) findViewById(R.id.listView); Item item = new Item(); item.setText1("1"); item.setText2("2"); item.setSelected(false); arrayList.add(item); Item item1 = new Item(); item1.setText1("1"); item1.setText2("2"); item1.setSelected(true); arrayList.add(item1); Item item2 = new Item(); item2.setText1("1"); item2.setText2("2"); item2.setSelected(true); arrayList.add(item2); adapter = new Adapter(); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { //Toast.makeText(getApplicationContext(), ""+position, 1).show(); for (Item item :arrayList){ item.setSelected(false); } arrayList.get(position).setSelected(true); System.out.println(arrayList.get(0).isSelected()); System.out.println(arrayList.get(1).isSelected()); System.out.println(arrayList.get(2).isSelected()); adapter.notifyDataSetChanged(); } }); } public class Adapter extends BaseAdapter { public Adapter() { } @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int position) { return arrayList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.row, parent, false); holder.text1 = (TextView) convertView.findViewById(R.id.textView1); holder.text2 = (TextView) convertView.findViewById(R.id.textView2); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text1.setText(arrayList.get(position).getText1()); holder.text2.setText(arrayList.get(position).getText2()); if(arrayList.get(position).isSelected()==true) { holder.text2.setVisibility(View.VISIBLE); } else { holder.text2.setVisibility(View.GONE); } return convertView; } class ViewHolder { TextView text1; TextView text2; } } } javabeen get set public class Item { private String text1; private String text2; private boolean isSelected; public String getText1() { return text1; } public void setText1(String text1) { this.text1 = text1; } public String getText2() { return text2; } public void setText2(String text2) { this.text2 = text2; } public boolean isSelected() { return isSelected; } public void setSelected(boolean isSelected) { this.isSelected = isSelected; } } main xml file <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout> row xml file <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="120dp" android:orientation="horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" android:textSize="30dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" > <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" android:textSize="30dp" /> </LinearLayout> </LinearLayout> </LinearLayout> 

Переопределите прослушиватель кликов с помощью этого

 btn_dots.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (arg0.getVisibility() == View.GONE) { arg0.setVisibility(View.VISIBLE); } else { arg0.setVisibility(View.GONE); } } }); 

Вы можете сделать это в одном строковом коде:

 btn_dots.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { arg0.setVisibility(arg0.getVisibility() == View.GONE ? View.VISIBLE : View.GONE); } });