ViewPager不响应在Fragment中动态创建的布局区域中的触摸 [英] ViewPager not responding to touch in layout area created dynamically in Fragment

查看:59
本文介绍了ViewPager不响应在Fragment中动态创建的布局区域中的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用日历实现Viewpager.我正在使用日历Fragment的相同三个实例来测试ViewPager,让我们调用将一个片段传输到下一个SWIPING的平滑动作.滑动不会在片段本身中动态创建布局的片段区域中起作用,但是页面的其余部分(日期按钮的上方和下方)滑动效果很好!这是整个片段代码不适合的一些代码,因此必须编辑一些声明和按钮侦听器方法

I'm trying to implement Viewpager with a calendar. I'm using the same three instances of the calendar Fragment for testing the ViewPager lets call the smooth motion of transferring one fragment to the next SWIPING. the swiping will not work in the area of the fragment that the layout was created dynamically in the fragment itself but the rest of page(above and below the date buttons)the swiping works great! here is some of the code the entire frag code would not fit so had to edit some declarations and button listener methods

//主要活动

public class calenderview extends AppCompatActivity {
ArrayList<Fragment> fraglist;
 ViewPager pager;
calenderviewpageradaptor adaptor;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calenderview);
    fraglist = new ArrayList<>();
    fraglist.add(new Calview_frag()); fraglist.add(new Calview_frag()); fraglist.add(new Calview_frag());
     adaptor = new calenderviewpageradaptor(getSupportFragmentManager(),fraglist);
     pager = (ViewPager)findViewById(R.id.pager);
    pager.setAdapter(adaptor);
}

public class calenderviewpageradaptor extends FragmentStatePagerAdapter {

    ArrayList<Fragment>  adaptorlist;
    public calenderviewpageradaptor(FragmentManager fm,ArrayList<Fragment> list){
        super (fm);
        this.adaptorlist = list;
    }

    @Override
    public int getCount(){
        return 3;
    }
    @Override
    public Fragment getItem(int pos){
                return adaptorlist.get(pos);

    }
      }

}

