0
我在一個片段中創建了可擴展列表視圖,並且工作正常。不過,我想在可展開的listview子項被點擊時開始一個活動。我找了幾個小時試圖找到解決辦法,但我找不到它。有人請爲此問題提供代碼。如何在可擴展列表項目單擊事件上啓動新活動
import android.R.color;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;
// This is a listfragment class
public class Categories extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.categories, null);
ExpandableListView elv = (ExpandableListView) v.findViewById(R.id.expandableListView1);
elv.setAdapter(new SavedTabsListAdapter());
return v;
}
public class SavedTabsListAdapter extends BaseExpandableListAdapter {
private String[] groups = { "g1", "g2"};
private String[][] children = {
{ "c1", "2},
{ "1", "2"}
};
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public int getChildrenCount(int i) {
return children[i].length;
}
@Override
public Object getGroup(int i) {
return groups[i];
}
@Override
public Object getChild(int i, int i1) {
return children[i][i1];
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
TextView textView = new TextView(CategoriesFragment.this.getActivity());
textView.setText(getGroup(i).toString());
return textView;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
TextView textView = new TextView(CategoriesFragment.this.getActivity());
textView.setText(getChild(i, i1).toString());
return textView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
}
謝謝..最後它的工作 – Kabil
不知道你在做什麼。我會保持你的適配器作爲你的片段的字段,然後當onChildClick被調用時,你可以做myListAdapter.getChild(groupPos,childPos)來取回子對象。使這個子對象具有需要的數據來決定什麼是動作。 –