在fragment中嵌套listview时,item的内容显示不完整,代码写的有点乱

Infragment.java主要代码


```java

public class InfFragment  extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout swipeRefreshLayou;
    ListView lv_Inf;
    String[] arrayFriends=new String[]{"小红","小花","小美"};
    int[] head=new int[]{R.mipmap.xh,R.mipmap.xhua,R.mipmap.xm};
    String[] logo=new String[]{"数学第一题怎么做呀?","这周末有空吗?","记得晚上出来吃饭哦!"};
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_inf;
    }

    @Override
    protected void initView() {
         swipeRefreshLayou=find(R.id.inf_swiperefresh);
        swipeRefreshLayou.setOnRefreshListener(this);
       lv_Inf= find(R.id.list);
       setData();//给列表添加数据
    }
    private void setData() {
        SimpleAdapter simpleAdapter=new SimpleAdapter(
                getActivity(),
                getData(),
                R.layout.layout_inf_item,
                new String[]{"name","head","logo"},
                new int[]{R.id.iv_item_name,R.id.lv_item_head,R.id.lv_item_logo}
        );
       lv_Inf.setAdapter(simpleAdapter);
    }
    private List<Map<String,Object>> getData() {
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        for (int i=0;i<head.length;i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name",arrayFriends[i]);
            map.put("head",head[i]);
            map.put("logo",logo[i]);
            list.add(map);
        }
        return list;
    }
    @Override
    public void onRefresh() {

        swipeRefreshLayou.setRefreshing(false);
    }

}




fragment_inf.xml布局代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    >

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/inf_swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/ll_inf"
        android:background="@color/btnLogin"
        >
        <ImageView
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:layout_width="40sp"
            android:layout_height="40sp"
            android:src="@mipmap/tx"
            />
        <TextView
            android:textSize="20dp"
            android:layout_marginTop="25dp"
            android:layout_marginLeft="120dp"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="消息"
            />
        <ImageView
            android:layout_marginTop="25dp"
            android:layout_width="match_parent"
            android:layout_height="30sp"
            android:src="@drawable/ic_inf_add_24"
            />
    </LinearLayout>
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="88dp"
       app:layout_constraintTop_toBottomOf="@id/ll_inf">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_search_24" />
        <EditText
            android:id="@+id/inf_edt_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="18dp"
            android:background="@color/white"
            android:cursorVisible="false"
            android:focusable="auto"
            android:gravity="center"
            android:hint="搜索"
            android:minHeight="30dp"
            android:textSize="16sp" />
    </androidx.appcompat.widget.Toolbar>
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="560dp"
            app:layout_constraintBottom_toBottomOf="@+id/inf_swiperefresh"
            app:layout_constraintTop_toBottomOf="@+id/toolbar"

            />
</androidx.constraintlayout.widget.ConstraintLayout>



layout_inf_item.xml布局代码

```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:src="@mipmap/qq333"
        android:id="@+id/iv_item_name"
        android:layout_width="60dp"
        android:layout_height="60dp"
        />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lv_item_head"
            android:textSize="25sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lv_item_logo"
            android:textSize="20sp"
            />
    </LinearLayout>

</LinearLayout>

我的代码运行截图如下

img

我想要的是下面这种效果

img

求帮助啊 我觉得应该是嵌套的问题,因为我是自己网上学习的fragment,这个listview是跟着别人做的,我就想把两个结合起来,结果就成这样了

问题出在适配器上哦,就是你的SimpleAdapter,建议你看下有关适配器的介绍。

SimpleAdapter(Context context, List> data, int resource, String[] from, int[] to)
在SimpleAdapter 构造中int[] to ,需要传递int[] 数据,题主是传递的一个new int[] 这种方式有问题

// 列表项组件Id 数组
  int[] to = { R.mipmap.xh,R.mipmap.xhua,R.mipmap.xm};

private void setData() {
        SimpleAdapter simpleAdapter=new SimpleAdapter(
                getActivity(),
                getData(),
                R.layout.layout_inf_item,
                new String[]{"name","head","logo"},
                to
        );
       lv_Inf.setAdapter(simpleAdapter);
    }

别用SimpleAdapter了,实战中用到的地方很少。自定义一个通用的Adapter继承BaseAdapter吧,可以满足你后面的任何需求,或者用一个第三方的库,方便好用很多,可以自己百度搜一下。还有ListView也是被淘汰的了,用RecycleView。

看不出问题,最好就自己调试打断点到图片加载的地方吧
还有布局那里,ListView应该放在SwipeRefreshLayout这个里面的吧,不然不能下拉刷新的哦
然后ListView已经淘汰的了...该去看看RecyclerView了哦

兄弟们,问题解决了。
一个很低级的错误。。。。是因为我在item的布局写的ImageView的id和第一个TextView的id搞反了,所以在运行出来的那一串数字实际上是我要显示的头像,而我的name则传到ImageView里面去了。后面我换回来之后又出现了一个问题,就是我的图片读不出来,登录后直接闪退了。
后面我把我的图片从mipmap里取出来放到drawable里,再把R.mipmap改为R.drawable,就显示正常了,具体原因我也不清楚,总之问题算是解决了。
最后感谢各位的帮助!