【发布时间】:2018-01-04 06:19:04
【问题描述】:
我仍然没有使用 ViewHolder 类,(我知道列表视图的性能,并将尽快实现它)但我已经尝试在数组适配器上覆盖这两个方法(getViewTypeCount() 和 getItemViewType ) 但继续不工作。
在我看来,我的数组适配器只处理一种类型的视图,所以理论上我不必覆盖这些方法,但我不知道发生了什么,只是当我滚动我的 listView 时,位置变得杂乱无章。
我的 ArrayAdapter:
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);
}
我的listView行:
<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>
我的列表视图:
<ListView
android:id="@+id/task_list_view"
android:layout_width="368dp"
android:layout_height="475dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:defaultFocusHighlightEnabled="true"
android:divider="@color/borderColor"
android:dividerHeight="1px"
android:gravity="center_horizontal|top"
android:theme="@style/MyPicker"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
【问题讨论】:
-
为什么不能用recyclerview代替listview?
-
为什么要使用?我不知道recycleview
标签: java android listview scrollview android-arrayadapter