我有一個自定義列表視圖ListFragment
與3種類型的標題(1st.Green背景,2nd.Black背景,3rd.Gray背景),你可以在video看到。實現標題作爲FloatingGroupExpandableListView
我想自定義綠色標題,即點擊綠色標題後,下一個綠色標題之間的所有視圖(項目)必須最小化,&也再次點擊那個綠色標題,最小化的視圖應該是exapanded 。
像FloatingGroupExpandableListView一樣的功能,它使用一些虛擬物品,在我的情況下,必須定製視圖。
我不知道如何修改我的代碼來完成此任務。
public class ContentsFragment extends ListFragment implements OnTouchListener
{
final Context context = getActivity();
private MyCustomAdapter mAdapter;
Activity temp = getActivity();
TextView tv1,tv3;
String []s = new String[500];
ArrayList<GS> q = new ArrayList<GS>();
ListView lv;
int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBAdapter db = DBAdapter.getDBAdapter(getActivity());
if (!db.checkDatabase())
db.createDatabase(getActivity());
db.openDatabase();
q = db.getData();
mAdapter = new MyCustomAdapter(getActivity());
mAdapter.addGreenSeparatorItem(new ContentWrapper("PART I", null, null, null, null));//group
mAdapter.addSeparatorItem(new ContentWrapper(q.get(0).getA_name(),null,null,null,null));//child
mAdapter.addItem(new ContentWrapper(q.get(0).getAS_name(), q.get(0).getDesc_art(),q.get(0).getExtra(),q.get(0).getPart(),q.get(0).getItalic()));//child
for (int i = 1; i <= 14; i++) {
if(i==5) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART II", null, null, null, null));
if(i==12) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART III", null, null, null, null));
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null,null,null,null));
}
if(!(q.get(i).getExtra()==null))
mAdapter.addGraySeparatorItem(new ContentWrapper(q.get(i).getExtra(),null,null,null,null));//child
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art(),q.get(i).getExtra(),q.get(i).getPart(),q.get(i).getItalic()));
}
for (int i = 15; i < 36; i++) {
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null,null,null,null));
}
if(!(q.get(i).getExtra()==null))
mAdapter.addGraySeparatorItem(new ContentWrapper(q.get(i).getExtra(),null,null,null,null));
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art(),q.get(i).getExtra(),q.get(i).getPart(),q.get(i).getItalic()));
}
mAdapter.addItem(new ContentWrapper(q.get(460).getAS_name(), q.get(460).getDesc_art(),q.get(460).getExtra(),q.get(460).getPart(),q.get(460).getItalic()));
for (int i=36; i < 158; i++) {
if(i==41) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART IV", null, null, null, null));//group
if(i==60) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART IVA", null, null, null, null));//group
if(i==61) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART V", null, null, null, null));
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null,null,null,null));
}
if(!(q.get(i).getExtra()==null))
mAdapter.addGraySeparatorItem(new ContentWrapper(q.get(i).getExtra(),null,null,null,null));
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art(),q.get(i).getExtra(),q.get(i).getPart(),q.get(i).getItalic()));
}
mAdapter.addItem(new ContentWrapper(q.get(461).getAS_name(), q.get(461).getDesc_art(),q.get(461).getExtra(),q.get(461).getPart(),q.get(461).getItalic()));
for (int i=158; i < 210; i++) {
if(i==163) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART VI", null, null, null, null));
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null,null,null,null));
}
if(!(q.get(i).getExtra()==null))
mAdapter.addGraySeparatorItem(new ContentWrapper(q.get(i).getExtra(),null,null,null,null));
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art(),q.get(i).getExtra(),q.get(i).getPart(),q.get(i).getItalic()));
}
mAdapter.addItem(new ContentWrapper(q.get(462).getAS_name(), q.get(462).getDesc_art(),q.get(462).getExtra(),q.get(462).getPart(),q.get(462).getItalic()));
for (int i=210; i < 218; i++) {
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null,null,null,null));
}
if(!(q.get(i).getExtra()==null))
mAdapter.addGraySeparatorItem(new ContentWrapper(q.get(i).getExtra(),null,null,null,null));
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art(),q.get(i).getExtra(),q.get(i).getPart(),q.get(i).getItalic()));
}
mAdapter.addItem(new ContentWrapper(q.get(463).getAS_name(), q.get(463).getDesc_art(),q.get(463).getExtra(),q.get(463).getPart(),q.get(463).getItalic()));
for (int i=218; i < 244; i++) {
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null,null,null,null));
}
if(!(q.get(i).getExtra()==null))
mAdapter.addGraySeparatorItem(new ContentWrapper(q.get(i).getExtra(),null,null,null,null));
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art(),q.get(i).getExtra(),q.get(i).getPart(),q.get(i).getItalic()));
}
mAdapter.addItem(new ContentWrapper(q.get(464).getAS_name(), q.get(464).getDesc_art(),q.get(464).getExtra(),q.get(464).getPart(),q.get(464).getItalic()));
for (int i=244; i < 459; i++) {
if(i==248) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART VII", null, null, null, null));
if(i==249) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART VIII", null, null, null, null));
if(i==257) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART IX", null, null, null, null));
if(i==273) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART IXA", null, null, null, null));
if(i==291) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART X", null, null, null, null));
if(i==293) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XI", null, null, null, null));
if(i==314) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XII", null, null, null, null));
if(i==353) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XIII", null, null, null, null));
if(i==360) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XIV", null, null, null, null));
if(i==377) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XIVA", null, null, null, null));
if(i==379) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XV", null, null, null, null));
if(i==386) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XVI", null, null, null, null));
if(i==400) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XVII", null, null, null, null));
if(i==421) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XIX", null, null, null, null));
if(i==431) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XX", null, null, null, null));
if(i==432) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XXI", null, null, null, null));
if(i==455) mAdapter.addGreenSeparatorItem(new ContentWrapper("PART XXII", null, null, null, null));
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(new ContentWrapper(q.get(i).getA_name(), null,null,null,null));
}
if(!(q.get(i).getExtra()==null))
mAdapter.addGraySeparatorItem(new ContentWrapper(q.get(i).getExtra(),null,null,null,null));
mAdapter.addItem(new ContentWrapper(q.get(i).getAS_name(), q.get(i).getDesc_art(),q.get(i).getExtra(),q.get(i).getPart(),q.get(i).getItalic()));
}
setListAdapter(mAdapter);
}
//Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_GRAY_SEPARATOR = 2;
private static final int TYPE_GREEN_SEPARATOR = 3;
private static final int TYPE_MAX_COUNT = TYPE_GREEN_SEPARATOR + 1;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
private TreeSet<Integer> mGraySeparatorsSet = new TreeSet<Integer>();
private TreeSet<Integer> mGreenSeparatorsSet = new TreeSet<Integer>();
private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
private LayoutInflater mInflater;
public MyCustomAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
}
public void addItem(ContentWrapper value) {
mData.add(value);
notifyDataSetChanged();
}
public void addSeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public void addGraySeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mGraySeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public void addGreenSeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mGreenSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public ContentWrapper getItem(int position) {
return mData.get(position);
}
@Override
public int getItemViewType(int position) {
int viewType = TYPE_ITEM;
if(mSeparatorsSet.contains(position))
viewType = TYPE_SEPARATOR;
else if(mGraySeparatorsSet.contains(position))
viewType = TYPE_GRAY_SEPARATOR;
else if(mGreenSeparatorsSet.contains(position))
viewType = TYPE_GREEN_SEPARATOR;
return viewType;
// return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public long getItemId(int position) {
Log.v("getItemId Position", ""+position);
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.white, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.black, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
case 2:
convertView = mInflater.inflate(R.layout.gray, null);
holder.textView = (TextView)convertView.findViewById(R.id.textViewGray);
break;
case 3:
convertView = mInflater.inflate(R.layout.green, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparatorGreen);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
} holder.textView.setText(mData.get(position).getItem());
getListView().setFastScrollEnabled(true);
if (type == TYPE_ITEM) {
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.description);
TextView desc = (TextView) dialog.findViewById(R.id.textView3);
TextView part = (TextView) dialog.findViewById(R.id.textView1);
TextView italic = (TextView) dialog.findViewById(R.id.textView2);
TextView title = (TextView) dialog.findViewById(R.id.textView4);
italic.setTypeface(null, Typeface.ITALIC);
title.setText(mData.get(position).getItem());
desc.setText(mData.get(position).getItemDescription());
part.setText(mData.get(position).getPart());
italic.setText(mData.get(position).getItalic());
dialog.show();
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
}
});
}else if(type == 1) {
holder.textView.setOnClickListener(null);
}
else if(type == 2)
{
holder.textView.setOnClickListener(null);
}
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
public TextView header;
int previousTop = 0;
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
謝謝,請幫忙。 。 。 。 。
對不起,_這個問題並不廣泛適用於大量的聽衆。但是,我期待從官方的消息來源或其他誰做過這項工作的答案! –
你想要做的是'ExpandableListView'的確切行爲。在你發佈的視頻中(可怕的例子是你想要的btw)我看到兩種類型的標題+正常的行(我不知道這是你的意思是3種類型的標題)。你想要做的是使用'ExpandableListView',其中組視圖是綠色部分,其餘(黑色標題和普通行)將是兩種類型的子行。 – Luksprog
@Luksprog如果您仔細觀看視頻,它也會顯示帶有灰色背景的標題,視頻正在顯示上述代碼的輸出!有3種類型的頭1st.Green背景,2nd.Black背景,3rd.Gray背景!然後正常的白色背景行是可點擊的項目,點擊時會顯示對話框中的數據!因此,綠色部分將是組視圖&其餘部分(黑色標題,灰色標題,普通行)將是3種子行。 –