【问题标题】:ViewPager and ListFragment with onLongClick strange behaviour具有 onLongClick 奇怪行为的 ViewPager 和 ListFragment
【发布时间】:2013-12-19 11:51:46
【问题描述】:

在我的应用程序中,我在 SherlockFragmentActivity 中使用视图寻呼机,并附加到 ViewPager 5 ListFragments,每个 ListFragments 代表一天!

在您将看到的每个 ListFragment 中,我在 ListFragment 的根 ListView 上使用 LongClicks 的侦听器。

问题是奇怪的是,当从代表星期一的第一个 Fragment 触发 LongClick 事件时,一切正常,但是当我从代表星期二的下一个 Fragment 触发事件时,我进入了即将触发的 Activity从第一个 Fragment 开始,这意味着它是从 MondayListFragment 中触发的!

我似乎找不到我做错了什么。任何帮助,将不胜感激!提前谢谢!

第一个列表片段

周一公共类扩展 SherlockListFragment 实现 AdapterView.OnItemLongClickListener {

public String department;
private String gradeString;
public int grade;
private int mSelectedPosition;
public boolean personal;
private ArrayList<String> lessonTitlesList;
private ArrayList<Lessons> lessonsList;
private DatabaseUtils shareOptions;
private ArrayList<String> titlesSet;
private Cursor c;
private SimpleCursorAdapter smc;
private String[] from = {"Lesson_title", "Time", "Room"};
private int[] to = {R.id.lessons,R.id.timeCol,R.id.roomComments};

private final String FRAGMENT_DAY_NAME = "Δευτέρα";

private boolean fromStart = false;

@Override
public void onActivityCreated(Bundle pack) {
    super.onActivityCreated(pack);
    shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
    shareOptions.instantiatePreferences(getActivity().getApplicationContext());
    lessonTitlesList = new ArrayList<String>();
    lessonsList = new ArrayList<Lessons>();
    titlesSet = new ArrayList<String>();

    this.department = getArguments().getString("department");
    this.grade = getArguments().getInt("grade");
    this.personal = getArguments().getBoolean("personal");

    this.gradeString = shareOptions.gradeParser(grade);

    shareOptions.finishDb();

    if (personal) {
        ListPopulatorPersonal();
        shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
        c = shareOptions.getLessonsByDayCursor(FRAGMENT_DAY_NAME, titlesSet);
        fromStart = true;
    } else{
        ListPopulatorWeekly(department, this.gradeString);
        shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
        c = shareOptions.getLessonsByDeptAndSemesterCursor(department, gradeString, FRAGMENT_DAY_NAME);
    }

    lessonsListPopulator();
    if(shareOptions.isLowerThanIcs()){
        smc = new SimpleCursorAdapter(getActivity().getApplicationContext(),R.layout.list_row_lessons,c,from,to);
    }else
        smc = new SimpleCursorAdapter(getActivity().getApplicationContext(),R.layout.list_row_lessons,c,from,to,0);
        smc.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if(columnIndex == cursor.getColumnIndexOrThrow("Room")){
                String comments = shareOptions.commentsAnalyse(cursor.getString(cursor.getColumnIndexOrThrow("Room")),
                        cursor.getString(cursor.getColumnIndexOrThrow("Comments")));
                TextView tv = (TextView) view;
                tv.setText(comments);
                return true;
            }
            if(columnIndex == cursor.getColumnIndexOrThrow("Time")){
                String comments = "Ώρα: " + cursor.getString(cursor.getColumnIndexOrThrow("Time"));
                TextView tv = (TextView) view;
                tv.setText(comments);
                return true;
            }

            return false;
        }
    });

    this.getListView().setOnItemLongClickListener(this);

    this.setEmptyText(getString(R.string.noLessonDay));
    setListAdapter(smc);
    this.shareOptions.finishDb();
    this.getListView().setBackgroundColor(Color.WHITE);
    this.getListView().setDivider(new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, DatabaseUtils.GRADIENT_COLORS));
    this.getListView().setDividerHeight(3);

}

private void initialiseAndRefresh(){
    if (personal) {
        ListPopulatorPersonal();
        shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
        c = shareOptions.getLessonsByDayCursor(FRAGMENT_DAY_NAME, titlesSet);
        fromStart = true;
    } else{
        ListPopulatorWeekly(department, this.gradeString);
        shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
        c = shareOptions.getLessonsByDeptAndSemesterCursor(department, gradeString, FRAGMENT_DAY_NAME);
    }

    lessonsListPopulator();
    if(shareOptions.isLowerThanIcs()){
        smc = new SimpleCursorAdapter(getActivity().getApplicationContext(),R.layout.list_row_lessons,c,from,to);
    }else
        smc = new SimpleCursorAdapter(getActivity().getApplicationContext(),R.layout.list_row_lessons,c,from,to,0);
    smc.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if(columnIndex == cursor.getColumnIndexOrThrow("Room")){
                String comments = shareOptions.commentsAnalyse(cursor.getString(cursor.getColumnIndexOrThrow("Room")),
                        cursor.getString(cursor.getColumnIndexOrThrow("Comments")));
                TextView tv = (TextView) view;
                tv.setText(comments);
                return true;
            }
            if(columnIndex == cursor.getColumnIndexOrThrow("Time")){
                String comments = "Ώρα: " + cursor.getString(cursor.getColumnIndexOrThrow("Time"));
                TextView tv = (TextView) view;
                tv.setText(comments);
                return true;
            }

            return false;
        }
    });

    setListAdapter(smc);

}

