片段ListView通胀维持onItemClick选择不起作用

时间:2015-04-22 07:43:52

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

我有一个片段内的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。不确定这是否是贡献者。

1 个答案:

答案 0 :(得分:0)

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

我的示例代码是:

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

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