Oreo
Oreo

Reputation: 2594

How to implement Rounded Corner Effect in a List

I have successfully implemented Rounded Corner ListView inside CustomAdapter class, like this:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // convert view = design
    View v = convertView;
    if (v == null) {
        viewHolder = new ViewHolder();
        v = layoutInflater.inflate(layout, null);
        viewHolder.textTitle = (TextView) v.findViewById(R.id.textTitle);

        if (position == 0 && arrayList.size() == 1) {
            v.setBackgroundResource(R.drawable.top_list_selector);
        }
        else if (position == 0) {
            v.setBackgroundResource(R.drawable.top_list_selector);
        }
        else if (position == arrayList.size() - 1) {
            v.setBackgroundResource(R.drawable.bottom_list_selector);
        } else {
            v.setBackgroundResource(R.drawable.mid_list_selector);
        }

        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }

But I don't know How can I implement same in below class, I do understand that in place of arrayList I have to use countryList but what about view and position ?

public class CountryFragment extends Fragment {

    View view;

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

        displayListView();

        return view;
        }

    private void displayListView() {

        //Array list of countries
        List<String> countryList = new ArrayList<String>();
        countryList.add("Country 1");
        countryList.add("Country 2");
        countryList.add("Country 3");
        countryList.add("Country 4");
        countryList.add("Country 5");

        //create an ArrayAdapter from the String Array
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.adapter_social, countryList);
        ListView listView = (ListView) view.findViewById(R.id.listCountry);

        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);

        //enables filtering for the contents of the given ListView
        listView.setTextFilterEnabled(true);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                switch( position )
                {
                    case 0:
                        Toast.makeText(getActivity(), "Country 1", Toast.LENGTH_SHORT).show();
                        break;
                        .....
                }
            }
        });
    }
}

Upvotes: 1

Views: 81

Answers (1)

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

On the 1 code example, the use created a custom adapter to display alist of items, something like

MyCustomeAdapter extends BaseAdapter

Where as in your code you simply displayed already build in adapter, which do not support to Override getView() method in which all this changes can be made.

So, in order to do what the example is doing, create a class

static class MyListAdapter extends BaseAdapter {    


        private List<View> views = new ArrayList<View>();

        public void addView(View view){
            views.add(view);
        }

        public int getCount() {
            return views.size();
        }

        public Object getItem(int position) {
            return views.get(position).getTag();
        }

        public long getItemId(int position) {
            return views.get(position).getId();
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            return views.get(position);
        }
    }

And instead of doing

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.adapter_social, countryList);

Do

MyListAdapter listAdapter = new MyListAdapter();

 LayoutInflater inflater = getLayoutInflater();  

        int item_layout = 0 ;

        for(int i = 0 ; i < values.length;i++) {

            //decide which layout to show depending on the item position
            if(i==0) {
                item_layout = R.layout.top_list_layout ;
            } else if(i==values.length-1) {
                item_layout = R.layout.bottom_list_layout;
            } else {
                item_layout = R.layout.mid_list_layout;
            }


            TextView button = (TextView)inflater.inflate(item_layout, null); 

            button.setText(values[i]);
            button.setTag(new Integer(i));

            listAdapter.addView(button);
        }




        //lv.setSelector(R.color.transparent);
        lv.setAdapter(listAdapter);

Upvotes: 1

Related Questions