在使用操作栏创建的Tab片段下设置ExpandaleListViews?

时间:2013-10-22 19:36:21

标签: java android android-fragments

我正在构建一个App(一个目录)!

我已经自定义了本教程中的代码和示例............ “http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

但挑战是:

如何在Tab片段中放置可扩展列表视图: 那就是:

  • 排名靠前的ExpanbableListView
  • 游戏中的ExpanbableListView
  • 电影中的ExpanbableListView。

我尝试了许多方式将代码放入这些课程(评分最高,或游戏或电影)!!

但我的TabsPagerAdapter类总是会收到此错误:

  • 类型不匹配:无法从最高评级转换为片段。
  • 类型不匹配:无法从游戏转换为片段。
  • 类型不匹配:无法从电影转换为片段。

注意:我正在维护教程中的名称......但是我的应用程序中更改了它们。

以下是将扩展列表活动放入游戏片段的代码示例。

     import com.example.uganda.R;
     import android.app.ExpandableListActivity;
     import android.content.Context;
     import android.os.Bundle;
     import android.view.LayoutInflater;
     import android.view.View;
     import android.view.ViewGroup;
     import android.widget.BaseExpandableListAdapter;
     import android.widget.TextView;
     public class Games extends ExpandableListActivity {

   private static final String arrGroupElements[] = { "Football", "Rugby", "Cricket"};

   /**
* strings for child elements
*/

private static final String arrChildElements[][] = {
{ "Premiership teams","Country teams", "2014 worldCup"},

{ "Europe competitions","African competitions"},

{ "International tests","Local tonerments"}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_games);
setListAdapter(new ExpandableListAdapter(this));
}

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context myContext;
public ExpandableListAdapter(Context context) {
myContext = context;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.games_subitem_layout, null);
}
TextView yourSelection = (TextView) convertView
.findViewById(R.id.articleContentTextView);
yourSelection
.setText(arrChildElements[groupPosition][childPosition]);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return arrChildElements[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return arrGroupElements.length;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.games_item_layout, null);
}
TextView groupName = (TextView) convertView
.findViewById(R.id.articleHeaderTextView);
groupName.setText(arrGroupElements[groupPosition]);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

}

(这是用于产生错误的TabsPageAdapter的代码)

   import com.uganda.TopRated;
   import com.uganda.Games;
   import com.uganda.Movies;
   import android.support.v4.app.Fragment;
   import android.support.v4.app.FragmentManager;
   import android.support.v4.app.FragmentPagerAdapter;

公共类TabsPagerAdapter扩展了FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated activity
        return new Private();
    case 1:
        // Games activity
        return new Missionary();
    case 2:
        // Movies activity
        return new Government();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}

1 个答案:

答案 0 :(得分:0)

在所有地方使用import android.support.v4.app.Fragmentimport android.app.Fragment。确保不要将这两者混合在一起。