Fragment中使用listview的用法
背景:
在android项目中,经常用到关于listview的,经过很多博客博主的文章,我尝试很多测试都失败了,终于通过不断的总结调试终于有了可靠的解决方案,这里分享给小伙伴们;
先上效果图:
1、定义xml
xml 主要有两个;第一个位listview,还有一个为item;
1.1 fragment_my.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="vertical"
android:background="#dee9f7">
<ListView
android:id="@+id/my_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
1.2 my_menu.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">
<ImageView
android:id="@+id/myMenu_image"
android:layout_margin="10dp"
android:layout_width="80px"
android:layout_height="80px" />
<TextView
android:id="@+id/myMenu_name"
android:textSize="23dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
1.3 Fragment核心代码
package com.liu.znybq.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.liu.znybq.R;
import com.liu.znybq.utils.L;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
* 项目名: android_znybq
* 包名: com.liu.znybq.fragment
* 文件名: FragmentHome
* 创建者: shi860715@126.com liubj
* 创建时间: 2020/4/16 12:50
* 描述: TODO
*/
public class FragmentMy extends Fragment implements AdapterView.OnItemClickListener {
ListView listView;
SimpleAdapter simpleAdapter;
/**
* 布局文件和framagement 关联
* @param inflater
* @param container
* @param savedInstanceState
* @return
*/
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_my,container,false);
listView =view.findViewById(R.id.my_list);
simpleAdapter = new SimpleAdapter(getActivity(),getData(),R.layout.my_menu,new String[]{"title","image"},new int[]{R.id.myMenu_name,R.id.myMenu_image});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(this);
return view;
}
private List<Map<String,Object>> getData() {
String [] titles={"水果1","水果2","水果3","水果4","水果5","水果6","水果7"};
int [] images={R.drawable.shuiguo_0,R.drawable.shuiguo_1,R.drawable.shuiguo_2,
R.drawable.shuiguo_3,R.drawable.shuiguo_4,
R.drawable.shuiguo_5,R.drawable.shuiguo_6};
List<Map<String,Object>> list= new ArrayList<>();
for(int i=0;i<7;i++){
Map map = new HashMap();
map.put("title",titles[i]);
map.put("image",images[i]);
list.add(map);
}
return list;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
L.i("position:"+position);
String text = listView.getAdapter().getItem(position).toString();
L.i("position:"+position+"text"+text );
}
}
1.4 测试
通过点击事件可用得到,里面的json格式的输出
2020-04-16 12:43:31.393 7455-7455/com.liu.znybq I/ZNYBQ: position:6text{image=2131230868, title=水果7}