AndrowListView implementation (custom game list) prevents screen flickering, sets the split line in android not recommended method, consider backward compatibility, use the recommended new method, mobile phones of earlier versions may be incompatible

Source: Internet
Author: User

Activity Type

Package com. kane. listview;


Import java. util. ArrayList;
Import java. util. Date;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;


Import com. kane. listview. adapter. GameAdapter;
Import com. kane. listview. util. Globals;


Import android. OS. Bundle;
Import android. app. Activity;
Import android. util. Log;
Import android. view. Menu;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. View. OnTouchListener;
Import android. widget. AbsListView. OnScrollListener;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. AdapterView. OnItemLongClickListener;
Import android. widget. AbsListView;
Import android. widget. Button;
Import android. widget. ListView;
Import android. widget. TextView;
Import android. widget. Toast;


Public class MainActivity extends Activity {
Private ListView listView;
Private GameAdapter adapter;
Private List <Map <String, Object> allValues = new ArrayList <Map <String, Object> ();
Private Button btn1;
Private Button btn2;
Private Button btn3;
Private Button btn4;


// All image Arrays
Private int [] allImgs = {R. drawable. ph1, R. drawable. ph2, R. drawable. ph3, R. drawable. ph4 };
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

Globals. init (this); // initialize the current interface data

SetContentView (R. layout. activity_main );
// Initialize some data, which should be read from the database or network theoretically
For (int I = 0; I <5; I ++ ){
Map <String, Object> map = new HashMap <String, Object> ();
Map. put ("title", "Game name" + I );
Map. put ("time", "2013-04-23 action" + I );
Map. put ("img", allImgs [I % 4]);
AllValues. add (map );
}
Btn1 = (Button) findViewById (R. id. btn1 );
Btn1.setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
Btn1.setBackgroundResource (R. drawable. news_selected );

}
});
Btn2 = (Button) findViewById (R. id. btn2 );
Btn2.setOnTouchListener (new OnTouchListener (){

@ Override
Public boolean onTouch (View v, MotionEvent event ){
If (event. getAction () = MotionEvent. ACTION_DOWN) {// Press


Btn2.setBackgroundResource (R. drawable. strategy_selected );


} Else if (event. getAction () = MotionEvent. ACTION_UP) {//


Btn2.setBackgroundResource (R. drawable. strategy_select );
}


Return false;


}

});
// Obtain the component
ListView = (ListView) findViewById (R. id. list );
// Value adpter
Adapter = new GameAdapter (this, allValues );
ListView. setAdapter (adapter );
// List Row Element listening
ListView. setOnItemClickListener (new OnItemClickListener (){


@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3 ){
// Arg2 indicates the row number of the current vertex. Based on this value, data of the current row can be obtained.
Map <String, Object> map = allValues. get (arg2 );
Toast. makeText (MainActivity. this,
"The currently clicked items are:" + map. get ("title"), Toast. LENGTH_SHORT)
. Show ();
}
});
// Long press
ListView. setOnItemLongClickListener (new OnItemLongClickListener (){
@ Override
Public boolean onItemLongClick (AdapterView <?> Arg0, View arg1,
Int arg2, long arg3 ){
// Arg2 indicates the row number of the current vertex. Based on this value, data of the current row can be obtained.
Toast. makeText (MainActivity. this, "delete:" + arg2,
Toast. LENGTH_SHORT). show ();


Return false;
}
});

// Three methods of scrolling
ListView. setOnScrollListener (new OnScrollListener (){

@ Override
Public void onScrollStateChanged (AbsListView view, int scrollState ){
If (scrollState = OnScrollListener. SCROLL_STATE_IDLE ){
System. out. println ("the current status is idle ........-------");
} Else if (scrollState = OnScrollListener. SCROLL_STATE_TOUCH_SCROLL ){
System. out. println ("Touch Screen rolling status ......------");
} Else if (scrollState = OnScrollListener. SCROLL_STATE_FLING) {// fly, hand off the screen
System. out. println ("inertial rolling status ......------");
}
// The syso effect is similar to Log. I (tag, msg );
}

/**
* Scroll through the page. three parameters: the line number of the first element on the interface. Number of elements displayed on the page, total number of data
*/
@ Override
Public void onScroll (AbsListView view, int firstVisibleItem,
Int visibleItemCount, int totalItemCount ){
System. out. println ("current display item:" + firstVisibleItem + "+"
+ VisibleItemCount + "-->" + totalItemCount
+ "------");
}
});

}

}


Custom adapter class

Package com. kane. listview. adapter;


Import java. util. List;
Import java. util. Map;


Import com. kane. listview. util. Globals;


Import com. kane. listview. R;
Import android. content. Context;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. AbsListView. LayoutParams;
Import android. widget. BaseAdapter;
Import android. widget. TextView;


/**
* Custom adapter
* @ Author lenovo
*
*/
Public class GameAdapter extends BaseAdapter {
Private Context ctx;
// For android, set up classes as few as possible for performance. The list should be displayed on our interface. Each line of the List has multiple contents. We use map instead of entity classes.
Private List <Map <String, Object> allValues;

/**
* Only these two parameters are used here. The other parameters are configured in this class as needed.
* @ Param ctx
* @ Param allValues
*/
Public GameAdapter (Context ctx, List <Map <String, Object> allValues ){
This. ctx = ctx;
This. allValues = allValues;
}

// Obtain the total number of list elements
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return allValues. size ();
}


