小张学编程ing 2022-05-17 22:02 采纳率: 60%
浏览 83
已结题

在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是跟着别人做的,我就想把两个结合起来,结果就成这样了

  • 写回答

5条回答 默认 最新

  • windcore 2022-05-17 22:16
    关注
    获得0.60元问题酬金

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

    评论

报告相同问题?

问题事件

  • 系统已结题 5月25日
  • 创建了问题 5月17日

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题