我正在创建一个应用程序,它有一个可扩展的列表视图我能够获得可扩展的列表视图,但我的子点击不起作用。我试图在可扩展列表视图中单击子项时显示Toast消息。请帮助我,让我知道我在做什么错。提前谢谢。
My Activity code:
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AdminActivity extends AppCompatActivity {
Toolbar toolbar;
ExpandableListAdapter listAdapter;
List<String> titleText;
SQLiteDataBaseAdapter db;
HashMap<String, List<String>> listDataChild;
ExpandableListView login, android, ios, testing, java, dotNet, os, hr, others;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
toolbar = (Toolbar) findViewById(R.id.appBar);
toolbar.setTitle(" Admin Screen");
toolbar.setTitleTextColor(Color.WHITE);
login = (ExpandableListView) findViewById(R.id.expandableListViewLogin);
titleText = new ArrayList<>();
titleText.add("User Id Authentication");
titleText.add("Android Posts Authentication");
titleText.add("iOS Posts Authentication");
titleText.add("Testing Posts Authentication");
titleText.add("Java Posts Authentication");
titleText.add("Dot Net Posts Authentication");
titleText.add("OS Posts Authentication");
titleText.add("HR Posts Authentication");
titleText.add("Others Posts Authentication");
SQLiteDataBaseAdapter db = new SQLiteDataBaseAdapter(this);
List<String> childDataLogin = db.getLoginList();
List<String> childDataAndroid = db.getAndroidList();
List<String> childDataIos = db.getIosList();
List<String> childDataTesting = db.getTestingList();
List<String> childDataJava = db.getJavaList();
List<String> childDataDotNet = db.getDotNetList();
List<String> childDataOs = db.getOSList();
List<String> childDataHr = db.getHRList();
List<String> childDataOthers = db.getOthersList();
listDataChild = new HashMap<>();
listDataChild.put(titleText.get(0), childDataLogin); // Header, Child data
listDataChild.put(titleText.get(1), childDataAndroid);
listDataChild.put(titleText.get(2), childDataIos);
listDataChild.put(titleText.get(3), childDataTesting);
listDataChild.put(titleText.get(4), childDataJava);
listDataChild.put(titleText.get(5), childDataDotNet);
listDataChild.put(titleText.get(6), childDataOs);
listDataChild.put(titleText.get(7), childDataHr);
listDataChild.put(titleText.get(8), childDataOthers);
listAdapter = new MyExpandableListAdapter(this, titleText, listDataChild);
login.setAdapter(listAdapter);
// Listview on child click listener
login.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
titleText.get(groupPosition)
+ " : "
+ listDataChild.get(
titleText.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_admin, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Layout of child:
<?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="vertical">
<TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="? android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>
Adapter Code:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public MyExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.expandableListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
}
答案 0 :(得分:1)
请务必覆盖可扩展列表适配器的isChildSelectable
方法并返回true,如下所示:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
...
}
或
android:focusable="false"
在CheckBox
文件的expandlist_child_item.xml
部分内。
我希望这有助于某人。
答案 1 :(得分:1)
在isChildSelectable
的方法中返回true
替换
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
带
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}