【问题标题】:Fragment ListView Inflation to Maintain onItemClick Selection inoperative片段 ListView 膨胀以保持 onItemClick 选择无效
【发布时间】:2015-04-22 07:43:51
【问题描述】:

我有一个片段内的ListView。在onCreateView 部分中,我为列表设置了onItemClickListener,它突出显示了ListView 中的选定项目。我设置了两个ImageButtons 来在列表中上下导航。在选择时,一个新的Row 被膨胀(R.layout.selected_row),它的TextView 设置为选择项的内容(为了保持突出显示的选定状态)。不幸的是,在单击一个项目和/或单击ImageButton 时,似乎没有任何东西被夸大(列表上也没有发生任何明显的变化)。相信可能和适配器SelectedAdapter构造有关,IE:

 public SelectedAdapter(Context context, int textViewResourceId, List objects) {
            super(getActivity(), R.layout.listview_routines, routines);
        }

但是,我看不出这会如何阻止某些事情(通货膨胀)的发生。提前感谢您提供任何有助于解决此问题的意见或建议。

片段:

    public static class FragmentRoutine extends Fragment {
    DatabaseHandler db;
    private ListView routineListView;
    private List<Routine> routines = new ArrayList<Routine>();
    ArrayAdapter<Routine> routineAdapter;
    Routine longClickedItemRoutines;
    private SelectedAdapter selectedAdapter;

    public FragmentRoutine() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.routines,
                container, false);
        db = new DatabaseHandler(getActivity().getApplicationContext());
        routineListView = (ListView) rootView.findViewById(R.id.routineList);
        registerForContextMenu(routineListView);
        db.closeDB();
        if (db.getExerciseCount() != 0)
            routines.clear();
        routines.addAll(db.getAllRoutines());
        populateList();
        selectedAdapter = new SelectedAdapter(this.getActivity(), 0, routines);
        selectedAdapter.setNotifyOnChange(true);
        routineListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                longClickedItemRoutines = routines.get(position);
                return false;
            }
        });
        routineListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView arg0, View view,
                                    int position, long id) {

                selectedAdapter.setSelectedPosition(position);
            }
        });

        // move up event handler
        ImageButton btnMoveUp = (ImageButton) rootView.findViewById(R.id.btnMoveUp);
        btnMoveUp.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View arg0) {
                moveUp();
            }
        });

        // move down event handler
        ImageButton btnMoveDown = (ImageButton) rootView.findViewById(R.id.btnMoveDown);
        btnMoveDown.setOnClickListener(new AdapterView.OnClickListener() {
            public void onClick(View arg0) {
                moveDown();
            }
        });

        setHasOptionsMenu(true);
        return rootView;
    }

    // Move selected item "up" in the ViewList.
    private void moveUp(){

        int selectedPos = selectedAdapter.getSelectedPosition();
        Routine currentRoutine = routines.get(selectedPos);
        if (selectedPos > 0 ){
            routines.remove(selectedPos);
            String str = currentRoutine.getTagName();
            routines.add(selectedPos-1, currentRoutine);
            // set selected position in the adapter
            selectedAdapter.setSelectedPosition(selectedPos-1);
        }
    }

    // Move selected item "down" in the ViewList.
    private void moveDown(){

        int selectedPos = selectedAdapter.getSelectedPosition();
        Routine currentRoutine = routines.get(selectedPos);
        if (selectedPos < routines.size()-1 ){
            routines.remove(selectedPos);
            String str = currentRoutine.getTagName();
            routines.add(selectedPos+1, currentRoutine);
            // set selected position in the adapter
            selectedAdapter.setSelectedPosition(selectedPos+1);
        }
    }

    public class SelectedAdapter extends ArrayAdapter<Routine>{

        // used to keep selected position in ListView
        private int selectedPos = -1;   // init value for not-selected

        public SelectedAdapter(Context context, int textViewResourceId, List objects) {
            super(getActivity(), R.layout.listview_routines, routines);
        }

        public void setSelectedPosition(int pos){
            selectedPos = pos;
            // inform the view of this change
            notifyDataSetChanged();
        }

        public int getSelectedPosition(){
            return selectedPos;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            // only inflate the view if it's null
            if (v == null) {
                v = getActivity().getLayoutInflater().inflate(R.layout.selected_row, null);
            }

            // get text view
            TextView label = (TextView)v.findViewById(R.id.txtExample);

            // change the row color based on selected state
            if(selectedPos == position){
                v.setBackgroundColor(Color.CYAN);
            }else{
                v.setBackgroundColor(Color.WHITE);
            }

            label.setText(this.getItem(position).toString());
            return(v);
        }
    }

    private void populateList() {
        routineAdapter = new SaveListAdapterT();
        routineListView.setAdapter(routineAdapter);
    }

    public class SaveListAdapterT extends ArrayAdapter<Routine> {
        public SaveListAdapterT() {
            super(getActivity(), R.layout.listview_routines, routines);
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if (view == null)
                view = getActivity().getLayoutInflater().inflate(R.layout.listview_routines, parent, false);

            Routine currentToDoSave = routines.get(position);

            TextView name = (TextView) view.findViewById(R.id.name);
            name.setText(currentToDoSave.getTagName());

            return view;
        }

    }
}

附加信息:

上面提到的适配器 (SelectedAdapter) 确实需要我传递 routines.add(selectedPos+1, currentRoutine); 来代替 routines.add(selectedPos-1, str);,我认为这是将 String 值传递给新的 Row 的最直接方法。不确定这是否是贡献者。

【问题讨论】:

    标签: android android-fragments android-listview onitemclicklistener android-adapterview


    【解决方案1】:

    我将尝试解决您的 ImageButtons 不可点击的问题。 我从不喜欢在onCreateView() 中有这么多的GUI 代码,我建议将一些代码移到onViewCreated 方法中。 onViewCreated 出现在 onCreateView 之后。我认为的问题是框架何时可以或不能可靠地设置视图的时间。显然使用 onViewCreated 更可靠。

    我的示例代码是:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
       return inflater.inflate(R.layout.routines,
                    container, false);
       ...
    }
    

    所以你可以看到我的onCreateView 只是设置了主视图。有几次,这解决了其他 SO 用户的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多