Jump to content
  • Hello visitors, welcome to the Hacker World Forum!

    Red Team 1949  (formerly CHT Attack and Defense Team) In this rapidly changing Internet era, we maintain our original intention and create the best community to jointly exchange network technologies. You can obtain hacker attack and defense skills and knowledge in the forum, or you can join our Telegram communication group to discuss and communicate in real time. All kinds of advertisements are prohibited in the forum. Please register as a registered user to check our usage and privacy policy. Thank you for your cooperation.

    TheHackerWorld Official

Recommended Posts

2229560-20220916102123754-1133360349.jpg

Android基础开发——ListView

16. ListView

实现滑动条目

16.1 list_item布局
<?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">

    <TextView
        android:id="@+id/tv"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

在这里插入图片描述

16.2 Java Bean
package com.dingjiaxiong.mylistview;

public class Bean {
    
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
16.3 设置数据

在这里插入图片描述

16.4 辅助类——Adapter
package com.dingjiaxiong.mylistview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter {

    private static final String TAG = "dingjiaxiong";
    private List<Bean> data;
    private Context context;

    //构造方法
    public MyAdapter(List<Bean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view == null){
            view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
        }

        TextView textView = view.findViewById(R.id.tv);
        textView.setText(data.get(i).getName());

        Log.e(TAG, "getView: "+ i);

        return view;
    }
}
16.5 配置数据
//拿到listview
ListView listView = findViewById(R.id.lv);

listView.setAdapter(new MyAdapter(data , this));

运行

在这里插入图片描述

16.6 list_view的点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.e("dingjiaxiong", "onItemClick: " + i);
    }
});

在这里插入图片描述

16.7 优化 ViewHolder

MyAdapter.java

package com.dingjiaxiong.mylistview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseAdapter {

    private static final String TAG = "dingjiaxiong";
    private List<Bean> data;
    private Context context;

    //构造方法
    public MyAdapter(List<Bean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        
        ViewHolder viewHolder;
        
        if(view == null){
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
            viewHolder.textView = view.findViewById(R.id.tv);
            view.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder) view.getTag();
        }


        viewHolder.textView.setText(data.get(i).getName());

        Log.e(TAG, "getView: "+ i);

        return view;
    }
    
    private final class ViewHolder{
        TextView textView;
    }
    
}

在这里插入图片描述

2229560-20220916102350281-1841300895.jpg

Link to post
Link to comment
Share on other sites

 Share

discussion group

discussion group

    You don't have permission to chat.
    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...