在从活动转移到片段的同时实现抽象方法

时间:2018-04-28 06:22:07

标签: android android-fragments onclicklistener android-fragmentactivity

我正在尝试在片段中添加自定义图像滑块插件。这个滑块插件在Activity中运行良好,但是当从活动移动到片段时,它显示:class必须被声明为abstract或实现抽象方法。我刚开始学习片段。在搜索解决方案时,我得到了实现抽象类的建议,但我很困惑如何添加?

public class Home_Screen extends Fragment implements BaseSliderView.OnSliderClickListener {

private SliderLayout mDemoSlider;
private static final String getURL = "";
HashMap<String, String> url_maps;

GridView gridview;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    mDemoSlider = getActivity().findViewById(R.id.slider);

    // Creating volley request obj
    JsonArrayRequest billionaireReq = new JsonArrayRequest(getURL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    url_maps = new HashMap<String, String>();
                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            url_maps.put(obj.getString("title") + " - " + obj.getString("releaseYear"), obj.getString("image"));

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    for (String name : url_maps.keySet()) {
                        TextSliderView textSliderView = new TextSliderView(getActivity());
                        // initialize a SliderLayout
                        textSliderView
                                .description(name)
                                .descriptionSize(20)
                                .image(url_maps.get(name))
                                .setScaleType(BaseSliderView.ScaleType.CenterCrop)
                                .setOnSliderClickListener(Home_Screen.this);

                        //add your extra information
                        textSliderView.bundle(new Bundle());
                        textSliderView.getBundle().putString("extra", name);

                        mDemoSlider.addSlider(textSliderView);
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity().getApplicationContext(), "network issue: please enable wifi/mobile data", Toast.LENGTH_SHORT).show();
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(billionaireReq);

    mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Stack);
    mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Top);
    mDemoSlider.setCustomAnimation(new DescriptionAnimation());
    mDemoSlider.setDuration(4000);

    mDemoSlider.setPresetTransformer("Stack");

    // Inflate the layout for this photo
    View view = inflater.inflate(R.layout.home_screen, container, false);
    gridview = (GridView) view.findViewById(R.id.customgrid);
    gridview.setAdapter(new CustomAdapter(getActivity(), osNameList, osImages));
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            final Intent intent;
            switch(position)
            {
                case 0:
                    intent =  new Intent(v.getContext(), IntroActivity.class);
                    break;

                case 1:
                    intent =  new Intent(v.getContext(), WadaActivity.class);
                    break;

                case 2:
                    intent =  new Intent(v.getContext(), InfoActivity.class);
                    break;
            }
            startActivity(intent);


        }

    });
    return view;

}

}

生成以下错误

Information:Gradle tasks [:app:assembleDebug]
C:\AndroidApp\app\src\main\java\com\info\androidapp\Home_Screen.java
Error:(34, 8) error: Home_Screen is not abstract and does not override abstract method onSliderClick(BaseSliderView) in OnSliderClickListener
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED in 5s

我的布局文件如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:custom="http://schemas.android.com/tools"
    android:orientation="vertical">

    <in.myinnos.imagesliderwithswipeslibrary.SliderLayout
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        custom:auto_cycle="true"
        custom:indicator_visibility="visible"
        custom:pager_animation="Stack"
        custom:pager_animation_span="1100" />

    <GridView
        android:id="@+id/customgrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/slider"
        android:numColumns="3"
        android:background="@color/ColorPrimary"
        android:textColor="@color/white"
        android:paddingTop="20dp"
        android:paddingLeft="30dp"
        android:verticalSpacing="20dp" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您的班级必须实施此BaseSliderView.OnSliderClickListener此界面。

public class Home_Screen extends Fragment implements **BaseSliderView.OnSliderClickListener** {

所以有两种方法可以解决这个问题:

  1. 删除implements BaseSliderView.OnSliderClickListener并构造实现此接口的实例。在Home_Screen.this使用实例代替setOnSliderClickListener

  2. 在activity或baseActivity中找到OnSliderClickListener方法。并将其复制为片段。

答案 1 :(得分:0)

检查一下:

public class Home_Screen extends Fragment implements BaseSliderView.OnSliderClickListener {

private SliderLayout mDemoSlider;
private static final String getURL = "";
HashMap<String, String> url_maps;

GridView gridview;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    // Inflate the layout for this photo
    View view = inflater.inflate(R.layout.home_screen, container, false);
    gridview = (GridView) view.findViewById(R.id.customgrid);
    gridview.setAdapter(new CustomAdapter(getActivity(), osNameList, osImages));
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            final Intent intent;
            switch(position)
            {
                case 0:
                    intent =  new Intent(v.getContext(), IntroActivity.class);
                    break;

                case 1:
                    intent =  new Intent(v.getContext(), WadaActivity.class);
                    break;

                case 2:
                    intent =  new Intent(v.getContext(), InfoActivity.class);
                    break;
            }
            startActivity(intent);


        }

    });

mDemoSlider = view.findViewById(R.id.slider);

    // Creating volley request obj
    JsonArrayRequest billionaireReq = new JsonArrayRequest(getURL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    url_maps = new HashMap<String, String>();
                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            url_maps.put(obj.getString("title") + " - " + obj.getString("releaseYear"), obj.getString("image"));

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    for (String name : url_maps.keySet()) {
                        TextSliderView textSliderView = new TextSliderView(getActivity());
                        // initialize a SliderLayout
                        textSliderView
                                .description(name)
                                .descriptionSize(20)
                                .image(url_maps.get(name))
                                .setScaleType(BaseSliderView.ScaleType.CenterCrop)
                                .setOnSliderClickListener(Home_Screen.this);

                        //add your extra information
                        textSliderView.bundle(new Bundle());
                        textSliderView.getBundle().putString("extra", name);

                        mDemoSlider.addSlider(textSliderView);
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity().getApplicationContext(), "network issue: please enable wifi/mobile data", Toast.LENGTH_SHORT).show();
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(billionaireReq);

    mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Stack);
    mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Top);
    mDemoSlider.setCustomAnimation(new DescriptionAnimation());
    mDemoSlider.setDuration(4000);

    mDemoSlider.setPresetTransformer("Stack");


    return view;

}

@override
 public void onSliderClick(BaseSliderView slider){
//do some thing
}
}