/**
 * Populates a list if the user chose to see his/her personal programm
 * */
private void ListPopulatorPersonal() {
    shareOptions = new DatabaseUtils("OptionsDBRead",getActivity().getApplicationContext());
    titlesSet = shareOptions.getOptionsRows();
    shareOptions.finishOptionDb();
    shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
    lessonsList = shareOptions.getLessonsByDay(FRAGMENT_DAY_NAME, titlesSet);
    shareOptions.finishDb();
}

/**
 * Populates the lessonsList for the weekly programm to be shown.
 * */
public void ListPopulatorWeekly(String dept, String semester) {
    shareOptions = new DatabaseUtils("LessonsDB",getActivity().getApplicationContext());
    lessonsList = shareOptions.getLessonsByDeptAndSemester(dept, semester, FRAGMENT_DAY_NAME);
    shareOptions.finishDb();
}

/**
 * Gets the lesson Titles from the lessonsList and adds them to the lessonTitlesList for further use.
 * */
private void lessonsListPopulator() {
    for (Lessons a : this.lessonsList) {
        lessonTitlesList.add(a.getLesson_Title());
    }
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Lessons a = lessonsList.get(position);

    ArrayList<String> mArrayList = new ArrayList<String>();

    mArrayList.add(0, a.getLesson_Title());
    mArrayList.add(1, a.getTime());
    mArrayList.add(2, a.getProffessor());
    mArrayList.add(3, a.getRoom());

    if(a.getComments() != null) mArrayList.add(4, a.getComments());

    Intent i = new Intent(getActivity(), ShowLesson.class);
    Bundle pack = new Bundle();

    pack.putString("lesson", lessonsList.get(position).getLesson_Title());
    pack.putStringArrayList("myArray", mArrayList);
    pack.putString("department",department);
    pack.putInt("grade", grade);
    pack.putBoolean("personal", personal);
    pack.putInt("day", 0);

    i.putExtras(pack);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    startActivity(i);
}

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    mSelectedPosition = position;
    registerForContextMenu(this.getListView());
    getListView().showContextMenu();
    return true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    getActivity().getMenuInflater().inflate(R.menu.list_fragment_options, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.edit){
        Intent tempIntent = new Intent(getActivity(),EditLessonActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        tempIntent.putExtra("title",lessonsList.get(mSelectedPosition).getLesson_Title());
        tempIntent.putExtra("time", lessonsList.get(mSelectedPosition).getTime());
        tempIntent.putExtra("professor", lessonsList.get(mSelectedPosition).getProffessor());
        tempIntent.putExtra("room",lessonsList.get(mSelectedPosition).getRoom());
        tempIntent.putExtra("department",department);
        tempIntent.putExtra("day",FRAGMENT_DAY_NAME);
        if(lessonsList.get(mSelectedPosition).getComments().equals("")){
            tempIntent.putExtra("comments","-");
        }else{
            tempIntent.putExtra("comments",lessonsList.get(mSelectedPosition).getComments());
        }
        startActivity(tempIntent);
    }else if(item.getItemId() == R.id.add){
        Intent addLessonActivityIntent = new Intent(getActivity(),AddLessonActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        if(fromStart){
            addLessonActivityIntent.putExtra("department",DatabaseUtils.mDepartment);
            addLessonActivityIntent.putExtra("grade",DatabaseUtils.mStringGrade);
            addLessonActivityIntent.putExtra("day",FRAGMENT_DAY_NAME);
        }else{
            addLessonActivityIntent.putExtra("department",department);
            addLessonActivityIntent.putExtra("grade",gradeString);
            addLessonActivityIntent.putExtra("day",FRAGMENT_DAY_NAME);
        }
        Bundle pack = new Bundle();
        pack.putBoolean("personal",personal);
        addLessonActivityIntent.putExtras(pack);
        startActivity(addLessonActivityIntent);
    }else if(item.getItemId() == R.id.delete){
        Lessons temp = lessonsList.get(mSelectedPosition);
        shareOptions = new DatabaseUtils("LessonsDBWrite",getActivity().getApplicationContext());
        shareOptions.deleteByLessonCustom(temp, FRAGMENT_DAY_NAME);
        shareOptions.finishDb();
        initialiseAndRefresh();
    }
    return super.onContextItemSelected(item);
}

}

其余的 ListFragments 完全相同,而不是 FRAGMENT_DAY_NAME,所以没有必要也发布它们!

如果您需要我发布任何其他代码部分,请告诉我!

【问题讨论】:

    标签: android actionbarsherlock android-viewpager android-listfragment


    【解决方案1】:

    您应该为每个片段创建不同的 onItemLongClick 侦听器。

    【讨论】:

    • 每个片段都有一个onItemLongClickListener!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多