listview中item中控件的點擊事件(獲取listview中item的控件id)


點擊標題右邊的三個杠並固定便於打開目錄

效果圖

 

 前言

這個問題也有叫做listview焦點問題,listview的item混亂或重復問題,listview獲取view,獲取id問題。網上關於這個的講解都挺多的,但是沒有幾個說清楚的,這個問題也是困擾了我很久,解決后發現問題好像也沒有想象的那么復雜,可能也是出於這個原因,所以相關的有效解決辦法很少。

先說下我的需求,就是一個上傳列表,點擊item里面的按鈕,便可以暫停,再次點擊,便繼續上傳。

正確參考

如有時間,建議先看以下教程,寫得更為詳細(只是需求有些許不同)。

Android ListView Checkbox 混亂 - Android 基礎教程 - 簡單教程,簡單編程 (twle.cn)

Android ListView:實現item內部控件的點擊事件_JZHowe's blog-CSDN博客

錯誤參考

將listview的高度設置為fill或者match,其實並不需要,設置為wrap也是可以的。(我也不清楚他們這樣說的原因)

可能遇到的問題和解決辦法

1.符號不顯示

logd打印圖片為空,說明沒有獲取到圖片。

(有時候是獲取不到上下文,更改獲取圖片的代碼或位置即可)

Log.d(TAG, "onChangeContinue: "+bitmap_test);

  

 

  實現代碼

其實代碼挺簡單的,主要就是一些位置的原因。

 

XML

activity_main.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout 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"
    android:padding="8dp"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
</LinearLayout>

listview_item.xml

注意添加屬性,以便於控件可以點擊:android:focusable="false"  

 

<?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:padding="8dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:textColor="#1D1D1C"
        android:textSize="20sp"
        android:layout_weight="3" />

    <CheckBox
        android:id="@+id/checked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/text_checked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="好呀"
        android:focusable="false"
        android:textColor="#999"
        android:layout_weight="1" />

</LinearLayout>

JAVA

LanguageBean.java

搭配list記錄狀態

package com.example.checkbox_check;

public class LanguageBean  {

    private String name;
    private Boolean checked;
    private String text;

    public LanguageBean (String name, Boolean checked,String text) {
        this.name = name;
        this.checked = checked;
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public Boolean getChecked() {
        return checked;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

LanguageAdapter.java

注意代碼位置即可。

package com.example.checkbox_check;

import android.content.Context;
import android.widget.BaseAdapter;

import android.util.Log;

import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CheckBox;

import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;

import java.util.LinkedList;

public class LanguageAdapter extends BaseAdapter {

    private LinkedList<LanguageBean> mData;
    private Context mContext;
    public LanguageAdapter(LinkedList<LanguageBean> mData, Context mContext) {

        this.mData = mData;
        this.mContext = mContext;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final int index = position;
        ViewHolder holder = null;
//        convertView = null;

        // 調試輸出信息的時候不推薦用 info 級別,信息太多太雜
        Log.d("xunyan", String.valueOf(index) + " " + String.valueOf(convertView));

        if (convertView == null) {

            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, parent, false);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.checked = (CheckBox) convertView.findViewById(R.id.checked);
            holder.text = (TextView) convertView.findViewById(R.id.text_checked);

            convertView.setTag(holder);   //將Holder存儲到convertView中
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.checked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mData.get(index).setChecked(isChecked);
            }
        });

        holder.text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mData.get(index).getText() == "不好") {
                    mData.get(index).setText("好的!");
                } else {
                    mData.get(index).setText("不好");
                }
                mOnItemChangeContinue.onChangeContinue(position);
            }
        });

        holder.name.setText(mData.get(index).getName());
        holder.checked.setChecked(mData.get(index).getChecked());
        holder.text.setText(mData.get(index).getText());

        return convertView;
    }

    /**
     * 改變傳輸符號(這里的作用只是刷新列表)
     */
    /********接口*****/
    public interface onItemChangeContinue {
        void onChangeContinue(int position);

    }

    private onItemChangeContinue mOnItemChangeContinue;

    public void setOnItemDeleteClickListener(onItemChangeContinue mOnItemChangeContinue) {
        this.mOnItemChangeContinue = mOnItemChangeContinue;
    }

    /******************/

    static class ViewHolder {
        TextView name;
        CheckBox checked;
        TextView text;
    }
}

MainActivity.java

package com.example.checkbox_check;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener  {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<LanguageBean> mData = new LinkedList<LanguageBean>();

        mData.add(new LanguageBean("Kotlin",true,"不好"));
        mData.add(new LanguageBean("Scala", false,"不好"));
        mData.add(new LanguageBean("Swift",false,"不好"));
        mData.add(new LanguageBean("TypeScript", false,"不好"));
        mData.add(new LanguageBean("java",false,"不好"));
        mData.add(new LanguageBean("Python", false,"不好"));
        mData.add(new LanguageBean("PHP",true,"不好"));
        mData.add(new LanguageBean("Perl", true,"不好"));

        //創建一個 YetAdapter
        LanguageAdapter languageAdapter = new LanguageAdapter((LinkedList<LanguageBean>) mData,getApplicationContext());
        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(languageAdapter);
        listView.setOnItemClickListener(this);

        languageAdapter.setOnItemDeleteClickListener(new LanguageAdapter.onItemChangeContinue() {
            @Override
            public void onChangeContinue(int position) {
                languageAdapter.notifyDataSetChanged(); //刷新列表
            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getApplicationContext(),"你點擊了第" + position + "項",Toast.LENGTH_SHORT).show();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM