如何在listView滚动时修改ArrayAdapter更改位置?

时间:2018-01-04 06:19:05

标签: java android listview scrollview android-arrayadapter

我仍然没有使用ViewHolder类,(我知道列表视图的性能,并会尽快实现)但我已经尝试在数组适配器上覆盖这两个方法(getViewTypeCount()和{ {1}})但继续不工作。

在我看来我的数组适配器onle句柄有一种类型的视图,所以理论上我没有覆盖那些方法,但我不知道发生了什么,就在我滚动我的listView时,位置变得杂乱无章。

我的ArrayAdapter:

getItemViewType

我的listView行:

    private Context context;
    private ArrayList<Task> taskArrayList;

    public TasksAdapter(@NonNull Context c, @NonNull ArrayList<Task> objects) {
        super(c, R.layout.task_row, objects);
        this.context = c;
        this.taskArrayList = objects;

    }
    @Override
    public int getViewTypeCount() {
        return taskArrayList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE );

        Task task = taskArrayList.get(position);
        View view = inflater.inflate(R.layout.task_row, parent, false);
        if(TaskActivity.getIsClicked() && TaskActivity.getPositionClicked()-1 == position){
            view.setBackgroundResource(R.color.backgroundSelectedItem);
        }      
        TextView timeTextView = view.findViewById(R.id.timeTextView);
        TextView taskNameView = view.findViewById(R.id.taskNameText);
        TextView dateTextView = view.findViewById(R.id.dateTextView);
        FloatingActionButton fab = view.findViewById(R.id.myFabTask);
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM");
        String dateString = sdf.format(task.getDate());
        int hour = (int) task.getTime()/3600000;
        int minutes = (int) (task.getTime() % 3600000) / 60000;
        String stringHour = Integer.toString(hour);
        String stringMinutes = Integer.toString(minutes);
        if(stringHour.length() == 1){stringHour = "0"+stringHour;}
        if(stringMinutes.length() == 1){stringMinutes = "0"+stringMinutes;}
        String timeString = stringHour+":"+stringMinutes;
        timeTextView.setText(timeString);
        taskNameView.setText(task.getName());
        dateTextView.setText(dateString);
        return view;
    }

My Main code where i call and use the adapter:




         mTaskListView = findViewById(R.id.task_list_view);
            refreshAdapter();
            isClicked = false;
            positionClicked = 0;
            mTaskListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if(positionClicked == (position+1) || positionClicked == 0){

                        if(!isClicked){
                            isClicked = true;
                            positionClicked = position+1;
    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

                            changeLayoutVisibility(1);
                            Task task = adapter.getItem(position);
                           parent.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.backgroundSelectedItem));

                        }else {
                            isClicked = false;
                            positionClicked = 0;                      mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);                   
                            changeLayoutVisibility(0);
                        parent.getChildAt(position).setBackgroundColor(Color.WHITE);

                        }
                    }
                }
            });


Function on Main activity:

    private void refreshAdapter() {
        adapter = null;
        //Get the array with tasks
        adapter = new TasksAdapter(this, mDBAdapter.getAllTask());
        //Set the list view
        mTaskListView.setAdapter(adapter);
    }

我的列表视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@android:color/white"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">


<TextView
    android:id="@+id/timeTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="25dp"
    android:layout_marginStart="25dp"
    android:layout_marginTop="17dp"
    android:layout_marginBottom="17dp"
    android:text="00:00"
    android:textColor="@color/controlColorNormal"
    android:textSize="30sp" />

<TextView
    android:id="@+id/taskNameText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/timeTextView"
    android:layout_marginLeft="38dp"
    android:layout_marginStart="38dp"
    android:layout_toEndOf="@+id/timeTextView"
    android:layout_toRightOf="@+id/timeTextView"
    android:text="Nome da tarefa"
    android:textColor="@android:color/black"
    android:textSize="16sp" />

<TextView
    android:id="@+id/dateTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/taskNameText"
    android:layout_alignStart="@+id/taskNameText"
    android:layout_below="@+id/taskNameText"
    android:text="Data adicionada"
    android:textColor="@color/colorHint" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/myFabTask"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="16dp"
    android:src="@mipmap/ic_action_send"
    app:elevation="4dp"
    app:fabSize="mini">

</android.support.design.widget.FloatingActionButton>

3 个答案:

答案 0 :(得分:0)

  

不要在getView()中执行以下操作。

//get and format the date
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM");
String dateString = sdf.format(task.getDate());

//convert the milliseconds to minute and hour
int hour = (int) task.getTime()/3600000;
int minutes = (int) (task.getTime() % 3600000) / 60000;
String stringHour = Integer.toString(hour);
String stringMinutes = Integer.toString(minutes);
if(stringHour.length() == 1){stringHour = "0"+stringHour;}
if(stringMinutes.length() == 1){stringMinutes = "0"+stringMinutes;}

String timeString = stringHour+":"+stringMinutes;

创建返回类型为 String 的新函数,并在timeTextView.setText中调用该函数。

  • 制作函数像这样:

    private String getTimeStirng(Object date){
        //get and format the date
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM");
        String dateString = sdf.format(date);
    
        //convert the milliseconds to minute and hour
        int hour = (int) task.getTime()/3600000;
        int minutes = (int) (date % 3600000) / 60000;
        String stringHour = Integer.toString(hour);
        String stringMinutes = Integer.toString(minutes);
        if(stringHour.length() == 1){stringHour = "0"+stringHour;}
        if(stringMinutes.length() == 1){stringMinutes = "0"+stringMinutes;}
    
        String timeString = stringHour+":"+stringMinutes;
    
        return timeString;
    }
    
  • 称之为:timeTextView.setText(getTimeString(task.getDate()));

感谢。 快乐的编码。

答案 1 :(得分:0)

你说你的适配器只处理一种类型的视图,但是:

  1. @Override getViewTypeCount()返回taskArrayList的大小时,系统会理解您拥有taskArrayList.size()类型的视图。在你的情况下,它应该是1。
  2. 列表项在getItemViewType()方法中,值return必须是视图类型的标识,而不是项目的位置。在你的情况下,它应该默认返回0。
  3. 希望有所帮助!

答案 2 :(得分:0)

我只是通过改变来解决它:

parent.
    getChildAt(position).
    setBackgroundColor(getResources().getColor(R.color.backgroundSelectedItem));

人:

view.setBackgroundResource(R.color.backgroundSelectedItem);