Android-listview usage.

Source: Internet
Author: User

Listview is powerful and has many usage methods. Here is a simple usage method. First, let's take a look:

The function of this list is to select an item and display it. Other functions are the same as those of listview.

To achieve the above effect, you need to make a baseadapter and modify the attributes of the listview and the right slider.

The code for mylistadapter is as follows:

import java.lang.reflect.Field;import android.content.Context;import android.graphics.Color;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.*;public class MyListAdapter extends BaseAdapter{private Context mContext;private boolean[] isFocused ;private TextView[] ItemContent ;private int ItemCount;private int whichClick = -1;public StoryListAdapter(Context c, String[] str, int length){this.mContext = c;this.isFocused = new boolean[length];this.ItemContent = new TextView[length];this.ItemCount = length;for (int i = 0; i < length; i++){isFocused[i] = false;}TextView v;for (int i = 0; i < length; i++){v = new TextView(mContext);v.setText("◆"+str[i]);v.setTextSize(30);ItemContent[i] = v;}}//@Overridepublic int getCount(){// TODO Auto-generated method stub//return ListData.length;return ItemCount;}//@Overridepublic Object getItem(int position){// TODO Auto-generated method stubreturn position;}//@Overridepublic long getItemId(int position){// TODO Auto-generated method stubreturn position;}//@Overridepublic View getView(int position, View convertView, ViewGroup parent){TextView v = ItemContent[position];if(isFocused[position])v.setTextColor(Color.WHITE);elsev.setTextColor(Color.BLACK);v.setBackgroundColor(isFocused[position]?Color.rgb(100,66,24):Color.TRANSPARENT);return v;}public void setCurrentSel(int index){isFocused[whichClick == -1 ? 0 : whichClick] = false;whichClick = index;isFocused[index] = true;notifyDataSetInvalidated();}public void setUnCurrentSel(){for (int i = 0; i < ItemCount; i++){isFocused[i] = false;}notifyDataSetInvalidated();}public int getCurrentSel(){int ret = 0 ;if(whichClick != -1)ret = whichClick; return ret;}}

The following method is used to call the main program:

String [] listtestdata = {"test data 1", "Test Data 2", "Test Data 3", "test data 4", "test data 5 ", "Test Data 6", "test data 7", "test data 8", "Test Data 9", "Test Data 10", "Test Data 11 ", "Test Data 12", "Test Data 13", "Test Data 14", "test data 15", "Test Data 16"}; mylistadapter; listview = (listview) findviewbyid (R. id. main_list_view); mylistadapter = new mylistadapter (this, listtestdata, listtestdata. length); listview. setadapter (storylistadapter); storylistadapter. setcurrentsel (0 );

At this point, the above results are basically achieved, but there are still many problems that we can solve one by one:

1. After you click the list, the background of the list turns black. You can set the attribute of Android: cachecolorhint to transparent. Use the following property values:

Android: cachecolorhint = "#000000"

2. Remove the split line in the middle of the list item:Android: divider = "#00000000"The value can also point to a drawable image object (Android: divider = "@ drawable/list_line"If the image height is greater than the system pixel, you can set a height. Android: dividerhight = "10px"

3. When the listview is dragged, the background of the listview turns black. You can use the following code to solve the problem:Android: scrollingcache = "false"

4. There is a black shadow on the top and bottom of the listview. You can use the following code to solve the problem:Android: fadingedge = "NONE"

5. the slider on the right of the listview overwrites the content of the list item. You can use the following code to solve the problem:Android: scrollbarstyle = "outsideinset"

6. Modify the distance between the slide on the right side of listvew and the list items. You can use the following code to solve the problem:Android: paddingright = "10dip"And can be modified as needed.

7. Modify the color of the slide bar on the right. You can use the following code to solve the problem:

Android: scrollbartrackvertical = "@ drawable/scrollbar_vertical_track"
Android: scrollgateumbvertical = "@ drawable/scrollbar_vertical_thumb"

Among them, scrollbar_vertical_track and scrollbar_vertical_thumb are the xml configuration files of Slide bars. In demoapi, you only need to modify the start color and end color as needed. The content of the XML file is as follows:

Scrollbar_vertical_track.xml:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">     <gradient android:startColor="#D6C9B5" android:endColor="#D6C9B5"             android:angle="0"/>    <corners android:radius="0dp" /></shape>

Scrollbar_vertical_thumb.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <gradient android:startColor="#634117" android:endColor="#634117"            android:angle="0"/>    <corners android:radius="6dp" /></shape>

Android: startcolor = "# d6c9b5" is the starting color of # RGB.

8. When you click an item, there is no background color change. You must specify its listselector attribute in listview, as shown below:

Android: listselector = "@ layout/list_press"

The content of list_press.xml is as follows:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" /></selector> 

At this point, the above results have been completed.

The following describes all the complete settings of the listview attribute:

<ListView    android:id="@+id/main_list_view"      android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:scrollingCache="false"    android:fadingEdge="none"    android:cacheColorHint="#000000"    android:divider="#00000000"    android:scrollbarStyle="outsideInset"    android:paddingRight="10dip"    android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track"    android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"        android:listSelector="@layout/list_press" >

9. How to Use and customize fastscroller, and display a slider on the Right of listview, as shown below:

To implement it, you only need to set its attributes in listview, as shown below:

    android:fastScrollEnabled="true"     android:focusable="true" 

However, sometimes you will find that the setting property is invalid and the slider does not appear in the scrolling listview. The reason is that this attribute has a minimum record limit. When the listview record can be displayed within 4 screens (that is, four pages are rolled), no slider will appear. It may be that API designers think that so few records do not need to be quickly rolled.

How can I modify the picture of the slider of fastscroller:

You can use the following code:

Try {field F = abslistview. class. getdeclaredfield ("mfastscroller"); F. setaccessible (true); object o = f. get (listview); F = f. getType (). getdeclaredfield ("mthumbdrawable"); F. setaccessible (true); drawable = (drawable) F. get (o); // R. drawable. scrollbar is the custom image drawable = getresources (). getdrawable (R. drawable. scrollbar); F. set (O, drawable);} catch (exception e) {Throw new runtimeexception (E );}

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.