How can I create a list with only certain items expandable?

馋奶兔 提交于 2020-01-03 10:59:11

问题


I am trying to compose a list with some items expandable and some single items. I wish to have it so that when either a single item or expandable list child is clicked, I can call an intent based on the text of the item.

I suppose expandable lists would work, but is there a way to set items in an expandable list so that they don't have the expandable list icon? Should I use something other than a string array?

What is the best way to do this?

Thanks

Below is my incomplete code, I would like to insert the items "Zone 1", "Zone 2", "Zone 3" between the items "Atlanta" and "Boston" or as a subset of Atlanta:

package test.lists.special;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SpecialList extends ListActivity{
TextView toptext;
String[] items={"Atlanta", "Boston", "Chicago", "Dallas"};

@Override

 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);
  setListAdapter(new ArrayAdapter<String>(this,
  android.R.layout.simple_list_item_1,
  items));
  toptext=(TextView)findViewById(R.id.toptext);
 }

public void onListItemClick(ListView parent, View v, int position,
 long id) {
     Intent intent = new Intent();

     if (parent.getItemAtPosition(position)=="Atlanta")
     {
           //THIS IS WHERE I WISH TO INSERT "ZONE 1", "ZONE 2", "ZONE 3" 
                  //between Atlanta and Boston in the list
     }
     else if (parent.getItemAtPosition(position)=="Boston")
     {
          intent.setClass(this, test.lists.special.Boston.class);
           startActivity(intent); 
     }
     else if (parent.getItemAtPosition(position)=="Chicago")
     {
             intent.setClass(this, test.lists.special.Chicago.class);
              startActivity(intent); 
     }
     else if (parent.getItemAtPosition(position)=="Dallas")
     {
            intent.setClass(this, test.lists.special.Dallas.class);
             startActivity(intent); 
     }
     else if (parent.getItemAtPosition(position)=="Zone 1")
     {
            intent.setClass(this, test.lists.special.Atlanta.Zone1.class);
             startActivity(intent); 
     }
     else if (parent.getItemAtPosition(position)=="Zone 2")
     {
            intent.setClass(this, test.lists.special.Atlanta.Zone2.class);
             startActivity(intent); 
     }
     else if (parent.getItemAtPosition(position)=="Zone 3")
     {
            intent.setClass(this, test.lists.special.Atlanta.Zone3.class);
             startActivity(intent); 
     }
    }

}

回答1:


Working and tested code below:

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View; 
import android.view.ViewGroup;
import android.widget.ArrayAdapter; 
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class SpecialList extends ListActivity{ 

    TextView toptext; 
    String[] items={"Atlanta", "Boston", "Chicago", "Dallas"};
    private CityListAdapter mListOfCities;

    boolean mAtlantaListExpanded;

    @Override 
    public void onCreate(Bundle icicle) { 
        super.onCreate(icicle); 
        setContentView(R.layout.main); 

        mAtlantaListExpanded = false;
        mListOfCities = new CityListAdapter();
        setListAdapter(mListOfCities);

        for (int n=0; n < items.length; n++)
        {
            mListOfCities.add(items[n]);
        }
    } 

    public void onListItemClick(ListView parent, View v, int position, long id) 
    { 
         Intent intent = new Intent(); 

         if (parent.getItemAtPosition(position)=="Atlanta") 
         { 
             mAtlantaListExpanded = !mAtlantaListExpanded;
             mListOfCities.clear();
             for (int n=0; n < items.length; n++)
             {
                mListOfCities.add(items[n]);
             }           
             Log.i("SpecialList", "Atlanta");            
         } 
         else if (parent.getItemAtPosition(position)=="Boston") 
         { 
             Log.i("SpecialList", "Boston");
         } 
         else if (parent.getItemAtPosition(position)=="Chicago") 
         { 
             Log.i("SpecialList", "Chicago");
         } 
         else if (parent.getItemAtPosition(position)=="Dallas") 
         { 
             Log.i("SpecialList", "Dallas");
         } 
    }

    class CityListAdapter extends ArrayAdapter<String> {    

        CityListAdapter() {         
            super(SpecialList.this, R.layout.one_city_row);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            View row = null;
            LayoutInflater inflater = getLayoutInflater();

            if ((position == 0) && (!mAtlantaListExpanded))
            {
                row = inflater.inflate(R.layout.expandable_city_row, parent, false);
                TextView city = (TextView) row.findViewById(R.id.Expandable_TextView01);
                city.setText(items[position]);

                TextView cityZone1 = (TextView) row.findViewById(R.id.Zone_TextView01);
                cityZone1.setText("Zone 1");

                TextView cityZone2 = (TextView) row.findViewById(R.id.Zone_TextView02);
                cityZone2.setText("Zone 2");

                TextView cityZone3 = (TextView) row.findViewById(R.id.Zone_TextView03);
                cityZone3.setText("Zone 3");

                cityZone1.setOnClickListener(
                    new Button.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                             Log.i("SpecialList", "Zone 1");
                        }
                    }
                );

                cityZone2.setOnClickListener(
                    new Button.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                             Log.i("SpecialList", "Zone 2");
                        }
                    }
                );

                cityZone3.setOnClickListener(
                    new Button.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                             Log.i("SpecialList", "Zone 3");
                        }
                    }
                );              
            }
            else
            {
                row = inflater.inflate(R.layout.one_city_row, parent, false);
                TextView city = (TextView) row.findViewById(R.id.City_TextView01);
                city.setText(items[position]);
            }                   

            return(row);
        }
    }
} 

Click on "Atlanta" cause redraw of the list, toggling between showing only "Atlanta" or expanded list. R.layout.one_city_row.xml is simple layout with one TextView, R.layout.Expandable_city_row.xml has additional suboptions as TextView elements, as may be concluded from the attached code snippet.

Using this approach you may customize list completely according to your needs. On the places where I put Log.i(), you should put your intents.




回答2:


If I have understood your problem, what do you want is a list of items that has a different elements in the row, depending of the type of the item.

What I would recommend to you is to extend the class BaseAdapter and create your own row. In this way you control how the current row'll be printed depending on the type, showing or hiding whatever.

In the official Android documentation, section API Demos - ImageSwitcher you'll find an example.



来源:https://stackoverflow.com/questions/2994455/how-can-i-create-a-list-with-only-certain-items-expandable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!