How do I get each list in the list view clicked to a different place within the Fragment? (Beginner: There's coding)

Asked 2 years ago, Updated 2 years ago, 97 views

public class SocialFragment extends Fragment {
    private static final String TAG = SocialFragment.class.getSimpleName();
    View rootView;
    ExpandableListView lv;
    private String[] groups;
    private String[][] children;


    public SocialFragment() {

    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        groups = new String[]{"Test1", "Test2", "Test3", "Test4"};

        children = new String[][]{
                {"a","b","c","d"},
                {"a"",n"",d"",dd"",hahaha"},
                {"l"",m"",b"",s"},
                {"""",""}
    };
        final Class<?>[] ACTIVITIES = { Empty1.class, Empty1.class, List1.class, List2.class, List3.class, List4.class, Empty1.class};
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_lineup, container, false);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        lv = (ExpandableListView) view.findViewById(R.id.expListView);


    }

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private final LayoutInflater inf;
        private String[] groups;
        private String[][] children;

        public ExpandableListAdapter(String[] groups, String[][] children) {
            this.groups = groups;
            this.children = children;
        }

        @Override
        public int getGroupCount() {
            return groups.length;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

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

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


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

            ViewHolder holder;
            if (convertView == null) {
                convertView = inf.inflate(R.layout.list_item, parent, false);
                holder = new ViewHolder();

                holder.text = (TextView) convertView.findViewById(R.id.lblListItem);
                convertView.setTag(holder);
            } } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.text.setText(getChild(groupPosition, childPosition).toString());

            return convertView;
        }

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

            if (convertView == null) {
                convertView = inf.inflate(R.layout.list_group, parent, false);

                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.lblListHeader);
                convertView.setTag(holder);
            } } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.text.setText(getGroup(groupPosition).toString());

            return convertView;
        }

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

        private class ViewHolder {
            TextView text;
        }
    }
}

This is the java file that applied the expandable list view. If you click 'a', 'b', 'c', 'c', ....... etc. on the children list, I want to put a url link differently. What should I do? For example, when you click ww.naver in the list, google. (not a new window, just a web view to work in the fragment...).) I'm a beginner I would appreciate your help

android fragment listview

2022-09-22 16:11

2 Answers

You can fill it out as follows.

If you want to press items in each group/subview...

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

            // ... omission

            holder.text.setText(getChild(groupPosition, childPosition).toString());
            holder.text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /// Click Processing
                }
            };

            // ... omission
            return convertView;
        }

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

            holder.text.setText(getGroup(groupPosition).toString());
            holder.text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /// Click Processing
                }
            };

            return convertView;
        }

You can process OnClickListener in TextView as above.

Also, we recommend using RecyclerView rather than ListView.

It's similar, but it's also easier to use.

RecyclerView


2022-09-22 16:11

I'll give you an additional sample code

First of all, you can process the data as follows.

ArrayList<Data object name> itemList;

Please write in the following form as a separate class.
public class data object name {
    public String url;
}

Please fill it out as above.

The following code is used to invoke the Web browser through Intent:

Uri uri = Uri.parse("http://www.google.com");
Intent intent  = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

And when you put it together, you can do the following:

If you work with itemList, it is simple to use, so you can get the value of a specific location through get.

Use groupPostion and childPosition to handle actions that fit the item.

In this case, there should be two item lists, right?

@Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                // ... omission
    holder.text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
               String url = itemList.get(groupPosition).url;
               Uri uri  = Uri.parse(url);
               Intent intent = new Intent(Intent.ACTION_VIEW, uri);
              context.startActivity(intent);
        }
    };
}

You can create an example as shown above.

You have to set the context and item list from the outside.

I will link the ExpandableListView example that I wrote in the past.

ExpandableListView


2022-09-22 16:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.