Can't you make sure that only one item in the ListView is expanded without using Expandable ListView?

Asked 2 years ago, Updated 2 years ago, 57 views

Without ExpandableListView

in ListView

There are items like "AA", "BB", "CC", and "DD" Can't we have "xxx" and "yyy" as child nodes only for "DD" of these four items? And when you press DD, it opens.

To be exact, the function itself is the same as ExpandableListView. By the way, I want to apply this to only one item in the general ListView without using Ex.

What should I do?

listview

2022-09-22 12:51

1 Answers

I wrote a simple example code. It is good to use RecyclerView, but you are using ListView, so I wrote it as ListView.

activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".TestActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

TestActivity.java

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        final String[] nameArray = {
                "Brill/Color", "Display", "Echo", "Alert Settings",
                "Tuning", "Others", "Target", "OS/Barge Mark", "TT",
                "AIS", "System", "Key Operation", "Touch Operation"};

        final List<ListItem> dataSet = new ArrayList<>();
        for (String name : nameArray) {
            dataSet.add(new ListItem(name));
        }

        final TestAdapter adapter = new TestAdapter(dataSet);
        final ListView listView = findViewById(R.id.listView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dataSet.get(position).isExpand = !dataSet.get(position).isExpand;
                adapter.notifyDataSetChanged();
            }
        });
    }

    static class TestAdapter extends BaseAdapter {

        final List<ListItem> dataSet;

        TestAdapter(List<ListItem> dataSet) {
            this.dataSet = dataSet;
        }

        @Override
        public int getCount() {
            return dataSet.size();
        }

        @Override
        public ListItem getItem(int position) {
            return dataSet.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.expandable_row, null);
                holder = new ViewHolder();
                holder.title = convertView.findViewById(R.id.title);
                holder.expandableSection = convertView.findViewById(R.id.expand_section);
                convertView.setTag(holder);
            } } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.title.setText(getItem(position).name);
            holder.expandableSection.setVisibility(getItem(position).isExpand ? View.VISIBLE : View.GONE);
            return convertView;
        }

        class ViewHolder {
            TextView title;
            FrameLayout expandableSection;
        }
    }

    static class ListItem {
        public String name;
        public boolean isExpand = false;

        ListItem(String name) {
            this.name = name;
        }
    }
}

You were using ArrayAdapter, but for the function you mentioned, you must inherit BaseAdapter to implement CustomAdapter.

The String array to be used for ListView was placed in a data object called ListItem for expand. The setOnItemClickListener() changes the isExpand value in the data object of the selected position. After that, by calling notifyDataSetChanged() on the adapter to update the list, it works by making the expansion area Visible/Gone according to the isExpand value, as implemented in getView().

The layout for each Listrow is written as follows.

expandable_row.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="wrap_content"
    app:cardUseCompatPadding="true">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/row_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:textColor="@android:color/black"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <FrameLayout
            android:id="@+id/expand_section"
            android:layout_width="0dp"
            android:layout_height="200dp"
            android:background="@color/colorPrimary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title" />

    </android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

It seems complicated, but if you understand the flow, it has a simpler structure than you think :) I hope it helps you.


2022-09-22 12:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.