Android ListView Default to select an implementation code _android

Source: Internet
Author: User

Here is the directory generated using TOC:

layout file definition

◦listview definition
◦item template Definition

• code

◦ Initialization List
◦ User Click to process

• Effect

--------------------------------------------------------------------------------

To use ListView to achieve a recharge option, the default want to select the second item, for a afternoon, finally finished. Originally did not use Java to write the Android application, and separated for a long time did not write, everything is unfamiliar, the half-baked become big dull melon ...

Layout file definition

In two parts, part of the definition of ListView, part of the item template, which is the definition of row.

ListView definition

Also simple to say, here are the ListView definitions in the Layout file:

  <listview
    android:id= "@+id/recharge_method_list"
    android:layout_width= "Fill_parent"
    android: layout_height= "Wrap_content"
    android:layout_margintop= "6DP"
    android:dividerheight= "2DP"
    android: divider= "@color/ssq_bkgnd"
    android:background= "@android: Color/white" android:choicemode= "Singlechoice"
    android:listselector= "@null"
    >
  </ListView>

Hey, don't say, Csdn's markdown editor is much better than the original default editor, and inserting the code is simpler. This is the first time to use Csdn markdown, praise one.

Item Template Definition

The item template is defined as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" horizontal "android:layout_width=" Match_parent "android:layout_height=" 60DP "Android : gravity= "center_vertical" android:background= "@drawable/option_selector" > <imageview android:id= "@+id/re
    Charge_method_icon "android:layout_width=" 40DP "android:layout_height=" 40DP "android:layout_marginleft=" 20DP " android:layout_marginright= "4DP"/> <linearlayout android:layout_width= "0DP" android:layout_height = "Wrap_content" android:layout_weight= "1" android:orientation= "vertical" > <textview android:i D= "@+id/recharge_method_name" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a Ndroid:textsize= "15sp"/> <textview android:id= "@+id/recharge_method_clue" Android:layout_widt H= "Wrap_content" Android:layout_height= "Wrap_content" android:textsize= "12sp"/> </LinearLayout> <imageview android:i D= "@+id/recharge_method_checked" android:layout_width= "34DP" android:layout_height= "28DP" Android:layout_margi nright= "16DP" android:src= "@drawable/option_checked" android:visibility= "invisible"/> </linearlayout&gt ;

In order to display a selection icon for a ListView item, add a ImageView to the item template file, and control its display and hide to achieve the desired effect. Steal a lazy, this is relatively simple to achieve, in the ListView of the number of the item in memory, performance, and so little impact.

Code

The code is relatively simple, in two parts, part of the initialization list, part of the user clicks on a list of items after switching the check mark.

Initializing a list

The Initrechargelist () method is used to initialize the list of recharge methods as follows:

  private void Initrechargelist () {actiontexts = new string[]{getString (R.string.recharge_unionpay), Getstri
    Ng (R.string.recharge_alipay), getString (R.string.recharge_bestpay)}; Actionclue = new string[]{getString (R.string.recharge_unionpay_clue), getString (R.string.recharge_alipay_clue), G
    Etstring (R.string.recharge_bestpay_clue)}; Actionimages = new int[]{R.drawable.unionpay, R.drawable.recharge_icon_alipay, R.drawable.recharg
    E_icon_bestpay};
    ActionList = (ListView) Findviewbyid (r.id.recharge_method_list);
    Actionitems = new arraylist 

The above code is a list of initialization recharge methods. The use of ListView is also simpler, View–row Template–data–adapter, four elements.

The question I encountered was: How to select an item by default.

In fact, there are only three items in my list, no matter which one will not be visible, it should be visible on the Android phone.

I started by calling the ListView Setadapter method, using Getchildat (1) to get the second corresponding View, you guessed, yes, crashed: NullPointerException. Empty pointer Ah, in C + + when the old lover, instead of Java to write Android, she came back and I dated.

It took me a while to figure out: Setadapter () was actually asynchronous, calling this method, and ListView's item was not created immediately, but was created in the next round of message processing. To figure this out, there is a workaround in the previous code: Use Post () to submit a Runnable () object, in Runnable () to do the default selection of this initialization action.

As you can see, I have new a Runnable to the post () method, the 2nd item is found in run (), the check icon is displayed, and the View corresponding to the 2nd item is saved to the Lastcheckedoption member variable. We will use lastcheckedoption this variable, combined with Onitemclicklistener to achieve the ListView of the three item mutually exclusive selection effect.
Markdown How to indent each paragraph before ... In confusion ... Four spaces will give this paragraph the background color, very good ...

User Click to process

Click Processing is done through the Adapterview.onitemclickedlistener interface. The code is as follows:

  Private Adapterview.onitemclicklistener ItemListener = new Adapterview.onitemclicklistener () {

    @Override
    public void Onitemclick (adapterview<?> parent, view view, int position, long id) {
      if (lastcheckedoption!= null) {
        lastcheckedoption.setvisibility (view.invisible);
      }
      Lastcheckedoption = View.findviewbyid (r.id.recharge_method_checked);
      Lastcheckedoption.setvisibility (view.visible);
    }
  ;

As you can see, I saved the last selected item by the lastcheckedoption variable to indicate the selected effect of the icon, the user clicks on a certain, first hide the last item of the selected icon, and then show the current, there is a seemingly mutually exclusive effect.

It's all so simple, it's done.

Effect

The final effect is purple:

Well, inserting a picture is much more useful than the original Markdown editor.

--------------------------------------------------------------------------------

Well, I can't believe I'm writing Android apps again, and I feel high.

--------------------------------------------------------------------------------

Also do not know markdown version of the editor to write out the blog, published a swollen how it is, generated a directory, direct TOC is OK, or very convenient. Finished, or did not understand the beginning of the paragraph indentation how to do it.

also said to support offline editing, praise.

There is one point: the left and right columns, you can see the effect, than GitHub wiki page editing a little stronger.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.