ListView只允许展开其中一个item的方法
Posted 卜筮老人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView只允许展开其中一个item的方法相关的知识,希望对你有一定的参考价值。
xml文件代码:
<?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:orientation="vertical"> <RelativeLayout android:layout_below="@id/image" android:id="@+id/relativelayout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/line1" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="我是文字 要被隐藏" /> </LinearLayout> </RelativeLayout> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@mipmap/ic_launcher" /> </RelativeLayout>
java代码:
public class MainActivity extends AppCompatActivity { ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listview); } @Override protected void onStart() { super.onStart(); myAdapter = new MyAdapter(); listview.setAdapter(myAdapter); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int childCount = ((RelativeLayout) view).getChildCount(); if(myAdapter.getView() != null){ myAdapter.getView().setVisibility(View.GONE); } for (int i = 0; i < childCount; i++) { int relativelayout = R.id.relativelayout; //要隐藏布局的外层一个容器ID int id1 = ((RelativeLayout) view).getChildAt(i).getId(); if(relativelayout == id1){//如果是要隐藏布局的外层一个容器 就循环寻找要隐藏的容器 RelativeLayout childAt = (RelativeLayout) ((RelativeLayout) view).getChildAt(i); int childCount1 = childAt.getChildCount(); for (int j = 0; j < childCount1; j++) { int line1 = R.id.line1; int id2 = childAt.getChildAt(j).getId(); if(line1 == id2){ childAt.getChildAt(j).setVisibility(View.VISIBLE); myAdapter.setView((LinearLayout) childAt.getChildAt(j)); } } } } } }); } MyAdapter myAdapter; class MyAdapter extends BaseAdapter { LinearLayout view; //要隐藏或者展示的view ——如果是多个,可以修改为一个Entity public LinearLayout getView(){ return view; } public void setView(LinearLayout view){ this.view = null; this.view = view; } @Override public int getCount() { return 7; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder vh; if (convertView == null) { vh = new ViewHolder(); convertView = View.inflate(MainActivity.this, R.layout.item, null); vh.image = (ImageView) convertView.findViewById(R.id.image); vh.text = (TextView) convertView.findViewById(R.id.text); convertView.setTag(vh); } else { vh = (ViewHolder) convertView.getTag(); } return convertView; } class ViewHolder { ImageView image; TextView text; } } }
耦合度很高,只要布局文件里面的结构改变,onItemClick的代码就要改。。。。
以上是关于ListView只允许展开其中一个item的方法的主要内容,如果未能解决你的问题,请参考以下文章
android的ListView点击item使item展开的做法