公共类Calview_frag扩展了片段{

public class Calview_frag extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    private LinearLayout.LayoutParams getdaysLayoutParams() {
        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(

                ViewGroup.LayoutParams.WRAP_CONTENT,

                ViewGroup.LayoutParams.WRAP_CONTENT);

        buttonParams.weight = 1;

        return buttonParams;

    }
    private void addDaysinCalendar(ViewGroup.LayoutParams buttonParams, DisplayMetrics metrics) {

        int engDaysArrayCounter = 0;

        LinearLayout.LayoutParams doitwithmarg = new LinearLayout.LayoutParams(120,120);
        doitwithmarg.setMarginStart(cacmarg);
        LinearLayout.LayoutParams doittoit = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT,

ViewGroup.LayoutParams.WRAP_CONTENT); doittoit.weight = 1;

ViewGroup.LayoutParams.WRAP_CONTENT); doittoit.weight=1;

        for (int weekNumber = 0; weekNumber < 6; ++weekNumber) {

            for (int dayInWeek = 0; dayInWeek < 7; ++dayInWeek) {

                Button day = new  Button(getContext());

                day.setBackground(getResources().getDrawable(R.drawable.roundshapeclear));
                day.setTextColor(getResources().getColor(R.color.GREY));
                day.setBackgroundColor(Color.TRANSPARENT);
                day.setTextSize((int) metrics.density *5 );
                day.setSingleLine();

                if(dayInWeek!=0){
                    day.setLayoutParams(doitwithmarg);
                    Space s = new Space(getContext());
                    s.setLayoutParams(doittoit);
                    weeks[weekNumber].addView(s);
                    days[engDaysArrayCounter] = day;
                    weeks[weekNumber].addView(day);}

                  else{
                    day.setLayoutParams(doitwithmarg);
                    days[engDaysArrayCounter] = day;
                    weeks[weekNumber].addView(day);}

                ++engDaysArrayCounter;

            }

        }

    }
    private void init(View view) {

        DisplayMetrics metrics = getResources().getDisplayMetrics();

        calendar = Calendar.getInstance();



        weekOneLayout = (LinearLayout)view.findViewById(R.id.calendar_week_1);

        weekTwoLayout = (LinearLayout)view.findViewById(R.id.calendar_week_2);

        weekThreeLayout = (LinearLayout)view.findViewById(R.id.calendar_week_3);

        weekFourLayout = (LinearLayout)view.findViewById(R.id.calendar_week_4);

        weekFiveLayout = (LinearLayout)view.findViewById(R.id.calendar_week_5);

        weekSixLayout = (LinearLayout)view.findViewById(R.id.calendar_week_6);

        currentYear = (TextView)view.findViewById(R.id.current_date);

        currentMonth = (TextView)view.findViewById(R.id.current_month);

        currentDateDay = chosenDateDay = calendar.get(Calendar.DAY_OF_MONTH);



        if (userMonth != 0 && userYear != 0) {

            currentDateMonth = chosenDateMonth = userMonth;

            currentDateYear = chosenDateYear = userYear;

        } else {

            currentDateMonth = chosenDateMonth = calendar.get(Calendar.MONTH);

            currentDateYear = chosenDateYear = calendar.get(Calendar.YEAR);

        }



        currentYear.setText("" + chosenDateYear);

        currentMonth.setText(ENG_MONTH_NAMES[currentDateMonth]);


        initializeDaysWeeks();

        if (userButtonParams != null) {

            defaultButtonParams = userButtonParams;

        } else {

            defaultButtonParams = getdaysLayoutParams();

        }

        addDaysinCalendar(defaultButtonParams,metrics);



        initCalendarWithDate(chosenDateYear, chosenDateMonth, chosenDateDay,view);



    }



    private void initializeDaysWeeks() {

        weeks = new LinearLayout[6];

        days = new Button[6 * 7];

        weeks[0] = weekOneLayout;

        weeks[1] = weekTwoLayout;

        weeks[2] = weekThreeLayout;

        weeks[3] = weekFourLayout;

        weeks[4] = weekFiveLayout;

        weeks[5] = weekSixLayout;

    }
    private void initCalendarWithDate(int year, int month, int day, View v) {

        if (calendar == null)

            calendar = Calendar.getInstance();

        calendar.set(year, month, day);

        int daysInCurrentMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);


        chosenDateYear = year;

        chosenDateMonth = month;

        chosenDateDay = day;

        calendar.set(year, month, 1);

        int firstDayOfCurrentMonth = calendar.get(Calendar.DAY_OF_WEEK);

        Log.d(tag,"FFFFFFiiiiiiiiiiirrrrrrrrrrsssssssssst day of month int"+firstDayOfCurrentMonth);

        calendar.set(year, month, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Log.d(tag,"LLLLLLLLLLLLlaaaaasttttttttttt day of month int"+daysInCurrentMonth);

        int dayNumber = 1;

        int daysLeftInFirstWeek = 0;
        firstDayOfCurrentMonth= firstDayOfCurrentMonth-1;
        int indexOfDayAfterLastDayOfMonth = 0;

        if (firstDayOfCurrentMonth != 0) {

            daysLeftInFirstWeek = firstDayOfCurrentMonth;

            indexOfDayAfterLastDayOfMonth = daysLeftInFirstWeek + daysInCurrentMonth;

       >                 for (int i = firstDayOfCurrentMonth; i < firstDayOfCurrentMonth + daysInCurrentMonth; ++i) {
                //checks all dates of calendar to set background and text colors
                if (currentDateMonth == chosenDateMonth

                        && currentDateYear == chosenDateYear

                        && dayNumber == currentDateDay){
                    days[i].setBackground(getResources().getDrawable(R.drawable.roundshapegrey));
                    days[i].setTextColor(Color.WHITE);

                } else {
                    days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));

                    days[i].setTextColor(Color.BLACK);


                }



                int[] dateArr = new int[3];

                dateArr[0] = dayNumber;

                dateArr[1] = chosenDateMonth;

                dateArr[2] = chosenDateYear;

                days[i].setTag(dateArr);

                days[i].setText(String.valueOf(dayNumber));



                days[i].setOnClickListener(new View.OnClickListener() {

                    @Override

                    public void onClick(View v) {
                        onDayClick(v);

                    }

                });

                ++dayNumber;

            }
      > 
            for (int i = 7; i < 7 + daysInCurrentMonth; ++i) {

                if (currentDateMonth == chosenDateMonth

                        && currentDateYear == chosenDateYear

                        && dayNumber == currentDateDay) {
                    days[i].setBackground(getResources().getDrawable(R.drawable.roundshape));


                    days[i].setTextColor(Color.WHITE);

                } else {
                    days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));

                    days[i].setTextColor(Color.BLACK);


                }



                int[] dateArr = new int[3];

                dateArr[0] = dayNumber;

                dateArr[1] = chosenDateMonth;

                dateArr[2] = chosenDateYear;

                days[i].setTag(dateArr);

                days[i].setText(String.valueOf(dayNumber));



                days[i].setOnClickListener(new View.OnClickListener() {

                    @Override

                    public void onClick(View v) {
                        onDayClick(v);

                    }

                });

                ++dayNumber;

            }

        }

