【发布时间】:2014-01-14 19:23:51
【问题描述】:
我刚开始使用 android,并试图在其片段中创建一个带有 listview 的选项卡式布局。什么是最好的选择。? 我已经尝试过以下哪些有效: 1.为listview制作自定义适配器 2. 带有虚拟片段的两个选项卡操作栏布局 当我尝试使用 listview 活动作为片段时,应用程序崩溃。我已经粘贴了下面的代码。 应用程序概念:票务预订应用程序!。为此,我创建了一个包含电影名称、类别和评论详细信息的电影类。 MovieManager 制作电影列表。应用程序在打开片段活动时崩溃,该片段活动将电影列表保存在不同类别 [标签] 中。
在打开时使应用崩溃的 Activity:
//包含不同类别的电影列表的活动 公共类 AllMovies 扩展 FragmentActivity {
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
ListViewHollyLV;
ListViewBollyLV;
ListViewKollyLV;
private void ListCreater()
{
HollyLV = (ListView)findViewById(R.id.bollyListView1);
newMovieListActivity().CurrentLV = (ListView)findViewById(R.id.bollyListView1);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_movies);
ListCreater();
// Set up the action bar to show tabs.
finalActionBaractionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// for each of the sections in the app, add a tab to the action bar.
Tab tab = actionBar.newTab().setText(R.string.hello_world).setTabListener(new TabListener( this, "holly", MovieListActivity.class));
actionBar.addTab(tab);
tab = actionBar.newTab().setText(R.string.hello_world).setTabListener(new TabListener( this, "bolly", Fragi2.class));
actionBar.addTab(tab);
}
static class TabListener implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class mClass;
/** Constructor used each time a new tab is created.
* * @paramactivity The host Activity, used to instantiate the fragment
* * @paramtag The identifier tag for the fragment
* * @paramclz The fragment's Class, used to instantiate the fragment
* */
publicTabListener(Activity activity, String tag, Class clz)
{ mActivity = activity;
mTag = tag;
mClass = clz; }
/* The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(Tab tab, FragmentTransactionft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(R.id.fragmentCnt, mFragment, mTag);
}
else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransactionft)
{
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransactionft) {
// User selected the already selected tab. Usually do nothing.
}
}
//This fragment is used for “holly” tab
public static class MovieListActivity extends Fragment {
ListViewCurrentLV;
@Override
public View onCreateView(LayoutInflaterinflater, ViewGroup container,
Bundle savedInstanceState){
// Inflate the menu; this adds items to the action bar if it is present.
View V = inflater.inflate(R.layout.activity_frag1, container, false);
ManageMovieList.MakeList(); // Makes separate list of Hollywood and bollywood //movies
LoadMovieList();
return V;
}
@Override
public void onPause() {
super.onPause();
}
//Loads the Hollywood movie list to the listview using custom adapter
private void LoadMovieList()
{
ManageMovieList.HollywoodMovieAdapter(getActivity(), CurrentLV);
}
}
/// Dummy Fragment class (was created while trying tabbed layout) which contains only ///textview. Still this fragment is used for “bolly” tab
public static class Fragi1 extends Fragment {
@Override
public View onCreateView(LayoutInflaterinflater, ViewGroup container,
Bundle savedInstanceState){
// Inflate the menu; this adds items to the action bar if it is present.
View V = inflater.inflate(R.layout.activity_frag1, container, false);
return V;
}
}
}
自定义适配器
公共类 CustomMovieArrayAdapter 扩展 ArrayAdapter{
private final Context context;
ArrayList<Movie> _Movies ;
static final String REVIEW = "com.example.ticketonline";
publicCustomMovieArrayAdapter(Context context, ArrayList<Movie>movieItems) {
super(context, R.layout.movie_list_item, movieItems);
this.context = context;
_Movies = movieItems;
Movie currentMovie = new Movie();
ArrayList<Movie> _Movies ;
}
String Title;
String category;
int Id;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflaterinflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.movie_list_item, parent, false);
TextViewtextName = (TextView) convertView.findViewById(R.id.textViewMovieName);
TextViewtextCate = (TextView) convertView.findViewById(R.id.textViewMovieCategory);
ImageViewimageView = (ImageView) convertView.findViewById(R.id.imageViewMoviePoster);
Button btn = (Button)convertView.findViewById(R.id.buttonSelectMovie);
textName.setText(_Movies.get(position).GetMovieName(position));
textCate.setText(_Movies.get(position).GetMovieCategory(position));
imageView.setImageResource(_Movies.get(position).GetMoviePosterID(position));
finalint p = position;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ReviewAndBook.class);
intent.putExtra(REVIEW, Movie.GetMovieReview(p));
intent.putExtra(android.content.Intent.EXTRA_TEXT, Movie.GetMovieDirector(p));
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, Movie.GetMovieCast(p));
context.startActivity(intent);
}
});
}
returnconvertView;
}
}
MovieManager 类中的方法
public static void HollywoodMovieAdapter(Context cnt,ListViewmovieListUI)
{
//ListViewmovieListUI = (ListView)findViewById(R.id.movieListView);
ArrayList<Movie> _hollywoodMovies = ManageMovieList.RecentMovies_Holly;
movieListUI.setAdapter( new CustomMovieArrayAdapter(cnt, _hollywoodMovies));
}
【问题讨论】:
标签: android android-fragments fragment