如何使用子级搜索过滤器实现expandablelistview?

时间:2018-11-21 20:59:23

标签: android android-search

我已经通过子活动创建了一个expandablelistview,现在我想为孩子实现带有搜索过滤器的searchView,但是我不知道该怎么做。我已经在互联网上搜索了解决方案,但我尝试过的方法却无效。

我希望能够在孩子之后搜索过滤器,例如“超级”,当我单击“超级”时,它将打开活动。

有人可以帮我吗?

这里是我的代码:

MainActivity:

public class MainActivity extends AppCompatActivity {

ExpandableListView expandableListView;

List<String> langs;
Map<String, List<String>> topics;
ExpandableListAdapter listAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
    fillData();

    listAdapter = new MyExListAdapter(this,langs,topics);

    expandableListView.setAdapter(listAdapter);

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {



            switch (groupPosition)

            {
                case 0: // Dette er groupPosition (altså Java)


                    switch (childPosition)
                    {
                        case 0:  // Dette er childPosition
                            Super();
                            break;
                        case 1:
                            //do something
                            break;
                    }

                    switch (childPosition)
                    {
                        case 1:
                            Encapsulation();
                            break;
                        case 2:
                            //do something
                            break;
                    }

                    switch (childPosition)
                    {
                        case 2:
                            Methods();
                            break;
                        case 3:
                            //do something
                            break;
                    }

                    break;     //Husk break her for at undgå du åbner for begge parents første child


                case 1: // Dette er groupPosition (altså C)

                    switch (childPosition)
                    {
                        case 0:
                            Procedure();
                            break;
                        case 1:
                            //do something
                            break;
                    }


                    switch (childPosition)
                    {
                        case 1:
                            Pointer();
                            break;
                        case 2:
                            //do something
                            break;
                    }


                    switch (childPosition)
                    {
                        case 2:
                            Array();
                            break;
                        case 3:
                            //do something
                            break;
                    }

                    break;


            }

            return false;

        }

        // Hovedgruppe Java

        private void Super() {

            Intent MyIntent = new Intent(MainActivity.this, Super.class);
            MyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Bruges for at undgå den åbner to af samme activities (flag activity clear top)
            startActivity(MyIntent);
        }

        private void Encapsulation() {

            Intent MyIntent = new Intent(MainActivity.this, Encapsulation.class);
            MyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Bruges for at undgå den åbner to af samme activities (flag activity clear top)
            startActivity(MyIntent);
        }

        private void Methods() {

            Intent MyIntent = new Intent(MainActivity.this, Methods.class);
            MyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(MyIntent);
        }



        // Hovedgruppe C

        private void Procedure() {

            Intent MyIntent = new Intent(MainActivity.this, Procedure.class);
            MyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Bruges for at undgå den åbner to af samme activities (flag activity clear top)
            startActivity(MyIntent);
        }

        private void Pointer() {

            Intent MyIntent = new Intent(MainActivity.this, Pointer.class);
            MyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Bruges for at undgå den åbner to af samme activities (flag activity clear top)
            startActivity(MyIntent);
        }

        private void Array() {

            Intent MyIntent = new Intent(MainActivity.this, Array.class);
            MyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(MyIntent);
        }


    });
}

public void fillData()
{
    langs = new ArrayList<>();
    topics = new HashMap<>();

    langs.add("Java");
    langs.add("C");

    List<String> java = new ArrayList<>();
    List<String> C = new ArrayList<>();


    // Hovedgruppe java

    java.add("Super");
    java.add("Encapsulation");
    java.add("Methods");

    // Hovedgruppe C

    C.add("Procedure");
    C.add("Pointers");
    C.add("Array");
    C.add("Array");
    C.add("Array");
    C.add("Array");
    C.add("Array");


    topics.put(langs.get(0),java);
    topics.put(langs.get(1),C);
}
}

MyExListAdapter:

public class MyExListAdapter extends BaseExpandableListAdapter
{

Context context;
List<String> langs;
Map<String, List<String>> topics;

public MyExListAdapter(Context context, List<String> langs, Map<String, List<String>> topics) {
    this.context = context;
    this.langs = langs;
    this.topics = topics;
}

@Override
public int getGroupCount() {
    return langs.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return topics.get(langs.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return langs.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return topics.get(langs.get(groupPosition)).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    String lang = (String) getGroup(groupPosition);

    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_parent,null);
    }

    TextView txtParent = (TextView) convertView.findViewById(R.id.txtParent);
    txtParent.setText(lang);

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    String topic = (String) getChild(groupPosition,childPosition);

    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_child,null);
    }

    TextView txtChild = (TextView) convertView.findViewById(R.id.txtChild);
    txtChild.setText(topic);

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

activity_main.xml:

<RelativeLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <ExpandableListView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/expandableListView"
        android:groupIndicator="@null"
        android:cacheColorHint="#00000000"
        android:listSelector="@android:color/transparent"

        />

</RelativeLayout>

0 个答案:

没有答案