//显示上个月和下个月的天

//Displays previous and next month days

        if (month > 0)

            calendar.set(year, month - 1, 1);

        else

            calendar.set(year - 1, 11, 1);

        int daysInPreviousMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);



        for (int i = daysLeftInFirstWeek - 1; i >= 0; --i) {

            int[] dateArr = new int[3];



            if (chosenDateMonth > 0) {

                if (currentDateMonth == chosenDateMonth - 1

                        && currentDateYear == chosenDateYear

                        && daysInPreviousMonth == currentDateDay) {

                } else {
                    days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));

                }



                dateArr[0] = daysInPreviousMonth;

                dateArr[1] = chosenDateMonth - 1;

                dateArr[2] = chosenDateYear;

            } else {

                if (currentDateMonth == 11

                        && currentDateYear == chosenDateYear - 1

                        && daysInPreviousMonth == currentDateDay) {

                } else {
                    days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));


                }



                dateArr[0] = daysInPreviousMonth;

                dateArr[1] = 11;

                dateArr[2] = chosenDateYear - 1;

            }

            days[i].setTag(dateArr);
            days[i].setTextColor(getResources().getColor(R.color.GREY));;

            days[i].setText("");

            days[i].setOnClickListener(null);

        }

        int nextMonthDaysCounter = 1;

        for (int i = indexOfDayAfterLastDayOfMonth; i < days.length; ++i) {

            int[] dateArr = new int[3];


            if (chosenDateMonth < 11) {

                if (currentDateMonth == chosenDateMonth + 1

                        && currentDateYear == chosenDateYear

                        && nextMonthDaysCounter == currentDateDay) {

                    //       days[i].setBackground(getResources().getDrawable(R.drawable.roundshapegrey));
                    days[i].setTextColor(Color.WHITE);

                } else {

                    days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));

                }



                dateArr[0] = nextMonthDaysCounter;

                dateArr[1] = chosenDateMonth + 1;

                dateArr[2] = chosenDateYear;

            } else {

                if (currentDateMonth == 0

                        && currentDateYear == chosenDateYear + 1

                        && nextMonthDaysCounter == currentDateDay) {
                    //        selectedDayButton.setBackground(getResources().getDrawable(R.drawable.roundshapegrey));
                    selectedDayButton.setTextColor(Color.WHITE);
                    //        days[i].setBackground(getResources().getDrawable(R.drawable.roundshapegrey));
                    days[i].setTextColor(Color.WHITE);
                } else {

                    days[i].setBackground(getResources().getDrawable(R.drawable.roundshapeclear));

                }



                dateArr[0] = nextMonthDaysCounter;

                dateArr[1] = 0;

                dateArr[2] = chosenDateYear + 1;

            }

            days[i].setTag(dateArr);

            days[i].setText("");
            days[i].setOnClickListener(null);
        }

        calendar.set(chosenDateYear, chosenDateMonth, chosenDateDay);
        currentYear.setText(" " + chosenDateYear);
        currentMonth.setText(ENG_MONTH_NAMES[chosenDateMonth]);


    }

