我想在我的Android應用程序活動中創建可以滑過的片段。更具體地說,這些片段包含圖像,我想滑過這些。方法需要對象並從擴展類中查找對象
現在要完成此操作,我使用FragmentPagerAdapter。我現在遇到的問題是,我在擴展FragmentPagerAdaptor的類中的一個方法需要返回一個Fragment對象,但根據編譯器它會找到一個不同的對象。 這很奇怪,因爲我返回的對象是來自擴展片段的類,因此它應該在我看來有效。
該問題的方法是在SlidesPageAdaptor
類getItem(int position)
和錯誤是
Incompatible types
Required: android.support.v4.app.Fragment
Found: ourpackagename.fragment_slides
SlidesPageAdaptor.java
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class SlidesPageAdaptor extends FragmentPagerAdapter {
String[] devices;
String[] deviceDescription;
public SlidesPageAdaptor(FragmentManager fm, Context context) {
super(fm);
Resources resources = context.getResources();
devices = resources.getStringArray(R.array.devices);
deviceDescription = resources.getStringArray(R.array.device_description);
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString(fragment_slides.DescriptionKey, deviceDescription[position]);
bundle.putInt(fragment_slides.ImageIDKey, getImageID(position));
fragment_slides fragmentSlide = new fragment_slides();
fragmentSlide.setArguments(bundle);
return fragmentSlide;
}
private int getImageID(int position) {
int id = 0;
switch (position)
{
case 0:
id = R.drawable.abc_btn_check_material;
break;
case 1:
id = R.drawable.abc_ab_share_pack_mtrl_alpha;
break;
case 2:
id = R.drawable.abc_btn_radio_material;
break;
}
return id;
}
@Override
public CharSequence getPageTitle(int position) {
return devices[position];
}
@Override
public int getCount() {
return devices.length;
}
}
而且fragment_slides.class
(我知道這個名字不好,但現在不重要):
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class fragment_slides extends Fragment {
public static final String ImageIDKey = "imagekey";
public static final String DescriptionKey = "descriptionkey";
public fragment_slides() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_slides_images, container, false);
Bundle bundle = getArguments();
if(bundle != null)
{
int imageID = bundle.getInt(ImageIDKey);
String description = bundle.getString(DescriptionKey);
setValues(view, imageID, description);
}
return view;
//return inflater.inflate(R.layout.fragment_slides_images, container, false);
}
private void setValues(View view, int imageID, String description) {
ImageView imageview = (ImageView) view.findViewById(R.id.imageViewSlide);
imageview.setImageResource(imageID);
TextView textView = (TextView) view.findViewById(R.id.tvSlideTest);
textView.setText(description);
}
}
我不認爲其他活動/類是必需的,但我可以在需要時提供它們。
這是導致問題的唯一問題。你爲什麼包括_also _? – Codebender
我的不好,固定... –
謝謝你!這樣的錯誤,應該看到.. – Someguy