点击标题右边的三个杠并固定便于打开目录
效果图
前言
这个问题也有叫做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(); } }