public Calview_frag() {//required empty constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View layoutview = inflater.inflate(R.layout.fragment_calview,container,false);
     but = (ImageButton)layoutview.findViewById(R.id.getitbutton);
     fbut = (ImageButton)layoutview.findViewById(R.id.getit2button);
    display = getActivity().getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    screenheight = size.y;
    screenwidth = size.x;
    Log.d(tag,"sssssssssssccreen size  "+screenwidth);
    if(screenwidth<1300)
        cacmarg = screenwidth/50;
    else
        cacmarg = screenwidth/25;
    init(layoutview);
    return layoutview;
}

}

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:orientation="vertical">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <ImageButton
                    android:id="@+id/getitbutton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_alignParentLeft="true"
                    android:src="@drawable/backbutton"
                    tools:layout_editor_absoluteY="0dp" />
                <LinearLayout

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_centerHorizontal="true"

                    android:gravity="center"

                    android:orientation="horizontal">

                    <TextView

                        android:id="@+id/current_month"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:textColor="@color/bluegrey"

                        android:textSize="28sp" />


                    <TextView

                        android:id="@+id/current_date"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:paddingLeft="4dp"

                        android:textColor="@color/bluegrey"

                        android:textSize="28sp" />

                </LinearLayout>

                <ImageButton
                    android:id="@+id/getit2button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@drawable/forwardbutton"
                    />
      </RelativeLayout>

            <LinearLayout

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:orientation="horizontal"

                android:weightSum="7">


                <Button

                    android:id="@+id/button5"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_weight="1"

                    android:background="@android:color/transparent"

                    android:text="@string/sunday"

                    android:textColor="@color/bluegrey"

                    android:textSize="16sp" />


                <Button

                    android:id="@+id/button4"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_weight="1"

                    android:background="@android:color/transparent"

                    android:text="@string/monday"

                    android:textColor="@color/bluegrey"

                    android:textSize="16sp" />


                <Button

                    android:id="@+id/button10"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_weight="1"

                    android:background="@android:color/transparent"

                    android:text="@string/tuesday"

                    android:textColor="@color/bluegrey"

                    android:textSize="16sp" />


                <Button

                    android:id="@+id/button9"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_weight="1"

                    android:background="@android:color/transparent"

                    android:text="@string/wednesday"

                    android:textColor="@color/bluegrey"

                    android:textSize="16sp" />


                <Button

                    android:id="@+id/button8"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_weight="1"

                    android:background="@android:color/transparent"

                    android:text="@string/thursday"

                    android:textColor="@color/bluegrey"

                    android:textSize="16sp" />


                <Button

                    android:id="@+id/button7"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_weight="1"

                    android:background="@android:color/transparent"

                    android:text="@string/friday"

                    android:textColor="@color/bluegrey"

                    android:textSize="16sp" />


                <Button

                    android:id="@+id/button6"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_weight="1"

                    android:background="@android:color/transparent"

                    android:text="@string/saturday"

                    android:textColor="@color/bluegrey"

                    android:textSize="16sp" />


            </LinearLayout>


            <LinearLayout

                android:id="@+id/calendar_week_1"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:orientation="horizontal"

                android:weightSum="7" />


            <LinearLayout

                android:id="@+id/calendar_week_2"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:orientation="horizontal"

                android:weightSum="7" />


            <LinearLayout

                android:id="@+id/calendar_week_3"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:orientation="horizontal"

                android:weightSum="7" />


            <LinearLayout

                android:id="@+id/calendar_week_4"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:orientation="horizontal"

                android:weightSum="7" />


            <LinearLayout

                android:id="@+id/calendar_week_5"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:orientation="horizontal"

                android:weightSum="7" />


            <LinearLayout

                android:id="@+id/calendar_week_6"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:orientation="horizontal"

                android:weightSum="7" /> </LinearLayout>

</ScrollView>

</LinearLayout>

推荐答案

您要将Fragment的实例传递给适配器吗?不,我想.您应该将片段列表与片段管理器一起传递给适配器,还应该相应地更改calendarViewPager.

are you passing the instance of the Fragment to the adapter? No I think. You should pass the list of fragments to the adapter along with fragment manager and should also change the calendarViewPager accordingly.

  List<Fragment> fragments =new ArrayList<>();
  fragments.add(Calview_frag());
  fragments.add(Calview_frag());
  fragments.add(Calview_frag());
      calenderviewpageradaptor adaptor =
                new calenderviewpageradaptor(getSupportFragmentManager(),fragments);

这篇关于ViewPager不响应在Fragment中动态创建的布局区域中的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