AndrowListView实现(自定义游戏列表)android中不推荐的方法,要考虑向下兼容,用了推荐的新方法,可能不兼容旧版本系统的手机

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="0dp"
        android:divider="#00000000"
        android:background="#eeeeee"
        android:layout_height="0dp" android:layout_weight="8" >
    </ListView>
    <!--  divider用来设置边界线的-->
    <!-- 没给下面的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>

AndrowListView实现(自定义游戏列表)android中不推荐的方法,要考虑向下兼容,用了推荐的新方法,可能不兼容旧版本系统的手机

AndrowListView实现(自定义游戏列表)android中不推荐的方法,要考虑向下兼容,用了推荐的新方法,可能不兼容旧版本系统的手机,布布扣,bubuko.com

AndrowListView实现(自定义游戏列表)android中不推荐的方法,要考虑向下兼容,用了推荐的新方法,可能不兼容旧版本系统的手机

上一篇:一张图让你看懂Android不同密度屏幕图片的像素制作比例


下一篇:②安卓学习(分享案例):迷彩灯 图片切换 跟随手指的圈