SetScrollY在片段中不起作用

时间:2018-06-24 07:33:27

标签: android android-fragments scroll scrollview android-fragmentactivity

我正在做一个日历应用程序,我希望在显示当前时间时将片段中的滚动视图滚动到特定的Y位置,我在ACTIVITY中尝试了此代码,成功了,但是在FRAGMENT中不起作用。有人可以帮我吗?谢谢。

这是断层的xml:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="utar.fyp2.DayFragment">

<!-- TODO: Update blank fragment layout -->
<ScrollView
    android:id="@+id/daySV"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/RLforall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/datePickerLL"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal">

        </RelativeLayout>

        <LinearLayout
            android:id="@+id/timeLL"
            android:layout_width="90dp"
            android:layout_height="match_parent"
            android:layout_marginTop="60dp"
            android:orientation="vertical">

        </LinearLayout>

        <RelativeLayout
            android:id="@+id/contentRL"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="90dp"
            android:layout_marginTop="60dp"/>

        <Button
            android:id="@+id/btntest"
            android:text="click"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>
</ScrollView>

这是代码:

public class DayFragment extends Fragment {

    //declare element
    private LinearLayout timeLL;
    private RelativeLayout contentRL;
    private RelativeLayout RLforall;
    private ScrollView daySV;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";

    // TODO: Rename and change types of parameters
    private Integer mParam1;

    private OnFragmentInteractionListener mListener;

    public DayFragment() {
        // Required empty public constructor
    }


    // TODO: Rename and change types and number of parameters
    public static DayFragment newInstance(int param1) {
        DayFragment fragment = new DayFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getInt(ARG_PARAM1);
        }
    }

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

        View v = inflater.inflate(R.layout.fragment_day,container,false);

        timeLL = v.findViewById(R.id.timeLL);
        contentRL = v.findViewById(R.id.contentRL);

        GradientDrawable gradientDrawableForTime = new GradientDrawable();
        gradientDrawableForTime.setStroke(1, Color.BLACK);
        gradientDrawableForTime.setColor(Color.rgb(190,238,53));

        GradientDrawable gradientDrawableForContent = new GradientDrawable();
        gradientDrawableForContent.setStroke(1,Color.BLACK);


        LinearLayout.LayoutParams matchParentLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        for (int i=0;i<24;i++)
        {
            //for time at left side
            TextView tv = new TextView(getActivity());
            tv.setText(""+i+":00");
            tv.setHeight(120);
            tv.setBackground(gradientDrawableForTime);
            //tv.setBackgroundColor(Color.argb(55,55,5));
            tv.setGravity(Gravity.CENTER);
            timeLL.addView(tv);

            //for line at right side
            TextView tv2 = new TextView(getActivity());
            tv2.setBackground(gradientDrawableForContent);
            tv2.setLayoutParams(matchParentLP);
            tv2.setY(i*120);
            //tv2.setZ(1);
            tv2.setHeight((int)(tv2.getY()+120));
            tv2.setOnClickListener(new View.OnClickListener() {

                //String eventid;
                @Override
                public void onClick(View view) {
                    //Toast.makeText(DayListActivity.this, key ,Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(getActivity(), AddEventActivity.class);
                    startActivity(intent);

                }
            });
            contentRL.addView(tv2);
        }

        RLforall = (RelativeLayout)v.findViewById(R.id.RLforall);
        TextView line = new TextView(getActivity());
        line.setHeight(5);
        GradientDrawable gradientDrawableForLine = new GradientDrawable();
        gradientDrawableForLine.setStroke(3,Color.BLACK);
        line.setBackground(gradientDrawableForLine);
        Calendar currentCalendar = Calendar.getInstance();
        int h = currentCalendar.get(Calendar.HOUR_OF_DAY);
        Double m = currentCalendar.get(Calendar.MINUTE)/60.0;
        line.setY((int)(120 + ((h+m)*120)));
        line.setLayoutParams(matchParentLP);
        RLforall.addView(line);

        **//this is where the setScrollY doesn't work**
        daySV = v.findViewById(R.id.daySV);
        daySV.setScrollY((int)(120 + ((h+m)*120)));


      Toast.makeText(getActivity(),"donescoll",Toast.LENGTH_SHORT).show();
        //daySV.scrollTo(0,(int)(120 + ((h+m)*120)));

        //ignore this line below 1st
        Button button = v.findViewById(R.id.btntest);
        //button.setText(mParam1.toString());
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onButtonPressed(25);
            }
        });

        return v;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(int data) {
        if (mListener != null) {
            mListener.onFragmentInteraction(data);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
           mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
               + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(int  data);
    }
}

0 个答案:

没有答案