Using listview for Android to implement multi-menu functions

Source: Internet
Author: User

We know that many software products have an interface for setting menu items, such as moji weather. When there are many options in the settings, we can choose to use listview.

The layout file is as follows:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@drawable/splash_background"><ListView android:id="@+id/more_List"android:layout_width="fill_parent"android:layout_height="fill_parent"android:divider="@drawable/list_bottom_line"android:listSelector="#00000000"android:cacheColorHint="#00000000"></ListView></LinearLayout>

Simple: Only one listview

Item layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="8dip"android:background="@drawable/list_item"><ImageView android:id="@+id/item_icon"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextView android:id="@+id/more_item_text"android:layout_marginLeft="10dip"android:textSize="20dip"android:textColor="#000000"android:layout_width="wrap_content"android:layout_height="wrap_content"/><ImageView android:id="@+id/item_image"android:layout_marginTop="5dip"android:layout_marginLeft="10dip"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/triangle"/></LinearLayout>

Set it to horizontal layout. the icon, text description, and arrow icon are displayed at one time.

The main code is as follows:

Package com. cloay. news; import com. cloay. news. bean. moreAdapter; import com. cloay. news. service. mainService; import com. cloay. news. util. alertDlgUtil; import com. waps. appConnect; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. listView;/*** more interfaces include help and more. * @ Author Cloay * 2012-3-3*09:45:12 */public class MoreActivity extends NewsReaderActivity {private ListView moreList; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. more); MainService. allActivity. add (this); moreList = (ListView) findViewById (R. id. more_List); MoreAdapter adapter = new MoreAdapter (this); moreList. setAdapter (adapte R); moreList. setOnItemClickListener (new OnItemClickListener () {// set the corresponding response for each item @ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {switch (position) {case 0: // startActivity (AboutActivity. class); break; case 1: // share Intent intent = new Intent (Intent. ACTION_SEND); intent. setType ("text/plain"); intent. putExtra (Intent. EXTRA_SUBJECT, "share"); intent. putExtra (Intent. EXTRA_TEXT, "I wowould like to share this with you... "); startActivity (Intent. createChooser (intent, getTitle (); break; case 2: // help start Activity (HelpActivity. class); break; case 3: // feedback AppConnect. getInstance (MoreActivity. this ). showFeedback (); break; case 4: // exit AlertDlgUtil. alertExit (MoreActivity. this); break ;}});} private void startActivity (Class <?> Cls) {Intent intent = new Intent (this, cls); this. startActivity (intent );}}

The following is the adapter that provides data for listview:

Package com. cloay. news. bean; import java. util. arrayList; import java. util. list; import com. cloay. news. r; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. textView;/*** provide data for more listview * @ author Cloay * 06:28:14 */public class MoreAdapter extends BaseAdapter {private LayoutInflater inflater; private Context context; public static final List <String> more_list = new ArrayList <String> (); // provide public MoreAdapter (Context context) {this for the entry. context = context; inflater = LayoutInflater. from (this. context); more_list.add (context. getResources (). getString (R. string. main_square); more_list.add (context. getResources (). getString (R. string. share); more_list.add (context. getResources (). getString (R. string. help_text); more_list.add (context. getResources (). getString (R. string. advice); more_list.add (context. getResources (). getString (R. string. exit_title);} @ Overridepublic int getCount () {return more_list.size (); // number of entries} @ Overridepublic Object getItem (int position) {return null ;} @ Overridepublic long getItemId (int position) {return 0 ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {if (convertView = null) {convertView = inflater. inflate (R. layout. more_list, null);} ImageView icon = (ImageView) convertView. findViewById (R. id. item_icon); // set the icon if (0 = position) {icon. setBackgroundResource (R. drawable. icon_square);} else if (1 = position) {icon. setBackgroundResource (android. r. drawable. ic_menu_share);} else if (2 = position) {icon. setBackgroundResource (R. drawable. icon_setting);} else if (3 = position) {icon. setBackgroundResource (android. r. drawable. ic_menu_edit);} else {icon. setBackgroundResource (R. drawable. menu_exit);} TextView text = (TextView) convertView. findViewById (R. id. more_item_text); // specifies the text description of the entry. setText (more_list.get (position); return convertView ;}}

Run the following command:

I will introduce it here. If you have any questions, please leave a message for discussion!

Note: Please indicate the source for reprinting!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.