Android开发使用SimpleAdapter与ListView列表展示增加头和尾
程序员文章站
2022-05-01 19:15:55
SimpleAdapterActivity.javapackage com.mw.app.view.activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.w....
SimpleAdapterActivity.java
package com.mw.app.view.activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.mw.app.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SimpleAdapterActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_adapter_layout);
ListView listView = findViewById(R.id.simple_adapter_layout_list_view);
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
for(int i=0;i<30;i++){
Map<String,String> map = new HashMap<String,String>();
map.put("imgText","客服"+i);
map.put("text1","电话"+i);
map.put("text2","邮件地址"+i);
list.add(map);
}
String[] form = {"imgText","text1","text2"};
int[] idArr = {R.id.simple_adapter_layout_img_text,R.id.simple_adapter_layout_text1,R.id.simple_adapter_layout_text2};
SimpleAdapter simpleAdapter = new SimpleAdapter(this,list,R.layout.simple_adapter_item_layout,form,idArr);
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map<String,String> map = (Map<String, String>) parent.getItemAtPosition(position);
Log.i("mw",map.toString());
}
});
}
}
simple_adapter_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/simple_adapter_layout_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
simple_adapter_item_layout.xml
<?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="match_parent"
>
<TextView
android:id="@+id/simple_adapter_layout_img_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/kf"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textSize="20sp"
>
</TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="168dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="@+id/simple_adapter_layout_text1"
android:layout_width="wrap_content"
android:layout_height="65dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="30sp"></TextView>
<TextView
android:id="@+id/simple_adapter_layout_text2"
android:layout_width="wrap_content"
android:layout_height="65dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="30sp"></TextView>
</LinearLayout>
</LinearLayout>
增加l列表头和尾
/**增加头和尾 最好在listView.setAdapter 之前新增 */ listView.addHeaderView(LayoutInflater.from(this).inflate(R.layout.simple_adapter_head_layout,null)); Button buttonFoot = new Button(this); buttonFoot.setText("加载更多"); //buttonFoot.setWidth(getWindowManager().getDefaultDisplay().getWidth()); buttonFoot.setWidth(this.getApplicationContext().getResources().getDisplayMetrics().widthPixels); listView.addFooterView(buttonFoot);
SimpleAdapterActivity.java
package com.mw.app.view.activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.mw.app.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SimpleAdapterActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_adapter_layout);
ListView listView = findViewById(R.id.simple_adapter_layout_list_view);
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
for(int i=0;i<30;i++){
Map<String,String> map = new HashMap<String,String>();
map.put("imgText","客服"+i);
map.put("text1","电话"+i);
map.put("text2","邮件地址"+i);
list.add(map);
}
String[] form = {"imgText","text1","text2"};
int[] idArr = {R.id.simple_adapter_layout_img_text,R.id.simple_adapter_layout_text1,R.id.simple_adapter_layout_text2};
SimpleAdapter simpleAdapter = new SimpleAdapter(this,list,R.layout.simple_adapter_item_layout,form,idArr);
/**增加头和尾 最好在listView.setAdapter 之前新增 */
listView.addHeaderView(LayoutInflater.from(this).inflate(R.layout.simple_adapter_head_layout,null));
Button buttonFoot = new Button(this);
buttonFoot.setText("加载更多");
//buttonFoot.setWidth(getWindowManager().getDefaultDisplay().getWidth());
buttonFoot.setWidth(this.getApplicationContext().getResources().getDisplayMetrics().widthPixels);
listView.addFooterView(buttonFoot);
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map<String,String> map = (Map<String, String>) parent.getItemAtPosition(position);
Log.i("mw",map.toString());
}
});
}
}
simple_adapter_head_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="@drawable/bg"></ImageView>
</LinearLayout>
头
尾
本文地址:https://blog.csdn.net/m0_37622302/article/details/107784297
上一篇: 立秋要吃什么