// What is the element returned by each row? position indicates the position
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return allValues. get (position );
}

// Obtain the ID of each element.
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
/**
* ConvertView returns the interface content of a row. It is a reusable row component to improve performance preparation. If the current row has not been created before, the value is null and needs to be created by yourself.
* If you have already created enough row components, convertView has content here. You only need to modify the data to form a control loop.
*/
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
If (convertView = null ){
// Create a new component and automatically create a component by dynamically reading xml; com. kane. listview. R
ConvertView = LayoutInflater. from (ctx). inflate (
R. layout. my_adapter_item, null );
// Set the height. Set the layout parameters at a tenth of the current page.
ConvertView. setLayoutParams (new LayoutParams (
LayoutParams. MATCH_PARENT, Globals. SCREEN_HEIGHT/10 ));
}
// Set data in the component
// Obtain the textview of the data to be set.
TextView imgText = (TextView) convertView. findViewById (R. id. line_img );
// Set the height of the image component
ImgText. getLayoutParams (). height = Globals. SCREEN_HEIGHT/10;
TextView titleText = (TextView) convertView. findViewById (R. id. line_title );
TextView timeText = (TextView) convertView. findViewById (R. id. line_time );
// Set the data and use map to transmit values for efficiency
Map <String, Object> map = allValues. get (position );
TitleText. setText (map. get ("title"). toString ());
TimeText. setText (map. get ("time"). toString ());
// Set the image. setBackground is a network resource.
ImgText. setBackgroundResource (Integer) map. get ("img "));
Return convertView;
}


}

Get interface property class

Package com. kane. listview. util;
Import android. app. Activity;
Public class Globals {


Public static int SCREEN_WIDTH;
Public static int SCREEN_HEIGHT;


Public static void init (Activity ){
// Initialize the width and height of the screen. This method is not recommended, but backward compatible. We currently support Version 2.3 or above, but the new method uses version 4.0 or above, so the old method is used.
SCREEN_WIDTH = a. getWindowManager (). getdefadisplay display (). getWidth ();
SCREEN_HEIGHT = a. getWindowManager (). getdefadisplay display (). getHeight ();
}

}

Main Interface

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<! -- View the layout to be split -->
<LinearLayout android: layout_width = "match_parent" android: layout_height = "0dp" android: layout_weight = "0.9"
Android: background = "@ drawable/topbg" android: orientation = "horizontal">
<TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: gravity = "center_vertical"
Android: background = "@ drawable/guanyu"/>
<TextView android: layout_width = "0dp" android: layout_height = "wrap_content" android: gravity = "center"
Android: textColor = "# ffffff" android: textSize = "20sp"
Android: text = "popular nowadays" android: layout_weight = "4"/>
<TextView android: layout_width = "0dp" android: layout_height = "match_parent" android: layout_weight = "1"/>
</LinearLayout>
<ListView
Android: id = "@ + id/list"
Android: layout_width = "match_parent"
Android: dividerHeight = "1dp"
Android: divider = "#00000000"
Android: background = "# eeeeee"
Android: layout_height = "0dp" android: layout_weight = "8">
</ListView>
<! -- Divider is used to set the border --> <! -- Android: cacheColorHint = "#00000000" prevents screen flickering -->

<! -- The following linear percentage is automatically overwhelmed by the above -->
<LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal">
<Button android: layout_width = "0dp" android: layout_weight = "1" android: layout_height = "wrap_content" android: id = "@ + id/btn1"
Android: background = "@ drawable/news_select"/>
<Button android: layout_width = "0dp" android: layout_weight = "1" android: layout_height = "wrap_content" android: id = "@ + id/btn2"
Android: background = "@ drawable/strategy_select"/>
<Button android: layout_width = "0dp" android: layout_weight = "1" android: layout_height = "wrap_content" android: id = "@ + id/btn3"
Android: background = "@ drawable/ranking_select"/>
<Button android: layout_width = "0dp" android: layout_weight = "1" android: layout_height = "wrap_content" android: id = "@ + id/btn4"
Android: background = "@ drawable/free_select"/>

</LinearLayout>
</LinearLayout>

Line Element Interface

<! -- Custom adapter, a line -->
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/listbg"
Android: orientation = "horizontal"
Android: paddingBottom = "5dp"
Android: paddingLeft = "5dp"
Android: paddingRight = "5dp"
Android: paddingTop = "5dp">


<TextView
Android: id = "@ + id/line_img"
Android: layout_width = "0dp"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"/>


<LinearLayout
Android: layout_width = "0dp"
Android: layout_height = "wrap_content"
Android: layout_weight = "4"
Android: orientation = "vertical">


<TextView
Android: id = "@ + id/line_title"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: textColor = "#000000"
Android: textSize = "14sp"/>


<TextView
Android: id = "@ + id/line_time"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: textColor = "# bbbbbb"
Android: textSize = "10sp"/>
</LinearLayout>


<TextView
Android: layout_width = "0dp"
Android: layout_height = "wrap_content"
Android: layout_weight = "0.75"/>


</LinearLayout>


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.