我正在构建一个应用程序(一个目录)!
我已经自定义了本教程中的代码和示例............” http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1 / "
但挑战在于:
如何在选项卡片段中放置可扩展列表视图:即:
- ExpanbableListView 在 Top-rated
- 游戏中的 ExpanbableListView
- 电影中的可扩展列表视图。
我尝试了很多方法将代码放入这些类(顶级、游戏或电影)!
但是我的 TabsPagerAdapter 类总是会出现这个错误:
- 类型不匹配:无法从 Top-rated 转换为 Fragment。
- 类型不匹配:无法从游戏转换为片段。
- 类型不匹配:无法从电影转换为片段。
注意:我将名称保留为教程中的名称……但它们在我的应用程序中已更改。
这是将扩展列表活动放入游戏片段中的代码示例。
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;
}
}