Android-ExpandableListView的setOnClickListener失效

前端开发 前端开发 主题:1148 回复:2395

Android-ExpandableListView的setOnClickListener失效

晚风撩人 发布于 2017-06-21 字数 12559 浏览 1248 回复 1

有人说把item的focus设置为false就行,但是我的不行,先谢谢大家了,上源码

package com.example.borec;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import weibo4j.model.Status;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.google.gson.Gson;

public class ExpandableActivity extends ExpandableListActivity {
private ImageView im = null;
private TextView textview1 = null;

public void onCreate(Bundle savedInstanceState) {
// System.out.println("正在获取数据");
this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
super.onCreate(savedInstanceState);
setContentView(R.layout.exapandable_listactivity);
im = (ImageView) findViewById(R.id.helpact);
textview1 = (TextView) findViewById(R.id.textView1);
im.setOnClickListener(new ImageListener());
textview1.setOnClickListener(new TextViewListener());
makeList();
}

public class ImageListener implements OnClickListener {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent t = new Intent(ExpandableActivity.this,
GridLayoutActivity.class);
startActivity(t);
finish();
}

}

public class TextViewListener implements OnClickListener {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent t = new Intent(ExpandableActivity.this,
GridLayoutActivity.class);
startActivity(t);
finish();
}

}

public void makeList() {
List<List<Map<String, String>>> children = new ArrayList<List<Map<String, String>>>();
List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
// Map<String,String> group = new HashMap<String, String>();

SQLiteDatabase db = MainService.getDatabase();

ArrayList<String> userNameList = MainService.readUserName(db);
if (userNameList != null) {
for (int i = 0; i < userNameList.size(); i++) {
Map<String, String> group = new HashMap<String, String>();
group.put("group", userNameList.get(i));
groups.add(group);
// group.clear();
// System.out.println("group is " + group);
}
// 以上没有错误

ArrayList<String> uidList = MainService.readUid(db);
if (uidList != null) {
for (int i = 0; i < uidList.size(); i++) {
ArrayList<Status> statusList = readStatusByUid(uidList
.get(i));
List<Map<String, String>> childOfGroup = new ArrayList<Map<String, String>>();
if (statusList != null) {
for (int j = 0; j < statusList.size()
&& statusList.get(j) != null; j++) {
// System.out.println("j = " + j);
Map<String, String> childData = new HashMap<String, String>();
childData.put("childData", statusList.get(j)
.getText() + statusList.get(j).getAnnotations());
childOfGroup.add(childData);
// childData.clear();
// System.out.println(childData);
}
children.add(childOfGroup);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"出了点小错,报告给作者吧", Toast.LENGTH_LONG);
toast.show();
}
}
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
this, groups, R.layout.group, new String[] { "group" },
new int[] { R.id.groupTo }, children, R.layout.child,
new String[] { "childData" },
new int[] { R.id.childTo });
// 将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity
setListAdapter(sela);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"您一个用户都没添加呢,添加完再查看吧", Toast.LENGTH_LONG);
toast.show();
}
db.close();

ExpandableListView list = getExpandableListView();

list.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> ada, View view,
int index, long longIndex) {
System.out.println("arg2 = " + index + "arg3 = " + longIndex);
return false;
}
});


list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Toast toast = Toast.makeText(getApplicationContext(),
// "长按将删除该微博", Toast.LENGTH_LONG);
//toast.show();
//TextView t;
//if ((t = (TextView) info.targetView.findViewById(R.id.childTo)) != null){}
System.out.println("arg2 = " + arg2 + "arg3 = " + arg3);
}
});

OnCreateContextMenuListener menuListener = new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View arg1,
ContextMenuInfo arg2) {
menu.setHeaderTitle(R.string.contentMenu);
menu.add(0, 0, 0, R.string.confirm);
}
};
list.setOnCreateContextMenuListener(menuListener);
} else {
Toast t = Toast.makeText(getApplicationContext(), "您还没有添加备份名单呢",
Toast.LENGTH_LONG);
t.show();
}
}

// 生成一个SimpleExpandableListAdapter对象
// 1.context
// 2.一级条目的数据
// 3.用来设置一级条目样式的布局文件
// 4.指定一级条目数据的key
// 5.指定一级条目数据控件的id
// 6.指定二级条目的数据
// 7.用来设置二级条目样式的布局文件
// 8.指定二级条目数据的key
// 9.指定二级条目数据显示控件的id

public boolean onContextItemSelected(MenuItem item) {
SQLiteDatabase db = MainService.getDatabase();
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) item
.getMenuInfo();
TextView t = null;
if ((t = (TextView) info.targetView.findViewById(R.id.childTo)) != null) {
String text = t.getText().toString();
if (!TextUtils.isEmpty(text)) {
if (item.getItemId() == 0) {
text = """ + text + """;
try {
db.delete("status", "text=" + text, null);
} catch (Exception e) {
System.out.println(e);
}
Toast toast = Toast.makeText(getApplicationContext(),
"微博删除成功", Toast.LENGTH_LONG);
toast.show();
makeList();
ExpandableListView list = getExpandableListView();

list.expandGroup(list
.getPackedPositionGroup(info.packedPosition));
}
}
} else if ((t = (TextView) info.targetView.findViewById(R.id.groupTo)) != null) {
String name = t.getText().toString();
if (!TextUtils.isEmpty(name)) {
if (item.getItemId() == 0) {
name = """ + name + """;
try {
db.delete("users", "user_name=" + name, null);
} catch (Exception e) {
System.out.println(e);
}
Toast toast = Toast.makeText(getApplicationContext(),
"用户删除成功", Toast.LENGTH_LONG);
toast.show();
makeList();
}
}
}
return false;
}

public ArrayList<Status> readStatusByUid(String uid) {
Gson gson = new Gson();
ArrayList<Status> statusList = new ArrayList<Status>();
SQLiteDatabase db = MainService.getDatabase();
Cursor cursor = null;
System.out.println("要查询的uid是" + uid);
cursor = db.query("status", new String[] { "create_at", "id", "text",
"user_name", "uid", "annotations" }, "uid=?",
new String[] { uid }, null, null, null);
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount() && cursor.moveToNext(); i++) {
String info = new String();
info = "{" + ""createdAt":" + """
+ cursor.getString(cursor.getColumnIndex("create_at"))
+ "",";
info = info + ""id":" + """
+ cursor.getString(cursor.getColumnIndex("id")) + "",";
info = info + ""text":" + """
+ cursor.getString(cursor.getColumnIndex("text")) + "",";
info = info + ""user":{"screenName":""
+ cursor.getString(cursor.getColumnIndex("user_name"))
+ "",";
info = info + ""uid":" + """
+ cursor.getString(cursor.getColumnIndex("uid")) + ""}";
if (cursor.getString(cursor.getColumnIndex("annotations")) == null)
info = info + "}" + "n";
else
info = info
+ ","annotations":"
+ """
+ cursor.getString(cursor.getColumnIndex("annotations"))
+ """ + "}" + "n";
Status t = null;
try {
t = gson.fromJson(info, Status.class);
} catch (Exception e) {
System.out.println(e);
}
statusList.add(t);
}
// cursor.getString(cursor.getColumnIndex("create_at"))
// System.out.println(statusList);
db.close();
return statusList;
}
}

// 写一个makelist函数

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

支持 Markdown 语法,需要帮助?

评论(1

瑾兮 2017-06-29 1 楼

在你的item布局中,需要添加onClick事件的组件中添加以下属性:

android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"