以列表控件(如:ListView或RecyclerView)实现如图所示界面效果:(后面我给ListView里面加了item的点击事件)
实验过程:
首先我们要在activity-main.xml创建一个ListView,设置id为lstView
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lstView"></ListView>
</LinearLayout>
然后开始创建一个Layout Source File,右键在layout上面然后创建,取名为items,这个是lstView里面每个项的样式,根据实验报告,我们创建一个大概这样的样式:
items.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Icon"></ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:id="@+id/Title"
></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:id="@+id/Source"
></TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|right"
android:id="@+id/Time"
></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
有了上述这些就可以准备写MainActivity中的代码了,首先我们要创建一个数组,和四个字符串组,数组是用来储存图片的文件路径的,在安卓里面文件的路径返回的是整数型。比如我们可以看到:
这里new1这个图片的路径返回的是整数型1500001.
其它四个数组存储内容,标题,来源,时间。
然后我们要声明一个ListView,并且为它绑定刚刚我们在activity-main.xml所创建的lstView
然后我们要创建一个Haspmap<String,Object>的数列,里面存着Haspmap<String,Object>
每个map里面存的是ListView里面每一项的数据。
然后我们通过for循环向Hashmap里面添加实例
代码:
for(int i = 0;i<icons.length;i++)
{
Map<String,Object> map=new HashMap<String, Object>() ;
map.put("标题",titles[i]);//第一个参数是Key,也就是为变量取一个名字
map.put("来源",sources[i]);
map.put("图片",icons[i]);
map.put("时间",times[i]);
itemsPicker.add(map);
}
然后我们再设置适配器,这次我们用的是SimpleAdapter,名字是简单适配器,但是其实相对之前发布的博客里的适配器,要复杂一些。
我们可以看到,SimpleAdapter里面一共有五个参数
第一个参数是上下文,我们这里是用MainActivity,所以直接填this
第二个参数是我刚所创建的Haspmap的数列也就是List<Haspmap<String,Object>>
第三个参数是我们所创建的界面,也就是上文中所创建的items.xml
第四个是map里面对应的Key,上文有讲到
第五个是items里面控件的名字,与四个参数相对应,把第四个参数里面的值赋给控件里面
代码:
SimpleAdapter adapter=new SimpleAdapter(this,itemsPicker,R.layout.items,new String[]{"标题","来源","图片","时间"},new int[]{R.id.Title,R.id.Source,R.id.Icon,R.id.Time});
然后为listView装配适配器。
listView.setAdapter(adapter);
上面就把界面给实现了,点击运行可以出来界面。后续可以不看,我后面实现的是点击ListView里面的item,根据点击的是哪一个跳转到DetailActivity,并且携带相对应的数据。
现在开始实现点击ListView里面的item,根据点击的是哪一个跳转到DetailActivity,并且携带相对应的数据。
先创建一个DetailActivity的活动,然后写它的页面
activity-detail.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:id="@+id/new_title">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:id="@+id/new_source"
>
</TextView>
<TextView
android:gravity="bottom|right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:id="@+id/new_time"></TextView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/new_content"></TextView>
</LinearLayout>
</LinearLayout>
然后要在MainActivity中写lstView的点击事件
position是一个返回你点击的是哪一项的整数型,从0开始
intent可以看成是通信的信使
intent的用法有点类似Hashmap,
不过添加数据是用putExtra
最后不要忘记startActivity(intent)
代码:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(MainActivity.this,DetailActivity.class);
switch (position)
{
case 0:
intent.putExtra("标题",titles[position]);
intent.putExtra("内容",contents[position]);
intent.putExtra("来源",sources[position]);
intent.putExtra("时间",times[position]);
break;
case 1:
intent.putExtra("标题",titles[position]);
intent.putExtra("内容",contents[position]);
intent.putExtra("来源",sources[position]);
intent.putExtra("时间",times[position]);
break;
}
startActivity(intent);
}
});
然后就是书写DetailActivity的代码,相对 简单,就一个需要注意的点,要
Intent intent =getIntent(),用intent的getStringExtra来获取数据:
package com.example.homework22;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DetailActivity extends AppCompatActivity {
private TextView txttitle;
private TextView txtcontent;
private TextView txtsource;
private TextView txttime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
txttitle=(TextView)findViewById(R.id.new_title);
txttime=(TextView)findViewById(R.id.new_time);
txtsource=(TextView)findViewById(R.id.new_source);
txtcontent=(TextView)findViewById(R.id.new_content);
Intent intent = getIntent();
txttitle.setText(intent.getStringExtra("标题"));
txttime.setText(intent.getStringExtra("时间"));
txtsource.setText(intent.getStringExtra("来源"));
txtcontent.setText(intent.getStringExtra("内容"));
}
}
代码:
MainActivity.java代码:
package com.example.homework22;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private int[] icons = {R.mipmap.news1,R.mipmap.news1};
private String[] contents = {"气到了,不能用QQ找到的图片?","随便用两个图吧"};
private String[] titles = {"标题一,图片放不上去啊","标题二,为什么找不到图片"};
private String[] sources={"来源:新华网","来源:西南石油大学"};
private String[] times={"日期:2020/11/20" ,"日期:2020/11/19"};
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.lstView);
List<Map<String,Object>> itemsPicker=new ArrayList<Map<String, Object>>();
for(int i = 0;i<icons.length;i++)
{
Map<String,Object> map=new HashMap<String, Object>() ;
map.put("标题",titles[i]);
map.put("来源",sources[i]);
map.put("图片",icons[i]);
map.put("时间",times[i]);
itemsPicker.add(map);
}
SimpleAdapter adapter=new SimpleAdapter(this,itemsPicker,R.layout.items,new String[]{"标题","来源","图片","时间"},new int[]{R.id.Title,R.id.Source,R.id.Icon,R.id.Time});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(MainActivity.this,DetailActivity.class);
switch (position)
{
case 0:
intent.putExtra("标题",titles[position]);
intent.putExtra("内容",contents[position]);
intent.putExtra("来源",sources[position]);
intent.putExtra("时间",times[position]);
break;
case 1:
intent.putExtra("标题",titles[position]);
intent.putExtra("内容",contents[position]);
intent.putExtra("来源",sources[position]);
intent.putExtra("时间",times[position]);
break;
}
startActivity(intent);
}
});
}
}