AndrowListView實現(自訂遊戲列表)防止螢幕閃爍,設定分割線android中不推薦的方法,要考慮向下相容,用了推薦的新方法,可能不相容舊版本系統的手機

來源:互聯網
上載者:User

activity類

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;


//所有圖片數組
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);//初始化當前介面資料

setContentView(R.layout.activity_main);
//初始化一些資料,理論上應該是從資料庫或網路讀取
for (int i = 0; i < 5; i++) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("title","遊戲名稱"+i);
map.put("time","2013-04-23   動作"+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) {//按下


btn2.setBackgroundResource(R.drawable.strategy_selected);


} else if (event.getAction() == MotionEvent.ACTION_UP) {//彈起


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


return false;


}

});
//取得組件
listView=(ListView)findViewById(R.id.list);
//賦值adpter
adapter=new GameAdapter(this,allValues);
listView.setAdapter(adapter);
//list中行元素監聽
listView.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// arg2表示當前點的行號,根據這個值,可以取得當前行的資料
Map<String, Object> map=allValues.get(arg2);
Toast.makeText(MainActivity.this,
"當前所點擊的是: " + map.get("title"), Toast.LENGTH_SHORT)
.show();
}
});
//長按
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// arg2表示當前點的行號,根據這個值,可以取得當前行的資料
Toast.makeText(MainActivity.this, "刪除: " + arg2,
Toast.LENGTH_SHORT).show();


return false;
}
});

//三種方式的滾動
listView.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState==OnScrollListener.SCROLL_STATE_IDLE) {
System.out.println("當前是空閑狀態........ ------- ");
} else if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
System.out.println("觸屏滾動狀態......------");
} else if (scrollState == OnScrollListener.SCROLL_STATE_FLING) {//飛翔,手離開螢幕
System.out.println("慣性滾動狀態......------");
}
//syso效果類似Log.i(tag, msg);
}

/**
* 滾動翻頁,三個參數:介面第一個元素的行號。介面顯示元素數量,資料的總數量
*/
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
System.out.println("當前的顯示項目: " + firstVisibleItem + " + "
+ visibleItemCount + " --> " + totalItemCount
+ "  ------ ");
}
});

}

}


自訂adapter類

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;


/**
 * 自訂adapter
 * @author lenovo
 *
 */
public class GameAdapter extends BaseAdapter {
private Context ctx;
//android為了效能盡量少建立類,我們的介面要顯示列表,列表每行有多個內容,我們用map裝,而不用實體類代表
private List<Map<String, Object>> allValues;

/**
* 這裡只用接收這兩個參數,其他的我們根據自己需要在這個類配置好
* @param ctx
* @param allValues
*/
public GameAdapter(Context ctx,List<Map<String, Object>> allValues){
this.ctx=ctx;
this.allValues=allValues;
}

//擷取列表元素總數
@Override
public int getCount() {
// TODO Auto-generated method stub
return allValues.size();
}


//每行返回的元素是什麼,position指的是位置
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return allValues.get(position);
}

//擷取每行元素的ID
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/**
* 返回某一行的介面內容 convertView是為了提高效能準備的重用的行組件, 如果當前行之前沒有建立過,則該值為null,需要自行建立.
* 如果之前已經建立了足夠的行組件,則這裡convertView有內容,只需要將裡面的資料修改了即可.形成控制項迴圈利用
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null) {
// 建立新的組件, 需要動態讀取xml自動建立組件;com.kane.listview.R
convertView = LayoutInflater.from(ctx).inflate(
R.layout.my_adapter_item, null);
// 設定高度,當前介面的十分之一,設定布局參數
convertView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, Globals.SCREEN_HEIGHT / 10));
}
//設定組件中的資料
//分別取得要設定資料的textview
TextView imgText=(TextView)convertView.findViewById(R.id.line_img);
//設定圖片組件的高度
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);
//設定資料,為了效率用map傳值
Map<String, Object> map=allValues.get(position);
titleText.setText(map.get("title").toString());
timeText.setText(map.get("time").toString());
//設定圖片;setBackground是網路資源
imgText.setBackgroundResource((Integer)map.get("img"));
return convertView;
}


}

獲得介面屬性類

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 a) {
// 初始化螢幕的寬度和高度,這裡的方法不推薦使用,但是向下相容,我們目前全支援2.3版本以上,但是新方法使用4.0以上,所以還是用舊方法
SCREEN_WIDTH = a.getWindowManager().getDefaultDisplay().getWidth();
SCREEN_HEIGHT = a.getWindowManager().getDefaultDisplay().getHeight();
}

}

主介面

<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" >
    <!-- 布局要拆分的看 -->
<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="時下熱門" 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用來設定邊界線的-->    <!-- android:cacheColorHint="#00000000"防止螢幕閃爍 -->

    <!-- 沒給下面的linear比例,自動被上面的壓倒底部 -->
<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>

 每行元素介面

<!-- 自訂的adapter,一行的樣子 -->
<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>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.