Tabhost在ExpandableList的getChildlView方法中创建问题

时间:2016-04-27 20:34:28

标签: android listview

我已将标签insilde一个expandablelistview适配器,并且由于其getCHildView方法标签在父点击中多次生成。如何解决这个问题。

这是我需要实现的一个例子。

http://i.stack.imgur.com/rvPZe.png

在左侧图像中,有可向用户显示的可扩展视图列表。一旦用户点击任何可扩展视图,我需要在可扩展列表视图的childlistview中显示选项卡,如右图所示。

请参阅下面的整个代码

我的活动类

public class CardList extends AppCompatActivity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_list);

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);
        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Toast.makeText(
                        getApplicationContext(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                listDataHeader.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT)
                        .show();
                return false;
            }
        });
        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();
            }
        });

        expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();
        listDataHeader.add("Top 250");
        listDataHeader.add("Now Showing");
        listDataHeader.add("Coming Soon..");
        List<String> top250 = new ArrayList<String>();
        top250.add("The Shawshank Redemption");
        List<String> nowShowing = new ArrayList<String>();
        nowShowing.add("The Conjuring");
        List<String> comingSoon = new ArrayList<String>();
        comingSoon.add("2 Guns");
        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);
    }
} 

活动xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#f4f4f4" >
    <ExpandableListView
        android:id="@+id/lvExp"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>
</LinearLayout>

ExpandableListAdapter

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private Button b;
    LinearLayout l;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override
    public View getChildView(final 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);
        }
        final View views = convertView;
        final ViewGroup parents =parent;
        TabHost host = (TabHost)convertView.findViewById(R.id.tabHost);
        host.setup();
        TabHost.TabSpec spec = host.newTabSpec("Tab One");
        spec.setContent(R.id.tab1);
        spec.setIndicator("Tab One");
        host.addTab(spec);
        TabHost.TabSpec spec1 = host.newTabSpec("Tab Two");
        spec1.setContent(R.id.tab2);
        spec1.setIndicator("Tab Two");
        host.addTab(spec1);
        TabHost.TabSpec spec2 = host.newTabSpec("Tab Three");
        spec2.setContent(R.id.tab3);
        spec2.setIndicator("Tab Three");
        host.addTab(spec2);
        host.setFocusable(false);
        host.setup();
        return convertView;
    }

    @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 int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(final 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);
            LinearLayout.LayoutParams firstParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 300);
            //Random rnd = new Random();
            int color = Color.argb(255, 104, 224, 201);

            convertView = infalInflater.inflate(R.layout.list_group, null);
            convertView.setLayoutParams(firstParam);
            convertView.setBackgroundColor(color);
        }
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        ImageButton img = (ImageButton)convertView.findViewById(R.id.imageButton) ;
        img.setFocusable(false);
        System.out.println("headerTitle 123-------------------> "+headerTitle);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("headerTitle -------------------> "+(String) getGroup(groupPosition));
                Intent i = new Intent(_context,DashboardActivity.class);
                i.putExtra("feroz",(String) getGroup(groupPosition));
                _context.startActivity(i);
            }
        });
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private Button addTextView(){
        Button Button = new Button(_context);
      Button.setText("submit");
        return Button;
    }
}

父视图的list_group.xml

    <?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="100dp"
    android:orientation="horizontal"
    android:padding="8dp"
    android:background="#000000"
android:weightSum="100"
    >        <TextView
            android:id="@+id/lblListHeader"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:textSize="17dp"
            android:text="hello "
            android:textColor="#fff"
            android:layout_weight="60"
            />
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/imageView"
            android:src="@drawable/line"
            android:layout_weight="10"/>
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/imageButton"
            android:layout_weight="30"
            android:src="@drawable/launch"
            android:background="#00000000"
            android:clickable="true" />
</LinearLayout>

子视图的list_item

<?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="100dip"
    android:orientation="horizontal"
    android:weightSum="100">
<TabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></TabWidget>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#ffc916"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="This is tab 1" />
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#da8200"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="This is tab 2" />
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#5b89ff"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="This is tab 3" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

经过少量的研究后,我知道问题出在转换视图上,一旦创建了一个孩子,你就会重复使用它。但不适用于TabHost。您每次都创建TabHost,即使已经创建了视图,也为同一个子进行了tabhost设置。我刚刚编辑了适配器类,并且研究了一个方法prepareListData()。这里是编辑过的代码: -

MyExpandableListAdapter.java

public class MyExpandableListAdapter extends BaseExpandableListAdapter     {
private Context _context;
private Button b;
LinearLayout l;
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 Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}
@Override
public View getChildView(final 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_child, null);

        TabHost host = (TabHost) convertView.findViewById(R.id.tabHost);
        host.setup();
        Log.e("tab count",""+host.getTabWidget().getTabCount());
        TabHost.TabSpec spec = host.newTabSpec("Tab One");
        spec.setContent(R.id.tab1);
        spec.setIndicator("Tab One");
        host.addTab(spec);
        TabHost.TabSpec spec1 = host.newTabSpec("Tab Two");
        spec1.setContent(R.id.tab2);
        spec1.setIndicator("Tab Two");
        host.addTab(spec1);
        TabHost.TabSpec spec2 = host.newTabSpec("Tab Three");
        spec2.setContent(R.id.tab3);
        spec2.setIndicator("Tab Three");
        host.addTab(spec2);
        host.setFocusable(false);
        host.setup();
    }

    return convertView;
}

@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 int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(final 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);
        LinearLayout.LayoutParams firstParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 300);
        //Random rnd = new Random();
        int color = Color.argb(255, 104, 224, 201);

        convertView = infalInflater.inflate(R.layout.list_group, null);
        convertView.setLayoutParams(firstParam);
        convertView.setBackgroundColor(color);
    }
    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    ImageButton img = (ImageButton)convertView.findViewById(R.id.imageButton) ;
    img.setFocusable(false);
    System.out.println("headerTitle 123-------------------> "+headerTitle);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("headerTitle -------------------> "+(String) getGroup(groupPosition));
            Intent i = new Intent(_context,DashboardActivity.class);
            i.putExtra("feroz",(String) getGroup(groupPosition));
            _context.startActivity(i);
        }
    });
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);
    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

private Button addTextView(){
    Button Button = new Button(_context);
    Button.setText("submit");
    return Button;
}
}

prepareListData()方法

private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();
    listDataHeader.add("Top 250");
    listDataHeader.add("Now Showing");
    listDataHeader.add("Coming Soon..");
    listDataHeader.add("Item 4..");
    listDataHeader.add("Item 5..");
    List<String> top250 = new ArrayList<String>();
    top250.add("The Shawshank Redemption");
    List<String> nowShowing = new ArrayList<String>();
    nowShowing.add("The Conjuring");
    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("2 Guns");
    listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
    listDataChild.put(listDataHeader.get(1), nowShowing);
    listDataChild.put(listDataHeader.get(2), comingSoon);
    listDataChild.put(listDataHeader.get(3), new ArrayList<String>());
    listDataChild.put(listDataHeader.get(4), comingSoon);
}