zoukankan      html  css  js  c++  java
  • 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:id="@+id/relativelayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/image">

    <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);
    }

    public static int position;
    public static boolean isShow = true;
    @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) {
    if(MainActivity.position == position){
    MainActivity.isShow = false;
    MainActivity.position = 99999999;
    }else{
    MainActivity.isShow = true;
    MainActivity.position = position;
    }
    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){
    if(MainActivity.isShow){
    childAt.getChildAt(j).setVisibility(View.VISIBLE);
    }else {
    childAt.getChildAt(j).setVisibility(View.GONE);
    }
    if(childAt.getChildAt(j).getVisibility() == View.INVISIBLE){
    Log.e("eee","出现这个日志,请检查你的类型转换,和xml结构是否符合当前的代码。");
    }
    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的代码就要改。。。。

    15:38更新一次:添加了,当点击同一个两次,第二次应该要GONE;

  • 相关阅读:
    LeetCode 【1】 Two Sum --001
    计算机网络-自定向下方法之应用层
    Android TextView自动换行、排列错乱问题及解决
    Android ScrollView内部组件设置android:layout_height="fill_parent"无效的解决办法
    Gson解析空字符串异常的处理
    AndroBench手机性能测试
    使用adb命令通过IP地址连接手机
    Android string.xml 添加特殊字符
    Android string资源 包含 数学符号等特殊字符 及 参数占位符
    Android Studio多渠道打包(二)
  • 原文地址:https://www.cnblogs.com/yaxiaoke/p/6061494.html
Copyright © 2011-2022 走看看