Android实现页面跳转、ListView及其事件
开发工具:Andorid Studio 1.3
运行环境:Android 4.4 KitKat
进入主页面后,使用ListView实现特定页面
点击其中任何一项水果,跳转到另外一个活动,使用Intent转换活动,并使用Bundle传递数据,跳转到特定页面
代码实现首先在主页面的xml文件中添加ListView控件并给予id
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".FruitList"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/fruitListView"> </ListView> </LinearLayout>接着设计一个ListView中的Item,用线性水平布局加一个ImageView和一个TextView满足要求,适当调整一下图片的大小和文字的大小以及边距,使得好看一些
<?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="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fruit_image" android:adjustViewBounds="true" android:maxHeight="@dimen/fruit_image_size" android:maxWidth="@dimen/fruit_image_size"/> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/fruit_image_size" android:paddingLeft="@dimen/activity_horizontal_margin" android:id="@+id/fruit_name" android:textSize="@dimen/fruit_name_size" android:gravity="center_vertical"/> </LinearLayout>在主类FruitList中获取ListView的控件,然后给List添加项,新建一个adapter绑定我们建的那个layout和List,最后绑定到ListView中
fruitListView = (ListView)this.findViewById(R.id.fruitListView); fruitList.add(new Fruit("apple", R.mipmap.apple)); fruitList.add(new Fruit("banana", R.mipmap.banana)); fruitList.add(new Fruit("cherry", R.mipmap.cherry)); fruitList.add(new Fruit("coco", R.mipmap.coco)); fruitList.add(new Fruit("kiwi", R.mipmap.kiwi)); fruitList.add(new Fruit("orange", R.mipmap.orange)); fruitList.add(new Fruit("pear", R.mipmap.pear)); fruitList.add(new Fruit("strawberry", R.mipmap.strawberry)); fruitList.add(new Fruit("watermelon", R.mipmap.watermelon)); FruitAdapter adapter = new FruitAdapter(FruitList.this, R.layout.fruit_item, fruitList); fruitListView.setAdapter(adapter);Class Fruit和class FruitAdapter的实现如下
public class Fruit { private String name; private int imageId; public Fruit(String _name, int _imageId){ this.name = _name; this.imageId = _imageId; } public String getName(){ return this.name; } public int getImageId(){ return this.imageId; } } public class FruitAdapter extends ArrayAdapter<Fruit> { private int resourceId; public FruitAdapter(Context context, int textViewResourceId, List<Fruit> fruits){ super(context, textViewResourceId, fruits); this.resourceId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent){ Fruit fruit = getItem(position); View view = LayoutInflater.from(this.getContext()).inflate(resourceId, null); ImageView fruitImage = (ImageView)view.findViewById(R.id.fruit_image); TextView fruitName = (TextView)view.findViewById(R.id.fruit_name); fruitImage.setImageResource(fruit.getImageId()); fruitName.setText(fruit.getName()); return view; } }点击某一个水果的响应是获取水果的名字,加入到一个Bundle中去,然后绑定到一个Intent,传递到另一个界面
fruitListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView listView = (ListView) parent; Fruit fruit = (Fruit) listView.getItemAtPosition(position); String name = fruit.getName(); Intent intent = new Intent(FruitList.this, ShowFruitActivity.class); Bundle bundle = new Bundle(); bundle.putString("name", name); intent.putExtras(bundle); startActivity(intent); //FruitList.this.finish(); } });长按某一个水果的响应是:找到点击的水果,然后在适配器中删除这一水果,然后适配器发出改变通知,ListView更新删除后的状态
fruitListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { try { ListView listView = (ListView) parent; Fruit fruit = (Fruit) listView.getItemAtPosition(position); FruitAdapter adapter = (FruitAdapter)listView.getAdapter(); adapter.remove(fruit); adapter.notifyDataSetChanged(); listView.invalidate(); } catch (Exception e) { Log.d("Hint", "Remove failed!"); return false; } return true; } });